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));
}
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;
}
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);
}
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;
}
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);
}
Aggregations