com.netflix.astyanax.serializers.StringSerializer Java Examples

The following examples show how to use com.netflix.astyanax.serializers.StringSerializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: V003.java    From mutagen-cassandra with Apache License 2.0 6 votes vote down vote up
@Override
protected void performMutation(Context context) {
	context.debug("Executing mutation {}",state.getID());
	final ColumnFamily<String,String> CF_TEST1=
		ColumnFamily.newColumnFamily("Test1",
			StringSerializer.get(),StringSerializer.get());

	MutationBatch batch=getKeyspace().prepareMutationBatch();
	batch.withRow(CF_TEST1,"row2")
		.putColumn("value1","chicken")
		.putColumn("value2","sneeze");

	try {
		batch.execute();
	}
	catch (ConnectionException e) {
		throw new MutagenException("Could not update columnfamily Test1",e);
	}
}
 
Example #2
Source File: EntityCollectionManagerImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public Health getHealth() {

    try {
        ColumnFamily<String, String> CF_SYSTEM_LOCAL =
            new ColumnFamily<String, String>( "system.local", StringSerializer.get(), StringSerializer.get(),
                StringSerializer.get() );

        OperationResult<CqlResult<String, String>> result =
            keyspace.prepareQuery( CF_SYSTEM_LOCAL )
                .setConsistencyLevel(ConsistencyLevel.CL_ONE)
                .withCql( "SELECT now() FROM system.local;" )
                .execute();

        if ( result.getResult().getRows().size() > 0 ) {
            return Health.GREEN;
        }
    }
    catch ( ConnectionException ex ) {
        logger.error( "Error connecting to Cassandra", ex );
    }

    return Health.RED;
}
 
Example #3
Source File: DeltaBlockingTest.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Test
public void testLargeDelta() throws IOException {
    DAOUtils daoUtils = new DAOUtils(_prefixLength, 64 * 1024);
    String delta = generateLargeDelta();
    String encodedDelta = StringUtils.repeat('0', _prefixLength) + delta;
    List<ByteBuffer> blocks = daoUtils.getDeltaBlocks(ByteBuffer.wrap(encodedDelta.getBytes()));
    assertEquals(blocks.size(), daoUtils.getNumDeltaBlocks(ByteBuffer.wrap(encodedDelta.getBytes())));
    List<TestRow> rows = Lists.newArrayListWithCapacity(blocks.size());
    UUID changeId = UUID.randomUUID();
    for (int i = 0; i < blocks.size(); i++) {
        rows.add(new TestRow(i, changeId, blocks.get(i)));
    }

    Iterator<DeltaIterator.BlockedDelta> iterator = new ListDeltaIterator(rows.iterator(), false, _prefixLength);
    assertEquals(iterator.hasNext(), true);
    assertEquals(StringSerializer.get().fromByteBuffer(daoUtils.skipPrefix(iterator.next().getContent())), delta);
    assertEquals(iterator.hasNext(), false);

    List<TestRow> reversedRows = Lists.reverse(rows);
    Iterator<DeltaIterator.BlockedDelta> reversedIterator = new ListDeltaIterator(reversedRows.iterator(), true, _prefixLength);
    assertEquals(reversedIterator.hasNext(), true);
    assertEquals(StringSerializer.get().fromByteBuffer(daoUtils.skipPrefix(reversedIterator.next().getContent())), delta);
    assertEquals(reversedIterator.hasNext(), false);
}
 
Example #4
Source File: SlotKeySerializerTest.java    From blueflood with Apache License 2.0 6 votes vote down vote up
@Test
public void testToFromByteBuffer() {
    Granularity expectedGranularity = Granularity.MIN_5;
    int expectedSlot = 10;
    int expectedShard = 1;

    ByteBuffer origBuff = StringSerializer.get().toByteBuffer(
            SlotKey.of(expectedGranularity, expectedSlot, expectedShard).toString());
    Assert.assertNotNull(origBuff);

    SlotKey slotKey = SlotKeySerializer.get().fromByteBuffer(origBuff.duplicate());
    Assert.assertEquals("Invalid granularity", expectedGranularity, slotKey.getGranularity());
    Assert.assertEquals("Invalid slot", expectedSlot, slotKey.getSlot());
    Assert.assertEquals("Invalid shard", expectedShard, slotKey.getShard());

    ByteBuffer newBuff = SlotKeySerializer.get().toByteBuffer(slotKey);
    Assert.assertEquals(origBuff, newBuff);
}
 
Example #5
Source File: MetaDaoImpl.java    From staash with Apache License 2.0 6 votes vote down vote up
@Override
public void writeMetaEntity(Entity entity) {
    // TODO Auto-generated method stub
    Keyspace ks = kscp.acquireKeyspace("meta");
    ks.prepareMutationBatch();
    MutationBatch m;
    OperationResult<Void> result;
    m = ks.prepareMutationBatch();
    m.withRow(dbcf, entity.getRowKey()).putColumn(entity.getName(), entity.getPayLoad(), null);
    try {
        result = m.execute();
        if (entity instanceof PaasTableEntity) {
            String schemaName = ((PaasTableEntity)entity).getSchemaName();
            Keyspace schemaks = kscp.acquireKeyspace(schemaName);
            ColumnFamily<String, String> cf = ColumnFamily.newColumnFamily(entity.getName(), StringSerializer.get(), StringSerializer.get());
            schemaks.createColumnFamily(cf, null);
        }
        int i = 0;
    } catch (ConnectionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 
Example #6
Source File: DefaultChangeEncoderTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeDecodeD2() {
    Delta delta = Deltas.mapBuilder().put("name", "bob").remove("x").build();
    Set<String> tags = ImmutableSet.of("tag0","tag1");
    ChangeEncoder changeEncoder = new DefaultChangeEncoder(2);
    // Encode and then decode the said delta, and verify if Change is as expected
    String encodedDelta = changeEncoder.encodeDelta(delta.toString(), EnumSet.of(ChangeFlag.MAP_DELTA), tags, new StringBuilder()).toString();
    assertEquals(encodedDelta, "D2:[\"tag0\",\"tag1\"]:{..,\"name\":\"bob\",\"x\":~}");
    Change change = changeEncoder.decodeChange(TimeUUIDs.newUUID(), StringSerializer.get().fromString(encodedDelta));
    assertEquals(change.getDelta(), delta);
    assertEquals(change.getTags(), tags);
}
 
Example #7
Source File: AbstractCassandraHystrixCommand.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * returns a ColumnFamily given a columnFamilyName
 * @param columnFamilyName
 * @param rowKeyClass
 * @return a constructed ColumnFamily
 */
@SuppressWarnings({"unchecked", "rawtypes"})
protected ColumnFamily getColumnFamilyViaColumnName(String columnFamilyName, Class rowKeyClass) {
    if (rowKeyClass == String.class) {
        return new ColumnFamily(columnFamilyName, StringSerializer.get(), StringSerializer.get());
    } else if (rowKeyClass == Integer.class) {
        return new ColumnFamily(columnFamilyName, IntegerSerializer.get(), StringSerializer.get());
    } else if (rowKeyClass == Long.class) {
        return new ColumnFamily(columnFamilyName, LongSerializer.get(), StringSerializer.get());
    } else {
        throw new IllegalArgumentException("RowKeyType is not supported: " + rowKeyClass.getSimpleName() + ". String/Integer/Long are supported, or you can define the ColumnFamily yourself and use the other constructor.");
    }
}
 
Example #8
Source File: CassandraMutagenImplTest.java    From mutagen-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 *
 *
 */
@Test
public void testData() throws Exception {

	final ColumnFamily<String,String> CF_TEST1=
		ColumnFamily.newColumnFamily("Test1",
			StringSerializer.get(),StringSerializer.get());

	ColumnList<String> columns;
	columns=keyspace.prepareQuery(CF_TEST1)
		.getKey("row1")
		.execute()
		.getResult();

	assertEquals("foo",columns.getStringValue("value1",null));
	assertEquals("bar",columns.getStringValue("value2",null));

	columns=keyspace.prepareQuery(CF_TEST1)
		.getKey("row2")
		.execute()
		.getResult();

	assertEquals("chicken",columns.getStringValue("value1",null));
	assertEquals("sneeze",columns.getStringValue("value2",null));

	columns=keyspace.prepareQuery(CF_TEST1)
		.getKey("row3")
		.execute()
		.getResult();

	assertEquals("bar",columns.getStringValue("value1",null));
	assertEquals("baz",columns.getStringValue("value2",null));
}
 
Example #9
Source File: EntityVersionSerializer.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public EntityVersion fromByteBuffer(final ByteBuffer byteBuffer) {

    // would use Composites.newDynamicCompositeParser(byteBuffer) but it is not implemented

    DynamicComposite composite = DynamicComposite.fromByteBuffer(byteBuffer);
    Preconditions.checkArgument(composite.size() == 3, "Composite should have 3 elements");

    final UUID version      = composite.get( 0, UUIDSerializer.get() );
    final UUID entityId     = composite.get( 1, UUIDSerializer.get() );
    final String entityType = composite.get( 2, StringSerializer.get() );

    return new EntityVersion( new SimpleId( entityId, entityType ), version);
}
 
Example #10
Source File: MvccEntitySerializationStrategyV3Impl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public EntityWrapper fromByteBuffer( final ByteBuffer byteBuffer ) {

    /**
     * We intentionally turn data corruption exceptions when we're unable to de-serialize
     * the data in cassandra.  If this occurs, we'll never be able to de-serialize it
     * and it should be considered lost.  This is an error that is occurring due to a bug
     * in serializing the entity.  This is a lazy recognition + repair signal for deployment with
     * existing systems.
     */

    EntityWrapper entityWrapper;


    try {
        Timer.Context time = bytesOutTimer.time();
        byte[] arr = byteBuffer.array();
        bytesOutHistorgram.update( arr == null ? 0 : arr.length);
        entityWrapper = MAPPER.readValue(arr, EntityWrapper.class);
        entityWrapper.size = arr.length;
        time.stop();
    }
    catch ( Exception e ) {
        if (log.isDebugEnabled()) {
            log.debug("Entity Wrapper Deserialized: " + StringSerializer.get().fromByteBuffer(byteBuffer));
        }
        throw new DataCorruptionException("Unable to read entity data", e);
    }

    // it's been deleted, remove it
    if ( entityWrapper.getEntityMap() == null) {
        return new EntityWrapper( entityWrapper.getId(), entityWrapper.getVersion(),MvccEntity.Status.DELETED,null,0 );
    }

    entityWrapper.setStatus(MvccEntity.Status.COMPLETE);

    // it's partial by default
    return entityWrapper;
}
 
Example #11
Source File: AstyanaxLockManagerImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
private ColumnFamily getLocksColumnFamily() {

        if ( columnFamily == null ) {

            columnFamily = ColumnFamily.newColumnFamily(
                CF_NAME, StringSerializer.get(), StringSerializer.get() );

            if ( logger.isDebugEnabled() ) {

                try {
                    final KeyspaceDefinition kd = keyspace.describeKeyspace();
                    final ColumnFamilyDefinition cfd = kd.getColumnFamily( columnFamily.getName() );
                    Map<String, Object> options = new HashMap<>( 1 );
                    options.put( "gc_grace_seconds", cfd.getGcGraceSeconds() );
                    options.put( "caching", cfd.getCaching() );
                    options.put( "compaction_strategy", cfd.getCompactionStrategy() );
                    options.put( "compaction_strategy_options", cfd.getCompactionStrategyOptions() );
                    logger.debug( "Locks column family {} exists with options: {}", cfd.getName(), options);

                } catch ( ConnectionException ce ) {
                    logger.warn("Error connecting to Cassandra for debug column family info", ce);
                }
            }
        }

        return columnFamily;
    }
 
Example #12
Source File: SlotStateSerializerTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@Test
public void testToFromByteBuffer() {
    ByteBuffer origBuff = StringSerializer.get().toByteBuffer("metrics_full,1,X");
    Assert.assertNotNull(origBuff);

    SlotState state = SlotStateSerializer.get().fromByteBuffer(origBuff.duplicate());
    Assert.assertEquals(state.getGranularity(), Granularity.FULL);
    Assert.assertEquals(state.getSlot(), 1);
    Assert.assertEquals(state.getState(), UpdateStamp.State.Rolled);

    ByteBuffer newBuff = SlotStateSerializer.get().toByteBuffer(state);
    Assert.assertEquals(origBuff, newBuff);
}
 
Example #13
Source File: AstyanaxDao.java    From staash with Apache License 2.0 5 votes vote down vote up
public AstyanaxDao(Keyspace keyspace, Class<T> entityType, String columnFamilyName) {
    this.keyspace     = keyspace;
    this.entityName   = entityNameFromClass(entityType);
    this.columnFamily = new ColumnFamily<String, String>(columnFamilyName, StringSerializer.get(), StringSerializer.get());
    this.prefix       = this.entityName + ":";
    
    manager = new DefaultEntityManager.Builder<T, String>()
            .withKeyspace(keyspace)
            .withColumnFamily(columnFamily)
            .withEntityType(entityType)
            .build();
}
 
Example #14
Source File: AstyanaxDao.java    From staash with Apache License 2.0 5 votes vote down vote up
public AstyanaxDao(Keyspace keyspace, Class<T> entityType) {
    this.keyspace     = keyspace;
    this.entityName   = entityNameFromClass(entityType);
    this.columnFamily = new ColumnFamily<String, String>(this.entityName, StringSerializer.get(), StringSerializer.get());
    this.prefix       = "";
    
    manager = new DefaultEntityManager.Builder<T, String>()
            .withKeyspace(keyspace)
            .withColumnFamily(columnFamily)
            .withEntityType(entityType)
            .build();
}
 
Example #15
Source File: InstanceDataDAOCassandra.java    From Raigad with Apache License 2.0 5 votes vote down vote up
public String findKey(String cluster, String instanceId, String dc) {
    try {
        final String selectClause = String.format(
                "SELECT * FROM %s WHERE %s = '%s' and %s = '%s' and %s = '%s'  ", CF_NAME_INSTANCES,
                CN_CLUSTER, cluster, CN_INSTANCEID, instanceId, CN_LOCATION, dc);

        logger.info(selectClause);

        final ColumnFamily<String, String> CF_INSTANCES_NEW = ColumnFamily.newColumnFamily(KS_NAME,
                StringSerializer.get(), StringSerializer.get());

        OperationResult<CqlResult<String, String>> result = bootKeyspace.prepareQuery(CF_INSTANCES_NEW)
                .withCql(selectClause).execute();

        if (result == null || result.getResult().getRows().size() == 0) {
            return null;
        }

        Row<String, String> row = result.getResult().getRows().getRowByIndex(0);
        return row.getKey();

    }
    catch (Exception e) {
        logger.warn("Caught an Unknown Exception during find a row matching cluster[" + cluster +
                "], id[" + instanceId + "], and region[" + dc + "]  ... -> "
                + e.getMessage());
        throw new RuntimeException(e);
    }
}
 
Example #16
Source File: QueryUtils.java    From staash with Apache License 2.0 5 votes vote down vote up
public static String formatQueryResult(CqlStatementResult rs, String cfname) {
    // TODO Auto-generated method stub
    String value = "";
    JsonObject response = new JsonObject();
    ColumnFamily<String, String> cf = ColumnFamily
            .newColumnFamily(cfname, StringSerializer.get(),
                    StringSerializer.get());
    Rows<String, String> rows = rs.getRows(cf);
    int rcount = 1;
    for (com.netflix.astyanax.model.Row<String, String> row : rows) {
        ColumnList<String> columns = row.getColumns();
        Collection<String> colnames = columns.getColumnNames();
        String rowStr = "";
        String colStr = "";
        if (colnames.contains("key") && colnames.contains("column1")) {
        	colStr = colStr + columns.getDateValue("column1", null).toGMTString();
        	rowStr = rowStr + columns.getStringValue("value", null); 
        	response.putString(colStr, rowStr);
        } else {
            JsonObject rowObj = new JsonObject();
         for (String colName:colnames) {
             //colStr = colStr+colname+",";
            value = columns.getStringValue(colName, null);
            //rowStr=rowStr+value+",";
            rowObj.putString(colName, value);
         }
         //rowobj.putString("columns", colStr);
         //rowobj.putString("values", rowStr);
         response.putObject(""+rcount++, rowObj);
        }
    }
    return response.toString();
    
}
 
Example #17
Source File: AstyanaxSupport.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
protected AstyanaxSample(Builder builder) {
    super(builder.clusterName, builder.hostname, builder.thriftPort);
    columnFamilyName = checkNotNull(builder.columnFamilyName, "columnFamilyName");
    sampleColumnFamily = new ColumnFamily<String, String>(
            columnFamilyName, // Column Family Name
            StringSerializer.get(), // Key Serializer
            StringSerializer.get()); // Column Serializer
}
 
Example #18
Source File: DefaultChangeEncoderTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeDecodeD3() {
    Delta delta = Deltas.mapBuilder().put("name", "bob").remove("x").build();
    Set<String> tags = ImmutableSet.of("tag0","tag1");
    ChangeEncoder changeEncoder = new DefaultChangeEncoder(3);
    // Encode and then decode the said delta, and verify if Change is as expected
    String encodedDelta = changeEncoder.encodeDelta(delta.toString(), EnumSet.of(ChangeFlag.MAP_DELTA), tags, new StringBuilder()).toString();
    assertEquals(encodedDelta, "D3:[\"tag0\",\"tag1\"]:M:{..,\"name\":\"bob\",\"x\":~}");
    Change change = changeEncoder.decodeChange(TimeUUIDs.newUUID(), StringSerializer.get().fromString(encodedDelta));
    // Because the change contains a lazy delta it will not be the exact same instance as "delta"
    assertEquals(change.getDelta().toString(), delta.toString());
    assertFalse(change.getDelta().isConstant());
    assertEquals(change.getTags(), tags);
}
 
Example #19
Source File: DefaultChangeEncoderTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeCompactionWithMapLiteral() {
    String c1 = "C1:{\"count\":4,\"first\":\"6b6dff41-e50b-11e5-b18e-0e83e95d75a9\",\"cutoff\":\"741bb5bc-a5dc-11e6-8d58-123665dcce6e\"," +
            "\"cutoffSignature\":\"b6fe61d13972264e9d7ab0c230c82855\",\"lastContentMutation\":\"cef920a5-fbd4-11e5-b18e-0e83e95d75a9\"," +
            "\"lastMutation\":\"cef920a5-fbd4-11e5-b18e-0e83e95d75a9\",\"compactedDelta\":\"{\\\"active\\\":true}\",\"lastTags\":[\"tag1\"]}";
    ChangeEncoder changeEncoder = new DefaultChangeEncoder();
    Compaction compaction = changeEncoder.decodeCompaction(StringSerializer.get().fromString(c1));
    assertEquals(compaction.getCount(), 4);
    assertEquals(compaction.getLastTags(), ImmutableSet.of("tag1"));
    // Compacted delta should be a lazy map literal
    assertTrue(compaction.getCompactedDelta().isConstant());
    assertTrue(compaction.getCompactedDelta() instanceof Literal);
    assertTrue(((Literal) compaction.getCompactedDelta()).getValue() instanceof LazyJsonMap);
    assertEquals(compaction.getCompactedDelta(), Deltas.literal(ImmutableMap.of("active", true)));
}
 
Example #20
Source File: DefaultChangeEncoderTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeCompactionWithDeletionDelta() {
    String c1 = "C1:{\"count\":4,\"first\":\"6b6dff41-e50b-11e5-b18e-0e83e95d75a9\",\"cutoff\":\"741bb5bc-a5dc-11e6-8d58-123665dcce6e\"," +
            "\"cutoffSignature\":\"b6fe61d13972264e9d7ab0c230c82855\",\"lastContentMutation\":\"cef920a5-fbd4-11e5-b18e-0e83e95d75a9\"," +
            "\"lastMutation\":\"cef920a5-fbd4-11e5-b18e-0e83e95d75a9\",\"compactedDelta\":\"~\",\"lastTags\":[\"tag1\"]}";
    ChangeEncoder changeEncoder = new DefaultChangeEncoder();
    Compaction compaction = changeEncoder.decodeCompaction(StringSerializer.get().fromString(c1));
    assertEquals(compaction.getCount(), 4);
    assertEquals(compaction.getLastTags(), ImmutableSet.of("tag1"));
    // Compacted delta should be a delete delta
    assertTrue(compaction.getCompactedDelta().isConstant());
    assertTrue(compaction.getCompactedDelta() instanceof Delete);
    assertEquals(compaction.getCompactedDelta(), Deltas.delete());
}
 
Example #21
Source File: DeltaBlockingTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlockedReadWrite() {
    String[] deltas = buildDeltas();
    String[] encodedDeltas = buildEncodedDeltas(deltas);
    List<TestRow> rows = Lists.newArrayListWithCapacity(deltas.length * 5); // lazy guess at future size
    for (String encodedDelta : encodedDeltas) {
        List<ByteBuffer> blocks = _daoUtils.getDeltaBlocks(ByteBuffer.wrap(encodedDelta.getBytes()));
        assertEquals(blocks.size(), _daoUtils.getNumDeltaBlocks(ByteBuffer.wrap(encodedDelta.getBytes())));
        UUID changeId = UUID.randomUUID();
        for (int i = 0; i < blocks.size(); i++) {
            rows.add(new TestRow(i, changeId, blocks.get(i)));
        }
    }
    Iterator<DeltaIterator.BlockedDelta> iterator = new ListDeltaIterator(rows.iterator(), false, _prefixLength);
    for (int i = 0; i < deltas.length; i++) {
        assertEquals(iterator.hasNext(), true);
        assertEquals(StringSerializer.get().fromByteBuffer(_daoUtils.skipPrefix(iterator.next().getContent())), deltas[i]);
    }
    assertEquals(iterator.hasNext(), false);

    List<TestRow> reversedRows = Lists.reverse(rows);
    Iterator<DeltaIterator.BlockedDelta> reversedIterator = new ListDeltaIterator(reversedRows.iterator(), true, _prefixLength);

    for (int i = deltas.length - 1; i >= 0; i--) {
        assertEquals(reversedIterator.hasNext(), true);
        assertEquals(StringSerializer.get().fromByteBuffer(_daoUtils.skipPrefix(reversedIterator.next().getContent())), deltas[i]);
    }
    assertEquals(reversedIterator.hasNext(), false);
}
 
Example #22
Source File: DeltaBlockingTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Test
void testRemovePrefix() {
    String[] deltas = buildDeltas();
    String[] encodedDeltas = buildEncodedDeltas(deltas);
    List<TestRow> rows = Lists.newArrayListWithCapacity(deltas.length * 5); // lazy guess at future size
    for (int i = 0; i < encodedDeltas.length; i++) {
        ByteBuffer byteDelta = ByteBuffer.wrap((encodedDeltas[i].getBytes()));
        assertEquals(StringSerializer.get().fromByteBuffer(_daoUtils.skipPrefix(byteDelta)), deltas[i]);
    }
}
 
Example #23
Source File: AstyanaxDataWriterDAO.java    From emodb with Apache License 2.0 4 votes vote down vote up
private ByteBuffer stringToByteBuffer(String str) {
    return StringSerializer.get().toByteBuffer(str);
}
 
Example #24
Source File: DynamicCompositeParserImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public String readString() {
    return read( StringSerializer.get() );
}
 
Example #25
Source File: DefaultChangeEncoderTest.java    From emodb with Apache License 2.0 4 votes vote down vote up
private void verifyDecodedChange(String encodedDelta, Delta expectedDelta, ImmutableSet<String> tags) {
    ChangeEncoder changeEncoder = new DefaultChangeEncoder();
    Change change = changeEncoder.decodeChange(TimeUUIDs.newUUID(), StringSerializer.get().toByteBuffer(encodedDelta));
    assertEquals(change.getDelta().toString(), expectedDelta.toString());
    assertEquals(change.getTags(), tags);
}
 
Example #26
Source File: DeltaBlockingTest.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Test
public void testFragmentedDelta() throws IOException {
    String delta = generateLargeDelta();
    String encodedDelta = StringUtils.repeat('0', _prefixLength) + delta;
    List<ByteBuffer> blocks = _daoUtils.getDeltaBlocks(ByteBuffer.wrap(encodedDelta.getBytes()));
    assertEquals(blocks.size(), _daoUtils.getNumDeltaBlocks(ByteBuffer.wrap(encodedDelta.getBytes())));
    List<TestRow> rows = Lists.newArrayList();
    UUID changeId = UUID.randomUUID();
    for (int i = 0; i < blocks.size() - 1; i++) {
        rows.add(new TestRow(i, changeId, blocks.get(i)));
    }

    UUID secondDeltaUUID = UUID.randomUUID();

    List<ByteBuffer> secondDeltaBlocks = _daoUtils.getDeltaBlocks(ByteBuffer.wrap("0000D3:[]:0:{..,\"name\":\"bobåååååຄຄຄຄຄຄຄຄຄຄ\"}".getBytes()));
    assertEquals(secondDeltaBlocks.size(), _daoUtils.getNumDeltaBlocks(ByteBuffer.wrap("0000D3:[]:0:{..,\"name\":\"bobåååååຄຄຄຄຄຄຄຄຄຄ\"}".getBytes())));

    for (int i = 0; i < secondDeltaBlocks.size(); i++) {
        rows.add(new TestRow(i, secondDeltaUUID, secondDeltaBlocks.get(i)));
    }

    Iterator<DeltaIterator.BlockedDelta> iterator = new ListDeltaIterator(rows.iterator(), false, _prefixLength);
    assertEquals(iterator.hasNext(), true);
    DeltaIterator.BlockedDelta secondBlockedDelta = iterator.next();
    assertEquals("000AD3:[]:0:{..,\"name\":\"bobåååååຄຄຄຄຄຄຄຄຄຄ\"}", StringSerializer.get().fromByteBuffer(secondBlockedDelta.getContent()));
    assertEquals(secondBlockedDelta.getNumBlocks(), secondDeltaBlocks.size());
    assertEquals(iterator.hasNext(), false);

    rows = Lists.newArrayList();

    for (int i = blocks.size() - 1; i >= 1; i--) {
        rows.add(new TestRow(i, changeId, blocks.get(i)));
    }

    for (int i = secondDeltaBlocks.size() - 1; i >= 0; i--) {
        rows.add(new TestRow(i, secondDeltaUUID, secondDeltaBlocks.get(i)));
    }

    Iterator<DeltaIterator.BlockedDelta> reversedIterator = new ListDeltaIterator(rows.iterator(), true, _prefixLength);
    assertEquals(reversedIterator.hasNext(), true);
    DeltaIterator.BlockedDelta reversedBlockedDelta = reversedIterator.next();
    assertEquals("000AD3:[]:0:{..,\"name\":\"bobåååååຄຄຄຄຄຄຄຄຄຄ\"}", StringSerializer.get().fromByteBuffer(reversedBlockedDelta.getContent()));
    assertEquals(reversedBlockedDelta.getNumBlocks(), secondDeltaBlocks.size());
    assertEquals(reversedIterator.hasNext(), false);
}
 
Example #27
Source File: DeltaBlockingTest.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Test
public void testOverwrittenDelta() throws IOException {
    String delta = generateLargeDelta();
    String encodedDelta = StringUtils.repeat('0', _prefixLength) + delta;
    List<ByteBuffer> blocks = _daoUtils.getDeltaBlocks(ByteBuffer.wrap(encodedDelta.getBytes()));
    assertEquals(blocks.size(), _daoUtils.getNumDeltaBlocks(ByteBuffer.wrap(encodedDelta.getBytes())));

    List<TestRow> rows = Lists.newArrayList();
    UUID changeId = UUID.randomUUID();
    for (int i = 0; i < blocks.size(); i++) {
        rows.add(new TestRow(i, changeId, blocks.get(i)));
    }

    int numExtraBlocks = 5;

    for (int i = blocks.size(); i < blocks.size() + numExtraBlocks; i++) {
        rows.add(new TestRow(i, changeId, ByteBuffer.wrap("this text should be ignored by the delta iterator!".getBytes())));
    }

    UUID secondDeltaUUID = UUID.randomUUID();

    List<ByteBuffer> secondDeltaBlocks = _daoUtils.getDeltaBlocks(ByteBuffer.wrap("0000D3:[]:0:{..,\"name\":\"bobåååååຄຄຄຄຄຄຄຄຄຄ\"}".getBytes()));
    assertEquals(secondDeltaBlocks.size(), _daoUtils.getNumDeltaBlocks(ByteBuffer.wrap("0000D3:[]:0:{..,\"name\":\"bobåååååຄຄຄຄຄຄຄຄຄຄ\"}".getBytes())));

    for (int i = 0; i < secondDeltaBlocks.size(); i++) {
        rows.add(new TestRow(i, secondDeltaUUID, secondDeltaBlocks.get(i)));
    }

    Iterator<DeltaIterator.BlockedDelta> iterator = new ListDeltaIterator(rows.iterator(), false, _prefixLength);
    assertTrue(iterator.hasNext());
    DeltaIterator.BlockedDelta overwrittenDeltaWithExtraBlocks = iterator.next();
    assertEquals(StringSerializer.get().fromByteBuffer(_daoUtils.skipPrefix(overwrittenDeltaWithExtraBlocks.getContent())), delta);
    assertEquals(overwrittenDeltaWithExtraBlocks.getNumBlocks(), blocks.size() + numExtraBlocks);
    assertTrue(iterator.hasNext());

    DeltaIterator.BlockedDelta secondBlockedDelta = iterator.next();
    assertEquals("000AD3:[]:0:{..,\"name\":\"bobåååååຄຄຄຄຄຄຄຄຄຄ\"}", StringSerializer.get().fromByteBuffer(secondBlockedDelta.getContent()));
    assertEquals(secondBlockedDelta.getNumBlocks(), secondDeltaBlocks.size());
    assertFalse(iterator.hasNext());

    rows = Lists.newArrayList();

    for (int i = blocks.size() + numExtraBlocks - 1; i >= blocks.size(); i--) {
        rows.add(new TestRow(i, changeId, ByteBuffer.wrap("this text should be ignored by the delta iterator!".getBytes())));
    }

    for (int i = blocks.size() - 1; i >= 0; i--) {
        rows.add(new TestRow(i, changeId, blocks.get(i)));
    }

    for (int i = secondDeltaBlocks.size() - 1; i >= 0; i--) {
        rows.add(new TestRow(i, secondDeltaUUID, secondDeltaBlocks.get(i)));
    }

    Iterator<DeltaIterator.BlockedDelta> reversedIterator = new ListDeltaIterator(rows.iterator(), true, _prefixLength);
    assertTrue(reversedIterator.hasNext());
    DeltaIterator.BlockedDelta reversedOverwrittenDeltaWithExtraBlocks = reversedIterator.next();
    assertEquals(StringSerializer.get().fromByteBuffer(_daoUtils.skipPrefix(reversedOverwrittenDeltaWithExtraBlocks.getContent())), delta);
    assertEquals(reversedOverwrittenDeltaWithExtraBlocks.getNumBlocks(), blocks.size() + numExtraBlocks);
    assertTrue(reversedIterator.hasNext());
    DeltaIterator.BlockedDelta reversedBlockedDelta = reversedIterator.next();
    assertEquals("000AD3:[]:0:{..,\"name\":\"bobåååååຄຄຄຄຄຄຄຄຄຄ\"}", StringSerializer.get().fromByteBuffer(reversedBlockedDelta.getContent()));
    assertEquals(reversedBlockedDelta.getNumBlocks(), secondDeltaBlocks.size());
    assertFalse(reversedIterator.hasNext());
}
 
Example #28
Source File: LocatorSerializer.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuffer toByteBuffer(Locator locator) {
    return StringSerializer.get().toByteBuffer(locator.toString());
}
 
Example #29
Source File: SlotStateSerializer.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public SlotState fromByteBuffer(ByteBuffer byteBuffer) {
    String stringRep = StringSerializer.get().fromByteBuffer(byteBuffer);
    return serDes.deserialize(stringRep);
}
 
Example #30
Source File: SlotStateSerializer.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuffer toByteBuffer(SlotState state) {
    return StringSerializer.get().toByteBuffer(serDes.serialize(state));
}