Search in sources :

Example 1 with RiakRetryFailedException

use of com.basho.riak.client.RiakRetryFailedException in project qi4j-sdk by Qi4j.

the class AbstractRiakMapEntityStore method applyChanges.

@Override
public void applyChanges(MapChanges changes) throws IOException {
    try {
        final Bucket bucket = riakClient.fetchBucket(bucketKey).execute();
        changes.visitMap(new MapChanger() {

            @Override
            public Writer newEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
                return new StringWriter(1000) {

                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                            bucket.store(ref.identity(), toString()).execute();
                        } catch (RiakException ex) {
                            throw new EntityStoreException("Unable to apply entity change: newEntity", ex);
                        }
                    }
                };
            }

            @Override
            public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
                return new StringWriter(1000) {

                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                            IRiakObject entity = bucket.fetch(ref.identity()).execute();
                            if (entity == null) {
                                throw new EntityNotFoundException(ref);
                            }
                            bucket.store(ref.identity(), toString()).execute();
                        } catch (RiakException ex) {
                            throw new EntityStoreException("Unable to apply entity change: updateEntity", ex);
                        }
                    }
                };
            }

            @Override
            public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
                try {
                    IRiakObject entity = bucket.fetch(ref.identity()).execute();
                    if (entity == null) {
                        throw new EntityNotFoundException(ref);
                    }
                    bucket.delete(ref.identity()).execute();
                } catch (RiakException ex) {
                    throw new EntityStoreException("Unable to apply entity change: removeEntity", ex);
                }
            }
        });
    } catch (RiakRetryFailedException ex) {
        throw new EntityStoreException("Unable to apply entity changes.", ex);
    }
}
Also used : IRiakObject(com.basho.riak.client.IRiakObject) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) StringWriter(java.io.StringWriter) Bucket(com.basho.riak.client.bucket.Bucket) EntityReference(org.qi4j.api.entity.EntityReference) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) RiakRetryFailedException(com.basho.riak.client.RiakRetryFailedException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) RiakException(com.basho.riak.client.RiakException)

Example 2 with RiakRetryFailedException

use of com.basho.riak.client.RiakRetryFailedException in project qi4j-sdk by Qi4j.

the class AbstractRiakMapEntityStore method get.

@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
    try {
        Bucket bucket = riakClient.fetchBucket(bucketKey).execute();
        IRiakObject entity = bucket.fetch(entityReference.identity()).execute();
        if (entity == null) {
            throw new EntityNotFoundException(entityReference);
        }
        String jsonState = entity.getValueAsString();
        return new StringReader(jsonState);
    } catch (RiakRetryFailedException ex) {
        throw new EntityStoreException("Unable to get Entity " + entityReference.identity(), ex);
    }
}
Also used : Bucket(com.basho.riak.client.bucket.Bucket) IRiakObject(com.basho.riak.client.IRiakObject) StringReader(java.io.StringReader) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) RiakRetryFailedException(com.basho.riak.client.RiakRetryFailedException)

Aggregations

IRiakObject (com.basho.riak.client.IRiakObject)2 RiakRetryFailedException (com.basho.riak.client.RiakRetryFailedException)2 Bucket (com.basho.riak.client.bucket.Bucket)2 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)2 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)2 RiakException (com.basho.riak.client.RiakException)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 EntityReference (org.qi4j.api.entity.EntityReference)1