private void handleAggregateCounterRow( Mutator<ByteBuffer> m, String key, long column, long value, UUID applicationId ) { if ( logger.isTraceEnabled() ) { logger.trace( "HACR: aggregateRow for app {} with key {} column {} and value {}", applicationId, key, column, value ); } if ( "o".equals( counterType ) || "p".equals( counterType ) ) { if ( m != null ) { HCounterColumn<Long> c = createCounterColumn( column, value, le ); m.addCounter( bytebuffer( key ), APPLICATION_AGGREGATE_COUNTERS.toString(), c ); } } if ( "n".equals( counterType ) || "p".equals( counterType ) ) { // create and add Count PrefixedSerializer ps = new PrefixedSerializer( applicationId, ue, se ); batcher.add( new Count( APPLICATION_AGGREGATE_COUNTERS.toString(), ps.toByteBuffer( key ), column, value ) ); } }
public KeyRange unprefixKeyRange(KeyRange prefixed) { KeyRange unprefixed = new KeyRange(); unprefixed.count = prefixed.count; unprefixed.end_token = prefixed.end_token; unprefixed.end_key = ps.fromByteBuffer(prefixed.end_key); unprefixed.start_token = prefixed.start_token; unprefixed.start_key = ps.fromByteBuffer(prefixed.start_key); return unprefixed; }
@Override public S fromByteBuffer(ByteBuffer bytes) { if ((bytes == null) || !bytes.hasArray()) { return null; } bytes = bytes.duplicate(); bytes.rewind(); if (bytes.limit() < prefixBytes.remaining()) { log.error("Unprefixed value received, throwing exception..."); throw new HectorSerializationException("Unexpected prefix value"); } if (compareByteArrays(prefixBytes.array(), prefixBytes.arrayOffset() + prefixBytes.position(), prefixBytes.remaining(), bytes.array(), bytes.arrayOffset() + bytes.position(), prefixBytes.remaining()) != 0) { return null; // incorrect prefix, return nothing } bytes.position(prefixBytes.remaining()); S s = suffixSerializer.fromByteBuffer(bytes); return s; }
@Override public Map<ByteBuffer, List<CounterSuperColumn>> multigetCounterSuperSlice( List<ByteBuffer> keys, ColumnParent columnParent, SlicePredicate predicate) throws HectorException { return ps.fromBytesMap(super.multigetCounterSuperSlice( ps.toBytesList(keys), columnParent, predicate)); }
@Override public <V> Map<S, V> fromBytesMap(Map<ByteBuffer, V> map) { Map<S, V> objMap = new LinkedHashMap<S, V>( computeInitialHashSize(map.size())); for (Entry<ByteBuffer, V> entry : map.entrySet()) { try { ByteBuffer bb = entry.getKey().slice(); S fbb = fromByteBuffer(bb); if (fbb != null) { objMap.put(fbb, entry.getValue()); } } catch (HectorSerializationException e) { // not a prefixed key, discard } } return objMap; }
public KeyRange prefixKeyRange(KeyRange unprefixed) { KeyRange prefixed = new KeyRange(); prefixed.count = unprefixed.count; prefixed.end_token = unprefixed.end_token; prefixed.end_key = ps.toByteBuffer(unprefixed.end_key); prefixed.start_token = unprefixed.start_token; prefixed.start_key = ps.toByteBuffer(unprefixed.start_key); return prefixed; }
@Test public void test() { UUIDSerializer ue = new UUIDSerializer(); StringSerializer se = new StringSerializer(); PrefixedSerializer<UUID, String> pe = new PrefixedSerializer<UUID, String>( prefixUUID, ue, se); Assert.assertEquals(str, pe.fromByteBuffer(pe.toByteBuffer(str))); }
public VirtualKeyspaceCassandraClient(Client client, ByteBuffer prefixBytes) { super(client); this.prefixBytes = prefixBytes; ps = new PrefixedSerializer<ByteBuffer, ByteBuffer>(prefixBytes, be, be); }
@Override public Map<ByteBuffer, List<Column>> getIndexedSlices( ColumnParent columnParent, IndexClause indexClause, SlicePredicate predicate) throws HectorException { return ps.fromBytesMap(super.getIndexedSlices(columnParent, indexClause, predicate)); }
@Override public void batchMutate(Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap) throws HectorException { super.batchMutate(ps.toBytesMap(mutationMap)); }
public KeyRange prefixKeyRange(KeyRange unprefixed) { KeyRange prefixed = new KeyRange(); prefixed.count = unprefixed.count; prefixed.end_token = unprefixed.end_token; prefixed.end_key = ps.toByteBuffer(unprefixed.end_key); prefixed.start_token = unprefixed.start_token; prefixed.start_key = ps.toByteBuffer(unprefixed.start_key); return prefixed; }
@Test public void testBadPrefix() { if (str == null) { // null serialization is always null, // so no prefix comparison takes place to test return; } UUIDSerializer ue = new UUIDSerializer(); StringSerializer se = new StringSerializer(); PrefixedSerializer<UUID, String> pe1 = new PrefixedSerializer<UUID, String>( prefixUUID, ue, se); UUID testUUID = UUID.randomUUID(); Assert.assertNotSame(prefixUUID, testUUID); PrefixedSerializer<UUID, String> pe2 = new PrefixedSerializer<UUID, String>( testUUID, ue, se); String result = pe2.fromByteBuffer(pe1.toByteBuffer(str)); Assert.assertNull("Different prefixes should fail comparison", result); }
@Override public Map<ByteBuffer, List<ColumnOrSuperColumn>> multiget_slice( List<ByteBuffer> keys, ColumnParent column_parent, SlicePredicate predicate, ConsistencyLevel consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException { return ps.fromBytesMap(super.multiget_slice(ps.toBytesList(keys), column_parent, predicate, consistency_level)); }
public <E> VirtualKeyspaceServiceImpl(String keyspaceName, E keyPrefix, Serializer<E> keyPrefixSerializer, ConsistencyLevelPolicy consistencyLevel, HConnectionManager connectionManager, FailoverPolicy failoverPolicy, Map<String, String> credentials) throws HectorTransportException { super(keyspaceName, consistencyLevel, connectionManager, failoverPolicy, credentials); prefixBytes = keyPrefixSerializer.toByteBuffer(keyPrefix); ps = new PrefixedSerializer<ByteBuffer, ByteBuffer>(prefixBytes, be, be); }
@Override public <V> Map<S, V> fromBytesMap(Map<ByteBuffer, V> map) { Map<S, V> objMap = new LinkedHashMap<S, V>( computeInitialHashSize(map.size())); for (Entry<ByteBuffer, V> entry : map.entrySet()) { try { ByteBuffer bb = entry.getKey().slice(); S fbb = fromByteBuffer(bb); if (fbb != null) { objMap.put(fbb, entry.getValue()); } } catch (HectorSerializationException e) { // not a prefixed key, discard } } return objMap; }
@Override public Map<ByteBuffer, List<SuperColumn>> getSuperRangeSlices( ColumnParent columnParent, SlicePredicate predicate, KeyRange keyRange) throws HectorException { return ps.fromBytesMap(super.getSuperRangeSlices(columnParent, predicate, prefixKeyRange(keyRange))); }
@Override public void batch_mutate( Map<ByteBuffer, Map<String, List<Mutation>>> mutation_map, ConsistencyLevel consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException { super.batch_mutate(ps.toBytesMap(mutation_map), consistency_level); }
@Override public ColumnOrSuperColumn get(ByteBuffer key, ColumnPath column_path, ConsistencyLevel consistency_level) throws InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException { return super.get(ps.toByteBuffer(key), column_path, consistency_level); }
@Test public void testNoPrefix() { if (str == null) { // null serialization is always null, // so no prefix comparison takes place to test return; } UUIDSerializer ue = new UUIDSerializer(); StringSerializer se = new StringSerializer(); PrefixedSerializer<UUID, String> pe1 = new PrefixedSerializer<UUID, String>( prefixUUID, ue, se); UUID testUUID = UUID.randomUUID(); Assert.assertNotSame(prefixUUID, testUUID); PrefixedSerializer<UUID, String> pe2 = new PrefixedSerializer<UUID, String>( testUUID, ue, se); try { ByteBuffer bb = pe1.toByteBuffer(str); bb.limit(5); // bb.get(new byte[bb.limit() - 5]); pe2.fromByteBuffer(bb); Assert.fail("Lack of prefix should raise exception"); } catch (HectorSerializationException e) { // yea } } }
@Override public Map<ByteBuffer, List<Column>> multigetSlice(List<ByteBuffer> keys, ColumnParent columnParent, SlicePredicate predicate) throws HectorException { return ps.fromBytesMap(super.multigetSlice(ps.toBytesList(keys), columnParent, predicate)); }