com.netflix.astyanax.MutationBatch Java Examples

The following examples show how to use com.netflix.astyanax.MutationBatch. 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: MvccEntitySerializationStrategyImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public MutationBatch write( final ApplicationScope applicationScope, final MvccEntity entity ) {
    Preconditions.checkNotNull( applicationScope, "applicationScope is required" );
    Preconditions.checkNotNull( entity, "entity is required" );

    final UUID colName = entity.getVersion();
    final Id entityId = entity.getId();

    return doWrite( applicationScope, entityId, new RowOp() {
        @Override
        public void doOp( final ColumnListMutation<UUID> colMutation ) {
                colMutation.putColumn( colName, getEntitySerializer()
                        .toByteBuffer( new EntityWrapper( entity.getStatus(), entity.getEntity() ) ) );
        }
    } );
}
 
Example #2
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 #3
Source File: InstanceDataDAOCassandra.java    From Raigad with Apache License 2.0 6 votes vote down vote up
public void createInstanceEntry(RaigadInstance instance) throws Exception {
    logger.info("Creating new instance entry");

    String key = getRowKey(instance);
    // If the key exists throw exception
    if (getInstance(instance.getApp(), instance.getDC(), instance.getId()) != null) {
        logger.info(String.format("Key already exists: %s", key));
        return;
    }

    // Grab the lock
    getLock(instance);

    MutationBatch mutationBatch = bootKeyspace.prepareMutationBatch();
    ColumnListMutation<String> columnListMutation = mutationBatch.withRow(CF_INSTANCES, key);
    columnListMutation.putColumn(CN_CLUSTER, instance.getApp(), null);
    columnListMutation.putColumn(CN_AZ, instance.getAvailabilityZone(), null);
    columnListMutation.putColumn(CN_INSTANCEID, instance.getInstanceId(), null);
    columnListMutation.putColumn(CN_HOSTNAME, instance.getHostName(), null);
    columnListMutation.putColumn(CN_IP, instance.getHostIP(), null);
    columnListMutation.putColumn(CN_LOCATION, instance.getDC(), null);
    columnListMutation.putColumn(CN_ASGNAME, instance.getAsg(), null);
    columnListMutation.putColumn(CN_UPDATETIME, TimeUUIDUtils.getUniqueTimeUUIDinMicros(), null);

    mutationBatch.execute();
}
 
Example #4
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 #5
Source File: AstyanaxDataWriterDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
public void purge(AstyanaxStorage storage, Runnable progress) {
    DeltaPlacement placement = (DeltaPlacement) storage.getPlacement();
    CassandraKeyspace keyspace = placement.getKeyspace();

    // Scan all the shards and delete all the rows we find.
    MutationBatch mutation = keyspace.prepareMutationBatch(SorConsistencies.toAstyanax(WriteConsistency.STRONG));
    Iterator<String> keyIter = _keyScanner.scanKeys(storage, ReadConsistency.STRONG);
    while (keyIter.hasNext()) {
        ByteBuffer rowKey = storage.getRowKey(keyIter.next());
        mutation.withRow(placement.getBlockedDeltaColumnFamily(), rowKey).delete();
        if (mutation.getRowCount() >= 100) {
            progress.run();
            execute(mutation, "purge %d records from placement %s", mutation.getRowCount(), placement.getName());
            mutation.discardMutations();
        }
    }
    if (!mutation.isEmpty()) {
        progress.run();
        execute(mutation, "purge %d records from placement %s", mutation.getRowCount(), placement.getName());
    }
}
 
Example #6
Source File: AstyanaxManifestPersister.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(String channel, ByteBuffer slabId) {
    // Deletes don't need to be durable.  If a delete is lost, the next reader to come along and find no events
    // will execute the delete again.
    MutationBatch mutation = _keyspace.prepareMutationBatch(ConsistencyLevel.CL_ANY);

    mutation.withRow(ColumnFamilies.MANIFEST, channel)
            .deleteColumn(slabId);

    mutation.withRow(ColumnFamilies.SLAB, slabId)
            .delete();

    execute(mutation);

    _deleteMeter.mark();
}
 
Example #7
Source File: AstyanaxWriter.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public void insertRollups(List<SingleRollupWriteContext> writeContexts) throws ConnectionException {
    if (writeContexts.size() == 0) {
        return;
    }
    Timer.Context ctx = Instrumentation.getBatchWriteTimerContext(writeContexts.get(0).getDestinationCF().getName());
    MutationBatch mb = keyspace.prepareMutationBatch();
    for (SingleRollupWriteContext writeContext : writeContexts) {
        Rollup rollup = writeContext.getRollup();
        int ttl = (int)TTL_PROVIDER.getTTL(
                writeContext.getLocator().getTenantId(),
                writeContext.getGranularity(),
                writeContext.getRollup().getRollupType()).get().toSeconds();
        AbstractSerializer serializer = Serializers.serializerFor(rollup.getClass());
        try {
            mb.withRow(writeContext.getDestinationCF(), writeContext.getLocator())
                    .putColumn(writeContext.getTimestamp(),
                            rollup,
                            serializer,
                            ttl);
        } catch (RuntimeException ex) {
            // let's not let stupidness prevent the rest of this put.
            log.warn(String.format("Cannot save %s", writeContext.getLocator().toString()), ex);
        }
    }
    try {
        mb.execute();
    } catch (ConnectionException e) {
        Instrumentation.markWriteError(e);
        log.error("Error writing rollup batch", e);
        throw e;
    } finally {
        ctx.stop();
    }
}
 
Example #8
Source File: AstyanaxDataWriterDAO.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Timed (name = "bv.emodb.sorAstyanaxDataWriterDAO.storeCompactedDeltas", absolute = true)
@Override
public void storeCompactedDeltas(Table tbl, String key, List<History> histories, WriteConsistency consistency) {
    checkNotNull(tbl, "table");
    checkNotNull(key, "key");
    checkNotNull(histories, "histories");
    checkNotNull(consistency, "consistency");

    AstyanaxTable table = (AstyanaxTable) tbl;
    for (AstyanaxStorage storage : table.getWriteStorage()) {
        DeltaPlacement placement = (DeltaPlacement) storage.getPlacement();
        CassandraKeyspace keyspace = placement.getKeyspace();

        ByteBuffer rowKey = storage.getRowKey(key);

        MutationBatch mutation = keyspace.prepareMutationBatch(SorConsistencies.toAstyanax(consistency));
        ColumnListMutation<UUID> rowMutation = mutation.withRow(placement.getDeltaHistoryColumnFamily(), rowKey);

        for (History history : histories) {
            rowMutation.putColumn(history.getChangeId(),
                    _changeEncoder.encodeHistory(history),
                    Ttls.toSeconds(_historyStore.getHistoryTtl(), 1, null));
        }
        execute(mutation, "store %d compacted deltas for placement %s, table %s, key %s",
                histories.size(), placement.getName(), table.getName(), key);
    }
}
 
Example #9
Source File: AstyanaxDataWriterDAO.java    From emodb with Apache License 2.0 5 votes vote down vote up
private boolean isThriftFramedTransportSizeOverrun(Execution<?> execution, ConnectionException exception) {
    // Thrift framed transport size overruns don't have an explicit exception, but they fall under the general
    // umbrella of "unknown" thrift transport exceptions.
    Optional<Throwable> thriftException =
            Iterables.tryFind(Throwables.getCausalChain(exception), Predicates.instanceOf(TTransportException.class));
    //noinspection ThrowableResultOfMethodCallIgnored
    if (!thriftException.isPresent() || ((TTransportException) thriftException.get()).getType() != TTransportException.UNKNOWN) {
        return false;
    }

    return execution instanceof MutationBatch &&
            getMutationBatchSize((MutationBatch) execution) >= MAX_THRIFT_FRAMED_TRANSPORT_SIZE;
}
 
Example #10
Source File: AstyanaxDataWriterDAO.java    From emodb with Apache License 2.0 5 votes vote down vote up
private int getMutationBatchSize(MutationBatch mutation) {
    assert mutation instanceof AbstractThriftMutationBatchImpl : "MutationBatch is not an instance of AbstractThriftMutationBatchImpl";
    try (CountingOutputStream countingOut = new CountingOutputStream(ByteStreams.nullOutputStream())) {
        TIOStreamTransport transport = new TIOStreamTransport(countingOut);
        Cassandra.batch_mutate_args args = new Cassandra.batch_mutate_args();
        args.setMutation_map(((AbstractThriftMutationBatchImpl) mutation).getMutationMap());

        args.write(new TBinaryProtocol(transport));
        return (int) countingOut.getCount();
    } catch (TException | IOException e) {
        throw Throwables.propagate(e);
    }
}
 
Example #11
Source File: AstyanaxStorageProvider.java    From emodb with Apache License 2.0 5 votes vote down vote up
@ParameterizedTimed(type = "AstyanaxStorageProvider")
@Override
public void deleteMetadata(Table tbl, String blobId) {
    AstyanaxTable table = (AstyanaxTable) Objects.requireNonNull(tbl, "table");
    Objects.requireNonNull(blobId);

    for (AstyanaxStorage storage : table.getWriteStorage()) {
        BlobPlacement placement = (BlobPlacement) storage.getPlacement();
        MutationBatch mutation = placement.getKeyspace().prepareMutationBatch(CONSISTENCY_STRONG);
        mutation.withRow(placement.getBlobColumnFamily(), storage.getRowKey(blobId))
                .deleteColumn(getColumn(ColumnGroup.A, 0));
        execute(mutation);
        _blobMetadataDeleteMeter.mark(mutation.getRowCount());
    }
}
 
Example #12
Source File: AstyanaxStorageProvider.java    From emodb with Apache License 2.0 5 votes vote down vote up
@ParameterizedTimed(type = "AstyanaxStorageProvider")
@Override
public void writeMetadata(Table tbl, String blobId, StorageSummary summary) {
    AstyanaxTable table = (AstyanaxTable) Objects.requireNonNull(tbl, "table");
    for (AstyanaxStorage storage : table.getWriteStorage()) {
        BlobPlacement placement = (BlobPlacement) storage.getPlacement();

        MutationBatch mutation = placement.getKeyspace().prepareMutationBatch(CONSISTENCY_STRONG)
                .setTimestamp(summary.getTimestamp());
        mutation.withRow(placement.getBlobColumnFamily(), storage.getRowKey(blobId))
                .putColumn(getColumn(ColumnGroup.A, 0), JsonHelper.asJson(summary));
        execute(mutation);
    }
    _blobMetadataWriteMeter.mark();
}
 
Example #13
Source File: InstanceDataDAOCassandra.java    From Raigad with Apache License 2.0 5 votes vote down vote up
private void getLock(RaigadInstance instance) throws Exception {
    String choosingkey = getChoosingKey(instance);
    MutationBatch m = bootKeyspace.prepareMutationBatch();
    ColumnListMutation<String> clm = m.withRow(CF_LOCKS, choosingkey);

    // Expire in 6 sec
    clm.putColumn(instance.getInstanceId(), instance.getInstanceId(), new Integer(6));
    m.execute();
    int count = bootKeyspace.prepareQuery(CF_LOCKS).getKey(choosingkey).getCount().execute().getResult();
    if (count > 1) {
        // Need to delete my entry
        m.withRow(CF_LOCKS, choosingkey).deleteColumn(instance.getInstanceId());
        m.execute();
        throw new Exception(String.format("More than 1 contender for lock %s %d", choosingkey, count));
    }

    String lockKey = getLockingKey(instance);
    OperationResult<ColumnList<String>> result = bootKeyspace.prepareQuery(CF_LOCKS).getKey(lockKey).execute();
    if (result.getResult().size() > 0 && !result.getResult().getColumnByIndex(0).getName().equals(instance.getInstanceId())) {
        throw new Exception(String.format("Lock already taken %s", lockKey));
    }

    clm = m.withRow(CF_LOCKS, lockKey);
    clm.putColumn(instance.getInstanceId(), instance.getInstanceId(), new Integer(600));
    m.execute();
    Thread.sleep(100);
    result = bootKeyspace.prepareQuery(CF_LOCKS).getKey(lockKey).execute();
    if (result.getResult().size() == 1 && result.getResult().getColumnByIndex(0).getName().equals(instance.getInstanceId())) {
        logger.info("Got lock " + lockKey);
        return;
    }
    else {
        throw new Exception(String.format("Cannot insert lock %s", lockKey));
    }
}
 
Example #14
Source File: InstanceDataDAOCassandra.java    From Raigad with Apache License 2.0 5 votes vote down vote up
public void deleteInstanceEntry(RaigadInstance instance) throws Exception {
    logger.info("Deleting dead instance entry");

    // Acquire the lock first
    getLock(instance);

    // Delete the row
    String key = findKey(instance.getApp(), instance.getInstanceId(), instance.getDC());

    if (key == null) {
        return;  // don't fail it
    }

    MutationBatch m = bootKeyspace.prepareMutationBatch();
    m.withRow(CF_INSTANCES, key).delete();
    m.execute();

    key = getLockingKey(instance);

    // Delete key
    m = bootKeyspace.prepareMutationBatch();
    m.withRow(CF_LOCKS, key).delete();
    m.execute();

    // Have to delete choosing key as well to avoid issues with delete
    // followed by immediate writes
    key = getChoosingKey(instance);
    m = bootKeyspace.prepareMutationBatch();
    m.withRow(CF_LOCKS, key).delete();
    m.execute();
}
 
Example #15
Source File: EdgeMetadataSerializationProxyImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public MutationBatch removeEdgeTypeFromSource( final ApplicationScope scope, final Edge edge ) {
    final MigrationRelationship<EdgeMetadataSerialization> migration = getMigrationRelationShip();

    if ( migration.needsMigration() ) {
        final MutationBatch aggregateBatch = keyspace.prepareMutationBatch();

        aggregateBatch.mergeShallow( migration.from.removeEdgeTypeFromSource( scope, edge ) );
        aggregateBatch.mergeShallow( migration.to.removeEdgeTypeFromSource( scope, edge ) );

        return aggregateBatch;
    }

    return migration.to.removeEdgeTypeFromSource( scope, edge );
}
 
Example #16
Source File: MvccEntitySerializationStrategyImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public MutationBatch mark( final ApplicationScope applicationScope, final Id entityId, final UUID version ) {
    Preconditions.checkNotNull( applicationScope, "applicationScope is required" );
    Preconditions.checkNotNull( entityId, "entity id is required" );
    Preconditions.checkNotNull( version, "version is required" );

    return doWrite( applicationScope, entityId, new RowOp() {
        @Override
        public void doOp( final ColumnListMutation<UUID> colMutation ) {
            colMutation.putColumn( version, getEntitySerializer()
                    .toByteBuffer( new EntityWrapper( MvccEntity.Status.COMPLETE, Optional.<Entity>absent() ) ) );
        }
    } );
}
 
Example #17
Source File: MvccLogEntrySerializationProxyImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public MutationBatch delete( final ApplicationScope applicationScope, final Id entityId, final UUID version ) {
    final MigrationRelationship<MvccLogEntrySerializationStrategy> migration = getMigrationRelationShip();

    if ( migration.needsMigration() ) {
        final MutationBatch aggregateBatch = keyspace.prepareMutationBatch();

        aggregateBatch.mergeShallow( migration.from.delete( applicationScope, entityId, version ) );
        aggregateBatch.mergeShallow( migration.to.delete( applicationScope, entityId, version ) );

        return aggregateBatch;
    }

    return migration.to.delete( applicationScope, entityId, version );
}
 
Example #18
Source File: ALocatorIO.java    From blueflood with Apache License 2.0 5 votes vote down vote up
/**
 * Insert a locator with key = shard long value calculated using Util.getShard()
 * @param locator
 * @throws IOException
 */
@Override
public void insertLocator(Locator locator) throws IOException {
    Timer.Context timer = Instrumentation.getWriteTimerContext(CassandraModel.CF_METRICS_LOCATOR_NAME);
    try {
        MutationBatch mutationBatch = AstyanaxIO.getKeyspace().prepareMutationBatch();
        AstyanaxWriter.getInstance().insertLocator(locator, mutationBatch);
        mutationBatch.execute();
    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        timer.stop();
    }
}
 
Example #19
Source File: MvccLogEntrySerializationProxyImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public MutationBatch write( final ApplicationScope applicationScope, final MvccLogEntry entry ) {
    final MigrationRelationship<MvccLogEntrySerializationStrategy> migration = getMigrationRelationShip();

    if ( migration.needsMigration() ) {
        final MutationBatch aggregateBatch = keyspace.prepareMutationBatch();

        aggregateBatch.mergeShallow( migration.from.write( applicationScope, entry ) );
        aggregateBatch.mergeShallow( migration.to.write( applicationScope, entry ) );

        return aggregateBatch;
    }

    return migration.to.write( applicationScope, entry );
}
 
Example #20
Source File: MvccEntityDataMigrationImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
protected void executeBatch(final int targetVersion, final MutationBatch batch, final ProgressObserver po,
                            final AtomicLong count, com.datastax.driver.core.BatchStatement uniqueBatch) {
    try {

        batch.execute();
        session.execute(uniqueBatch);


        po.update( targetVersion, "Finished copying " + count + " entities to the new format" );
    }
    catch ( ConnectionException e ) {
        po.failed( targetVersion, "Failed to execute mutation in cassandra" );
        throw new DataMigrationException( "Unable to migrate batches ", e );
    }
}
 
Example #21
Source File: AstyanaxWriter.java    From blueflood with Apache License 2.0 5 votes vote down vote up
public void insertFull(Collection<? extends IMetric> metrics, boolean isRecordingDelayedMetrics, Clock clock) throws ConnectionException {
    Timer.Context ctx = Instrumentation.getWriteTimerContext(CassandraModel.CF_METRICS_FULL_NAME);

    try {
        MutationBatch mutationBatch = keyspace.prepareMutationBatch();
        for (IMetric metric: metrics) {
            final Locator locator = metric.getLocator();

            // key = shard
            // col = locator (acct + entity + check + dimension.metric)
            // value = <nothing>
            if (!LocatorCache.getInstance().isLocatorCurrentInBatchLayer(locator)) {
                if (mutationBatch != null)
                    insertLocator(locator, mutationBatch);
                LocatorCache.getInstance().setLocatorCurrentInBatchLayer(locator);
            }

            if (isRecordingDelayedMetrics) {
                //retaining the same conditional logic that was used to insertLocator(locator, batch) above.
                if (mutationBatch != null) {
                    insertLocatorIfDelayed(metric, mutationBatch, clock);
                }
            }

            insertMetric(metric, mutationBatch);

            Instrumentation.markFullResMetricWritten();
        }
        // insert it
        try {
            mutationBatch.execute();
        } catch (ConnectionException e) {
            Instrumentation.markWriteError(e);
            log.error("Connection exception during insertFull", e);
            throw e;
        }
    } finally {
        ctx.stop();
    }
}
 
Example #22
Source File: MvccEntitySerializationStrategyImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public MutationBatch delete( final ApplicationScope applicationScope, final Id entityId, final UUID version ) {
    Preconditions.checkNotNull( applicationScope, "applicationScope is required" );
    Preconditions.checkNotNull( entityId, "entity id is required" );
    Preconditions.checkNotNull( version, "version is required" );


    return doWrite( applicationScope, entityId, new RowOp() {
        @Override
        public void doOp( final ColumnListMutation<UUID> colMutation ) {
            colMutation.deleteColumn( version );
        }
    } );
}
 
Example #23
Source File: MvccEntitySerializationStrategyV3Impl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public MutationBatch delete( final ApplicationScope applicationScope, final Id entityId, final UUID version ) {
    Preconditions.checkNotNull( applicationScope, "applicationScope is required" );
    Preconditions.checkNotNull( entityId, "entity id is required" );
    Preconditions.checkNotNull( version, "version is required" );


    return doWrite( applicationScope, entityId, version, colMutation -> colMutation.deleteColumn( Boolean.TRUE ) );
}
 
Example #24
Source File: MvccEntitySerializationStrategyV3Impl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public MutationBatch mark( final ApplicationScope applicationScope, final Id entityId, final UUID version ) {
    Preconditions.checkNotNull(applicationScope, "applicationScope is required");
    Preconditions.checkNotNull(entityId, "entity id is required");
    Preconditions.checkNotNull(version, "version is required");


    return doWrite(applicationScope, entityId, version, colMutation ->
            colMutation.putColumn(COL_VALUE,
                entitySerializer.toByteBuffer(new EntityWrapper(entityId, version, MvccEntity.Status.DELETED, null, 0))
            )
    );
}
 
Example #25
Source File: AstyanaxWriter.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private void insertMetric(IMetric metric, MutationBatch mutationBatch) {
    try {
        mutationBatch.withRow(CassandraModel.CF_METRICS_FULL, metric.getLocator())
                    .putColumn(metric.getCollectionTime(),
                            metric.getMetricValue(),
                            Serializers.serializerFor(Object.class),
                            metric.getTtlInSeconds());
    } catch (RuntimeException e) {
        log.error("Error serializing full resolution data", e);
    }
}
 
Example #26
Source File: NodeSerializationImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public MutationBatch mark( final ApplicationScope scope, final Id node, final long timestamp ) {
    ValidationUtils.validateApplicationScope( scope );
    ValidationUtils.verifyIdentity( node );
    GraphValidation.validateTimestamp( timestamp, "timestamp" );

    MutationBatch batch = keyspace.prepareMutationBatch().withConsistencyLevel( fig.getWriteCL() );

    batch.withRow( GRAPH_DELETE, ScopedRowKey.fromKey( scope.getApplication(), node ) ).setTimestamp( timestamp )
         .putColumn( COLUMN_NAME, timestamp );

    return batch;
}
 
Example #27
Source File: MvccLogEntrySerializationStrategyImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Do the column update or delete for the given column and row key
 *
 * @param collectionScope We need to use this when getting the keyspace
 */
private MutationBatch doWrite( ApplicationScope collectionScope, Id entityId, UUID version, RowOp op ) {

    final MutationBatch batch = keyspace.prepareMutationBatch();

    final long timestamp = version.timestamp();

    if (logger.isTraceEnabled()) {
        logger.trace("Writing version with timestamp '{}'", timestamp);
    }

    final Id applicationId = collectionScope.getApplication();

    final ScopedRowKey<K> key = createKey( applicationId, entityId );

    op.doOp( batch.withRow( CF_ENTITY_LOG, key ) );

    return batch;
}
 
Example #28
Source File: EdgeMetadataSerializationV2Impl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public MutationBatch removeIdTypeToTarget( final ApplicationScope scope, final Edge edge ) {
    return removeIdTypeToTarget( scope, edge.getTargetNode(), edge.getType(), edge.getSourceNode().getType(),
            edge.getTimestamp() );
}
 
Example #29
Source File: WriteUniqueVerifyTest.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoFields() throws ConnectionException {

    final ApplicationScope collectionScope = mock( ApplicationScope.class );
    final Keyspace keyspace = mock(Keyspace.class);
    final MutationBatch batch = mock(MutationBatch.class);

    when(keyspace.prepareMutationBatch()).thenReturn(batch);

    // set up the mock to return the entity from the start phase
    final Entity entity = generateEntity();

    final MvccEntity mvccEntity = fromEntity( entity );

    // run the stage
    WriteUniqueVerify newStage = new WriteUniqueVerify( uvstrat, fig, keyspace, cassandraConfig, null, null, null, session );


   newStage.call( new CollectionIoEvent<>( collectionScope, mvccEntity ) ) ;

   // if we get here, it's a success.  We want to test no exceptions are thrown

    verify(batch, never()).execute();
}
 
Example #30
Source File: EdgeShardSerializationImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public MutationBatch removeShardMeta( final ApplicationScope scope,
                                      final Shard shard,   final DirectedEdgeMeta metaData) {

    ValidationUtils.validateApplicationScope( scope );
    GraphValidation.valiateShard( shard );
    GraphValidation.validateDirectedEdgeMeta( metaData );



    final ScopedRowKey rowKey = ScopedRowKey.fromKey( scope.getApplication(), metaData );

    final MutationBatch batch = keyspace.prepareMutationBatch();

    // write the row with a current timestamp so we can ensure that it's persisted with updated shard meta
    long batchTimestamp = System.currentTimeMillis();

    batch.withTimestamp(batchTimestamp).withRow( EDGE_SHARDS, rowKey )
        .deleteColumn( shard.getShardIndex() ).setTimestamp(batchTimestamp);

    return batch;
}