berkleyBD implementation
This commit is contained in:
84
core/src/main/java/ru/trader/store/berkeley/BDBMarket.java
Normal file
84
core/src/main/java/ru/trader/store/berkeley/BDBMarket.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package ru.trader.store.berkeley;
|
||||
|
||||
import ru.trader.core.*;
|
||||
import ru.trader.store.berkeley.entities.BDBGroup;
|
||||
import ru.trader.store.berkeley.entities.BDBItem;
|
||||
import ru.trader.store.berkeley.entities.BDBPlace;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class BDBMarket extends AbstractMarket {
|
||||
private final BDBStore store;
|
||||
|
||||
|
||||
public BDBMarket(BDBStore store) {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Place createPlace(String name, double x, double y, double z) {
|
||||
return new PlaceProxy(new BDBPlace(name, x, y, z), store);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Group createGroup(String name, GROUP_TYPE type) {
|
||||
return new BDBGroup(name, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Item createItem(String name, Group group) {
|
||||
return new ItemProxy(new BDBItem(name, group.getName()), store);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addPlace(Place place) {
|
||||
store.getPlaceAccessor().put(((PlaceProxy) place).getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removePlace(Place place) {
|
||||
store.getPlaceAccessor().delete(((PlaceProxy) place).getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addGroup(Group group) {
|
||||
store.getGroupAccessor().put((BDBGroup) group);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeGroup(Group group) {
|
||||
store.getGroupAccessor().delete((BDBGroup) group);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addItem(Item item) {
|
||||
store.getItemAccessor().put(((ItemProxy) item).getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeItem(Item item) {
|
||||
store.getItemAccessor().delete(((ItemProxy) item).getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Place> get() {
|
||||
return store.getPlaceAccessor().getAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Group> getGroups() {
|
||||
return store.getGroupAccessor().getAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Item> getItems() {
|
||||
return store.getItemAccessor().getAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStat getStat(OFFER_TYPE type, Item item) {
|
||||
//TODO: добавить
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
package ru.trader.store.berkeley;
|
||||
|
||||
import com.sleepycat.je.Database;
|
||||
import com.sleepycat.je.DatabaseConfig;
|
||||
import com.sleepycat.je.DatabaseException;
|
||||
import com.sleepycat.je.Environment;
|
||||
import com.sleepycat.je.EnvironmentConfig;
|
||||
import com.sleepycat.persist.EntityStore;
|
||||
import com.sleepycat.persist.StoreConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.trader.core.*;
|
||||
import ru.trader.store.berkeley.dao.*;
|
||||
import ru.trader.store.berkeley.entities.BDBGroup;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -14,7 +17,12 @@ public class BDBStore {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BDBStore.class);
|
||||
|
||||
private final Environment dbEnvironment;
|
||||
private final Database db;
|
||||
private final EntityStore store;
|
||||
private final VendorDA<Vendor> vDA;
|
||||
private final OfferDA<Offer> oDA;
|
||||
private final ItemDA<Item> iDA;
|
||||
private final GroupDA<Group> gDA;
|
||||
private final PlaceDA<Place> pDA;
|
||||
|
||||
public BDBStore(String path){
|
||||
EnvironmentConfig envConfig = new EnvironmentConfig();
|
||||
@@ -24,9 +32,15 @@ public class BDBStore {
|
||||
dbEnvironment = new Environment(new File(path), envConfig);
|
||||
|
||||
try {
|
||||
DatabaseConfig dbConfig = new DatabaseConfig();
|
||||
StoreConfig dbConfig = new StoreConfig();
|
||||
dbConfig.setAllowCreate(true);
|
||||
db = dbEnvironment.openDatabase(null, "Trader", dbConfig);
|
||||
store = new EntityStore(dbEnvironment, "TraderStore", dbConfig);
|
||||
vDA = new VendorDA<>(store, v -> new VendorProxy(v, this));
|
||||
oDA = new OfferDA<>(store, o -> new OfferProxy(o, this));
|
||||
iDA = new ItemDA<>(store, i -> new ItemProxy(i, this));
|
||||
gDA = new GroupDA<>(store, g -> g);
|
||||
pDA = new PlaceDA<>(store, p -> new PlaceProxy(p, this));
|
||||
|
||||
} catch (DatabaseException e){
|
||||
LOG.error("Error on open DB, path {}", path);
|
||||
LOG.error("",e);
|
||||
@@ -34,11 +48,31 @@ public class BDBStore {
|
||||
}
|
||||
}
|
||||
|
||||
public VendorDA<Vendor> getVendorAccessor() {
|
||||
return vDA;
|
||||
}
|
||||
|
||||
public OfferDA<Offer> getOfferAccessor() {
|
||||
return oDA;
|
||||
}
|
||||
|
||||
public ItemDA<Item> getItemAccessor() {
|
||||
return iDA;
|
||||
}
|
||||
|
||||
public GroupDA<Group> getGroupAccessor() {
|
||||
return gDA;
|
||||
}
|
||||
|
||||
public PlaceDA<Place> getPlaceAccessor() {
|
||||
return pDA;
|
||||
}
|
||||
|
||||
|
||||
public void close(){
|
||||
if (db != null) {
|
||||
if (store != null) {
|
||||
try {
|
||||
db.close();
|
||||
store.close();
|
||||
} catch (DatabaseException e){
|
||||
LOG.error("Error on close DB", e);
|
||||
}
|
||||
|
||||
42
core/src/main/java/ru/trader/store/berkeley/ItemProxy.java
Normal file
42
core/src/main/java/ru/trader/store/berkeley/ItemProxy.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package ru.trader.store.berkeley;
|
||||
|
||||
import ru.trader.core.AbstractItem;
|
||||
import ru.trader.core.Group;
|
||||
import ru.trader.store.berkeley.entities.BDBItem;
|
||||
|
||||
public class ItemProxy extends AbstractItem {
|
||||
private final BDBItem item;
|
||||
private final BDBStore store;
|
||||
private final Group group;
|
||||
|
||||
public ItemProxy(BDBItem item, BDBStore store) {
|
||||
this.item = item;
|
||||
this.group = store.getGroupAccessor().get(item.getGroupId());
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
protected long getId(){
|
||||
return item.getId();
|
||||
}
|
||||
|
||||
protected BDBItem getEntity(){
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateName(String name) {
|
||||
item.setName(name);
|
||||
store.getItemAccessor().update(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return item.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Group getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
}
|
||||
72
core/src/main/java/ru/trader/store/berkeley/OfferProxy.java
Normal file
72
core/src/main/java/ru/trader/store/berkeley/OfferProxy.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package ru.trader.store.berkeley;
|
||||
|
||||
import ru.trader.core.*;
|
||||
import ru.trader.store.berkeley.entities.BDBOffer;
|
||||
|
||||
public class OfferProxy extends AbstractOffer {
|
||||
private final BDBOffer offer;
|
||||
private final Item item;
|
||||
private BDBStore store;
|
||||
private Vendor vendor;
|
||||
|
||||
public OfferProxy(BDBOffer offer, BDBStore store) {
|
||||
this.offer = offer;
|
||||
this.item = store.getItemAccessor().get(offer.getItemId());
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
protected long getId() {
|
||||
return offer.getId();
|
||||
}
|
||||
|
||||
protected BDBOffer getEntity() {
|
||||
return offer;
|
||||
}
|
||||
|
||||
protected void setVendor(VendorProxy vendor) {
|
||||
offer.setVendor(vendor.getId());
|
||||
this.vendor = vendor;
|
||||
store.getOfferAccessor().update(offer);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updatePrice(double price) {
|
||||
offer.setPrice(price);
|
||||
store.getOfferAccessor().update(offer);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateCount(long count) {
|
||||
offer.setCount(count);
|
||||
store.getOfferAccessor().update(offer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OFFER_TYPE getType() {
|
||||
return offer.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vendor getVendor() {
|
||||
if (vendor == null){
|
||||
vendor = store.getVendorAccessor().get(offer.getVendorId());
|
||||
}
|
||||
return vendor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPrice() {
|
||||
return offer.getPrice();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCount() {
|
||||
return offer.getCount();
|
||||
}
|
||||
|
||||
}
|
||||
79
core/src/main/java/ru/trader/store/berkeley/PlaceProxy.java
Normal file
79
core/src/main/java/ru/trader/store/berkeley/PlaceProxy.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package ru.trader.store.berkeley;
|
||||
|
||||
import ru.trader.core.AbstractPlace;
|
||||
import ru.trader.core.Vendor;
|
||||
import ru.trader.store.berkeley.entities.BDBPlace;
|
||||
import ru.trader.store.berkeley.entities.BDBVendor;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class PlaceProxy extends AbstractPlace {
|
||||
private final BDBPlace place;
|
||||
private BDBStore store;
|
||||
|
||||
public PlaceProxy(BDBPlace place, BDBStore store) {
|
||||
this.place = place;
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
protected long getId(){
|
||||
return place.getId();
|
||||
}
|
||||
|
||||
protected BDBPlace getEntity(){
|
||||
return place;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Vendor createVendor(String name) {
|
||||
return new VendorProxy(new BDBVendor(name, place.getId()), store);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateName(String name) {
|
||||
place.setName(name);
|
||||
store.getPlaceAccessor().update(place);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updatePosition(double x, double y, double z) {
|
||||
place.setPosition(x, y, z);
|
||||
store.getPlaceAccessor().update(place);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addVendor(Vendor vendor) {
|
||||
store.getVendorAccessor().put(((VendorProxy)vendor).getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeVendor(Vendor vendor) {
|
||||
store.getVendorAccessor().delete(((VendorProxy)vendor).getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return place.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getX() {
|
||||
return place.getX();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getY() {
|
||||
return place.getY();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getZ() {
|
||||
return place.getZ();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Vendor> get() {
|
||||
return store.getVendorAccessor().getAllByPlace(place.getId());
|
||||
}
|
||||
|
||||
}
|
||||
112
core/src/main/java/ru/trader/store/berkeley/VendorProxy.java
Normal file
112
core/src/main/java/ru/trader/store/berkeley/VendorProxy.java
Normal file
@@ -0,0 +1,112 @@
|
||||
package ru.trader.store.berkeley;
|
||||
|
||||
import ru.trader.core.*;
|
||||
import ru.trader.store.berkeley.entities.BDBOffer;
|
||||
import ru.trader.store.berkeley.entities.BDBVendor;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class VendorProxy extends AbstractVendor {
|
||||
private final BDBVendor vendor;
|
||||
private final BDBStore store;
|
||||
private Place place;
|
||||
|
||||
public VendorProxy(BDBVendor vendor, BDBStore store) {
|
||||
this.vendor = vendor;
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
protected long getId(){
|
||||
return vendor.getId();
|
||||
}
|
||||
|
||||
protected BDBVendor getEntity(){
|
||||
return vendor;
|
||||
}
|
||||
|
||||
protected void setPlace(PlaceProxy place) {
|
||||
vendor.setPlace(place.getId());
|
||||
store.getVendorAccessor().update(vendor);
|
||||
this.place = place;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Offer createOffer(OFFER_TYPE type, Item item, double price, long count) {
|
||||
return new OfferProxy(new BDBOffer(type, ((ItemProxy)item).getId(), price, count, vendor.getId()), store);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateName(String name) {
|
||||
vendor.setName(name);
|
||||
store.getVendorAccessor().update(vendor);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateDistance(double distance) {
|
||||
vendor.setDistance(distance);
|
||||
store.getVendorAccessor().update(vendor);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addService(SERVICE_TYPE service) {
|
||||
vendor.add(service);
|
||||
store.getVendorAccessor().update(vendor);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeService(SERVICE_TYPE service) {
|
||||
vendor.remove(service);
|
||||
store.getVendorAccessor().update(vendor);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addOffer(Offer offer) {
|
||||
OfferProxy oProxy = ((OfferProxy)offer);
|
||||
oProxy.setVendor(this);
|
||||
store.getOfferAccessor().put(oProxy.getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeOffer(Offer offer) {
|
||||
store.getOfferAccessor().delete(((OfferProxy) offer).getEntity());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return vendor.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Place getPlace() {
|
||||
if (place == null){
|
||||
place = store.getPlaceAccessor().get(vendor.getPlaceId());
|
||||
}
|
||||
return place;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDistance() {
|
||||
return vendor.getDistance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(SERVICE_TYPE service) {
|
||||
return vendor.has(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Offer> get(OFFER_TYPE type) {
|
||||
return store.getOfferAccessor().getAllByType(vendor.getId(), type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Offer get(OFFER_TYPE type, Item item) {
|
||||
return store.getOfferAccessor().get(vendor.getId(), type, ((ItemProxy)item).getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(OFFER_TYPE type, Item item) {
|
||||
return store.getOfferAccessor().has(vendor.getId(), type, ((ItemProxy)item).getId());
|
||||
}
|
||||
}
|
||||
48
core/src/main/java/ru/trader/store/berkeley/dao/DAUtils.java
Normal file
48
core/src/main/java/ru/trader/store/berkeley/dao/DAUtils.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package ru.trader.store.berkeley.dao;
|
||||
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.sleepycat.persist.EntityCursor;
|
||||
import com.sleepycat.persist.EntityIndex;
|
||||
import com.sleepycat.persist.SecondaryIndex;
|
||||
|
||||
public class DAUtils {
|
||||
|
||||
public static <T, K, E> T get(EntityIndex<K, E> index, K key, Function<E, T> convertFunc){
|
||||
return convertFunc.apply(index.get(key));
|
||||
}
|
||||
|
||||
public static <T, K, E> Collection<T> get(EntityIndex<K, E> index, K from, K to, Function<E, T> convertFunc){
|
||||
Collection<T> res = new LinkedList<>();
|
||||
try (EntityCursor<E> cursor = index.entities(from, true, to, true)){
|
||||
for(E entity : cursor){
|
||||
res.add(convertFunc.apply(entity));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static <T, E> Collection<T> getAll(EntityIndex<?, E> index, Function<E, T> convertFunc){
|
||||
Collection<T> res = new LinkedList<>();
|
||||
try (EntityCursor<E> cursor = index.entities()){
|
||||
for(E entity : cursor){
|
||||
res.add(convertFunc.apply(entity));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static <T, K, E> Collection<T> getAll(SecondaryIndex<K, ?, E> index, K key, Function<E, T> convertFunc){
|
||||
Collection<T> res = new LinkedList<>();
|
||||
try (EntityCursor<E> cursor = index.subIndex(key).entities()){
|
||||
for(E entity : cursor){
|
||||
res.add(convertFunc.apply(entity));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
38
core/src/main/java/ru/trader/store/berkeley/dao/GroupDA.java
Normal file
38
core/src/main/java/ru/trader/store/berkeley/dao/GroupDA.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package ru.trader.store.berkeley.dao;
|
||||
|
||||
import com.sleepycat.persist.EntityStore;
|
||||
import com.sleepycat.persist.PrimaryIndex;
|
||||
import ru.trader.store.berkeley.entities.BDBGroup;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class GroupDA<T> {
|
||||
private final PrimaryIndex<String, BDBGroup> indexById;
|
||||
private final Function<BDBGroup,T> convertFunc;
|
||||
|
||||
public GroupDA(EntityStore store, Function<BDBGroup, T> convertFunc) {
|
||||
this.convertFunc = convertFunc;
|
||||
this.indexById = store.getPrimaryIndex(String.class, BDBGroup.class);
|
||||
}
|
||||
|
||||
public T get(String name){
|
||||
return DAUtils.get(indexById, name, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAll(){
|
||||
return DAUtils.getAll(indexById, convertFunc);
|
||||
}
|
||||
|
||||
public BDBGroup put(BDBGroup group){
|
||||
return indexById.put(group);
|
||||
}
|
||||
|
||||
public void update(BDBGroup group){
|
||||
indexById.putNoReturn(group);
|
||||
}
|
||||
|
||||
public void delete(BDBGroup group){
|
||||
indexById.delete(group.getName());
|
||||
}
|
||||
}
|
||||
45
core/src/main/java/ru/trader/store/berkeley/dao/ItemDA.java
Normal file
45
core/src/main/java/ru/trader/store/berkeley/dao/ItemDA.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package ru.trader.store.berkeley.dao;
|
||||
|
||||
import com.sleepycat.persist.EntityStore;
|
||||
import com.sleepycat.persist.PrimaryIndex;
|
||||
import com.sleepycat.persist.SecondaryIndex;
|
||||
import ru.trader.store.berkeley.entities.BDBItem;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ItemDA<T> {
|
||||
private final PrimaryIndex<Long, BDBItem> indexById;
|
||||
private final SecondaryIndex<String, Long, BDBItem> indexByGroupId;
|
||||
private final Function<BDBItem,T> convertFunc;
|
||||
|
||||
public ItemDA(EntityStore store, Function<BDBItem, T> convertFunc) {
|
||||
this.convertFunc = convertFunc;
|
||||
this.indexById = store.getPrimaryIndex(Long.class, BDBItem.class);
|
||||
this.indexByGroupId = store.getSecondaryIndex(indexById, String.class, "groupId");
|
||||
}
|
||||
|
||||
public T get(long id){
|
||||
return DAUtils.get(indexById, id, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAll(){
|
||||
return DAUtils.getAll(indexById, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAll(String group){
|
||||
return DAUtils.getAll(indexByGroupId, group, convertFunc);
|
||||
}
|
||||
|
||||
public BDBItem put(BDBItem item){
|
||||
return indexById.put(item);
|
||||
}
|
||||
|
||||
public void update(BDBItem item){
|
||||
indexById.putNoReturn(item);
|
||||
}
|
||||
|
||||
public void delete(BDBItem item){
|
||||
indexById.delete(item.getId());
|
||||
}
|
||||
}
|
||||
93
core/src/main/java/ru/trader/store/berkeley/dao/OfferDA.java
Normal file
93
core/src/main/java/ru/trader/store/berkeley/dao/OfferDA.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package ru.trader.store.berkeley.dao;
|
||||
|
||||
import com.sleepycat.persist.*;
|
||||
import ru.trader.core.OFFER_TYPE;
|
||||
import ru.trader.store.berkeley.entities.BDBOffer;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class OfferDA<T> {
|
||||
private final PrimaryIndex<Long, BDBOffer> indexById;
|
||||
private final SecondaryIndex<Long, Long, BDBOffer> indexByItemId;
|
||||
private final SecondaryIndex<Long, Long, BDBOffer> indexByVendorId;
|
||||
private final SecondaryIndex<OFFER_TYPE, Long, BDBOffer> indexByType;
|
||||
private final Function<BDBOffer,T> convertFunc;
|
||||
|
||||
public OfferDA(EntityStore store, Function<BDBOffer, T> convertFunc) {
|
||||
this.convertFunc = convertFunc;
|
||||
this.indexById = store.getPrimaryIndex(Long.class, BDBOffer.class);
|
||||
this.indexByItemId = store.getSecondaryIndex(indexById, Long.class, "itemId");
|
||||
this.indexByVendorId = store.getSecondaryIndex(indexById, Long.class, "vendorId");
|
||||
this.indexByType = store.getSecondaryIndex(indexById, OFFER_TYPE.class, "type");
|
||||
}
|
||||
|
||||
public T get(long id){
|
||||
return DAUtils.get(indexById, id, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAll(){
|
||||
return DAUtils.getAll(indexById, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAllByItem(long itemId){
|
||||
return DAUtils.getAll(indexByItemId, itemId, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAllByVendor(long vendorId){
|
||||
return DAUtils.getAll(indexByVendorId, vendorId, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAllByType(OFFER_TYPE type){
|
||||
return DAUtils.getAll(indexByType, type, convertFunc);
|
||||
}
|
||||
|
||||
public T get(long vendorId, OFFER_TYPE type, long itemId){
|
||||
EntityJoin<Long,BDBOffer> join = new EntityJoin<>(indexById);
|
||||
join.addCondition(indexByVendorId, vendorId);
|
||||
join.addCondition(indexByType, type);
|
||||
join.addCondition(indexByItemId, itemId);
|
||||
BDBOffer entity;
|
||||
try (ForwardCursor<BDBOffer> cursor = join.entities())
|
||||
{
|
||||
entity = cursor.next();
|
||||
}
|
||||
return entity != null ? convertFunc.apply(entity) : null;
|
||||
}
|
||||
|
||||
public boolean has(long vendorId, OFFER_TYPE type, long itemId){
|
||||
EntityJoin<Long,BDBOffer> join = new EntityJoin<>(indexById);
|
||||
join.addCondition(indexByVendorId, vendorId);
|
||||
join.addCondition(indexByType, type);
|
||||
join.addCondition(indexByItemId, itemId);
|
||||
return join.keys().next() != null;
|
||||
}
|
||||
|
||||
public Collection<T> getAllByType(long vendorId, OFFER_TYPE type){
|
||||
EntityJoin<Long,BDBOffer> join = new EntityJoin<>(indexById);
|
||||
join.addCondition(indexByVendorId, vendorId);
|
||||
join.addCondition(indexByType, type);
|
||||
Collection<T> res = new LinkedList<>();
|
||||
try (ForwardCursor<BDBOffer> cursor = join.entities())
|
||||
{
|
||||
for(BDBOffer entity : cursor){
|
||||
res.add(convertFunc.apply(entity));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
public BDBOffer put(BDBOffer offer){
|
||||
return indexById.put(offer);
|
||||
}
|
||||
|
||||
public void update(BDBOffer offer){
|
||||
indexById.putNoReturn(offer);
|
||||
}
|
||||
|
||||
public void delete(BDBOffer offer){
|
||||
indexById.delete(offer.getId());
|
||||
}
|
||||
}
|
||||
58
core/src/main/java/ru/trader/store/berkeley/dao/PlaceDA.java
Normal file
58
core/src/main/java/ru/trader/store/berkeley/dao/PlaceDA.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package ru.trader.store.berkeley.dao;
|
||||
|
||||
import com.sleepycat.persist.EntityCursor;
|
||||
import com.sleepycat.persist.EntityStore;
|
||||
import com.sleepycat.persist.PrimaryIndex;
|
||||
import com.sleepycat.persist.SecondaryIndex;
|
||||
import ru.trader.store.berkeley.entities.BDBPlace;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PlaceDA<T> {
|
||||
private final PrimaryIndex<Long, BDBPlace> indexById;
|
||||
private final SecondaryIndex<Double, Long, BDBPlace> indexByDistance;
|
||||
private final Function<BDBPlace,T> convertFunc;
|
||||
|
||||
public PlaceDA(EntityStore store, Function<BDBPlace, T> convertFunc) {
|
||||
this.convertFunc = convertFunc;
|
||||
this.indexById = store.getPrimaryIndex(Long.class, BDBPlace.class);
|
||||
this.indexByDistance = store.getSecondaryIndex(indexById, Double.class, "distance");
|
||||
}
|
||||
|
||||
public T get(long id){
|
||||
return DAUtils.get(indexById, id, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAll(){
|
||||
return DAUtils.getAll(indexById, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAll(double x, double y, double z, double radius){
|
||||
double center = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
|
||||
Collection<T> res = new LinkedList<>();
|
||||
try (EntityCursor<BDBPlace> cursor = indexByDistance.entities(center < radius? 0 : center - radius, true, center + radius, true))
|
||||
{
|
||||
for(BDBPlace entity : cursor){
|
||||
double distance = Math.sqrt(Math.pow(x - entity.getX(), 2) + Math.pow(y - entity.getY(), 2) + Math.pow(z - entity.getZ(), 2));
|
||||
if (distance <= radius)
|
||||
res.add(convertFunc.apply(entity));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public BDBPlace put(BDBPlace place){
|
||||
return indexById.put(place);
|
||||
}
|
||||
|
||||
public void update(BDBPlace place){
|
||||
indexById.putNoReturn(place);
|
||||
}
|
||||
|
||||
public void delete(BDBPlace place){
|
||||
indexById.delete(place.getId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package ru.trader.store.berkeley.dao;
|
||||
|
||||
import com.sleepycat.persist.*;
|
||||
import ru.trader.store.berkeley.entities.BDBVendor;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class VendorDA<T> {
|
||||
private final PrimaryIndex<Long, BDBVendor> indexById;
|
||||
private final SecondaryIndex<Long, Long, BDBVendor> indexByPlaceId;
|
||||
private final Function<BDBVendor,T> convertFunc;
|
||||
|
||||
public VendorDA(EntityStore store, Function<BDBVendor, T> convertFunc) {
|
||||
this.convertFunc = convertFunc;
|
||||
this.indexById = store.getPrimaryIndex(Long.class, BDBVendor.class);
|
||||
this.indexByPlaceId = store.getSecondaryIndex(indexById, Long.class, "placeId");
|
||||
}
|
||||
|
||||
public T get(long id){
|
||||
return DAUtils.get(indexById, id, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAll(){
|
||||
return DAUtils.getAll(indexById, convertFunc);
|
||||
}
|
||||
|
||||
public Collection<T> getAllByPlace(long placeId){
|
||||
return DAUtils.getAll(indexByPlaceId, placeId, convertFunc);
|
||||
}
|
||||
|
||||
public BDBVendor put(BDBVendor vendor){
|
||||
return indexById.put(vendor);
|
||||
}
|
||||
|
||||
public void update(BDBVendor vendor){
|
||||
indexById.putNoReturn(vendor);
|
||||
}
|
||||
|
||||
public void delete(BDBVendor vendor){
|
||||
indexById.delete(vendor.getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package ru.trader.store.berkeley.entities;
|
||||
|
||||
import com.sleepycat.persist.model.Entity;
|
||||
import com.sleepycat.persist.model.PrimaryKey;
|
||||
import ru.trader.core.GROUP_TYPE;
|
||||
import ru.trader.core.Group;
|
||||
|
||||
|
||||
@Entity(version = 1)
|
||||
public class BDBGroup implements Group {
|
||||
|
||||
@PrimaryKey
|
||||
private String name;
|
||||
|
||||
private GROUP_TYPE type;
|
||||
|
||||
private BDBGroup() {
|
||||
}
|
||||
|
||||
public BDBGroup(String name, GROUP_TYPE type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GROUP_TYPE getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
@@ -2,26 +2,40 @@ package ru.trader.store.berkeley.entities;
|
||||
|
||||
import com.sleepycat.persist.model.*;
|
||||
|
||||
@Entity
|
||||
public class Item {
|
||||
@Entity(version = 1)
|
||||
public class BDBItem {
|
||||
@PrimaryKey(sequence="I_ID")
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@SecondaryKey(relate = Relationship.MANY_TO_ONE,
|
||||
relatedEntity = Group.class,
|
||||
relatedEntity = BDBGroup.class,
|
||||
onRelatedEntityDelete = DeleteAction.CASCADE)
|
||||
private String groupId;
|
||||
|
||||
private Item() {
|
||||
private BDBItem() {
|
||||
}
|
||||
|
||||
public Item(String name) {
|
||||
public BDBItem(String name, String groupId) {
|
||||
this.name = name;
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ru.trader.store.berkeley.entities;
|
||||
|
||||
import com.sleepycat.persist.model.*;
|
||||
import ru.trader.core.OFFER_TYPE;
|
||||
|
||||
@Entity(version = 1)
|
||||
public class BDBOffer {
|
||||
|
||||
@PrimaryKey(sequence = "O_ID")
|
||||
private long id;
|
||||
|
||||
@SecondaryKey(relate = Relationship.MANY_TO_ONE,
|
||||
relatedEntity = BDBItem.class,
|
||||
onRelatedEntityDelete = DeleteAction.NULLIFY)
|
||||
private long itemId;
|
||||
|
||||
@SecondaryKey(relate = Relationship.MANY_TO_ONE,
|
||||
relatedEntity = BDBVendor.class,
|
||||
onRelatedEntityDelete = DeleteAction.NULLIFY)
|
||||
private long vendorId;
|
||||
|
||||
@SecondaryKey(relate = Relationship.ONE_TO_ONE)
|
||||
private OFFER_TYPE type;
|
||||
|
||||
private double price;
|
||||
private long count;
|
||||
|
||||
public BDBOffer(OFFER_TYPE type, long itemId, double price, long count, long vendorId) {
|
||||
this.type = type;
|
||||
this.itemId = itemId;
|
||||
this.price = price;
|
||||
this.count = count;
|
||||
this.vendorId = vendorId;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public OFFER_TYPE getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public long getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(long count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public long getItemId() {
|
||||
return itemId;
|
||||
}
|
||||
|
||||
public long getVendorId() {
|
||||
return vendorId;
|
||||
}
|
||||
|
||||
public void setVendor(long vendorId) {
|
||||
this.vendorId = vendorId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package ru.trader.store.berkeley.entities;
|
||||
|
||||
import com.sleepycat.persist.model.Entity;
|
||||
import com.sleepycat.persist.model.PrimaryKey;
|
||||
import com.sleepycat.persist.model.Relationship;
|
||||
import com.sleepycat.persist.model.SecondaryKey;
|
||||
|
||||
@Entity(version = 1)
|
||||
public class BDBPlace {
|
||||
|
||||
@PrimaryKey(sequence = "P_ID")
|
||||
private long id;
|
||||
|
||||
@SecondaryKey(relate = Relationship.MANY_TO_ONE)
|
||||
private double distance;
|
||||
|
||||
private String name;
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
|
||||
|
||||
private BDBPlace() {
|
||||
}
|
||||
|
||||
public BDBPlace(String name, double x, double y, double z) {
|
||||
this.name = name;
|
||||
setPosition(x,y,z);
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public void setPosition(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ru.trader.store.berkeley.entities;
|
||||
|
||||
import com.sleepycat.persist.model.*;
|
||||
import ru.trader.core.SERVICE_TYPE;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
@Entity(version = 1)
|
||||
public class BDBVendor {
|
||||
|
||||
@PrimaryKey(sequence = "V_ID")
|
||||
private long id;
|
||||
|
||||
@SecondaryKey(relate = Relationship.MANY_TO_ONE,
|
||||
relatedEntity = BDBPlace.class,
|
||||
onRelatedEntityDelete = DeleteAction.NULLIFY)
|
||||
private long placeId;
|
||||
private String name;
|
||||
private double distance;
|
||||
|
||||
@SecondaryKey(relate=Relationship.ONE_TO_MANY)
|
||||
EnumSet<SERVICE_TYPE> services = EnumSet.noneOf(SERVICE_TYPE.class);
|
||||
|
||||
private BDBVendor() {
|
||||
}
|
||||
|
||||
public BDBVendor(String name, long placeId) {
|
||||
this.name = name;
|
||||
this.placeId = placeId;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getPlaceId() {
|
||||
return placeId;
|
||||
}
|
||||
|
||||
public void setPlace(long placeId){
|
||||
this.placeId = placeId;
|
||||
}
|
||||
|
||||
public double getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public void add(SERVICE_TYPE service){
|
||||
services.add(service);
|
||||
}
|
||||
|
||||
public void remove(SERVICE_TYPE service){
|
||||
services.remove(service);
|
||||
}
|
||||
|
||||
public boolean has(SERVICE_TYPE service){
|
||||
return services.contains(service);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package ru.trader.store.berkeley.entities;
|
||||
|
||||
import com.sleepycat.persist.model.Entity;
|
||||
import com.sleepycat.persist.model.PrimaryKey;
|
||||
import ru.trader.core.GROUP_TYPE;
|
||||
|
||||
|
||||
@Entity
|
||||
public class Group {
|
||||
@PrimaryKey
|
||||
private String name;
|
||||
|
||||
private GROUP_TYPE type;
|
||||
|
||||
private Group() {
|
||||
}
|
||||
|
||||
public Group(String name, GROUP_TYPE type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package ru.trader.store.berkeley.entities;
|
||||
|
||||
import com.sleepycat.persist.model.*;
|
||||
import ru.trader.core.OFFER_TYPE;
|
||||
|
||||
@Entity
|
||||
public class Offer {
|
||||
|
||||
@PrimaryKey(sequence = "O_ID")
|
||||
private long id;
|
||||
|
||||
@SecondaryKey(relate = Relationship.MANY_TO_ONE,
|
||||
relatedEntity = Item.class,
|
||||
onRelatedEntityDelete = DeleteAction.NULLIFY)
|
||||
private long itemId;
|
||||
|
||||
@SecondaryKey(relate = Relationship.MANY_TO_ONE,
|
||||
relatedEntity = Vendor.class,
|
||||
onRelatedEntityDelete = DeleteAction.NULLIFY)
|
||||
private long vendorId;
|
||||
|
||||
@SecondaryKey(relate = Relationship.ONE_TO_ONE)
|
||||
private OFFER_TYPE type;
|
||||
|
||||
private double price;
|
||||
|
||||
public Offer(Item item, OFFER_TYPE type, double price) {
|
||||
this.itemId = item.getId();
|
||||
this.type = type;
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package ru.trader.store.berkeley.entities;
|
||||
|
||||
import com.sleepycat.persist.model.Entity;
|
||||
import com.sleepycat.persist.model.PrimaryKey;
|
||||
import com.sleepycat.persist.model.Relationship;
|
||||
import com.sleepycat.persist.model.SecondaryKey;
|
||||
|
||||
@Entity
|
||||
public class Vendor {
|
||||
|
||||
@PrimaryKey(sequence = "V_ID")
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@SecondaryKey(relate = Relationship.ONE_TO_ONE)
|
||||
private double x;
|
||||
@SecondaryKey(relate = Relationship.ONE_TO_ONE)
|
||||
private double y;
|
||||
@SecondaryKey(relate = Relationship.ONE_TO_ONE)
|
||||
private double z;
|
||||
|
||||
private Vendor() {
|
||||
}
|
||||
|
||||
public Vendor(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user