数据库使用

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
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;
import com.sleepycat.je.Transaction;

public class BerkeleyDB {

	public static final Logger LOG = Logger.getLogger("BerkeleyDB");
	private static Database myDb = null;// 数据原文
	private static Database delDocDB = null; // 删除数据
	private static Database classCatalogDb;
	private static final String db_dir = "d:/index/test";
	private static Environment dbEnv = null;
	private static long docNum = 0;
	static {
		try {
			init();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private static void init() throws IOException {
		LOG.info("初始化DB......");
		try {
			File file = new File(db_dir + File.separator + "segments"
					+ File.separator + "bdbdata");
			if (!file.exists())
				file.mkdirs();
			EnvironmentConfig envConf = new EnvironmentConfig();
			envConf.setAllowCreate(true);
			envConf.setTransactional(true);
			envConf.setCachePercent(30);
			// envConf.setCacheSize(300000000);
			// envConf.setConfigParam("je.log.fileCacheSize","1000000");
			envConf.setConfigParam("je.log.fileMax", "1000000000");
			dbEnv = new Environment(file, envConf);
			// EnvironmentStats envStats = dbEnv.getStats(null);
			DatabaseConfig dbConf = new DatabaseConfig();
			dbConf.setAllowCreate(true);
			dbConf.setTransactional(true);
			// dbConf.setDeferredWrite(true);
			dbConf.setSortedDuplicates(false);

			String databaseName = "data";
			myDb = dbEnv.openDatabase(null, databaseName, dbConf);

			String delDocDBName = "delDB";
			delDocDB = dbEnv.openDatabase(null, delDocDBName, dbConf);// 也使用Properties

			// classCatalog = new StoredClassCatalog(myDb);
			classCatalogDb = dbEnv.openDatabase(null, "ClassCatalogDB", dbConf);
			classCatalog = new StoredClassCatalog(classCatalogDb);
			docNum = myDb.count();

			dataBinding = new SerialBinding(classCatalog, Properties.class);

		} catch (Exception de) {
			// LOG.info("BDB初始化失败,请确认文件目录存在,并且具有读写权限");
			de.printStackTrace();
		}
	}

	public static void close() throws IOException {
		LOG.info("关闭DB......");
		try {
			if (myDb != null) {
				myDb.close();
			}
			if (delDocDB != null) {
				delDocDB.close();
			}
			if (classCatalogDb != null) {
				classCatalogDb.close();
			}
			if (dbEnv != null) {
				dbEnv.cleanLog();
				dbEnv.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void syncDb() {

		if (myDb != null)
			try {
				myDb.sync();
			} catch (DatabaseException e) {
				e.printStackTrace();
			}

	}

	private static StoredClassCatalog classCatalog;
	private static EntryBinding dataBinding;

	public static synchronized long getRecordNum() throws DatabaseException {
		return docNum++;
	}

	public static synchronized void saveRecord(String docNo, Properties object)
			throws IOException, DatabaseException {
		if (docNo == null)
			return;
		if (object == null)
			object = localProperties;
		if (classCatalog == null || dataBinding == null) {
			classCatalog = new StoredClassCatalog(myDb);
			dataBinding = new SerialBinding(classCatalog, Properties.class);
		}
		DatabaseEntry theKey = new DatabaseEntry();
		DatabaseEntry theData = new DatabaseEntry();

		dataBinding.objectToEntry(object, theData);
		theKey.setData(docNo.getBytes());
		synchronized (myDb) {
			myDb.putNoOverwrite(null, theKey, theData);
		}
	}

	public static synchronized void saveDelDoc(String docNo, Properties object)
			throws IOException, DatabaseException {
		if (docNo == null)
			return;
		if (object == null)
			object = localProperties;
		if (classCatalog == null || dataBinding == null) {
			classCatalog = new StoredClassCatalog(delDocDB);
			dataBinding = new SerialBinding(classCatalog, Properties.class);
		}
		DatabaseEntry theKey = new DatabaseEntry();
		DatabaseEntry theData = new DatabaseEntry();

		dataBinding.objectToEntry(object, theData);
		theKey.setData(docNo.getBytes());
		synchronized (delDocDB) {
			delDocDB.put(null, theKey, theData);
		}
	}

	/**
	 * 从数据库取删除的数据,并组装成query
	 * 
	 * @throws IOException
	 * @throws DatabaseException
	 */
	public static List getDelDocQuery() throws IOException, DatabaseException {
		synchronized (delDocDB) {
			List delList = null;
			if (delDocDB.count() > 0) {
				delList = new ArrayList();
				Transaction tra = dbEnv.beginTransaction(null, null);
				com.sleepycat.je.Cursor cursor = delDocDB.openCursor(tra, null);
				DatabaseEntry foundKey = new DatabaseEntry();
				DatabaseEntry foundData = new DatabaseEntry();
				try {
					Properties properties = null;
					while (cursor.getNext(foundKey, foundData,
							com.sleepycat.je.LockMode.DEFAULT) == com.sleepycat.je.OperationStatus.SUCCESS) {
						properties = (Properties) dataBinding
								.entryToObject(foundData);
						if (properties != null
								&& properties.get("docNo") != null) {
							delList.add((String) properties.get("docNo"));
						}
						cursor.delete();
					}
					cursor.close();
					tra.commit();
				} catch (Exception e) {
					LOG.info("加载冗余数据发生错误");
					tra.abort();
					e.printStackTrace();
				}
				return delList;
			} else {
				return null;
			}
		}
	}

	public static synchronized void update(String docNo, Properties object)
			throws IOException, DatabaseException {
		if (docNo == null)
			return;
		if (object == null)
			object = localProperties;
		if (classCatalog == null || dataBinding == null) {
			classCatalog = new StoredClassCatalog(myDb);
			dataBinding = new SerialBinding(classCatalog, Properties.class);
		}
		DatabaseEntry theKey = new DatabaseEntry();
		DatabaseEntry theData = new DatabaseEntry();

		dataBinding.objectToEntry(object, theData);
		theKey.setData(docNo.getBytes());
		synchronized (myDb) {
			myDb.put(null, theKey, theData);
		}
	}

	private final static Properties localProperties = new Properties();

	public static Properties getRecord(String docNo) throws IOException {
		if (docNo == null)
			return localProperties;
		DatabaseEntry theKey = new DatabaseEntry((docNo.getBytes()));
		DatabaseEntry theData = new DatabaseEntry();
		Properties properties = null;
		try {
			if (myDb.get(null, theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
				properties = (Properties) dataBinding.entryToObject(theData);
			}
		} catch (Exception ex) {
			throw new IOException(ex.getMessage());
		}
		return properties;
	}

	public static Properties getDelDoc(String docNo) throws IOException {
		if (docNo == null)
			return localProperties;
		DatabaseEntry theKey = new DatabaseEntry((docNo.getBytes()));
		DatabaseEntry theData = new DatabaseEntry();
		Properties properties = null;
		try {
			if (delDocDB.get(null, theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
				properties = (Properties) dataBinding.entryToObject(theData);
			}
		} catch (Exception ex) {
			throw new IOException(ex.getMessage());
		}
		return properties;
	}

	public static void main(String[] args) {
		long start = 0;
		try {
			java.util.Properties properties = new java.util.Properties();
			properties
					.put("a",
							"测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息");
			properties.put("b",
					"测试数据信息测试数据信asdfasdfasdfasdf息测试数据信息测试数据信息测试数据信息");
			properties.put("c",
					"测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息");
			properties
					.put("d",
							"测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息");
			
			properties
					.put(
							"n",
							"测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息测试数据信息");

			start = System.currentTimeMillis();
			for (int i = 0; i < 100; i++) {

				if (i % 10 == 0) {
					System.out.print(" " + i);
				}
				saveRecord(i+"", properties);
			}
			System.out.println("插入记录时间:" + (System.currentTimeMillis() - start)
					+ " 命中率:" + dbEnv.getStats(null).getNCacheMiss());
			//
			start = System.currentTimeMillis();
			for (int i = 0; i < 20; i++) {

				Properties loadP = getRecord(String.valueOf(0L + i));

				System.out.println(loadP);

			}
			System.out.println("读取的时间20条记录时间:"
					+ (System.currentTimeMillis() - start) + " 命中率:"
					+ dbEnv.getStats(null).getNCacheMiss());
			for (int i = 0; i < 100; i++) {

				Properties loadP = getRecord(String.valueOf(0L + i));

				System.out.println(loadP);

			}
			System.out.println("读取的时间100条记录时间:"
					+ (System.currentTimeMillis() - start) + " 命中率:"
					+ dbEnv.getStats(null).getNCacheMiss());
			for (int i = 0; i < 100; i++) {

				Properties loadP = getRecord(String.valueOf(0L + i));

				System.out.println(loadP);

			}
			System.out.println("读取的时间1000条记录时间:"
					+ (System.currentTimeMillis() - start) + " 命中率:"
					+ dbEnv.getStats(null).getNCacheMiss());
			for (int i = 0; i < 100; i++) {

				Properties loadP = getRecord(String.valueOf(0L + i));

				System.out.println(loadP);

			}
			System.out.println("读取的时间10000条记录时间:"
					+ (System.currentTimeMillis() - start));
			// for(int i=0 ; i<100000 ; i++)
			// {
			//				
			// Properties loadP = getRecord(0L+i) ;
			//				
			// // System.out.println(loadP.get("j")) ;
			//				
			// }
			// System.out.println("读取的时间100000条记录时间:"+(System.currentTimeMillis()-start))
			// ;
			start = System.currentTimeMillis();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}
		System.out.println("写入文件时间:" + (System.currentTimeMillis() - start));
	}

	public static Database getDelDocDB() {
		return delDocDB;
	}

	public static void setDelDocDB(Database delDocDB) {
		BerkeleyDB.delDocDB = delDocDB;
	}

	public static EntryBinding getDataBinding() {
		return dataBinding;
	}

	public static void setDataBinding(EntryBinding dataBinding) {
		BerkeleyDB.dataBinding = dataBinding;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值