BerkeleyDB数据库,增删改,封装类(带字段压缩功能)

自己写的两个工具类,分享一下,供大家参考。主要是将BerkeleyDB数据库的常用操作封装起来,使用的时候就不用再写重复的代码。

提供两个类,一个是BerkeleyDB的封装类,一个是字节压缩类,两个类放一个工具包即可。

package com.laotoumo.util;

import java.awt.print.Paper;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sleepycat.je.Cursor;
import com.sleepycat.je.CursorConfig;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;

/**
 * BerkeleyDB 增删改封装类
 * 
 * @author Mo
 *
 */
public class BerkeleyDB {

	private static final Logger log = Logger.getLogger(BerkeleyDB.class);
	
	private Environment dbEnvironment = null;
	private Database database = null;

	public Database getDatabase() {
		return database;
	}
	
	/**
	 * 
	 * @param dbPath 数据库路径
	 * @param dbName 数据库名称
	 * @param readOnly 只读状态
	 * @throws DatabaseException
	 */
	public BerkeleyDB(String dbPath, String dbName, boolean readOnly)
			throws DatabaseException {
		
		log.debug(dbPath + "\t" + dbName + "\tReadOnly:" + readOnly);
		
		/*数据库文件*/
		File envHome = new File(dbPath,dbName);
		
		if (!envHome.exists()) {
			envHome.mkdirs();
		}
		
		/*打开一个环境,如果不存在则创建一个*/
		/*创建一个EnvironmentConfig配置对象*/
		EnvironmentConfig envConfig = new EnvironmentConfig();
		/*允许创建一个数据库环境*/
		envConfig.setAllowCreate(true);
		/*设置数据库缓存大小*/
		// envConfig.setCacheSize(64 *1024 *1024);
		/*使用一个指定路径和一个EnvironmentConfig配置对象创建Environment环境*/
		dbEnvironment = new Environment(envHome, envConfig);

		/*打开一个数据库,如果数据库不存在则创建一个*/
		DatabaseConfig dbConfig = new DatabaseConfig();
		if (!readOnly) {
			dbConfig.setAllowCreate(true);
			dbConfig.setDeferredWrite(true);
		}
		dbConfig.setReadOnly(readOnly);
		
		/*打开一个数据库,数据库名为dbName,数据库的配置为dbConfig*/
		database = dbEnvironment.openDatabase(null, dbName, dbConfig);
		//envConfig.setConfigParam("je.env.sharedLatches", "false");
	}

	/**
	 * 
	 * @param dbPath 数据库路径
	 * @param dbName 数据库名称
	 * @throws DatabaseException
	 */
	public BerkeleyDB(String dbPath, String dbName) throws DatabaseException {
		this(dbPath, dbName, false);
	}

	/**
	 * 关闭数据库
	 */
	public void close(){
		try {
			if (database != null) {
				database.close();
			}
			if (dbEnvironment != null) {
				/*在关闭环境之前清理日志*/
				dbEnvironment.cleanLog();
				dbEnvironment.close();
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
		} finally {
			database = null;
			dbEnvironment = null;
		}
	}

	/**
	 * 增删 合并 操作
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean put(DatabaseEntry key, DatabaseEntry value) {
		try {
			database.put(null, key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean put(Object key, Object value) {
		try {
			byte[] keys = BytesZip.toBytes((Serializable) key);// 本来就不长不用加密了
			byte[] values = BytesZip.gZip(BytesZip
					.toBytes((Serializable) value));// Map加密
			return put(keys, values);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public Object get(Object key) {
		try {
			return BytesZip.toObject(get(BytesZip.toBytes((Serializable) key)));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private boolean put(byte[] key, byte[] value) {
		return put(new DatabaseEntry(key), new DatabaseEntry(value));
	}

	public boolean contains(Object key) {
		try {
			return contains(BytesZip.toBytes((Serializable) key));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	private boolean contains(byte[] key) {
		try {
			DatabaseEntry theKey = new DatabaseEntry(key);
			DatabaseEntry theData = new DatabaseEntry();
			return (database.get(null, theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS);
		} catch (Exception e) {
			return false;
		}
	}

	private byte[] get(byte[] key) {
		DatabaseEntry theKey = null;
		DatabaseEntry theData = null;
		try {
			theKey = new DatabaseEntry(key);
			theData = new DatabaseEntry();
			if (database.get(null, theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
				byte[] retData = BytesZip.unGZip(theData.getData());// 在此解压缩
				theData.setData(null);
				return retData;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			theKey = null;
			theData = null;
		}
	}

	public boolean remove(Object key) {
		try {
			DatabaseEntry theKey = obj2Ent(key);
			database.delete(null, theKey);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public long getCount() {
		try {
			return database.count();
		} catch (DatabaseException e) {
			e.printStackTrace();
			return 0;
		}
	}

	public void sync() {
		database.sync();
	}

	/**
	 * 数据库合并
	 * 
	 * @param fromDb
	 * @return
	 * @throws Exception
	 */
	public boolean merge(BerkeleyDB fromDb) {
		Cursor cursor = null;
		CursorConfig config = new CursorConfig();
		//config.setDirtyRead(true);

		/*open cursor*/
		cursor = fromDb.getDatabase().openCursor(null, config);
		try {
			DatabaseEntry foundKey = new DatabaseEntry();
			DatabaseEntry foundData = new DatabaseEntry();
			while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
				this.put(foundKey, foundData);
			}
			foundKey = null;
			foundData = null;
			return true;
		} catch (DatabaseException de) {
			de.printStackTrace();
			return true;
		} finally {
			// Cursors must be closed.
			cursor.close();
		}
	}

	private DatabaseEntry obj2Ent(byte[] obj) throws Exception {
		if (obj == null) {
			return new DatabaseEntry();
		} else {
			return new DatabaseEntry(obj);
		}
	}

	private DatabaseEntry obj2Ent(Object obj) throws Exception {
		if (obj == null) {
			return new DatabaseEntry();
		} else {
			return new DatabaseEntry(BytesZip.toBytes((Serializable) obj));
		}
	}

	public List<Paper> list(int max) throws Exception {
		List<Paper> paperList = new ArrayList<Paper>();
		int len = 0;
		Cursor cursor = null;
		CursorConfig config = new CursorConfig();
		// config.setDirtyRead(true);
		cursor = database.openCursor(null, config); // open cursor
		try {
			DatabaseEntry foundKey = new DatabaseEntry();
			DatabaseEntry foundData = new DatabaseEntry();
			while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
				Object key = BytesZip.toObject(foundKey.getData());
				Object value = BytesZip.toObject(BytesZip.unGZip(foundData
						.getData()));
				paperList.add((Paper) value);
				System.out.println("Key - Data : " + key);// + " - " + value +
															// ""
				len++;
				if (len > max)
					break;
			}
		} catch (DatabaseException de) {
			System.err.println("Error accessing database." + de);
		} finally {
			// Cursors must be closed.
			cursor.close();
		}
		System.out.println(len);
		return paperList;
	}

}


package com.laotoumo.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.bzip2.CBZip2OutputStream;

import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.ZInputStream;
import com.jcraft.jzlib.ZOutputStream;

public class BytesZip {

	/**
	 * 将对象转换成字节数组
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] toBytes(Serializable object) throws Exception {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
		objectStream.writeObject(object);
		objectStream.flush();
		objectStream.close();
		return byteStream.toByteArray();
	}

	/**
	 * 将字节数组转换成对象
	 * 
	 * @param bytes
	 * @return
	 */
	public static Object toObject(byte[] bytes) throws Exception {
		if (bytes == null) {
			return null;
		} else {
			ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
			ObjectInputStream objectStream = new ObjectInputStream(byteStream);
			return objectStream.readObject();
		}

	}

	/***
	 * 压缩GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] gZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			GZIPOutputStream gzip = new GZIPOutputStream(bos);
			gzip.write(data);
			gzip.finish();
			gzip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unGZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			GZIPInputStream gzip = new GZIPInputStream(bis);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = gzip.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			gzip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 压缩Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] zip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ZipOutputStream zip = new ZipOutputStream(bos);
			ZipEntry entry = new ZipEntry("zip");
			entry.setSize(data.length);
			zip.putNextEntry(entry);
			zip.write(data);
			zip.closeEntry();
			zip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			ZipInputStream zip = new ZipInputStream(bis);
			while (zip.getNextEntry() != null) {
				byte[] buf = new byte[1024];
				int num = -1;
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				while ((num = zip.read(buf, 0, buf.length)) != -1) {
					baos.write(buf, 0, num);
				}
				b = baos.toByteArray();
				baos.flush();
				baos.close();
			}
			zip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 压缩BZip2
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] bZip2(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos);
			bzip2.write(data);
			bzip2.flush();
			bzip2.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压BZip2
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unBZip2(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			CBZip2InputStream bzip2 = new CBZip2InputStream(bis);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = bzip2.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			bzip2.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/**
	 * 把字节数组转换成16进制字符串
	 * 
	 * @param bArray
	 * @return
	 */
	public static String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 压缩数据
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] jzlib(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ZOutputStream zOut = new ZOutputStream(out,
					JZlib.Z_DEFAULT_COMPRESSION);
			DataOutputStream objOut = new DataOutputStream(zOut);
			objOut.write(object);
			objOut.flush();
			zOut.close();
			data = out.toByteArray();
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * 解压被压缩的数据
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] unjzlib(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(object);
			ZInputStream zIn = new ZInputStream(in);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = zIn.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			data = baos.toByteArray();
			baos.flush();
			baos.close();
			zIn.close();
			in.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	public static void main(String[] args) {

		Date a = new Date();
		Date b = new Date();
		long diff = 0;

		String s = "this is a test";

		System.out.println("Str:" + bytesToHexString(s.getBytes()));

		a = new Date();
		byte[] b1 = zip(s.getBytes());
		System.out.println("zip:" + bytesToHexString(b1));
		byte[] b2 = unZip(b1);
		System.out.println("unZip:" + new String(b2));
		b = new Date();
		diff = (b.getTime() - a.getTime());
		System.out.println("时间:\t" + (diff));

		a = new Date();
		byte[] b3 = bZip2(s.getBytes());
		System.out.println("bZip2:" + bytesToHexString(b3));
		byte[] b4 = unBZip2(b3);
		System.out.println("unBZip2:" + new String(b4));
		b = new Date();
		diff = (b.getTime() - a.getTime());
		System.out.println("时间:\t" + (diff));

		a = new Date();

		byte[] b5 = gZip(s.getBytes());
		System.out.println("bZip2:" + bytesToHexString(b5));
		byte[] b6 = unGZip(b5);
		System.out.println("unBZip2:" + new String(b6));
		b = new Date();
		diff = (b.getTime() - a.getTime());
		System.out.println("时间:\t" + (diff));

		a = new Date();

		byte[] b7 = jzlib(s.getBytes());
		System.out.println("jzlib:" + bytesToHexString(b7));
		byte[] b8 = unjzlib(b7);
		System.out.println("unjzlib:" + new String(b8));
		b = new Date();
		diff = (b.getTime() - a.getTime());
		System.out.println("时间:\t" + (diff));

	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值