【搜索引擎】Berkeley DB的API封装

前不久也封装了一个,但那个是基于存储映射实现的,因为要实现特定对象存储,所以过程有些复杂,今天是直接使用BerkeleyDB的API封装成的一个MyBerkeleyDB,简单易用。下面上代码:

步骤

1、确定类和变量

/*********************************
 * 使用BerkeleyDB封装了一些数据库操作
 * 包括设置缓冲区,设置编码,设置数据可库
 * 路径,存储键值对,根据键查找值,关闭数
 * 据库等操作。
 * @author Administrator
 *********************************/
public class MyBerkeleyDB {

    private Environment environment;    //环境
    private Database    database;       //数据库
    @SuppressWarnings("unused")
    private String      charset;        //编码
    private String      path;           //路径
    private long        chacheSize;     //缓冲区大小
}

2、实现构造函数和一些参数的修改器

    public MyBerkeleyDB(){
        charset = "utf-8";      //编码默认使用UTF-8
        chacheSize = 10000;     //缓冲区大小默认为10000
    }

    //设置编码
    public void setCharset(String charset)
    {
        this.charset = charset;
    }

    //设置路径
    public void setPath(String path){
        //判断Path是否存在
        File file = new File(path);
        if(file.mkdir()){
            System.out.println(path+"已创建!"); //不存在则创建一个
        }
        else{
            System.out.println(path+"已存在!"); //存在则说明已存在
        }

        //确定存储路径
        this.path = path;
    }

    //设置缓冲区大小
    public boolean setChacheSize(long size){
        if(size<=0 || size >=1000000000)
        {
            return false; //使用默认的大小
        }
        this.chacheSize = size;
        return true;
    }

3、创建环境

    //同时设置路径和缓冲区,创建环境
    public void setEnvironment(String path , long chacheSize){
        setPath(path);
        setChacheSize(chacheSize);
        //配置环境
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setAllowCreate(true);
        envConfig.setCacheSize(this.chacheSize);
        //创建环境
        environment = new Environment(new File(this.path),envConfig);
    }

4、打开数据库名为dbName的数据库和关闭的方法

    //打开名字是dbName的数据库
    public void open(String dbName)
    {
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        dbConfig.setSortedDuplicates(false); //不存储重复关键字
        this.database = environment.openDatabase(null, dbName, dbConfig);
    }

    //关闭
    public void close()
    {
        database.close();
        environment.close();
    }

5、put、get、del、size方法的实现

    //存储
    public void put(Object key,Object value)
    {
        DatabaseEntry k = new DatabaseEntry(toByteArray(key));
        DatabaseEntry v = new DatabaseEntry(toByteArray(value));
        database.put(null, k, v);
    }

    //取值
    public Object get(Object key)
    {
        DatabaseEntry k = new DatabaseEntry(toByteArray(key));
        DatabaseEntry v = new DatabaseEntry();

        database.get(null, k, v, LockMode.DEFAULT);
        if(v == null){
            return null;
        }
        return toObject(v.getData());
    }

    //按照键值删除数据
    public Object del(Object key)
    {
        DatabaseEntry k = new DatabaseEntry(toByteArray(key));  //键值转化
        Object value = get(key);                                //获取值
        database.removeSequence(null, k);                       //删除值
        return value;                                           //返回删除的值
    }

    //获取数据库存储数据的大小
    public long size()
    {
        return database.count();
    }

6、遍历数据库,包括获取所有的键值和获取所有键值对

    // 获取数据库所有的key键
    public ArrayList<Object> getAllKey()
    {
        ArrayList<Object> result = new ArrayList<Object>();
        Cursor cursor = database.openCursor(null, null);
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        while(cursor.getNext(key, value, LockMode.DEFAULT) == 
                OperationStatus.SUCCESS)
        {
            Object k = toObject(key.getData());
            if(k != null)
            {
                result.add(k);
                key = new DatabaseEntry();
            }
            else
            {
                key = new DatabaseEntry();
            }
        }
        cursor.close();
        return result;
    }

    //遍历数据库
    public ArrayList<Entry<Object,Object>> getAllItems()
    {
        Cursor cursor = null;//游标
        ArrayList<Entry<Object,Object>> result = new ArrayList<Entry<Object,Object>>();
        cursor = database.openCursor(null, null);
        DatabaseEntry theKey = new DatabaseEntry();
        DatabaseEntry theValue = new DatabaseEntry();
        //使用Cursor方法来遍历游标获取数据
        if(cursor.getFirst(theKey, theValue, LockMode.DEFAULT)
                == OperationStatus.SUCCESS)
        {
            Object key = toObject(theKey.getData());
            Object value = toByteArray(theValue.getData());
            Entry<Object,Object> entry = new SimpleEntry<Object,Object>(key,value);
            result.add(entry);
            while(cursor.getNext(theKey, theValue, LockMode.DEFAULT)
                    == OperationStatus.SUCCESS)
            {
                //System.out.println(new String(theKey.getData()));
                key = theKey.getData();
                value = theValue.getData();
                entry = new SimpleEntry<Object,Object>(key,value);
                result.add(entry);
            }
        }
        cursor.close();//关闭游标
        return result;
    }

7、两个工具函数:Object转为Byte[],Byte[]转为Object

//序列化对象
    private static byte[] toByteArray (Object obj) {      
        if(obj == null)
            return null;
        byte[] bytes = null;      
        ByteArrayOutputStream bos = new ByteArrayOutputStream();      
        try {        
            ObjectOutputStream oos = new ObjectOutputStream(bos);         
            oos.writeObject(obj);        
            oos.flush();         
            bytes = bos.toByteArray ();      
            oos.close();         
            bos.close();        
        } catch (IOException ex) {        
            //ex.printStackTrace();   
        }      
        return bytes;    
    }   

    //反序列化  
    private static Object toObject (byte[] bytes){    
        if(bytes == null)
            return null;
        Object obj = null;      
        try {        
            ByteArrayInputStream bis = new ByteArrayInputStream (bytes);        
            ObjectInputStream ois = new ObjectInputStream (bis);        
            obj = ois.readObject();      
            ois.close();   
            bis.close();   
        } catch (IOException ex) {        
            //ex.printStackTrace();   
        } catch (ClassNotFoundException ex) {        
            //ex.printStackTrace();   
        }
        return obj;    
    }   

8、main测试函数

public static void main(String[] args) throws Exception {
           MyBerkeleyDB db = new MyBerkeleyDB();
           db.setEnvironment("C:\\BerkeleyDB\\MyDatabase2", 1000000);
           db.open("myDB");//打开数据库

           //存储值与取值测试
           for(int i=10; i>=0; i--) {
                 String key = "Key"+i;
                 Integer value = i;
                 db.put(key , value);
                 System.out.println("[" + key + ":" + db.get(key) + "]");
           }

           //获取数据库键值对数量
           System.out.println(db.size());

           //删除数据测试
           System.out.println("删除:"+db.del("Key3"));

           //测试获取所有键值
           ArrayList<Object> set = db.getAllKey();
           System.out.println(set.size());
           Iterator<Object> it = set.iterator();
           while(it.hasNext())
           {
               String s = it.next().toString();
               System.out.println(s+":"+db.get(s));

           }

           //测试遍历数据库
           ArrayList<Entry<Object,Object>> r = db.getAllItems();
           for(int i = 0; i < r.size();i++){
               Object key = r.get(i).getKey();
               Object value = r.get(i).getValue();
               if(key != null && value != null)
                   System.out.println(key.toString()+"-"+(String)value);
           }

           db.close();
    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。