com.datastax.driver.core.querybuilder.Using Java Examples

The following examples show how to use com.datastax.driver.core.querybuilder.Using. 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: ScopedCacheSerializationImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private V writeValueCQL(CacheScope scope, K key, V value, Integer ttl) {

        Preconditions.checkNotNull( scope, "scope is required");
        Preconditions.checkNotNull( key, "key is required" );
        Preconditions.checkNotNull( value, "value is required");
        Preconditions.checkNotNull( ttl, "ttl is required");


        final String rowKeyString = scope.getApplication().getUuid().toString();
        final int bucket = BUCKET_LOCATOR.getCurrentBucket(rowKeyString);

        // determine column name based on K key to string
        final String columnName = key.toString();

        // serialize cache item
        byte[] cacheBytes;
        try {
            cacheBytes = MAPPER.writeValueAsBytes(value);
        } catch (JsonProcessingException jpe) {
            throw new RuntimeException("Unable to serialize cache value", jpe);
        }

        final Using timeToLive = QueryBuilder.ttl(ttl);


        // convert to ByteBuffer for the blob DataType in Cassandra
        final ByteBuffer bb = ByteBuffer.allocate(cacheBytes.length);
        bb.put(cacheBytes);
        bb.flip();

        final Statement cacheEntry = QueryBuilder.insertInto(SCOPED_CACHE_TABLE)
            .using(timeToLive)
            .value("key", getPartitionKey(scope, rowKeyString, bucket))
            .value("column1", DataType.text().serialize(columnName, ProtocolVersion.NEWEST_SUPPORTED))
            .value("value", bb);


        session.execute(cacheEntry);

        logger.debug("Wrote cache item to scope {}\n   key/value types {}/{}\n   key:value: {}:{}",
            scope.getApplication().getUuid(),
            key.getClass().getSimpleName(),
            value.getClass().getSimpleName(),
            key,
            value);

        return value;

    }