use of org.qi4j.library.locking.WriteLock 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);
}
}
}
Aggregations