protected ColumnFamily readColumnFamily(ReadCommand command, int consistency_level) throws InvalidRequestException { String cfName = command.getColumnFamilyName(); ThriftValidation.validateKey(command.key); if (consistency_level == ConsistencyLevel.ZERO) { throw new InvalidRequestException( "Consistency level zero may not be applied to read operations"); } if (consistency_level == ConsistencyLevel.ALL) { throw new InvalidRequestException( "Consistency level all is not yet supported on read operations"); } Row row; try { row = StorageProxy.readProtocol(command, consistency_level); } catch (IOException e) { throw new RuntimeException(e); } catch (TimeoutException e) { throw new RuntimeException(e); } if (row == null) { return null; } return row.getColumnFamily(cfName); }
@Test public void testGetColumn() throws IOException, ColumnFamilyNotDefinedException { Table table = Table.open("Keyspace1"); RowMutation rm; // add data rm = new RowMutation("Keyspace1", "key1"); rm.add(new QueryPath("Standard1", null, "Column1".getBytes()), "abcd".getBytes(), 0); rm.apply(); ReadCommand command = new SliceByNamesReadCommand( "Keyspace1", "key1", new QueryPath("Standard1"), Arrays.asList("Column1".getBytes())); Row row = command.getRow(table); IColumn col = row.cf.getColumn("Column1".getBytes()); assert Arrays.equals(col.value(), "abcd".getBytes()); }
private ReadCommand serializeAndDeserializeReadMessage(ReadCommand rm) throws IOException { ReadCommandSerializer rms = ReadCommand.serializer(); DataOutputBuffer dos = new DataOutputBuffer(); ByteArrayInputStream bis; rms.serialize(rm, dos); bis = new ByteArrayInputStream(dos.getData(), 0, dos.getLength()); return rms.deserialize(new DataInputStream(bis)); }
@Test public void testMakeReadMessage() throws IOException { ArrayList<byte[]> colList = new ArrayList<byte[]>(); colList.add("col1".getBytes()); colList.add("col2".getBytes()); ReadCommand rm, rm2; rm = new SliceByNamesReadCommand("Keyspace1", "row1", new QueryPath("Standard1"), colList); rm2 = serializeAndDeserializeReadMessage(rm); assert rm2.toString().equals(rm.toString()); rm = new SliceFromReadCommand( "Keyspace1", "row1", new QueryPath("Standard1"), ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.EMPTY_BYTE_ARRAY, true, 2); rm2 = serializeAndDeserializeReadMessage(rm); assert rm2.toString().equals(rm.toString()); rm = new SliceFromReadCommand( "Keyspace1", "row1", new QueryPath("Standard1"), "a".getBytes(), "z".getBytes(), true, 5); rm2 = serializeAndDeserializeReadMessage(rm); assertEquals(rm2.toString(), rm.toString()); }