private void executePutUnlessExists(CheckAndSetRequest checkAndSetRequest) { try { Map<Cell, byte[]> value = ImmutableMap.of(checkAndSetRequest.cell(), checkAndSetRequest.newValue()); putUnlessExists(checkAndSetRequest.table(), value); } catch (KeyAlreadyExistsException e) { throw new CheckAndSetException("Value unexpectedly present when running check and set", e); } }
private void executeCheckAndSet(CheckAndSetRequest request) { Preconditions.checkArgument(request.oldValue().isPresent()); runWrite(request.table(), table -> { //noinspection OptionalGetWithoutIsPresent table.update(request.cell(), AtlasDbConstants.TRANSACTION_TS, request.oldValue().get(), request.newValue()); return null; }); }
@Override public void checkAndSet(CheckAndSetRequest request) { TableStats s = timeOperation(request.table(), () -> super.checkAndSet(request)); // Only update stats after put was successful. s.totalPutCells.incrementAndGet(); // can only CAS one value incrementPutBytes(s, request.cell(), request.newValue()); }
request.cell(), request.table(), request.oldValue().orElse(null),
@Override public void checkAndSet(CheckAndSetRequest request) throws CheckAndSetException { TableReference tableRef = request.table(); Table table = getTableMap(tableRef); Cell cell = request.cell(); Optional<byte[]> oldValue = request.oldValue(); byte[] contents = request.newValue(); Key key = getKey(table, cell, AtlasDbConstants.TRANSACTION_TS); if (oldValue.isPresent()) { byte[] storedValue = table.entries.get(key); boolean succeeded = Arrays.equals(storedValue, oldValue.get()) && table.entries.replace(key, storedValue, copyOf(contents)); if (!succeeded) { byte[] actual = table.entries.get(key); // Re-fetch, something may have happened between get and replace throwCheckAndSetException(cell, tableRef, oldValue.get(), actual); } } else { byte[] oldContents = putIfAbsent(table, key, contents); if (oldContents != null) { throwCheckAndSetException(cell, tableRef, null, oldContents); } } }
private static CqlQuery insertIfNotExists(CheckAndSetRequest request) { Preconditions.checkState(!request.oldValue().isPresent(), "insertIfNotExists queries should only be made if we don't have an old value"); return ImmutableCqlQuery.builder() .safeQueryFormat("INSERT INTO \"%s\" (key, column1, column2, value)" + " VALUES (%s, %s, %s, %s) IF NOT EXISTS;") .addArgs( LoggingArgs.internalTableName(request.table()), UnsafeArg.of("row", encodeCassandraHexString(request.cell().getRowName())), UnsafeArg.of("column", encodeCassandraHexString(request.cell().getColumnName())), SafeArg.of("cassandraTimestamp", CASSANDRA_TIMESTAMP), UnsafeArg.of("newValue", encodeCassandraHexString(request.newValue()))) .build(); }
private static CqlQuery updateIfMatching(CheckAndSetRequest request) { Preconditions.checkState(request.oldValue().isPresent(), "updateIfMatching queries should only be made if we do have an old value"); return ImmutableCqlQuery.builder() .safeQueryFormat("UPDATE \"%s\" SET value=%s WHERE key=%s AND column1=%s AND column2=%s IF value=%s;") .addArgs( LoggingArgs.internalTableName(request.table()), UnsafeArg.of("newValue", encodeCassandraHexString(request.newValue())), UnsafeArg.of("row", encodeCassandraHexString(request.cell().getRowName())), UnsafeArg.of("column", encodeCassandraHexString(request.cell().getColumnName())), SafeArg.of("cassandraTimestamp", CASSANDRA_TIMESTAMP), UnsafeArg.of("oldValue", encodeCassandraHexString(request.oldValue().get()))) .build(); }
private void executePutUnlessExists(CheckAndSetRequest checkAndSetRequest) { try { Map<Cell, byte[]> value = ImmutableMap.of(checkAndSetRequest.cell(), checkAndSetRequest.newValue()); putUnlessExists(checkAndSetRequest.table(), value); } catch (KeyAlreadyExistsException e) { throw new CheckAndSetException("Value unexpectedly present when running check and set", e); } }
private void executeCheckAndSet(CheckAndSetRequest request) { Preconditions.checkArgument(request.oldValue().isPresent()); runWrite(request.table(), table -> { //noinspection OptionalGetWithoutIsPresent table.update(request.cell(), AtlasDbConstants.TRANSACTION_TS, request.oldValue().get(), request.newValue()); return null; }); }
@Override public void checkAndSet(CheckAndSetRequest request) { TableStats s = timeOperation(request.table(), () -> super.checkAndSet(request)); // Only update stats after put was successful. s.totalPutCells.incrementAndGet(); // can only CAS one value incrementPutBytes(s, request.cell(), request.newValue()); }
request.cell(), request.table(), request.oldValue().orElse(null),
@Override public void checkAndSet(CheckAndSetRequest request) throws CheckAndSetException { TableReference tableRef = request.table(); Table table = getTableMap(tableRef); Cell cell = request.cell(); Optional<byte[]> oldValue = request.oldValue(); byte[] contents = request.newValue(); Key key = getKey(table, cell, AtlasDbConstants.TRANSACTION_TS); if (oldValue.isPresent()) { byte[] storedValue = table.entries.get(key); boolean succeeded = Arrays.equals(storedValue, oldValue.get()) && table.entries.replace(key, storedValue, copyOf(contents)); if (!succeeded) { byte[] actual = table.entries.get(key); // Re-fetch, something may have happened between get and replace throwCheckAndSetException(cell, tableRef, oldValue.get(), actual); } } else { byte[] oldContents = putIfAbsent(table, key, contents); if (oldContents != null) { throwCheckAndSetException(cell, tableRef, null, oldContents); } } }
private static CqlQuery insertIfNotExists(CheckAndSetRequest request) { Preconditions.checkState(!request.oldValue().isPresent(), "insertIfNotExists queries should only be made if we don't have an old value"); return ImmutableCqlQuery.builder() .safeQueryFormat("INSERT INTO \"%s\" (key, column1, column2, value)" + " VALUES (%s, %s, %s, %s) IF NOT EXISTS;") .addArgs( LoggingArgs.internalTableName(request.table()), UnsafeArg.of("row", encodeCassandraHexString(request.cell().getRowName())), UnsafeArg.of("column", encodeCassandraHexString(request.cell().getColumnName())), SafeArg.of("cassandraTimestamp", CASSANDRA_TIMESTAMP), UnsafeArg.of("newValue", encodeCassandraHexString(request.newValue()))) .build(); }
private static CqlQuery updateIfMatching(CheckAndSetRequest request) { Preconditions.checkState(request.oldValue().isPresent(), "updateIfMatching queries should only be made if we do have an old value"); return ImmutableCqlQuery.builder() .safeQueryFormat("UPDATE \"%s\" SET value=%s WHERE key=%s AND column1=%s AND column2=%s IF value=%s;") .addArgs( LoggingArgs.internalTableName(request.table()), UnsafeArg.of("newValue", encodeCassandraHexString(request.newValue())), UnsafeArg.of("row", encodeCassandraHexString(request.cell().getRowName())), UnsafeArg.of("column", encodeCassandraHexString(request.cell().getColumnName())), SafeArg.of("cassandraTimestamp", CASSANDRA_TIMESTAMP), UnsafeArg.of("oldValue", encodeCassandraHexString(request.oldValue().get()))) .build(); }