Search code examples
javaxodus

Can both put and delete be transaction in Xodus?


Here's a batch method in our code:

@Override
public boolean batchPutDelete(String instance, final String storeName, final Map<String, String> properties, final String... keys) {
    final Boolean[] isSuccess = {false};
    final List<String> keyList = Arrays.asList(keys);
    final Environment env = Environments.newInstance(xodusRoot + instance);
    env.executeInTransaction(new TransactionalExecutable() {
        @Override
        public void execute(@NotNull final Transaction txn) {
            final Store store = env.openStore(storeName, StoreConfig.WITHOUT_DUPLICATES, txn);
            for (String key : keyList) {
                isSuccess[0] = store.delete(txn, StringBinding.stringToEntry(key));
            }
            Iterator<String> it = properties.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String string = properties.get(key);
                isSuccess[0] = store.put(txn, StringBinding.stringToEntry(key), StringBinding.stringToEntry(string));
            }
        }
    });
    env.close();
    return isSuccess[0];
}

I want to ask how transaction this code is, can both delete and put be transactional? The purpose of this method is to delete the existing keys first and then put it again with different values, the intention being it should be transaction so it any delete or add fails in the loop, it should roll back. Is this code doing that already?


Solution

  • Yes, transaction would commit either all changes, or nothing. In addition, you don't have to delete keys before putting new values for them.