optimize BDB place, vendor
This commit is contained in:
@@ -36,7 +36,7 @@ public interface Place extends Connectable<Place> {
|
|||||||
Vendor addVendor(String name);
|
Vendor addVendor(String name);
|
||||||
void remove(Vendor vendor);
|
void remove(Vendor vendor);
|
||||||
|
|
||||||
default int count(){
|
default long count(){
|
||||||
return get().size();
|
return get().size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class PlacesWrapper extends AbstractCollection<Vendor> {
|
|||||||
this.includeTransit = includeTransit;
|
this.includeTransit = includeTransit;
|
||||||
size = 0;
|
size = 0;
|
||||||
for (Place place : places) {
|
for (Place place : places) {
|
||||||
int count = place.count();
|
long count = place.count();
|
||||||
size += count > 0 ? count : includeTransit ? 1 : 0;
|
size += count > 0 ? count : includeTransit ? 1 : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,12 @@ import java.util.Collection;
|
|||||||
public class PlaceProxy extends AbstractPlace {
|
public class PlaceProxy extends AbstractPlace {
|
||||||
private final BDBPlace place;
|
private final BDBPlace place;
|
||||||
private BDBStore store;
|
private BDBStore store;
|
||||||
|
private long count;
|
||||||
|
|
||||||
public PlaceProxy(BDBPlace place, BDBStore store) {
|
public PlaceProxy(BDBPlace place, BDBStore store) {
|
||||||
this.place = place;
|
this.place = place;
|
||||||
this.store = store;
|
this.store = store;
|
||||||
|
count = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlaceProxy(BDBPlace place, BDBMarket market, BDBStore store) {
|
public PlaceProxy(BDBPlace place, BDBMarket market, BDBStore store) {
|
||||||
@@ -49,11 +51,13 @@ public class PlaceProxy extends AbstractPlace {
|
|||||||
@Override
|
@Override
|
||||||
protected void addVendor(Vendor vendor) {
|
protected void addVendor(Vendor vendor) {
|
||||||
store.getVendorAccessor().put(((VendorProxy)vendor).getEntity());
|
store.getVendorAccessor().put(((VendorProxy)vendor).getEntity());
|
||||||
|
if (count != -1) count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void removeVendor(Vendor vendor) {
|
protected void removeVendor(Vendor vendor) {
|
||||||
store.getVendorAccessor().delete(((VendorProxy)vendor).getEntity());
|
store.getVendorAccessor().delete(((VendorProxy)vendor).getEntity());
|
||||||
|
if (count != -1) count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -81,6 +85,22 @@ public class PlaceProxy extends AbstractPlace {
|
|||||||
return store.getVendorAccessor().getAllByPlace(place.getId());
|
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
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
|||||||
@@ -18,22 +18,34 @@ public class VendorProxy extends AbstractVendor {
|
|||||||
public VendorProxy(BDBVendor vendor, BDBStore store) {
|
public VendorProxy(BDBVendor vendor, BDBStore store) {
|
||||||
this.vendor = vendor;
|
this.vendor = vendor;
|
||||||
this.store = store;
|
this.store = store;
|
||||||
init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init(){
|
private void initSellCache(){
|
||||||
sell = new ConcurrentHashMap<>(20, 0.9f, 2);
|
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)) {
|
for (Offer offer : store.getOfferAccessor().getAllByType(vendor.getId(), OFFER_TYPE.SELL)) {
|
||||||
sell.put(((ItemProxy)offer.getItem()).getId(), offer);
|
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)) {
|
for (Offer offer : store.getOfferAccessor().getAllByType(vendor.getId(), OFFER_TYPE.BUY)) {
|
||||||
buy.put(((ItemProxy)offer.getItem()).getId(), offer);
|
buy.put(((ItemProxy)offer.getItem()).getId(), offer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<Long, Offer> getCache(OFFER_TYPE type){
|
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(){
|
protected long getId(){
|
||||||
|
|||||||
@@ -30,6 +30,14 @@ public class VendorDA<T> {
|
|||||||
return DAUtils.getAll(indexByPlaceId, placeId, convertFunc);
|
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){
|
public BDBVendor put(BDBVendor vendor){
|
||||||
return indexById.put(vendor);
|
return indexById.put(vendor);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user