@Override public ColumnListMutation<C> incrementCounterColumn(C columnName, long amount) { primary.incrementCounterColumn(columnName, amount); secondary.incrementCounterColumn(columnName, amount); return this; }
@Override public ResponseData batchMutate(Object key, Map<?, ?> nv) throws OperationException { MutationBatch m = AstyanaxConnection.instance.keyspace().prepareMutationBatch(); ColumnListMutation<Object> cf = m.withRow(cfs, key); for (Map.Entry<?, ?> entry : nv.entrySet()) { if (isCounter) cf.incrementCounterColumn(entry.getKey(), (Long) entry.getValue()); else cf.putColumn(entry.getKey(), entry.getValue(), valueSerializer, null); } try { OperationResult<Void> result = m.execute(); return new AstyanaxResponseData("", 0, result, key, nv); } catch (ConnectionException e) { throw new OperationException(e); } }
@Test public void testDeleteCounter() throws Exception { Column<String> column; String rowKey = "CounterRowDelete1"; String counterName = "MyCounter"; // Increment the column MutationBatch m = keyspace.prepareMutationBatch(); m.withRow(CF_COUNTER1, rowKey).incrementCounterColumn(counterName, 1); m.execute(); // // Read back the value // column = keyspace.prepareQuery(CF_COUNTER1).getRow(rowKey).getColumn(counterName).execute().getResult(); // Assert.assertNotNull(column); // Assert.assertEquals(column.getLongValue(), 1); // // // Delete the column // keyspace.prepareColumnMutation(CF_COUNTER1, rowKey, counterName).deleteCounterColumn().execute(); // // // Try to read back // // This should be non-existent // column = keyspace.prepareQuery(CF_COUNTER1).getRow(rowKey).getColumn(counterName).execute().getResult(); // Assert.assertNull(column); }
@SuppressWarnings("unchecked") public void incrementCountColumns(List<Tuple> inputs, TupleCounterMapper<K,C> tupleMapper) throws Exception { Map<String, MutationBatch> mutations = new HashMap<String, MutationBatch>(); for (Tuple input : inputs) { String keyspace = tupleMapper.mapToKeyspace(input); MutationBatch mutation = mutations.get(keyspace); if(mutation == null) { mutation = getKeyspace(keyspace).prepareMutationBatch(); mutations.put(keyspace, mutation); } String columnFamilyName = tupleMapper.mapToColumnFamily(input); K rowKey = tupleMapper.mapToRowKey(input); long incrementAmount = tupleMapper.mapToIncrementAmount(input); ColumnFamily<K, C> columnFamily = new ColumnFamily<K, C>(columnFamilyName, (Serializer<K>) serializerFor(tupleMapper.getKeyClass()), (Serializer<C>) serializerFor(tupleMapper.getColumnNameClass())); for (C columnName : tupleMapper.mapToColumnList(input)) { mutation.withRow(columnFamily, rowKey).incrementCounterColumn(columnName, incrementAmount); } } for(String key : mutations.keySet()) { mutations.get(key).execute(); } }
@Override public ResponseData put(Object key, Object colName, Object value) throws OperationException { MutationBatch m = AstyanaxConnection.instance.keyspace().prepareMutationBatch(); if (isCounter) m.withRow(cfs, key).incrementCounterColumn(colName, (Long) value); else m.withRow(cfs, key).putColumn(colName, value, valueSerializer, null); try { OperationResult<Void> result = m.execute(); return new AstyanaxResponseData("", 0, result, key, colName, value); } catch (ConnectionException e) { throw new OperationException(e); } }
@Test public void testIncrementCounter() throws Exception { long baseAmount, incrAmount = 100; Column<String> column; column = keyspace.prepareQuery(CF_COUNTER1).getRow("CounterRow1").getColumn("MyCounter").execute().getResult(); //Assert.assertNull(column); baseAmount = 0; MutationBatch m = keyspace.prepareMutationBatch(); m.withRow(CF_COUNTER1, "CounterRow1").incrementCounterColumn("MyCounter", incrAmount); m.execute(); // // column = keyspace.prepareQuery(CF_COUNTER1).getRow("CounterRow1").getColumn("MyCounter").execute().getResult(); // Assert.assertNotNull(column); // Assert.assertEquals(baseAmount + incrAmount, column.getLongValue()); // // m = keyspace.prepareMutationBatch(); // m.withRow(CF_COUNTER1, "CounterRow1").incrementCounterColumn("MyCounter", incrAmount); // m.execute(); // // column = keyspace.prepareQuery(CF_COUNTER1).getRow("CounterRow1").getColumn("MyCounter").execute().getResult(); // Assert.assertNotNull(column); // Assert.assertEquals(column.getLongValue(), baseAmount + 2 * incrAmount); }
@SuppressWarnings("unchecked") public void incrementCountColumn(Tuple input, TupleCounterMapper<K,C> tupleMapper) throws Exception { String columnFamilyName = tupleMapper.mapToColumnFamily(input); String keyspace = tupleMapper.mapToKeyspace(input); K rowKey = tupleMapper.mapToRowKey(input); long incrementAmount = tupleMapper.mapToIncrementAmount(input); MutationBatch mutation = getKeyspace(keyspace).prepareMutationBatch(); ColumnFamily<K, C> columnFamily = new ColumnFamily<K, C>(columnFamilyName, (Serializer<K>) serializerFor(tupleMapper.getKeyClass()), (Serializer<C>) serializerFor(tupleMapper.getColumnNameClass())); for (C columnName : tupleMapper.mapToColumnList(input)) { mutation.withRow(columnFamily, rowKey).incrementCounterColumn(columnName, incrementAmount); } mutation.execute(); }