berkeleydb储存URL队列的简单实现增、删、查(二)

2014-11-23 23:25:16 · 作者: · 浏览: 37
BName, String rankPage) { boolean mark = false; // 配置环境 https://community.oracle.com/thread/996592 start=0&tstart=0 问题地址 EnvironmentConfig envConfig = new EnvironmentConfig(); // 设置配置事务 envConfig.setTransactional(true); // 如果不存在就创建环境 envConfig.setAllowCreate(true); File file = new File(fileName); file.mkdirs(); try { Environment exampleEnv = new Environment(file, envConfig); Transaction txn = exampleEnv.beginTransaction(null, null); DatabaseConfig dbConfig = new DatabaseConfig(); dbConfig.setTransactional(true); dbConfig.setAllowCreate(true); dbConfig.setSortedDuplicates(false); Database exampleDb = exampleEnv.openDatabase(txn, databaseDBName, dbConfig); txn.commit(); DatabaseEntry theKey = new DatabaseEntry(url.getBytes("utf-8")); DatabaseEntry theData = new DatabaseEntry( rankPage.getBytes("utf-8")); exampleDb.put(null, theKey, theData); exampleDb.close(); exampleEnv.close(); } catch (Exception e) { e.printStackTrace(); mark = false; } return mark; } // 读取没有访问过的URL public String readerURL(String fileName, String databaseDBName) { // boolean mark = false; // 配置环境 EnvironmentConfig envConfig = new EnvironmentConfig(); // 设置配置事务 envConfig.setTransactional(true); // 如果不存在就创建环境 envConfig.setAllowCreate(true); File file = new File(fileName); String theKey = null; // file.mkdirs(); try { Environment exampleEnv = new Environment(file, envConfig); // Transaction txn = exampleEnv.beginTransaction(null,null); DatabaseConfig dbConfig = new DatabaseConfig(); dbConfig.setTransactional(true); dbConfig.setAllowCreate(true); dbConfig.setSortedDuplicates(false); Database myDB = exampleEnv.openDatabase(null, databaseDBName, dbConfig); // txn.commit(); // txn = exampleEnv.beginTransaction(null,null); Cursor cursor = myDB.openCursor(null, null); DatabaseEntry foundKey = new DatabaseEntry(); DatabaseEntry foundValue = new DatabaseEntry(); // cursor.getPrev()与cursor.getNext()的区别:一个是从前往后读取,一个是从后往前读取 // 这里讲访问遍历
数据库
全部数据while循环噶为if判断,则就只读取第一条数据 if (cursor.getNext(foundKey, foundValue, LockMode.DEFAULT) == OperationStatus.SUCCESS) { theKey = new String(foundKey.getData(), "UTF-8"); } cursor.close(); myDB.close(); exampleEnv.close(); } catch (Exception e) { e.printStackTrace(); } return theKey; } // 读取已经爬取过的URL public String readerUsedURL(String fileName, String databaseDBName, String url) { // 配置环境 EnvironmentConfig envConfig = new EnvironmentConfig(); // 设置配置事务 envConfig.setTransactional(true); // 如果不存在就创建环境 envConfig.setAllowCreate(true); File file = new File(fileName); String theKey = null; // file.mkdirs(); try { Environment exampleEnv = new Environment(file, envConfig); Transaction txn = exampleEnv.beginTransaction(null, null); DatabaseConfig dbConfig = new DatabaseConfig(); dbConfig.setTransactional(true); dbConfig.setAllowCreate(true); dbConfig.setSortedDuplicates(false); Database myDB = exampleEnv.o