Search in sources :

Example 36 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 using the specified timebucket.
 * @param k key
 * @param v value
 * @param timeBucket timebucket
 * @return true if the given (k,v) is successfully inserted into the store otherwise false.
 */
public boolean put(@Nullable K k, @Nullable V v, long timeBucket) {
    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 37 with Slice

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

the class SpillableArrayListMultimapImpl method put.

@Override
public boolean put(@Nullable K key, @Nullable V value) {
    SpillableArrayListImpl<V> spillableArrayList = getHelper(key);
    if (spillableArrayList == null) {
        Slice keyPrefix = keyValueSerdeManager.serializeDataKey(key, true);
        spillableArrayList = new SpillableArrayListImpl<V>(bucket, keyPrefix.toByteArray(), store, valueSerde);
        spillableArrayList.setup(context);
        cache.put(key, spillableArrayList);
    }
    spillableArrayList.add(value);
    return true;
}
Also used : Slice(com.datatorrent.netlet.util.Slice)

Example 38 with Slice

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

the class SpillableMapImpl method get.

@Override
public V get(Object o) {
    K key = (K) o;
    if (cache.getRemovedKeys().contains(key)) {
        return null;
    }
    V val = cache.get(key);
    if (val != null) {
        return val;
    }
    Slice valSlice = store.getSync(getBucketTimeOrId(key), keyValueSerdeManager.serializeDataKey(key, false));
    if (valSlice == null || valSlice == BucketedState.EXPIRED || valSlice.length == 0) {
        return null;
    }
    tmpInput.setBuffer(valSlice.buffer, valSlice.offset, valSlice.length);
    return keyValueSerdeManager.deserializeValue(tmpInput);
}
Also used : BufferSlice(org.apache.apex.malhar.lib.utils.serde.BufferSlice) Slice(com.datatorrent.netlet.util.Slice)

Example 39 with Slice

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

the class SpillableSetMultimapImpl method getHelper.

private SpillableSetImpl<V> getHelper(@NotNull K key) {
    SpillableSetImpl<V> spillableSet = cache.get(key);
    if (spillableSet == null) {
        long keyTime = -1;
        Pair<Integer, V> meta;
        if (timeExtractor != null) {
            keyTime = timeExtractor.getTime(key);
        }
        meta = map.get(key);
        if (meta == null) {
            return null;
        }
        Slice keyPrefix = keyValueSerdeManager.serializeDataKey(key, false);
        if (timeExtractor != null) {
            spillableSet = new SpillableSetImpl<>(keyPrefix.toByteArray(), store, valueSerde, new TimeExtractor.FixedTimeExtractor(keyTime));
        } else {
            spillableSet = new SpillableSetImpl<>(bucket, keyPrefix.toByteArray(), store, valueSerde);
        }
        spillableSet.setSize(meta.getLeft());
        spillableSet.setHead(meta.getRight());
        spillableSet.setup(context);
    }
    cache.put(key, spillableSet);
    return spillableSet;
}
Also used : Slice(com.datatorrent.netlet.util.Slice)

Example 40 with Slice

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

the class InMemSpillableStateStore method getSync.

@Override
public Slice getSync(long bucketId, @NotNull Slice key) {
    Map<Slice, Slice> bucket = store.get(bucketId);
    if (bucket == null) {
        bucket = Maps.newHashMap();
        store.put(bucketId, bucket);
    }
    if (key.getClass() == Slice.class) {
        // The hashCode of Slice was not correct, so correct it
        key = new BufferSlice(key);
    }
    return bucket.get(key);
}
Also used : BufferSlice(org.apache.apex.malhar.lib.utils.serde.BufferSlice) Slice(com.datatorrent.netlet.util.Slice) BufferSlice(org.apache.apex.malhar.lib.utils.serde.BufferSlice)

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