Search in sources :

Example 66 with Slice

use of com.datatorrent.netlet.util.Slice in project apex-malhar by apache.

the class FSWindowDataManager method retrieve.

private Object retrieve(FSWindowReplayWAL wal, long windowId) throws IOException {
    if (windowId > largestCompletedWindow || wal.walEndPointerAfterRecovery == null) {
        return null;
    }
    FileSystemWAL.FileSystemWALReader reader = wal.getReader();
    while (reader.getCurrentPointer() == null || reader.getCurrentPointer().compareTo(wal.walEndPointerAfterRecovery) < 0) {
        long currentWindow;
        if (wal.retrievedWindow == null) {
            wal.retrievedWindow = readNext(reader);
            Preconditions.checkNotNull(wal.retrievedWindow);
        }
        currentWindow = Longs.fromByteArray(wal.retrievedWindow.toByteArray());
        if (windowId == currentWindow) {
            Slice data = readNext(reader);
            Preconditions.checkNotNull(data, "data is null");
            wal.windowWalParts.put(currentWindow, reader.getCurrentPointer().getPartNum());
            // null or next window
            wal.retrievedWindow = readNext(reader);
            return fromSlice(data);
        } else if (windowId < currentWindow) {
            // no artifact saved corresponding to that window and artifact is not read.
            return null;
        } else {
            // windowId > current window so we skip the data
            skipNext(reader);
            wal.windowWalParts.put(currentWindow, reader.getCurrentPointer().getPartNum());
            // null or next window
            wal.retrievedWindow = readNext(reader);
            if (wal.retrievedWindow == null) {
                // nothing else to read
                return null;
            }
        }
    }
    return null;
}
Also used : Slice(com.datatorrent.netlet.util.Slice)

Example 67 with Slice

use of com.datatorrent.netlet.util.Slice in project apex-malhar by apache.

the class AbstractManagedStateImpl method getValueFromBucketAsync.

@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
protected Future<Slice> getValueFromBucketAsync(long bucketId, long timeBucket, @NotNull Slice key) {
    Preconditions.checkNotNull(key, "key");
    long bucketIdx = prepareBucket(bucketId);
    Bucket bucket = buckets.get(bucketIdx);
    synchronized (bucket) {
        Slice cachedVal = bucket.get(key, timeBucket, Bucket.ReadSource.MEMORY);
        if (cachedVal != null) {
            return Futures.immediateFuture(cachedVal);
        }
        ValueFetchTask valueFetchTask = new ValueFetchTask(bucket, key, timeBucket, this);
        tasksPerBucketId.put(bucket.getBucketId(), valueFetchTask);
        return readerService.submit(valueFetchTask);
    }
}
Also used : Slice(com.datatorrent.netlet.util.Slice)

Example 68 with Slice

use of com.datatorrent.netlet.util.Slice in project apex-malhar by apache.

the class IncrementalCheckpointManager method retrieveAllWindows.

/**
 * Retrieves artifacts available for all the windows saved by the enclosing partitions.
 * @return  artifact saved per window.
 * @throws IOException
 */
public Map<Long, Object> retrieveAllWindows() throws IOException {
    Map<Long, Object> artifactPerWindow = new HashMap<>();
    FileSystemWAL.FileSystemWALReader reader = getWal().getReader();
    reader.seek(getWal().getWalStartPointer());
    Slice windowSlice = readNext(reader);
    while (reader.getCurrentPointer().compareTo(getWal().getWalEndPointerAfterRecovery()) < 0 && windowSlice != null) {
        long window = Longs.fromByteArray(windowSlice.toByteArray());
        Object data = fromSlice(readNext(reader));
        artifactPerWindow.put(window, data);
        // null or next window
        windowSlice = readNext(reader);
    }
    reader.seek(getWal().getWalStartPointer());
    return artifactPerWindow;
}
Also used : FileSystemWAL(org.apache.apex.malhar.lib.wal.FileSystemWAL) HashMap(java.util.HashMap) Slice(com.datatorrent.netlet.util.Slice) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 69 with Slice

use of com.datatorrent.netlet.util.Slice in project apex-malhar by apache.

the class ManagedTimeStateMultiValue method put.

/**
 * Inserts the (k,v) into the store.
 * @param k key
 * @param v value
 * @return true if the given (k,v) is successfully inserted into the store otherwise false.
 */
@Override
public boolean put(@Nullable K k, @Nullable V v) {
    if (isKeyContainsMultiValue) {
        Slice keySlice = streamCodec.toByteArray(k);
        long bucketId = getBucketId(k);
        Slice valueSlice = store.getSync(bucketId, keySlice);
        List<V> listOb;
        if (valueSlice == null || valueSlice.length == 0) {
            listOb = new ArrayList<>();
        } else {
            listOb = (List<V>) streamCodec.fromByteArray(valueSlice);
        }
        listOb.add(v);
        return insertInStore(bucketId, timeBucket, keySlice, streamCodec.toByteArray(listOb));
    }
    return insertInStore(getBucketId(k), timeBucket, streamCodec.toByteArray(k), streamCodec.toByteArray(v));
}
Also used : Slice(com.datatorrent.netlet.util.Slice)

Example 70 with Slice

use of com.datatorrent.netlet.util.Slice in project apex-malhar by apache.

the class ManagedTimeStateMultiValue method get.

/**
 * Return the list of values from the store
 * @param k given key
 * @return list of values
 */
@Override
public List<V> get(@Nullable K k) {
    List<V> value = null;
    Slice valueSlice = store.getSync(getBucketId(k), streamCodec.toByteArray(k));
    if (valueSlice == null || valueSlice.length == 0 || valueSlice.buffer == null) {
        return null;
    }
    if (isKeyContainsMultiValue) {
        return (List<V>) streamCodec.fromByteArray(valueSlice);
    }
    value = new ArrayList<>();
    value.add((V) streamCodec.fromByteArray(valueSlice));
    return value;
}
Also used : Slice(com.datatorrent.netlet.util.Slice) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Slice (com.datatorrent.netlet.util.Slice)114 Test (org.junit.Test)65 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Input (com.esotericsoftware.kryo.io.Input)9 IOException (java.io.IOException)6 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 BufferSlice (org.apache.apex.malhar.lib.utils.serde.BufferSlice)4 Path (org.apache.hadoop.fs.Path)4 ObjectMapperString (com.datatorrent.common.util.ObjectMapperString)3 SerializationBuffer (org.apache.apex.malhar.lib.utils.serde.SerializationBuffer)3 StringSerde (org.apache.apex.malhar.lib.utils.serde.StringSerde)3 Attribute (com.datatorrent.api.Attribute)2 OperatorContext (com.datatorrent.api.Context.OperatorContext)2 Output (com.esotericsoftware.kryo.io.Output)2 RandomAccessFile (java.io.RandomAccessFile)2 Serializable (java.io.Serializable)2 HashSet (java.util.HashSet)2