@Override public void close( ) { if ( closed ) { return; } cursor.close(); transaction.abort(); nextValue = null; closed = true; }
/** * Close this instance, optionally comitting any changes. * * <p> * This closes or commits the underlying {@link Transaction}, and closes any unclosed iterators returned from * {@link #getRange getRange()}. This instance will end up being closed even if commit fails. * * @param commit true to commit changes (if any) * @return true if already closed or successfully closed/commited, false if {@code commit} is true but the commit fails */ public boolean close(boolean commit) { if (!this.closed.compareAndSet(false, true)) return true; if (this.log.isTraceEnabled()) this.log.trace("closing " + this); if (commit) { if (this.tx.commit()) return true; this.tx.abort(); return false; } this.tx.abort(); return true; }
private static <T> T computeInTransaction(@NotNull final TransactionalComputable<T> computable, @NotNull final Transaction txn) { try { while (true) { final T result = computable.compute(txn); if (txn.isReadonly() || // txn can be read-only if Environment is in read-only mode txn.isFinished() || // txn can be finished if, e.g., it was aborted within computable txn.flush()) { return result; } txn.revert(); } } finally { abortIfNotFinished(txn); } }
private static void abortIfNotFinished(@NotNull final Transaction txn) { if (!txn.isFinished()) { txn.abort(); } }
@Override public <T> T computeInExclusiveTransaction(@NotNull TransactionalComputable<T> computable) { final Transaction current = getCurrentTransaction(); if (current == null) { return super.computeInExclusiveTransaction(computable); } if (!current.isExclusive()) { throw new ExodusException("Current transaction should be exclusive"); } return computable.compute(current); }
private void checkIfTransactionCreatedAgainstThis(@NotNull final Transaction txn) { if (txn.getEnvironment() != this) { throw new ExodusException("Transaction is created against another Environment"); } }
/** * Return a read-only snapshot containing the same data as this instance. * * <p> * Though based on the same underlying data, the returned instance and this instance retain no references to each other. * * @return read-only snapshot * @throws IllegalStateException if this instance is closed */ public XodusKVStore readOnlySnapshot() { Preconditions.checkState(!this.closed.get(), "transaction closed"); return new XodusKVStore(TransactionType.READ_ONLY, this.tx.getReadonlySnapshot(), this.store); }
@Override public void executeInExclusiveTransaction(@NotNull TransactionalExecutable executable) { final Transaction current = getCurrentTransaction(); if (current == null) { super.executeInExclusiveTransaction(executable); } else { if (!current.isExclusive()) { throw new ExodusException("Current transaction should be exclusive"); } executable.execute(current); } }
@TearDown(Trial) @Override public void teardown() throws IOException { tx.abort(); super.teardown(); } }
private static void executeInTransaction(@NotNull final TransactionalExecutable executable, @NotNull final Transaction txn) { try { while (true) { executable.execute(txn); if (txn.isReadonly() || // txn can be read-only if Environment is in read-only mode txn.isFinished() || // txn can be finished if, e.g., it was aborted within executable txn.flush()) { break; } txn.revert(); } } finally { abortIfNotFinished(txn); } }
} finally { if (!success) this.tx.abort();
@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(); } }); }
@SuppressWarnings("CheckReturnValue") synchronized void executeInTransaction(final Runnable runnable) { this.environment.executeInTransaction( txn -> { try { IndexWriter indexWriter = this.createIndexWriter(this.directory, createIndexConfig(this.analyzer)); runnable.run(); } catch (IOException ex) { txn.abort(); throw new UncheckedIOException(ex); } finally { this.closeIndexSearcher(); this.closeIndexWriter(); } }); }
@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(); } }); }