@Override public Void doInKeyspace(KeyspaceService ks) throws HectorException { // Remove a Super Column. ks.remove( keySerializer.toByteBuffer(key), ThriftFactory.createSuperColumnPath(cf, supercolumnName, sNameSerializer)); return null; } }, consistency));
cp.setColumn(bytes("testInsertAndGetAndRemove")); for (int i = 0; i < 100; i++) { keyspace.insert("testInsertAndGetAndRemove_" + i, cp, StringSerializer.get().toByteBuffer("testInsertAndGetAndRemove_value_" + i)); Column col = keyspace.getColumn("testInsertAndGetAndRemove_" + i, cp); assertNotNull(col); String value = string(col.getValue()); keyspace.remove("testInsertAndGetAndRemove_" + i, cp); keyspace.getColumn("testInsertAndGetAndRemove_" + i, cp); fail("the value should already being deleted"); } catch (HNotFoundException e) {
keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", 5L)); keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", -1L)); keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("B", 10L)); CounterColumn counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); assertEquals(4, counter.value); counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); assertEquals(10, counter.value); keyspace.removeCounter("testInsertAndGetAndRemoveCounter_key1", cph); keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); } catch (HNotFoundException e) { counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); keyspace.removeCounter("testInsertAndGetAndRemoveCounter_key1", cph); counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); } catch (HNotFoundException e) {
@Test public void testMultigetColumn() throws HectorException { // insert value ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testMultigetColumn")); ArrayList<String> keys = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { keyspace.insert("testMultigetColumn_" + i, cp, StringSerializer.get().toByteBuffer("testMultigetColumn_value_" + i)); keys.add("testMultigetColumn_" + i); } // get value /* Map<String, Column> ms = keyspace.multigetColumn(keys, cp); for (int i = 0; i < 100; i++) { Column cl = ms.get(keys.get(i)); assertNotNull(cl); assertEquals("testMultigetColumn_value_" + i, string(cl.getValue())); } */ // remove value for (int i = 0; i < 100; i++) { keyspace.remove("testMultigetColumn_" + i, cp); } }
@Test public void testGetSlice() throws HectorException { // insert value ArrayList<String> columnnames = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testGetSlice_" + i)); keyspace.insert("testGetSlice", cp, StringSerializer.get().toByteBuffer("testGetSlice_Value_" + i)); columnnames.add("testGetSlice_" + i); } // get value ColumnParent clp = new ColumnParent("Standard1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); List<Column> cols = keyspace.getSlice("testGetSlice", clp, sp); assertNotNull(cols); assertEquals(100, cols.size()); Collections.sort(columnnames); ArrayList<String> gotlist = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { gotlist.add(string(cols.get(i).getName())); } assertEquals(columnnames, gotlist); ColumnPath cp = new ColumnPath("Standard1"); keyspace.remove("testGetSlice_", cp); keyspace.remove("testGetSlice", cp); }
@Test public void testGetRangeSlices() throws HectorException { for (int i = 0; i < 10; i++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testGetRangeSlices_" + i)); keyspace.insert("testGetRangeSlices0", cp, StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i)); keyspace.insert("testGetRangeSlices1", cp, StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i)); keyspace.insert("testGetRangeSlices2", cp, StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i)); } // get value ColumnParent clp = new ColumnParent("Standard1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); KeyRange range = new KeyRange(); range.setStart_key( "".getBytes()); range.setEnd_key( "".getBytes()); Map<String, List<Column>> keySlices = se.fromBytesMap(keyspace.getRangeSlices(clp, sp, range)); assertNotNull(keySlices); assertNotNull("testGetRangeSlices1 is null", keySlices.get("testGetRangeSlices1")); assertEquals("testGetRangeSlices_Value_0", string(keySlices.get("testGetRangeSlices1").get(0).getValue())); assertEquals(10, keySlices.get("testGetRangeSlices1").size()); ColumnPath cp = new ColumnPath("Standard1"); keyspace.remove("testGetRanageSlices0", cp); keyspace.remove("testGetRanageSlices1", cp); keyspace.remove("testGetRanageSlices2", cp); }
@Test public void testMultigetSlice() throws HectorException { // insert value ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testMultigetSlice")); ArrayList<String> keys = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { keyspace.insert("testMultigetSlice_" + i, cp, StringSerializer.get().toByteBuffer("testMultigetSlice_value_" + i)); keys.add("testMultigetSlice_" + i); } // get value ColumnParent clp = new ColumnParent("Standard1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); Map<String, List<Column>> ms = se.fromBytesMap(keyspace.multigetSlice(se.toBytesList(keys), clp, sp)); for (int i = 0; i < 100; i++) { List<Column> cl = ms.get(keys.get(i)); assertNotNull(cl); assertEquals(1, cl.size()); assertTrue(string(cl.get(0).getValue()).startsWith("testMultigetSlice_")); } // remove value for (int i = 0; i < 100; i++) { keyspace.remove("testMultigetSlice_" + i, cp); } }
@Test public void testGetSuperRangeSlices() throws HectorException { for (int i = 0; i < 10; i++) { ColumnPath cp = new ColumnPath("Super1"); cp.setSuper_column(bytes("SuperColumn_1")); cp.setColumn(bytes("testGetSuperRangeSlices_" + i)); keyspace.insert("testGetSuperRangeSlices0", cp, StringSerializer.get().toByteBuffer("testGetSuperRangeSlices_Value_" + i)); keyspace.insert("testGetSuperRangeSlices1", cp, StringSerializer.get().toByteBuffer("testGetSuperRangeSlices_Value_" + i)); } // get value ColumnParent clp = new ColumnParent("Super1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); KeyRange range = new KeyRange(); range.setStart_key( "".getBytes()); range.setEnd_key( "".getBytes()); Map<String, List<SuperColumn>> keySlices = se.fromBytesMap(keyspace.getSuperRangeSlices(clp, sp, range)); assertNotNull(keySlices); assertNotNull("testGetSuperRangSlices0 is null", keySlices.get("testGetSuperRangeSlices0")); assertEquals("testGetSuperRangeSlices_Value_0", string(keySlices.get("testGetSuperRangeSlices0").get(0).getColumns().get(0).getValue())); assertEquals(1, keySlices.get("testGetSuperRangeSlices1").size()); assertEquals(10, keySlices.get("testGetSuperRangeSlices1").get(0).getColumns().size()); ColumnPath cp = new ColumnPath("Super1"); keyspace.remove("testGetSuperRangeSlices0", cp); keyspace.remove("testGetSuperRangeSlices1", cp); }
cp2.setColumn(bytes("testGetSuperSlice_" + i)); keyspace.insert("testGetSuperSlice", cp, StringSerializer.get().toByteBuffer("testGetSuperSlice_Value_" + i)); keyspace.insert("testGetSuperSlice", cp2, StringSerializer.get().toByteBuffer("testGetSuperSlice_Value_" + i)); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); List<SuperColumn> cols = keyspace.getSuperSlice("testGetSuperSlice", clp, sp); keyspace.remove("testGetSuperSlice", cp);
/** * Test insertion of a supercolumn using insert */ @Test public void testInsertSuper() throws IllegalArgumentException, NoSuchElementException, IllegalStateException, HNotFoundException, Exception { // insert value ColumnParent columnParent = new ColumnParent("Super1"); columnParent.setSuper_column(StringSerializer.get().toByteBuffer("testInsertSuper_super")); Column column = new Column(StringSerializer.get().toByteBuffer("testInsertSuper_column")); column.setValue(StringSerializer.get().toByteBuffer("testInsertSuper_value")); column.setTimestamp(connectionManager.createClock()); keyspace.insert(StringSerializer.get().toByteBuffer("testInsertSuper_key"), columnParent, column); column.setName(StringSerializer.get().toByteBuffer("testInsertSuper_column2")); keyspace.insert(StringSerializer.get().toByteBuffer("testInsertSuper_key"), columnParent, column); // get value and assert ColumnPath cp2 = new ColumnPath("Super1"); cp2.setSuper_column(bytes("testInsertSuper_super")); SuperColumn sc = keyspace.getSuperColumn("testInsertSuper_key", cp2); assertNotNull(sc); assertEquals("testInsertSuper_super", string(sc.getName())); assertEquals(2, sc.getColumns().size()); assertEquals("testInsertSuper_value", string(sc.getColumns().get(0).getValue())); // remove value keyspace.remove("testInsertSuper_super", cp2); }
@Override public OrderedRows<K, N,V > doInKeyspace(KeyspaceService ks) throws HectorException { ColumnParent columnParent = new ColumnParent(columnFamilyName); Map<K, List<Column>> thriftRet = keySerializer.fromBytesMap( ks.getRangeSlices(columnParent, getPredicate(), keyRange.toThrift())); return new OrderedRowsImpl<K,N,V>((LinkedHashMap<K, List<Column>>) thriftRet, columnNameSerializer, valueSerializer); } }, consistency), this);
@Override public ColumnSlice<N, V> doInKeyspace(KeyspaceService ks) throws HectorException { ColumnParent columnParent = new ColumnParent(columnFamilyName); List<Column> thriftRet = ks.getSlice(keySerializer.toByteBuffer(key), columnParent, getPredicate()); return new ColumnSliceImpl<N, V>(thriftRet, columnNameSerializer, valueSerializer); } }, consistency), this);
@Override public Map<K,Integer> doInKeyspace(KeyspaceService ks) throws HectorException { ColumnParent columnParent = new ColumnParent(columnFamily); Map<K,Integer> counts = keySerializer.fromBytesMap( ks.multigetCount(keySerializer.toBytesList(keys), columnParent, slicePredicate.toThrift())); return counts; } }, consistency), this);
@Override public CounterSlice<N> doInKeyspace(KeyspaceService ks) throws HectorException { ColumnParent columnParent = new ColumnParent(columnFamilyName); List<CounterColumn> thriftRet = ks.getCounterSlice(keySerializer.toByteBuffer(key), columnParent, getPredicate()); return new CounterSliceImpl<N>(thriftRet, columnNameSerializer); } }, consistency), this);
ColumnParent cp = new ColumnParent("Counter1"); keyspace.addCounter("testGetCounterSlice", cp, createCounterColumn("testGetCounterSlice_" + i, i)); List<CounterColumn> cols = keyspace.getCounterSlice("testGetCounterSlice", clp, sp); keyspace.removeCounter("testGetCounterSlice", cp);
sta1.setColumn(bytes("deleteThroughInserBatch_col")); keyspace.insert("deleteThroughInserBatch_key", sta1, StringSerializer.get().toByteBuffer("deleteThroughInserBatch_val")); Column found = keyspace.getColumn("deleteThroughInserBatch_key", sta1); assertNotNull(found); keyspace.batchMutate(batchMutation); try { keyspace.getColumn("deleteThroughInserBatch_key", sta1); fail("Should not have found a value here"); } catch (Exception e) { cp.setColumn(bytes("testBatchMutateColumn_" + j)); Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp); assertNotNull(col); String value = string(col.getValue());
outerMutationMap.put("testBatchMutateColumn_" + i, mutationMap); keyspace.batchMutate(se.toBytesMap(outerMutationMap)); cp.setColumn(bytes("testBatchMutateColumn_" + j)); Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp); assertNotNull(col); String value = string(col.getValue()); outerMutationMap.put("testBatchMutateColumn_"+i, mutationMap); keyspace.batchMutate(se.toBytesMap(outerMutationMap)); cp.setColumn(bytes("testBatchMutateColumn_" + j)); try { keyspace.getColumn("testBatchMutateColumn_" + i, cp); fail(); } catch (HNotFoundException e) {
@Test public void testMultigetCount() { // insert 25 columns into 10 rows List<ByteBuffer> keys = new ArrayList<ByteBuffer>(); for ( int j=0; j < 10; j++ ) { for (int i = 0; i < 25; i++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(StringSerializer.get().toByteBuffer("testMultigetCount_column_" + i)); keyspace.insert("testMultigetCount_key_"+j, cp, StringSerializer.get().toByteBuffer("testMultigetCount_value_" + i)); } if (j % 2 == 0) { keys.add(StringSerializer.get().toByteBuffer("testMultigetCount_key_"+j)); } } // get value ColumnParent clp = new ColumnParent("Standard1"); SlicePredicate slicePredicate = new SlicePredicate(); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); slicePredicate.setSlice_range(sr); Map<ByteBuffer,Integer> counts = keyspace.multigetCount(keys, clp, slicePredicate); assertEquals(5,counts.size()); assertEquals(new Integer(25),counts.entrySet().iterator().next().getValue()); slicePredicate.setSlice_range(new SliceRange(StringSerializer.get().toByteBuffer(""), StringSerializer.get().toByteBuffer(""), false, 5)); counts = keyspace.multigetCount(keys, clp, slicePredicate); assertEquals(5,counts.size()); assertEquals(new Integer(5),counts.entrySet().iterator().next().getValue()); }
@Override public HColumn<N, V> doInKeyspace(KeyspaceService ks) throws HectorException { try { Column thriftColumn = ks.getColumn(keySerializer.toByteBuffer(key), ThriftFactory.createColumnPath(columnFamilyName, name, columnNameSerializer)); return new HColumnImpl<N, V>(thriftColumn, columnNameSerializer, valueSerializer); } catch (HNotFoundException e) { return null; } } }, consistency), this);
@Override public HCounterColumn<N> doInKeyspace(KeyspaceService ks) throws HectorException { try { CounterColumn thriftCounter = ks.getCounter(keySerializer.toByteBuffer(key), ThriftFactory.createColumnPath(columnFamilyName, name, columnNameSerializer)); return new HCounterColumnImpl<N>(thriftCounter, columnNameSerializer); } catch (HNotFoundException e) { return null; } } }, consistency), this);