Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
general cleanup for more java17 idiomatic
  • Loading branch information
robfrank committed Nov 3, 2024
1 parent ba1ba1d commit 6bf8400
Show file tree
Hide file tree
Showing 78 changed files with 1,333 additions and 1,675 deletions.
31 changes: 12 additions & 19 deletions engine/src/main/java/com/arcadedb/database/DatabaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* Thread local to store transaction data.
*/
public class DatabaseContext extends ThreadLocal<Map<String, DatabaseContext.DatabaseContextTL>> {
public static final DatabaseContext INSTANCE = new DatabaseContext();

public DatabaseContextTL init(final DatabaseInternal database) {
return init(database, null);
}
Expand Down Expand Up @@ -64,9 +66,8 @@ public DatabaseContextTL init(final DatabaseInternal database, final Transaction
}
}

if (current.transactions.isEmpty())
current.transactions.add(
firstTransaction != null ? firstTransaction : new TransactionContext(database.getWrappedDatabaseInstance()));
if (current.transactions.isEmpty()) current.transactions.add(
firstTransaction != null ? firstTransaction : new TransactionContext(database.getWrappedDatabaseInstance()));

return current;
}
Expand Down Expand Up @@ -98,8 +99,7 @@ public void clear() {

public DatabaseContextTL getContext(final String name) {
final DatabaseContextTL ctx = getContextIfExists(name);
if (ctx == null)
throw new DatabaseOperationException("Transaction context not found on current thread");
if (ctx == null) throw new DatabaseOperationException("Transaction context not found on current thread");
return ctx;
}

Expand All @@ -110,8 +110,7 @@ public DatabaseContextTL getContextIfExists(final String name) {

public DatabaseContextTL removeContext(final String name) {
final Map<String, DatabaseContextTL> map = get();
if (map != null)
return map.remove(name);
if (map != null) return map.remove(name);
return null;
}

Expand All @@ -124,8 +123,7 @@ public Database getActiveDatabase() {
final DatabaseContextTL tl = map.values().iterator().next();
if (tl != null) {
final TransactionContext tx = tl.getLastTransaction();
if (tx != null)
return tx.getDatabase();
if (tx != null) return tx.getDatabase();
}
}
return null;
Expand Down Expand Up @@ -166,25 +164,21 @@ public Binary getTemporaryBuffer2() {
}

public TransactionContext getLastTransaction() {
if (transactions.isEmpty())
return null;
if (transactions.isEmpty()) return null;
return transactions.get(transactions.size() - 1);
}

public void pushTransaction(final TransactionContext tx) {
if (transactions.size() + 1 > maxNested)
throw new TransactionException("Exceeded number of " + transactions.size()
+ " nested transactions. Check your code if you are beginning new transactions without closing the previous one by mistake. Otherwise change this limit with setMaxNested()");
if (transactions.size() + 1 > maxNested) throw new TransactionException("Exceeded number of " + transactions.size()
+ " nested transactions. Check your code if you are beginning new transactions without closing the previous one by mistake. Otherwise change this limit with setMaxNested()");

transactions.add(tx);
}

public TransactionContext popIfNotLastTransaction() {
if (transactions.isEmpty())
return null;
if (transactions.isEmpty()) return null;

if (transactions.size() > 1)
return transactions.remove(transactions.size() - 1);
if (transactions.size() > 1) return transactions.remove(transactions.size() - 1);

return transactions.get(0);
}
Expand All @@ -198,5 +192,4 @@ public void setMaxNested(final int maxNested) {
}
}

public static final DatabaseContext INSTANCE = new DatabaseContext();
}
Loading

0 comments on commit 6bf8400

Please sign in to comment.