Search in sources :

Example 31 with WriteBatch

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;
                }
            }
        }
    });
}
Also used : WriteOptions(org.iq80.leveldb.WriteOptions) Variable(org.apache.mesos.state.Variable) Iq80DBFactory.asString(org.iq80.leveldb.impl.Iq80DBFactory.asString) WriteBatch(org.iq80.leveldb.WriteBatch) IOException(java.io.IOException)

Example 32 with WriteBatch

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();
    }
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) StringWriter(java.io.StringWriter) EntityReference(org.qi4j.api.entity.EntityReference) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) WriteBatch(org.iq80.leveldb.WriteBatch) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

WriteBatch (org.iq80.leveldb.WriteBatch)32 IOException (java.io.IOException)22 DBException (org.iq80.leveldb.DBException)18 JniDBFactory.asString (org.fusesource.leveldbjni.JniDBFactory.asString)12 Map (java.util.Map)10 DB (org.iq80.leveldb.DB)8 DBIterator (org.iq80.leveldb.DBIterator)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)4 NavigableMap (java.util.NavigableMap)4 RawMessageTableEntry (co.cask.cdap.messaging.store.RawMessageTableEntry)3 RollingWriteBatch (org.apache.hadoop.yarn.server.timeline.RollingLevelDB.RollingWriteBatch)3 ImmutableMessageTableEntry (co.cask.cdap.messaging.store.ImmutableMessageTableEntry)2 RawPayloadTableEntry (co.cask.cdap.messaging.store.RawPayloadTableEntry)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 HashMap (java.util.HashMap)2 Row (co.cask.cdap.api.dataset.table.Row)1 Scanner (co.cask.cdap.api.dataset.table.Scanner)1 AbstractMessageTable (co.cask.cdap.messaging.store.AbstractMessageTable)1