Search in sources :

Example 11 with Mutation

use of org.apache.cassandra.db.Mutation in project cassandra by apache.

the class HintMessageTest method testSerializer.

@Test
public void testSerializer() throws IOException {
    SchemaLoader.prepareServer();
    SchemaLoader.createKeyspace(KEYSPACE, KeyspaceParams.simple(1), SchemaLoader.standardCFMD(KEYSPACE, TABLE));
    UUID hostId = UUID.randomUUID();
    long now = FBUtilities.timestampMicros();
    TableMetadata table = Schema.instance.getTableMetadata(KEYSPACE, TABLE);
    Mutation mutation = new RowUpdateBuilder(table, now, bytes("key")).clustering("column").add("val", "val" + 1234).build();
    Hint hint = Hint.create(mutation, now / 1000);
    HintMessage message = new HintMessage(hostId, hint);
    // serialize
    int serializedSize = (int) HintMessage.serializer.serializedSize(message, MessagingService.current_version);
    DataOutputBuffer dob = new DataOutputBuffer();
    HintMessage.serializer.serialize(message, dob, MessagingService.current_version);
    assertEquals(serializedSize, dob.getLength());
    // deserialize
    DataInputPlus di = new DataInputBuffer(dob.buffer(), true);
    HintMessage deserializedMessage = HintMessage.serializer.deserialize(di, MessagingService.current_version);
    // compare before/after
    assertEquals(hostId, deserializedMessage.hostId);
    assertHintsEqual(message.hint, deserializedMessage.hint);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Mutation(org.apache.cassandra.db.Mutation) UUID(java.util.UUID) Test(org.junit.Test)

Example 12 with Mutation

use of org.apache.cassandra.db.Mutation in project cassandra by apache.

the class BatchTest method testSerialization.

@Test
public void testSerialization() throws IOException {
    TableMetadata cfm = Keyspace.open(KEYSPACE).getColumnFamilyStore(CF_STANDARD).metadata();
    long now = FBUtilities.timestampMicros();
    int version = MessagingService.current_version;
    UUID uuid = UUIDGen.getTimeUUID();
    List<Mutation> mutations = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), bytes(i)).clustering("name" + i).add("val", "val" + i).build());
    }
    Batch batch1 = Batch.createLocal(uuid, now, mutations);
    assertEquals(uuid, batch1.id);
    assertEquals(now, batch1.creationTime);
    assertEquals(mutations, batch1.decodedMutations);
    DataOutputBuffer out = new DataOutputBuffer();
    Batch.serializer.serialize(batch1, out, version);
    assertEquals(out.getLength(), Batch.serializer.serializedSize(batch1, version));
    DataInputPlus dis = new DataInputBuffer(out.getData());
    Batch batch2 = Batch.serializer.deserialize(dis, version);
    assertEquals(batch1.id, batch2.id);
    assertEquals(batch1.creationTime, batch2.creationTime);
    assertEquals(batch1.decodedMutations.size(), batch2.encodedMutations.size());
    Iterator<Mutation> it1 = batch1.decodedMutations.iterator();
    Iterator<ByteBuffer> it2 = batch2.encodedMutations.iterator();
    while (it1.hasNext()) {
        try (DataInputBuffer in = new DataInputBuffer(it2.next().array())) {
            assertEquals(it1.next().toString(), Mutation.serializer.deserialize(in, version).toString());
        }
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Mutation(org.apache.cassandra.db.Mutation) UUID(java.util.UUID) Test(org.junit.Test)

Example 13 with Mutation

use of org.apache.cassandra.db.Mutation in project cassandra by apache.

the class BatchlogManagerTest method testAddBatch.

@Test
public void testAddBatch() throws IOException {
    long initialAllBatches = BatchlogManager.instance.countAllBatches();
    TableMetadata cfm = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD5).metadata();
    long timestamp = (System.currentTimeMillis() - DatabaseDescriptor.getWriteRpcTimeout() * 2) * 1000;
    UUID uuid = UUIDGen.getTimeUUID();
    // Add a batch with 10 mutations
    List<Mutation> mutations = new ArrayList<>(10);
    for (int j = 0; j < 10; j++) {
        mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(j)).clustering("name" + j).add("val", "val" + j).build());
    }
    BatchlogManager.store(Batch.createLocal(uuid, timestamp, mutations));
    Assert.assertEquals(initialAllBatches + 1, BatchlogManager.instance.countAllBatches());
    String query = String.format("SELECT count(*) FROM %s.%s where id = %s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.BATCHES, uuid);
    UntypedResultSet result = executeInternal(query);
    assertNotNull(result);
    assertEquals(1L, result.one().getLong("count"));
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Mutation(org.apache.cassandra.db.Mutation)

Example 14 with Mutation

use of org.apache.cassandra.db.Mutation in project cassandra by apache.

the class BatchlogManagerTest method testRemoveBatch.

@Test
public void testRemoveBatch() {
    long initialAllBatches = BatchlogManager.instance.countAllBatches();
    TableMetadata cfm = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD5).metadata();
    long timestamp = (System.currentTimeMillis() - DatabaseDescriptor.getWriteRpcTimeout() * 2) * 1000;
    UUID uuid = UUIDGen.getTimeUUID();
    // Add a batch with 10 mutations
    List<Mutation> mutations = new ArrayList<>(10);
    for (int j = 0; j < 10; j++) {
        mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(j)).clustering("name" + j).add("val", "val" + j).build());
    }
    // Store the batch
    BatchlogManager.store(Batch.createLocal(uuid, timestamp, mutations));
    Assert.assertEquals(initialAllBatches + 1, BatchlogManager.instance.countAllBatches());
    // Remove the batch
    BatchlogManager.remove(uuid);
    assertEquals(initialAllBatches, BatchlogManager.instance.countAllBatches());
    String query = String.format("SELECT count(*) FROM %s.%s where id = %s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.BATCHES, uuid);
    UntypedResultSet result = executeInternal(query);
    assertNotNull(result);
    assertEquals(0L, result.one().getLong("count"));
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Mutation(org.apache.cassandra.db.Mutation)

Example 15 with Mutation

use of org.apache.cassandra.db.Mutation in project cassandra by apache.

the class HintsWriteThenReadTest method verifyHints.

private void verifyHints(File directory, HintsDescriptor descriptor) {
    long baseTimestamp = descriptor.timestamp;
    int index = 0;
    try (HintsReader reader = HintsReader.open(new File(directory, descriptor.fileName()))) {
        for (HintsReader.Page page : reader) {
            Iterator<Hint> hints = page.hintsIterator();
            while (hints.hasNext()) {
                Hint hint = hints.next();
                long timestamp = baseTimestamp + index;
                Mutation mutation = hint.mutation;
                assertEquals(timestamp, hint.creationTime);
                assertEquals(dk(bytes(index)), mutation.key());
                Row row = mutation.getPartitionUpdates().iterator().next().iterator().next();
                assertEquals(1, Iterables.size(row.cells()));
                assertEquals(bytes(index), row.clustering().get(0));
                Cell cell = row.cells().iterator().next();
                assertNotNull(cell);
                assertEquals(bytes(index), cell.value());
                assertEquals(timestamp * 1000, cell.timestamp());
                index++;
            }
        }
    }
    assertEquals(index, HINTS_COUNT);
}
Also used : Mutation(org.apache.cassandra.db.Mutation) Row(org.apache.cassandra.db.rows.Row) File(java.io.File) Cell(org.apache.cassandra.db.rows.Cell)

Aggregations

Mutation (org.apache.cassandra.db.Mutation)16 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)9 TableMetadata (org.apache.cassandra.schema.TableMetadata)8 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)5 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)3 Row (org.apache.cassandra.db.rows.Row)3 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)3 Test (org.junit.Test)3 UUID (java.util.UUID)2 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)2 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)2 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 File (java.io.File)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1