use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.
the class JdbmEntityStoreMixin method applyChanges.
@WriteLock
@Override
public void applyChanges(MapChanges changes) throws IOException {
try {
changes.visitMap(new MapChanger() {
@Override
public Writer newEntity(final EntityReference ref, EntityDescriptor descriptor) throws IOException {
return new StringWriter(1000) {
@Override
public void close() throws IOException {
super.close();
byte[] stateArray = toString().getBytes("UTF-8");
long stateIndex = recordManager.insert(stateArray, serializer);
String indexKey = ref.toString();
index.insert(indexKey.getBytes("UTF-8"), stateIndex, false);
}
};
}
@Override
public Writer updateEntity(final EntityReference ref, EntityDescriptor descriptor) throws IOException {
return new StringWriter(1000) {
@Override
public void close() throws IOException {
super.close();
Long stateIndex = getStateIndex(ref.toString());
byte[] stateArray = toString().getBytes("UTF-8");
recordManager.update(stateIndex, stateArray, serializer);
}
};
}
@Override
public void removeEntity(EntityReference ref, EntityDescriptor descriptor) throws EntityNotFoundException {
try {
Long stateIndex = getStateIndex(ref.toString());
recordManager.delete(stateIndex);
index.remove(ref.toString().getBytes("UTF-8"));
} catch (IOException e) {
throw new EntityStoreException(e);
}
}
});
recordManager.commit();
} catch (Exception e) {
e.printStackTrace();
recordManager.rollback();
if (e instanceof IOException) {
throw (IOException) e;
} else if (e instanceof EntityStoreException) {
throw (EntityStoreException) e;
} else {
throw new IOException(e);
}
}
}
use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.
the class MongoMapEntityStoreMixin method applyChanges.
@Override
public void applyChanges(MapChanges changes) throws IOException {
db.requestStart();
final DBCollection entities = db.getCollection(collectionName);
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 {
super.close();
String jsonState = toString();
DBObject bsonState = (DBObject) JSON.parse(jsonState);
BasicDBObject entity = new BasicDBObject();
entity.put(IDENTITY_COLUMN, ref.identity());
entity.put(STATE_COLUMN, bsonState);
entities.insert(entity, writeConcern);
}
};
}
@Override
public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
return new StringWriter(1000) {
@Override
public void close() throws IOException {
super.close();
DBObject bsonState = (DBObject) JSON.parse(toString());
BasicDBObject entity = new BasicDBObject();
entity.put(IDENTITY_COLUMN, ref.identity());
entity.put(STATE_COLUMN, bsonState);
entities.update(byIdentity(ref), entity, false, false, writeConcern);
}
};
}
@Override
public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
DBObject entity = entities.findOne(byIdentity(ref));
if (entity == null) {
throw new EntityNotFoundException(ref);
}
entities.remove(entity, writeConcern);
}
});
db.requestDone();
}
use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.
the class MongoMapEntityStoreMixin method get.
@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
db.requestStart();
DBObject entity = db.getCollection(collectionName).findOne(byIdentity(entityReference));
if (entity == null) {
throw new EntityNotFoundException(entityReference);
}
DBObject bsonState = (DBObject) entity.get(STATE_COLUMN);
db.requestDone();
String jsonState = JSON.serialize(bsonState);
return new StringReader(jsonState);
}
use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.
the class GaeEntityStoreUnitOfWork method entityStateOf.
@Override
public EntityState entityStateOf(EntityReference reference) throws EntityStoreException, EntityNotFoundException {
Key key = KeyFactory.createKey("qi4j-entity", reference.identity());
try {
Entity entity = datastore.get(key);
GaeEntityState state = new GaeEntityState(this, valueSerialization, entity, module);
states.add(state);
return state;
} catch (com.google.appengine.api.datastore.EntityNotFoundException e) {
throw new EntityNotFoundException(reference);
}
}
use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.
the class JCloudsMapEntityStoreMixin method applyChanges.
@Override
public void applyChanges(MapChanges changes) throws IOException {
final BlobStore blobStore = storeContext.getBlobStore();
changes.visitMap(new MapChanger() {
@Override
public Writer newEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
return new StringWriter() {
@Override
public void close() throws IOException {
super.close();
Blob blob = blobStore.blobBuilder(ref.identity()).payload(ByteSource.wrap(toString().getBytes(UTF_8))).build();
blobStore.putBlob(container, blob);
}
};
}
@Override
public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
if (!blobStore.blobExists(container, ref.identity())) {
throw new EntityNotFoundException(ref);
}
return new StringWriter() {
@Override
public void close() throws IOException {
super.close();
Blob blob = blobStore.blobBuilder(ref.identity()).payload(ByteSource.wrap(toString().getBytes(UTF_8))).build();
blobStore.putBlob(container, blob);
}
};
}
@Override
public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
if (!blobStore.blobExists(container, ref.identity())) {
throw new EntityNotFoundException(ref);
}
blobStore.removeBlob(container, ref.identity());
}
});
}
Aggregations