package com.berkeley.dbje; import java.io.File; import java.io.UnsupportedEncodingException; import com.sleepycat.bind.tuple.IntegerBinding; import com.sleepycat.je.Database; import com.sleepycat.je.DatabaseConfig; import com.sleepycat.je.DatabaseEntry; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; import com.sleepycat.je.LockMode; import com.sleepycat.je.OperationStatus; public class MyBdb { private static MyBdb myBdb = null; private EnvironmentConfig envConfig; private Environment env; private DatabaseConfig dbConfig; private Database db; private MyBdb(){}//prevent others to construct the class object public static MyBdb newInstance(){//single alone pattern,must using static if(myBdb == null){ myBdb = new MyBdb(); } return myBdb; } //initialize the environment and database public void initEnv(){ try{ File file = new File("./db/"); if(!file.exists()) file.mkdirs(); envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); env = new Environment(file,envConfig); dbConfig = new DatabaseConfig(); dbConfig.setAllowCreate(true); db = env.openDatabase(null, "vertext", dbConfig);//use env to open db }catch(Exception e){ e.printStackTrace(); } } //close env and database public void close(){ try{ //close the database first! and then close the environemnt if(db!=null){ db.close(); } if(env!=null){ env.close(); } }catch(Exception e){ e.printStackTrace(); } } //insert key(int)&value(String) public int insert(int key,String value){ try { DatabaseEntry tkey = new DatabaseEntry(); IntegerBinding.intToEntry(key, tkey);//change int to entry(object) //class String can get bytes-->DatabaseEntry(byte[] data DatabaseEntry tvalue = new DatabaseEntry(value.getBytes("UTF-8")); //another method to change String into entry(object) //StringBinding.stringToEntry(value, tvalue); //you have finished changing object to entry,then put to db if(OperationStatus.SUCCESS != db.put(null, tkey, tvalue)){ System.out.println("database error:fail to insert data !"); return -1; } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); return -2; } return 0; } //through key(int) to get data(String) public String getRecord(int key){ String value = null; try{ DatabaseEntry tkey = new DatabaseEntry(); IntegerBinding.intToEntry(key, tkey); DatabaseEntry tvalue = new DatabaseEntry(); if(OperationStatus.SUCCESS != db.get(null, tkey, tvalue, LockMode.DEFAULT)){ System.out.println("database error:fail to get data records!"); }else{//get the record and change value = new String(tvalue.getData()); } }catch(Exception e){ e.printStackTrace(); } return value; } //insert object //object can be considered as key or value! public int insert(int key ,ObjectData oData){ try{ DatabaseEntry tkey = new DatabaseEntry(); IntegerBinding.intToEntry(key, tkey); DatabaseEntry tvalue = new DatabaseEntry(); ObjectTupleBinding obind = new ObjectTupleBinding(); obind.objectToEntry(oData, tvalue);//encapsulate key&value to One Entry if(OperationStatus.SUCCESS != db.put(null, tkey, tvalue)){ System.out.println("database error:fail to insert data"); return -1; } }catch(Exception e){ e.printStackTrace(); return -2; } return 0; } //get the object data public ObjectData getRecord(int key,boolean flag){ ObjectData od = null; try{ ObjectTupleBinding oBind = new ObjectTupleBinding(); DatabaseEntry tkey = new DatabaseEntry(); IntegerBinding.intToEntry(key, tkey); DatabaseEntry tvalue = new DatabaseEntry(); if(OperationStatus.SUCCESS == db.get(null, tkey, tvalue, LockMode.DEFAULT)){ od = (ObjectData)oBind.entryToObject(tvalue); } }catch(Exception e){ e.printStackTrace(); } return od; } }
实体对象类ObjectData代码如下:
package com.berkeley.dbje; public class ObjectData { private int key; private String name; private double sum; private long length; public ObjectData(int key,String name,double sum,long length){ this.key = key; this.name = name; this.sum = sum; this.length = length; } public ObjectData(){ } public int getKey() { return key; } public void setKey(int key) { this.key = key; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSum() { return sum; } public void setSum(double sum) { this.sum = sum; } public long getLength() { return length; } public void setLength(long length) { this.length = length; } }
package com.berkeley.dbje; import com.sleepycat.bind.tuple.TupleBinding; import com.sleepycat.bind.tuple.TupleInput; import com.sleepycat.bind.tuple.TupleOutput; /** * identify the regular of data convert * @author sang * */ public class ObjectTupleBinding extends TupleBinding{ @Override public Object entryToObject(TupleInput in) { //order is very important ObjectData od = new ObjectData(); od.setKey(in.readInt()); od.setName(in.readString()); od.setSum(in.readDouble()); od.setLength(in.readLong()); return od; } @Override public void objectToEntry(Object obj, TupleOutput out) { //invert object to class ObjectData ObjectData od = (ObjectData)obj; // Write the data to the TupleOutput (a DatabaseEntry). // Order is important. The first data written will be // the first bytes used by the default comparison routines. out.writeInt(od.getKey()); out.writeString(od.getName()); out.writeDouble(od.getSum()); out.writeLong(od.getLength()); //then the data will be written to database } }
package com.berkeley.dbje; public class MainTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MyBdb myBdb = MyBdb.newInstance(); ObjectData od = new ObjectData(); myBdb.initEnv(); myBdb.insert(12, "ad"); myBdb.insert(13,new ObjectData(1,"sa",12,1)); System.out.println(myBdb.getRecord(12)); od = myBdb.getRecord(13,true); if(od != null){ System.out.println(od.getName()); } myBdb.close(); } }