protected void removeEnv(String envKey) { synchronized (envs) { Environment env = envs.remove(envKey); if (env != null) env.close(); } }
@Override public Store compute(Transaction txn) { return env.openStore(storeName, StoreConfig.WITHOUT_DUPLICATES, txn); } });
protected Store getStore(Environment env, String storeName) { return env.computeInTransaction(new TransactionalComputable<Store>() { @Override public Store compute(Transaction txn) { return env.openStore(storeName, StoreConfig.WITHOUT_DUPLICATES, txn); } }); }
@Override public void close( ) throws LocalDBException { if ( environment != null && environment.isOpen() ) { environment.close(); try { Files.deleteIfExists( getDirtyFile().toPath() ); LOGGER.trace( () -> "deleted openLock file" ); } catch ( IOException e ) { LOGGER.error( "error creating openLock file: " + e.getMessage() ); } } status = LocalDB.Status.CLOSED; LOGGER.debug( () -> "closed" ); }
private synchronized void open() { final File loc = ProjectDatabase.getInstance().getBaseLocation(); if (nonNull(this.baseLocation) && !this.baseLocation.equals(loc)) { // change database if (nonNull(this.searcher)) { this.searcher.close(); this.searcher = null; } if (nonNull(this.environment)) { this.environment.close(); this.environment = null; } } if (isNull(this.environment)) { File env = new File(loc, "index"); this.environment = Environments.newContextualInstance(env); String location = this.environment.getLocation(); log.debug("open index database {}", location); try { this.searcher = new DocumentSearcher((ContextualEnvironment) this.environment); } catch (IOException e) { throw new UncheckedIOException(e); } } }
@Override public long size( final LocalDB.DB db ) throws LocalDBException { checkStatus( false ); return environment.computeInReadonlyTransaction( transaction -> { final Store store = getStore( db ); return store.count( transaction ); } ); }
@Override public void delete(Project project, Long buildId) { Environment env = getEnv(project.getId().toString()); Store store = getStore(env, PREV_COMMITS_STORE); env.executeInTransaction(new TransactionalExecutable() { @Override public void execute(Transaction txn) { store.delete(txn, new LongByteIterable(buildId)); } }); }
@Override public void truncate( final LocalDB.DB db ) throws LocalDBException { checkStatus( true ); { final long finalSize = this.size( db ); LOGGER.trace( () -> "begin truncate of " + db.toString() + ", size=" + finalSize ); } final Instant startDate = Instant.now(); environment.executeInTransaction( transaction -> { environment.truncateStore( db.toString(), transaction ); final Store newStoreReference = environment.openStore( db.toString(), StoreConfig.USE_EXISTING, transaction ); cachedStoreObjects.put( db, newStoreReference ); } ); { final long finalSize = this.size( db ); LOGGER.trace( () -> "completed truncate of " + db.toString() + " (" + TimeDuration.fromCurrent( startDate ).asCompactString() + ")" + ", size=" + finalSize ); } }
Store prevCommitsStore = getStore(env, PREV_COMMITS_STORE); Long lastBuildId = env.computeInTransaction(new TransactionalComputable<Long>() { env.executeInTransaction(new TransactionalExecutable() {
env.executeInTransaction(new TransactionalExecutable() { env.computeInReadonlyTransaction(new TransactionalComputable<ObjectId>() { env.executeInTransaction(new TransactionalExecutable() {
public static String getObjectName(@NotNull final Environment env) { return OBJECT_NAME_PREFIX + ", location=" + escapeLocation(env.getLocation()); } }
InnerIterator( final LocalDB.DB db ) { this.transaction = environment.beginReadonlyTransaction(); this.cursor = getStore( db ).openCursor( transaction ); doNext(); }
@SuppressWarnings("CheckReturnValue") synchronized <T> T searchInTransaction(final Supplier<T> fn) { return this.environment.computeInReadonlyTransaction( txn -> { try { final IndexReader indexReader = DocumentSearcher.createIndexReader(this.directory); IndexSearcher searcher = this.createIndexSearcher(indexReader); return fn.get(); } catch (IOException ex) { txn.abort(); throw new UncheckedIOException(ex); } finally { this.closeIndexSearcher(); } }); }
@Override public void visit(User user, Project project) { Environment env = getEnv(user.getId().toString()); Store store = getStore(env, PROJECT_VISIT_STORE); env.executeInTransaction(new TransactionalExecutable() { @Override public void execute(Transaction txn) { writeLong(store, txn, new LongByteIterable(project.getId()), System.currentTimeMillis()+1000L); } }); }
Store pullRequestStore = getStore(env, PULL_REQUEST_STORE); Long lastPullRequestUpdateId = env.computeInTransaction(new TransactionalComputable<Long>() { project, lastPullRequestUpdateId, BATCH_SIZE); for (PullRequestUpdate update: unprocessedPullRequestUpdates) { env.executeInTransaction(new TransactionalExecutable() { Long lastCodeCommentId = env.computeInTransaction(new TransactionalComputable<Long>() { for (CodeComment comment: unprocessedCodeComments) { if (comment.isValid()) { env.executeInTransaction(new TransactionalExecutable() {
static String getObjectName(@NotNull final Environment env) { return OBJECT_NAME_PREFIX + ", location=" + escapeLocation(env.getLocation()); }
@Setup(Trial) @Override public void setup(final BenchmarkParams b) throws IOException { super.setup(b); super.write(); tx = env.beginReadonlyTransaction(); // cannot share Cursor, as there's no Cursor.getFirst() to reset methods }
@Listen public void on(SystemStopping event) { synchronized (envs) { for (Environment env: envs.values()) env.close(); envs.clear(); } }
@Override public Collection<Long> getPullRequestIds(Project project, ObjectId commitId) { Environment env = getEnv(project.getId().toString()); Store store = getStore(env, PULL_REQUEST_STORE); return env.computeInTransaction(new TransactionalComputable<Collection<Long>>() { @Override public Collection<Long> compute(Transaction txn) { return readLongs(store, txn, new CommitByteIterable(commitId)); } }); }
@Override public Map<Day, Map<String, Integer>> getLineIncrements(Project project) { Environment env = getEnv(project.getId().toString()); Store store = getStore(env, DEFAULT_STORE); return env.computeInReadonlyTransaction(new TransactionalComputable<Map<Day, Map<String, Integer>>>() { @Override public Map<Day, Map<String, Integer>> compute(Transaction txn) { Map<Day, Map<String, Integer>> lineIncrements = new HashMap<>(); byte[] bytes = readBytes(store, txn, LINE_STATS_KEY); if (bytes != null) { @SuppressWarnings("unchecked") Map<Integer, Map<String, Integer>> storedMap = (Map<Integer, Map<String, Integer>>) SerializationUtils.deserialize(bytes); for (Map.Entry<Integer, Map<String, Integer>> entry: storedMap.entrySet()) lineIncrements.put(new Day(entry.getKey()), entry.getValue()); } return lineIncrements; } }); }