use of com.basho.riak.client.IRiakObject 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);
}
}
use of com.basho.riak.client.IRiakObject 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);
}
}
Aggregations