private void putIfNotUpdate( DbReadTable readTable, DbWriteTable writeTable, TableReference tableRef, List<Entry<Cell, Value>> batch, KeyAlreadyExistsException ex) { Map<Cell, Long> timestampByCell = Maps.newHashMap(); for (Entry<Cell, Value> entry : batch) { timestampByCell.put(entry.getKey(), entry.getValue().getTimestamp() + 1); } Map<Cell, Value> results = extractResults(readTable, tableRef, readTable.getLatestCells(timestampByCell, true)); ListIterator<Entry<Cell, Value>> iter = batch.listIterator(); while (iter.hasNext()) { Entry<Cell, Value> entry = iter.next(); Cell key = entry.getKey(); Value value = entry.getValue(); if (results.containsKey(key)) { if (results.get(key).equals(value)) { iter.remove(); } else { throw new KeyAlreadyExistsException( "primary key violation for key " + key + " with value " + value, ex); } } } writeTable.put(batch); }
@Override public void putWithTimestamps(TableReference tableRef, Multimap<Cell, Value> cellValues) throws KeyAlreadyExistsException { Iterable<List<Entry<Cell, Value>>> batches = IterablePartitioner.partitionByCountAndBytes( cellValues.entries(), config.mutationBatchCount(), config.mutationBatchSizeBytes(), tableRef, getValueSizingFunction()); runReadWrite(tableRef, (readTable, writeTable) -> { for (List<Entry<Cell, Value>> batch : batches) { try { writeTable.put(batch); } catch (KeyAlreadyExistsException e) { putIfNotUpdate(readTable, writeTable, tableRef, batch, e); } } return null; }); }
private void put(TableReference tableRef, Map<Cell, byte[]> values, long timestamp, boolean idempotent) { Iterable<List<Entry<Cell, byte[]>>> batches = IterablePartitioner.partitionByCountAndBytes( values.entrySet(), config.mutationBatchCount(), config.mutationBatchSizeBytes(), tableRef, getByteSizingFunction()); runReadWrite(tableRef, (readTable, writeTable) -> { for (List<Entry<Cell, byte[]>> batch : batches) { try { writeTable.put(batch, timestamp); } catch (KeyAlreadyExistsException e) { if (idempotent) { putIfNotUpdate(readTable, writeTable, tableRef, batch, timestamp, e); } else { throw e; } } } return null; }); }
private void putIfNotUpdate( DbReadTable readTable, DbWriteTable writeTable, TableReference tableRef, List<Entry<Cell, Value>> batch, KeyAlreadyExistsException ex) { Map<Cell, Long> timestampByCell = Maps.newHashMap(); for (Entry<Cell, Value> entry : batch) { timestampByCell.put(entry.getKey(), entry.getValue().getTimestamp() + 1); } Map<Cell, Value> results = extractResults(readTable, tableRef, readTable.getLatestCells(timestampByCell, true)); ListIterator<Entry<Cell, Value>> iter = batch.listIterator(); while (iter.hasNext()) { Entry<Cell, Value> entry = iter.next(); Cell key = entry.getKey(); Value value = entry.getValue(); if (results.containsKey(key)) { if (results.get(key).equals(value)) { iter.remove(); } else { throw new KeyAlreadyExistsException( "primary key violation for key " + key + " with value " + value, ex); } } } writeTable.put(batch); }
@Override public void putWithTimestamps(TableReference tableRef, Multimap<Cell, Value> cellValues) throws KeyAlreadyExistsException { Iterable<List<Entry<Cell, Value>>> batches = IterablePartitioner.partitionByCountAndBytes( cellValues.entries(), config.mutationBatchCount(), config.mutationBatchSizeBytes(), tableRef, getValueSizingFunction()); runReadWrite(tableRef, (readTable, writeTable) -> { for (List<Entry<Cell, Value>> batch : batches) { try { writeTable.put(batch); } catch (KeyAlreadyExistsException e) { putIfNotUpdate(readTable, writeTable, tableRef, batch, e); } } return null; }); }
private void put(TableReference tableRef, Map<Cell, byte[]> values, long timestamp, boolean idempotent) { Iterable<List<Entry<Cell, byte[]>>> batches = IterablePartitioner.partitionByCountAndBytes( values.entrySet(), config.mutationBatchCount(), config.mutationBatchSizeBytes(), tableRef, getByteSizingFunction()); runReadWrite(tableRef, (readTable, writeTable) -> { for (List<Entry<Cell, byte[]>> batch : batches) { try { writeTable.put(batch, timestamp); } catch (KeyAlreadyExistsException e) { if (idempotent) { putIfNotUpdate(readTable, writeTable, tableRef, batch, timestamp, e); } else { throw e; } } } return null; }); }