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 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 Date getVisitDate(UserFacade user, ProjectFacade project) { Environment env = getEnv(user.getId().toString()); Store store = getStore(env, PROJECT_VISIT_STORE); return env.computeInTransaction(new TransactionalComputable<Date>() { @Override public Date compute(Transaction txn) { long millis = readLong(store, txn, new LongByteIterable(project.getId()), -1); if (millis != -1) return new Date(millis); else return null; } }); }
@Override public Collection<ObjectId> getFixCommits(Project project, Long issueNumber) { Environment env = getEnv(project.getId().toString()); Store store = getStore(env, FIX_COMMITS_STORE); return env.computeInTransaction(new TransactionalComputable<Collection<ObjectId>>() { @Override public Collection<ObjectId> compute(Transaction txn) { return readCommits(store, txn, new LongByteIterable(issueNumber)); } }); }
@Override public boolean isNotified(User user, PullRequest request) { Environment env = getEnv(request.getTargetProject().getId().toString()); Store store = getStore(env, PULL_REQUEST_NOTIFICATION_STORE); return env.computeInTransaction(new TransactionalComputable<Boolean>() { @Override public Boolean compute(Transaction txn) { return readBoolean(store, txn, new LongsByteIterable(Lists.newArrayList(user.getId(), request.getId())), false); } }); }
@Override public boolean isNotified(User user, Issue issue) { Environment env = getEnv(issue.getProject().getId().toString()); Store store = getStore(env, ISSUE_NOTIFICATION_STORE); return env.computeInTransaction(new TransactionalComputable<Boolean>() { @Override public Boolean compute(Transaction txn) { return readBoolean(store, txn, new LongsByteIterable(Lists.newArrayList(user.getId(), issue.getId())), false); } }); }
@Override public Date getPullRequestVisitDate(User user, PullRequest request) { Environment env = getEnv(request.getTargetProject().getId().toString()); Store store = getStore(env, PULL_REQUEST_VISIT_STORE); return env.computeInTransaction(new TransactionalComputable<Date>() { @Override public Date compute(Transaction txn) { long millis = readLong(store, txn, new LongsByteIterable(Lists.newArrayList(user.getId(), request.getId())), -1); if (millis != -1) return new Date(millis); else return null; } }); }
@Override public Date getPullRequestCodeCommentsVisitDate(User user, PullRequest request) { Environment env = getEnv(request.getTargetProject().getId().toString()); Store store = getStore(env, PULL_REQUEST_CODE_COMMENTS_VISIT_STORE); return env.computeInTransaction(new TransactionalComputable<Date>() { @Override public Date compute(Transaction txn) { long millis = readLong(store, txn, new LongsByteIterable(Lists.newArrayList(user.getId(), request.getId())), -1); if (millis != -1) return new Date(millis); else return null; } }); }
@Override public Date getIssueVisitDate(User user, Issue issue) { Environment env = getEnv(issue.getProject().getId().toString()); Store store = getStore(env, ISSUE_VISIT_STORE); return env.computeInTransaction(new TransactionalComputable<Date>() { @Override public Date compute(Transaction txn) { long millis = readLong(store, txn, new LongsByteIterable(Lists.newArrayList(user.getId(), issue.getId())), -1); if (millis != -1) return new Date(millis); else return null; } }); }
@Override public Date getCodeCommentVisitDate(User user, CodeComment comment) { Environment env = getEnv(comment.getProject().getId().toString()); Store store = getStore(env, CODE_COMMENT_VISIT_STORE); return env.computeInTransaction(new TransactionalComputable<Date>() { @Override public Date compute(Transaction txn) { long millis = readLong(store, txn, new LongsByteIterable(Lists.newArrayList(user.getId(), comment.getId())), -1); if (millis != -1) return new Date(millis); else return null; } }); }
@Override public boolean remove( final LocalDB.DB db, final String key ) throws LocalDBException { checkStatus( true ); return environment.computeInTransaction( transaction -> { final Store store = getStore( db ); return store.delete( transaction, bindMachine.keyToEntry( key ) ); } ); }
@Override public Collection<ObjectId> getPrevCommits(Project project, Long buildId) { Environment env = getEnv(project.getId().toString()); Store defaultStore = getStore(env, DEFAULT_STORE); Store prevCommitsStore = getStore(env, PREV_COMMITS_STORE); return env.computeInTransaction(new TransactionalComputable<Collection<ObjectId>>() { @Override public Collection<ObjectId> compute(Transaction txn) { if (readLong(defaultStore, txn, LAST_BUILD_KEY, 0) < buildId) { batchWorkManager.submit(getBatchWorker(project.getId()), new Prioritized(PRIORITY)); return null; } else { return readCommits(prevCommitsStore, txn, new LongByteIterable(buildId)); } } }); }
@Override public boolean put( final LocalDB.DB db, final String key, final String value ) throws LocalDBException { checkStatus( true ); return environment.computeInTransaction( transaction -> { final ByteIterable k = bindMachine.keyToEntry( key ); final ByteIterable v = bindMachine.valueToEntry( value ); final Store store = getStore( db ); return store.put( transaction, k, v ); } ); }
@SuppressWarnings("CheckReturnValue") synchronized <T> T computeInTransaction(final Supplier<T> fn) { return this.environment.computeInTransaction( txn -> { try { IndexWriter indexWriter = this.createIndexWriter(this.directory, createIndexConfig(this.analyzer)); IndexReader indexReader = DocumentSearcher.createIndexReader(this.directory); IndexSearcher indexSearcher = this.createIndexSearcher(indexReader); return fn.get(); } catch (IOException ex) { txn.abort(); throw new UncheckedIOException(ex); } finally { this.closeIndexSearcher(); this.closeIndexWriter(); } }); }
Store prevCommitsStore = getStore(env, PREV_COMMITS_STORE); Long lastBuildId = env.computeInTransaction(new TransactionalComputable<Long>() {
Store pullRequestStore = getStore(env, PULL_REQUEST_STORE); Long lastPullRequestUpdateId = env.computeInTransaction(new TransactionalComputable<Long>() { Long lastCodeCommentId = env.computeInTransaction(new TransactionalComputable<Long>() {
@LocalDB.WriteOperation public boolean putIfAbsent( final LocalDB.DB db, final String key, final String value ) throws LocalDBException { checkStatus( true ); return environment.computeInTransaction( transaction -> { final ByteIterable k = bindMachine.keyToEntry( key ); final ByteIterable v = bindMachine.valueToEntry( value ); final Store store = getStore( db ); final ByteIterable existingValue = store.get( transaction, k ); if ( existingValue != null ) { return false; } return store.put( transaction, k, v ); } ); }