Search in sources :

Example 1 with Action

use of com.cinchapi.concourse.server.storage.Action in project concourse by cinchapi.

the class Buffer method chronologize.

@Override
public Map<Long, Set<TObject>> chronologize(String key, long record, long start, long end, Map<Long, Set<TObject>> context) {
    Set<TObject> snapshot = Iterables.getLast(context.values(), Sets.<TObject>newLinkedHashSet());
    if (snapshot.isEmpty() && !context.isEmpty()) {
        // CON-474: Empty set is placed in the context if it was the last
        // snapshot known to the database
        context.remove(Time.NONE);
    }
    Iterator<Write> it = iterator(key, record, end - 1);
    try {
        while (it.hasNext()) {
            Write write = it.next();
            long timestamp = write.getVersion();
            Text $key = write.getKey();
            long $record = write.getRecord().longValue();
            Action action = write.getType();
            if ($key.toString().equals(key) && $record == record) {
                snapshot = Sets.newLinkedHashSet(snapshot);
                Value value = write.getValue();
                if (action == Action.ADD) {
                    snapshot.add(value.getTObject());
                } else if (action == Action.REMOVE) {
                    snapshot.remove(value.getTObject());
                }
                if (timestamp >= start) {
                    context.put(timestamp, snapshot);
                }
            }
        }
        return Maps.filterValues(context, emptySetFilter);
    } finally {
        Iterators.close(it);
    }
}
Also used : TObject(com.cinchapi.concourse.thrift.TObject) Action(com.cinchapi.concourse.server.storage.Action) Value(com.cinchapi.concourse.server.model.Value) Text(com.cinchapi.concourse.server.model.Text)

Example 2 with Action

use of com.cinchapi.concourse.server.storage.Action in project concourse by cinchapi.

the class TableRecord method chronologize.

/**
 * Return a time series of values that holds the data stored for {@code key}
 * after each modification.
 *
 * @param key the field name
 * @param start the start timestamp (inclusive)
 * @param end the end timestamp (exclusive)
 * @return the time series of values held in the {@code key} field between
 *         {@code start} and {@code end}
 */
public Map<Long, Set<Value>> chronologize(Text key, long start, long end) {
    read.lock();
    try {
        Map<Long, Set<Value>> context = Maps.newLinkedHashMap();
        List<CompactRevision<Value>> revisions = history.get(key);
        Set<Value> snapshot = Sets.newLinkedHashSet();
        if (revisions != null) {
            Iterator<CompactRevision<Value>> it = revisions.iterator();
            while (it.hasNext()) {
                CompactRevision<Value> revision = it.next();
                long timestamp = revision.getVersion();
                if (timestamp >= end) {
                    break;
                } else {
                    Action action = revision.getType();
                    snapshot = Sets.newLinkedHashSet(snapshot);
                    Value value = revision.getValue();
                    if (action == Action.ADD) {
                        snapshot.add(value);
                    } else if (action == Action.REMOVE) {
                        snapshot.remove(value);
                    }
                    if (timestamp >= start) {
                        context.put(timestamp, snapshot);
                    }
                }
            }
        }
        if (snapshot.isEmpty()) {
            // CON-474: If the last snapshot is empty, add it here so that
            // the Buffer has the proper context
            context.put(Time.NONE, snapshot);
        }
        return context;
    } finally {
        read.unlock();
    }
}
Also used : Action(com.cinchapi.concourse.server.storage.Action) Set(java.util.Set) Value(com.cinchapi.concourse.server.model.Value)

Example 3 with Action

use of com.cinchapi.concourse.server.storage.Action in project concourse by cinchapi.

the class Segment method acquire.

/**
 * Append the {@code write} to this {@link Segment} using the
 * {@code executor} to asynchronously write to all the contained
 * {@link Block blocks}.
 *
 * @param write
 * @param executor
 * @return a {@link Receipt} that contains the {@link Revision Revisions}
 *         that were created as a consequence of the transfer
 * @throws InterruptedException
 */
public Receipt acquire(Write write, AwaitableExecutorService executor) throws InterruptedException {
    Preconditions.checkState(isMutable(), "Cannot transfer Writes to an immutable Segment");
    writeLock.lock();
    try {
        // @formatter:off
        Identifier record = write.getRecord();
        Text key = write.getKey();
        Value value = write.getValue();
        long version = write.getVersion();
        Action type = write.getType();
        Receipt.Builder receipt = Receipt.builder();
        Runnable[] tasks = Array.containing(() -> {
            TableArtifact artifact = table.insert(record, key, value, version, type);
            receipt.itemize(artifact);
        }, () -> {
            IndexArtifact artifact = index.insert(key, value, record, version, type);
            receipt.itemize(artifact);
        }, () -> {
            Collection<CorpusArtifact> artifacts = corpus.insert(key, value, record, version, type);
            receipt.itemize(artifacts);
        });
        // @formatter:on
        executor.await((task, error) -> Logger.error("Unexpected error when trying to transfer the following Write to the Database: {}", write, error), tasks);
        // CON-587: Set the min/max version of this Segment because it
        // cannot be assumed that revisions are inserted in monotonically
        // increasing order of version.
        maxTs = Math.max(write.getVersion(), maxTs);
        minTs = Math.min(write.getVersion(), minTs);
        return receipt.build();
    } finally {
        writeLock.unlock();
    }
}
Also used : Action(com.cinchapi.concourse.server.storage.Action) Text(com.cinchapi.concourse.server.model.Text) Identifier(com.cinchapi.concourse.server.model.Identifier) Value(com.cinchapi.concourse.server.model.Value)

Example 4 with Action

use of com.cinchapi.concourse.server.storage.Action in project concourse by cinchapi.

the class ChunkTest method testIterator.

@Test
public void testIterator() {
    int count = TestData.getScaleCount();
    Set<Revision<L, K, V>> revisions = Sets.newHashSetWithExpectedSize(count);
    for (int i = 0; i < count; ++i) {
        Revision<L, K, V> revision = null;
        while (revision == null || revisions.contains(revision)) {
            L locator = getLocator();
            K key = getKey();
            V value = getValue();
            long version = Time.now();
            Action type = Action.ADD;
            revision = chunk.makeRevision(locator, key, value, version, type);
        }
        chunk.insert(revision.getLocator(), revision.getKey(), revision.getValue(), revision.getVersion(), revision.getType());
        revisions.add(revision);
    }
    chunk.transfer(file);
    chunk = load(file, filter, chunk.manifest());
    Iterator<Revision<L, K, V>> it = chunk.iterator();
    Set<Revision<L, K, V>> stored = Sets.newHashSetWithExpectedSize(count);
    while (it.hasNext()) {
        stored.add(it.next());
    }
    Assert.assertEquals(revisions, stored);
}
Also used : Action(com.cinchapi.concourse.server.storage.Action) Revision(com.cinchapi.concourse.server.storage.db.Revision) ConcourseBaseTest(com.cinchapi.concourse.test.ConcourseBaseTest) Test(org.junit.Test)

Example 5 with Action

use of com.cinchapi.concourse.server.storage.Action in project concourse by cinchapi.

the class Write method fromByteBuffer.

/**
 * Return the Write encoded in {@code bytes} so long as those bytes adhere
 * to the format specified by the {@link #getBytes()} method. This method
 * assumes that all the bytes in the {@code bytes} belong to the Value. In
 * general, it is necessary to get the appropriate Write slice from the
 * parent ByteBuffer using {@link ByteBuffers#slice(ByteBuffer, int, int)}.
 *
 * @param bytes
 * @return the Value
 */
public static Write fromByteBuffer(ByteBuffer bytes) {
    int keySize = bytes.getInt();
    Action type = Action.values()[bytes.get()];
    long version = bytes.getLong();
    Identifier record = Identifier.fromByteBuffer(ByteBuffers.get(bytes, Identifier.SIZE));
    Text key = Text.fromByteBuffer(ByteBuffers.get(bytes, keySize));
    Value value = Value.fromByteBuffer(ByteBuffers.get(bytes, bytes.remaining()));
    return new Write(type, key, value, record, version);
}
Also used : Action(com.cinchapi.concourse.server.storage.Action) Identifier(com.cinchapi.concourse.server.model.Identifier) Value(com.cinchapi.concourse.server.model.Value) Text(com.cinchapi.concourse.server.model.Text)

Aggregations

Action (com.cinchapi.concourse.server.storage.Action)7 Value (com.cinchapi.concourse.server.model.Value)5 Text (com.cinchapi.concourse.server.model.Text)4 Identifier (com.cinchapi.concourse.server.model.Identifier)2 TObject (com.cinchapi.concourse.thrift.TObject)2 Composite (com.cinchapi.concourse.server.io.Composite)1 Revision (com.cinchapi.concourse.server.storage.db.Revision)1 ConcourseBaseTest (com.cinchapi.concourse.test.ConcourseBaseTest)1 Set (java.util.Set)1 Test (org.junit.Test)1