@Test public void testAddInsertionWithHint() { BatchMutation<String> batchMutate = new BatchMutation<String>(StringSerializer.get(), new BatchSizeHint(1, 50)); Column column = new Column(StringSerializer.get().toByteBuffer("c_name")); column.setValue(StringSerializer.get().toByteBuffer("c_val")); column.setTimestamp(System.currentTimeMillis()); batchMutate.addInsertion("key1", columnFamilies, column); Map<ByteBuffer, Map<String, List<Mutation>>> mutMap = batchMutate.getMutationMap(); assertEquals(1, mutMap.size()); assertEquals(ByteBuffer.wrap("key1".getBytes()), mutMap.keySet().iterator().next()); Map<String, List<Mutation>> cfMutMap = mutMap.values().iterator().next(); assertEquals(1, cfMutMap.size()); List<Mutation> cfMutList = cfMutMap.values().iterator().next(); assertTrue(cfMutList instanceof ArrayList); }
@Test public void testIsEmpty() { assertTrue(batchMutate.isEmpty()); // Insert a column Column c1 = new Column(StringSerializer.get().toByteBuffer("c_name")); c1.setValue(StringSerializer.get().toByteBuffer("c_val")); c1.setTimestamp(System.currentTimeMillis()); batchMutate.addInsertion("key1", columnFamilies, c1); assertFalse(batchMutate.isEmpty()); // Insert a Counter. CounterColumn cc1 = new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 13); batchMutate.addCounterInsertion("key1", columnFamilies, cc1); assertFalse(batchMutate.isEmpty()); }
@Override public <N> Mutator<K> addCounterDeletion(K key, String cf) { getPendingMutations().addDeletion(key, Arrays.asList(cf), new Deletion()); return this; }
/** * Batch executes all mutations scheduled to this Mutator instance by addInsertion, addDeletion etc. * May throw a HectorException which is a RuntimeException. * @return A MutationResult holds the status. */ @Override public MutationResult execute() { if (pendingMutations == null || pendingMutations.isEmpty()) { return new MutationResultImpl(true, 0, null); } final BatchMutation<K> mutations = pendingMutations.makeCopy(); pendingMutations = null; return new MutationResultImpl(keyspace.doExecuteOperation(new Operation<Void>(OperationType.WRITE) { @Override public Void execute(Cassandra.Client cassandra) throws Exception { cassandra.batch_mutate(mutations.getMutationMap(), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); return null; } })); }
@Test public void testBatchMutateBatchMutation() throws HectorException { BatchMutation<String> batchMutation = new BatchMutation<String>(StringSerializer.get()); List<String> columnFamilies = Arrays.asList("Standard1"); for (int i = 0; i < 10; i++) { col.setValue(StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j)); col.setTimestamp(connectionManager.createClock()); batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col); batchMutation = new BatchMutation<String>(StringSerializer.get()); batchMutation.addDeletion("testBatchMutateColumn_" + i, columnFamilies, deletion);
@Override public void batchMutate(BatchMutation batchMutate) throws HectorException { batchMutate(batchMutate.getMutationMap()); }
@Test public void testAddCounterDeletion() { Deletion counterDeletion = new Deletion(); SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("c_name")); counterDeletion.setPredicate(slicePredicate); batchMutate.addDeletion("key1", columnFamilies, counterDeletion); assertEquals(1, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); counterDeletion = new Deletion(); slicePredicate = new SlicePredicate(); slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("c_name2")); counterDeletion.setPredicate(slicePredicate); batchMutate.addDeletion("key1", columnFamilies, counterDeletion); assertEquals(2,batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); }
@Test public void testAddCounterInsertion() { // Insert a Counter. CounterColumn cc1 = new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 222); batchMutate.addCounterInsertion("key1", columnFamilies, cc1); // assert there is one outter map row with 'key' as the key Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap = batchMutate.getMutationMap(); assertEquals(1, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).size()); // add again with a different counter and verify there is one key and two mutations underneath // for "standard1" CounterColumn cc2 = new CounterColumn(StringSerializer.get().toByteBuffer("c_name2"), 44); batchMutate.addCounterInsertion("key1", columnFamilies, cc2); assertEquals(2, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); }
/** * Makes a shallow copy of the mutation object. * @return */ public BatchMutation<K> makeCopy() { return new BatchMutation<K>(keySerializer, mutationMap, sizeHint); }
@Test public void testAddSuperCounterInsertion() { // Create 1 super counter. CounterSuperColumn csc1 = new CounterSuperColumn(StringSerializer.get().toByteBuffer("c_name"), Arrays.asList(new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 123))); batchMutate.addSuperCounterInsertion("key1", columnFamilies, csc1); // assert there is one outter map row with 'key' as the key assertEquals(1, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); // add again with a different column and verify there is one key and two mutations underneath // for "standard1" CounterSuperColumn csc2 = new CounterSuperColumn(StringSerializer.get().toByteBuffer("c_name2"), Arrays.asList(new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 456))); batchMutate.addSuperCounterInsertion("key1", columnFamilies, csc2); assertEquals(2, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); } }
/** * Add a deletion request to the batch mutation. */ public BatchMutation<K> addDeletion(K key, List<String> columnFamilies, Deletion deletion) { Mutation mutation = new Mutation(); mutation.setDeletion(deletion); addMutation(key, columnFamilies, mutation); return this; }
@Test public void testAddInsertion() { Column column = new Column(StringSerializer.get().toByteBuffer("c_name")); column.setValue(StringSerializer.get().toByteBuffer("c_val")); column.setTimestamp(System.currentTimeMillis()); batchMutate.addInsertion("key1", columnFamilies, column); // assert there is one outter map row with 'key' as the key Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap = batchMutate.getMutationMap(); assertEquals(1, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).size()); // add again with a different column and verify there is one key and two mutations underneath // for "standard1" Column column2 = new Column(StringSerializer.get().toByteBuffer("c_name2")); column2.setValue(StringSerializer.get().toByteBuffer("c_val2")); column2.setTimestamp(System.currentTimeMillis()); batchMutate.addInsertion("key1",columnFamilies, column2); assertEquals(2, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); }
@Test public void testAddSuperInsertion() { Column column = new Column(StringSerializer.get().toByteBuffer("c_name")); column.setValue(StringSerializer.get().toByteBuffer("c_val")); column.setTimestamp(System.currentTimeMillis()); SuperColumn sc = new SuperColumn(StringSerializer.get().toByteBuffer("c_name"), Arrays.asList(column)); batchMutate.addSuperInsertion("key1", columnFamilies, sc); // assert there is one outter map row with 'key' as the key assertEquals(1, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); // add again with a different column and verify there is one key and two mutations underneath // for "standard1" column = new Column(StringSerializer.get().toByteBuffer("c_name")); column.setValue(StringSerializer.get().toByteBuffer("c_val")); column.setTimestamp(System.currentTimeMillis()); SuperColumn sc2 = new SuperColumn(StringSerializer.get().toByteBuffer("c_name2"), Arrays.asList(column)); batchMutate.addSuperInsertion("key1", columnFamilies, sc2); assertEquals(2, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); }
/** * Schedule an insertion of a supercolumn to be inserted in batch mode by {@link #execute()} */ @Override public <SN,N,V> Mutator<K> addInsertion(K key, String cf, HSuperColumn<SN,N,V> sc) { getPendingMutations().addSuperInsertion(key, Arrays.asList(cf), ((HSuperColumnImpl<SN,N,V>) sc).toThrift()); return this; }
@Override public <N,V> Mutator<K> addInsertion(K key, String cf, HColumn<N,V> c) { getPendingMutations().addInsertion(key, Arrays.asList(cf), ((HColumnImpl<N, V>) c).toThrift()); return this; }
@Override public <SN, N> Mutator<K> addCounter(K key, String cf, HCounterSuperColumn<SN, N> sc) { getPendingMutations().addSuperCounterInsertion(key, Arrays.asList(cf), ((HCounterSuperColumnImpl<SN, N>) sc).toThrift()); return this; }
@Override public <N> Mutator<K> addCounter(K key, String cf, HCounterColumn<N> c) { getPendingMutations().addCounterInsertion(key, Arrays.asList(cf), ((HCounterColumnImpl<N>) c).toThrift()); return this; }
private void addMutation(K key, List<String> columnFamilies, Mutation mutation) { Map<String, List<Mutation>> innerMutationMap = getInnerMutationMap(key); for (String columnFamily : columnFamilies) { List<Mutation> mutList = innerMutationMap.get(columnFamily); if (mutList == null) { if (sizeHint == null) { mutList = new LinkedList<Mutation>(); } else { mutList = new ArrayList<Mutation>(sizeHint.getNumOfColumns()); } innerMutationMap.put(columnFamily, mutList); } mutList.add(mutation); } }
assertNotNull(found); BatchMutation<String> batchMutation = new BatchMutation<String>(StringSerializer.get()); List<String> columnFamilies = Arrays.asList("Standard1"); for (int i = 0; i < 10; i++) { col.setValue(StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j)); col.setTimestamp(connectionManager.createClock()); batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col); deletion.setPredicate(slicePredicate); batchMutation.addDeletion("deleteThroughInserBatch_key", columnFamilies, deletion); keyspace.batchMutate(batchMutation); try {
@Override public void batchMutate(BatchMutation batchMutate) throws HectorException { batchMutate(batchMutate.getMutationMap()); }