/** * Copies entries from an existing environment db to a new one. If historyMap is not provided, * only logs the entries that would have been copied. * * @param sourceDir existing environment database directory * @param historyMap new environment db (or null for a dry run) * @return number of records * @throws DatabaseException */ private static int copyPersistEnv(File sourceDir, StoredSortedMap<String, Map> historyMap) throws DatabaseException { int count = 0; // open the source env history DB, copying entries to target env EnhancedEnvironment sourceEnv = setupCopyEnvironment(sourceDir, true); StoredClassCatalog sourceClassCatalog = sourceEnv.getClassCatalog(); DatabaseConfig historyDbConfig = HISTORY_DB_CONFIG.toDatabaseConfig(); historyDbConfig.setReadOnly(true); Database sourceHistoryDB = sourceEnv.openDatabase(null, URI_HISTORY_DBNAME, historyDbConfig); StoredSortedMap<String, Map> sourceHistoryMap = new StoredSortedMap<String, Map>( sourceHistoryDB, new StringBinding(), new SerialBinding<Map>(sourceClassCatalog, Map.class), true); Iterator<Entry<String, Map>> iter = sourceHistoryMap.entrySet().iterator(); while (iter.hasNext()) { Entry<String, Map> item = iter.next(); if (logger.isLoggable(Level.FINE)) { logger.fine(item.getKey() + " " + new JSONObject(item.getValue())); } if (historyMap != null) { historyMap.put(item.getKey(), item.getValue()); } count++; } StoredIterator.close(iter); sourceHistoryDB.close(); sourceEnv.close(); return count; }
/** * Populates a new environment db from an old environment db or a persist log. If path to new * environment is not provided, only logs the entries that would have been populated. * * @param sourcePath source of old entries: can be a path to an existing environment db, or a URL * or path to a persist log * @param envFile path to new environment db (or null for a dry run) * @return number of records * @throws DatabaseException * @throws IOException */ public static int populatePersistEnv(String sourcePath, File envFile) throws IOException { int count = 0; StoredSortedMap<String, Map> historyMap = null; EnhancedEnvironment targetEnv = null; StoredClassCatalog classCatalog = null; Database historyDB = null; if (envFile != null) { // set up target environment FileUtils.ensureWriteableDirectory(envFile); targetEnv = setupCopyEnvironment(envFile); classCatalog = targetEnv.getClassCatalog(); historyDB = targetEnv.openDatabase(null, URI_HISTORY_DBNAME, HISTORY_DB_CONFIG.toDatabaseConfig()); historyMap = new StoredSortedMap<String, Map>( historyDB, new StringBinding(), new SerialBinding<Map>(classCatalog, Map.class), true); } try { count = copyPersistSourceToHistoryMap(new File(sourcePath), historyMap); } finally { // in finally block so that we unlock the target env even if we // failed to populate it if (envFile != null) { logger.info(count + " records imported from " + sourcePath + " to BDB env " + envFile); historyDB.sync(); historyDB.close(); targetEnv.close(); } else { logger.info(count + " records found in " + sourcePath); } } return count; }