use of org.iq80.leveldb.WriteBatch in project jesos by groupon.
the class JLevelDBState method store.
@Override
public Future<Variable> store(final Variable variable) {
checkNotNull(variable, "variable is null");
checkState(!closed.get(), "already closed");
checkState(variable instanceof JVariable, "can not process native variable, use JVariable");
final JVariable v = (JVariable) variable;
return executor.submit(new Callable<Variable>() {
@Override
public Variable call() throws Exception {
final WriteOptions writeOptions = new WriteOptions();
writeOptions.sync(true);
final String internedName = v.getName().intern();
synchronized (internedName) {
final JVariable current = load(internedName);
if (current == null || current.getUuid().equals(v.getUuid())) {
final JVariable update = new JVariable(internedName, v.value());
final WriteBatch writeBatch = db.createWriteBatch();
writeBatch.delete(bytes(internedName));
writeBatch.put(bytes(internedName), update.getEntry().toByteArray());
db.write(writeBatch, writeOptions);
return update;
} else {
return null;
}
}
}
});
}
use of org.iq80.leveldb.WriteBatch in project qi4j-sdk by Qi4j.
the class LevelDBEntityStoreMixin method applyChanges.
@Override
public void applyChanges(MapChanges changes) throws IOException {
final WriteBatch writeBatch = db.createWriteBatch();
try {
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();
writeBatch.put(ref.identity().getBytes(charset), jsonState.getBytes(charset));
}
};
}
@Override
public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
return new StringWriter(1000) {
@Override
public void close() throws IOException {
super.close();
String jsonState = toString();
writeBatch.put(ref.identity().getBytes(charset), jsonState.getBytes(charset));
}
};
}
@Override
public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
writeBatch.delete(ref.identity().getBytes(charset));
}
});
db.write(writeBatch);
} finally {
writeBatch.close();
}
}
Aggregations