Archived
0

optimize BDB place, vendor

This commit is contained in:
iMoHax
2015-08-12 15:12:49 +03:00
parent 83ddd5c3e9
commit 7ecdd03727
5 changed files with 46 additions and 6 deletions

View File

@@ -36,7 +36,7 @@ public interface Place extends Connectable<Place> {
Vendor addVendor(String name);
void remove(Vendor vendor);
default int count(){
default long count(){
return get().size();
}

View File

@@ -16,7 +16,7 @@ public class PlacesWrapper extends AbstractCollection<Vendor> {
this.includeTransit = includeTransit;
size = 0;
for (Place place : places) {
int count = place.count();
long count = place.count();
size += count > 0 ? count : includeTransit ? 1 : 0;
}
}

View File

@@ -10,10 +10,12 @@ import java.util.Collection;
public class PlaceProxy extends AbstractPlace {
private final BDBPlace place;
private BDBStore store;
private long count;
public PlaceProxy(BDBPlace place, BDBStore store) {
this.place = place;
this.store = store;
count = -1;
}
public PlaceProxy(BDBPlace place, BDBMarket market, BDBStore store) {
@@ -49,11 +51,13 @@ public class PlaceProxy extends AbstractPlace {
@Override
protected void addVendor(Vendor vendor) {
store.getVendorAccessor().put(((VendorProxy)vendor).getEntity());
if (count != -1) count++;
}
@Override
protected void removeVendor(Vendor vendor) {
store.getVendorAccessor().delete(((VendorProxy)vendor).getEntity());
if (count != -1) count--;
}
@Override
@@ -81,6 +85,22 @@ public class PlaceProxy extends AbstractPlace {
return store.getVendorAccessor().getAllByPlace(place.getId());
}
@Override
public long count() {
if (count == -1){
count = store.getVendorAccessor().count(place.getId());
}
return count;
}
@Override
public boolean isEmpty() {
if (count > -1){
return count == 0;
}
return !store.getVendorAccessor().contains(place.getId());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@@ -18,22 +18,34 @@ public class VendorProxy extends AbstractVendor {
public VendorProxy(BDBVendor vendor, BDBStore store) {
this.vendor = vendor;
this.store = store;
init();
}
private void init(){
private void initSellCache(){
sell = new ConcurrentHashMap<>(20, 0.9f, 2);
buy = new ConcurrentHashMap<>(20, 0.9f, 2);
for (Offer offer : store.getOfferAccessor().getAllByType(vendor.getId(), OFFER_TYPE.SELL)) {
sell.put(((ItemProxy)offer.getItem()).getId(), offer);
}
}
private void initBuyCache(){
buy = new ConcurrentHashMap<>(20, 0.9f, 2);
for (Offer offer : store.getOfferAccessor().getAllByType(vendor.getId(), OFFER_TYPE.BUY)) {
buy.put(((ItemProxy)offer.getItem()).getId(), offer);
}
}
private Map<Long, Offer> getCache(OFFER_TYPE type){
return type == OFFER_TYPE.SELL ? sell : buy;
if (type == OFFER_TYPE.SELL){
if (sell == null){
initSellCache();
}
return sell;
} else {
if (buy == null){
initBuyCache();
}
return buy;
}
}
protected long getId(){

View File

@@ -30,6 +30,14 @@ public class VendorDA<T> {
return DAUtils.getAll(indexByPlaceId, placeId, convertFunc);
}
public boolean contains(long placeId){
return indexByPlaceId.contains(placeId);
}
public long count(long placeId){
return indexByPlaceId.subIndex(placeId).count();
}
public BDBVendor put(BDBVendor vendor){
return indexById.put(vendor);
}