Archived
0

add Berkeley DB entities

This commit is contained in:
iMoHax
2014-09-30 14:04:26 +04:00
parent 7eee3700e4
commit 402351da6f
7 changed files with 187 additions and 0 deletions

View File

@@ -36,6 +36,10 @@
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>
<dependency>
<groupId>com.sleepycat</groupId>
<artifactId>je</artifactId>
</dependency>
</dependencies>
<packaging>jar</packaging>

View File

@@ -0,0 +1,55 @@
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
public class BDBStore {
private static final Logger LOG = LoggerFactory.getLogger(BDBStore.class);
private final Environment dbEnvironment;
private final Database db;
public BDBStore(String path){
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
envConfig.setSharedCache(true);
envConfig.setTransactional(true);
dbEnvironment = new Environment(new File(path), envConfig);
try {
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
db = dbEnvironment.openDatabase(null, "Trader", dbConfig);
} catch (DatabaseException e){
LOG.error("Error on open DB, path {}", path);
LOG.error("",e);
throw e;
}
}
public void close(){
if (db != null) {
try {
db.close();
} catch (DatabaseException e){
LOG.error("Error on close DB", e);
}
}
if (dbEnvironment != null) {
try {
dbEnvironment.cleanLog();
dbEnvironment.close();
} catch (DatabaseException e){
LOG.error("Error on close DB environment", e);
}
}
}
}

View File

@@ -0,0 +1,22 @@
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;
}
}

View File

@@ -0,0 +1,27 @@
package ru.trader.store.berkeley.entities;
import com.sleepycat.persist.model.*;
@Entity
public class Item {
@PrimaryKey(sequence="I_ID")
private long id;
private String name;
@SecondaryKey(relate = Relationship.MANY_TO_ONE,
relatedEntity = Group.class,
onRelatedEntityDelete = DeleteAction.CASCADE)
private String groupId;
private Item() {
}
public Item(String name) {
this.name = name;
}
public long getId() {
return id;
}
}

View File

@@ -0,0 +1,32 @@
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;
}
}

View File

@@ -0,0 +1,33 @@
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;
}
}