storm.trident.state.OpaqueValue Java Examples

The following examples show how to use storm.trident.state.OpaqueValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpaqueMap.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public List<T> multiGet(List<List<Object>> keys) {
    List<CachedBatchReadsMap.RetVal<OpaqueValue>> curr = _backing.multiGet(keys);
    List<T> ret = new ArrayList<T>(curr.size());
    for(CachedBatchReadsMap.RetVal<OpaqueValue> retval: curr) {
        OpaqueValue val = retval.val;
        if(val!=null) {
            if(retval.cached) {
                ret.add((T) val.getCurr());
            } else {
                ret.add((T) val.get(_currTx));
            }
        } else {
            ret.add(null);
        }
    }
    return ret;
}
 
Example #2
Source File: LRUMemoryMapState.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<List<Object>> getTuples() {
    return new Iterator<List<Object>>() {

        private Iterator<Map.Entry<List<Object>, T>> it = db.entrySet().iterator();

        public boolean hasNext() {
            return it.hasNext();
        }

        public List<Object> next() {
            Map.Entry<List<Object>, T> e = it.next();
            List<Object> ret = new ArrayList<Object>();
            ret.addAll(e.getKey());
            ret.add(((OpaqueValue)e.getValue()).getCurr());
            return ret;
        }

        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
}
 
Example #3
Source File: MemoryMapState.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<List<Object>> getTuples() {
    return new Iterator<List<Object>>() {

        private Iterator<Map.Entry<List<Object>, T>> it = db.entrySet().iterator();

        public boolean hasNext() {
            return it.hasNext();
        }

        public List<Object> next() {
            Map.Entry<List<Object>, T> e = it.next();
            List<Object> ret = new ArrayList<Object>();
            ret.addAll(e.getKey());
            ret.add(((OpaqueValue)e.getValue()).getCurr());
            return ret;
        }

        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
}
 
Example #4
Source File: ESIndexMapState.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public State makeState(Map conf, IMetricsContext iMetricsContext, int i, int i2) {
    Options options = new Options(conf);
    ESIndexMapState<OpaqueValue<T>> mapState = new ESIndexMapState<>(clientFactory.makeClient(conf), serializer, new BulkResponseHandler.LoggerResponseHandler(), options.reportError());
    MapState ms  = OpaqueMap.build(new CachedMap(mapState, options.getCachedMapSize()));
    return new SnapshottableMap<OpaqueValue<T>>(ms, new Values(options.getGlobalKey()));
}
 
Example #5
Source File: ValueSerializer.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public OpaqueValue<T> deserialize(byte[] value) throws IOException {
    ObjectNode node = mapper.readValue(value, ObjectNode.class);
    long currTxid = node.get(FIELD_CURR_TIXD).asLong();
    T val = mapper.readValue(mapper.writeValueAsBytes(node.get(FIELD_CURR)), type);
    JsonNode prevNode = node.get(FIELD_PREV);
    T prev = (prevNode.isNull()) ? null : mapper.readValue(mapper.writeValueAsBytes(prevNode), type);
    return new OpaqueValue<>(currTxid, val, prev);
}
 
Example #6
Source File: ValueSerializerTest.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeOpaqueValueWithNoPreviousValue( ) throws IOException {
    OpaqueValueSerializer<FooDocument> serializer = new OpaqueValueSerializer<>(FooDocument.class);
    byte[] value = serializer.serialize(new OpaqueValue<>(1L, new FooDocument("foo")));

    OpaqueValue<FooDocument> actual = serializer.deserialize(value);
    Assert.assertNotNull(actual);
    Assert.assertEquals(1L, (long)actual.getCurrTxid());
    Assert.assertEquals("foo", actual.getCurr().value);
}
 
Example #7
Source File: ValueSerializerTest.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeOpaqueValueWithPreviousValue( ) throws IOException {
    OpaqueValueSerializer<FooDocument> serializer = new OpaqueValueSerializer<>(FooDocument.class);
    byte[] value = serializer.serialize(new OpaqueValue<>(1L, new FooDocument("foo"), new FooDocument("bar")));

    OpaqueValue<FooDocument> actual = serializer.deserialize(value);
    Assert.assertNotNull(actual);
    Assert.assertEquals(1L, (long)actual.getCurrTxid());
    Assert.assertEquals("foo", actual.getCurr().value);
    Assert.assertEquals("bar", actual.getPrev().value);
}
 
Example #8
Source File: OpaqueMap.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public List<T> multiUpdate(List<List<Object>> keys, List<ValueUpdater> updaters) {
    List<CachedBatchReadsMap.RetVal<OpaqueValue>> curr = _backing.multiGet(keys);
    List<OpaqueValue> newVals = new ArrayList<OpaqueValue>(curr.size());
    List<T> ret = new ArrayList<T>();
    for(int i=0; i<curr.size(); i++) {
        CachedBatchReadsMap.RetVal<OpaqueValue> retval = curr.get(i);
        OpaqueValue<T> val = retval.val;
        ValueUpdater<T> updater = updaters.get(i);
        T prev;
        if(val==null) {
            prev = null;
        } else {
            if(retval.cached) {
                prev = val.getCurr();
            } else {
                prev = val.get(_currTx);
            }
        }
        T newVal = updater.update(prev);
        ret.add(newVal);
        OpaqueValue<T> newOpaqueVal;
        if(val==null) {
            newOpaqueVal = new OpaqueValue<T>(_currTx, newVal);
        } else {
            newOpaqueVal = val.update(_currTx, newVal);
        }
        newVals.add(newOpaqueVal);
    }
    _backing.multiPut(keys, newVals);
    return ret;
}
 
Example #9
Source File: ESIndexMapState.java    From storm-trident-elasticsearch with Apache License 2.0 4 votes vote down vote up
public static <T> Factory<OpaqueValue<T>> opaque(ClientFactory client, Class<T> type) {
    return new OpaqueFactory<>(client, StateType.OPAQUE, new OpaqueValueSerializer<>(type));
}
 
Example #10
Source File: ESIndexMapState.java    From storm-trident-elasticsearch with Apache License 2.0 4 votes vote down vote up
public OpaqueFactory(ClientFactory clientFactory, StateType stateType, ValueSerializer<OpaqueValue<T>> serializer) {
    super(clientFactory, stateType, serializer);
}
 
Example #11
Source File: CassandraCqlMapState.java    From storm-cassandra-cql with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
public static StateFactory opaque(CqlRowMapper mapper) {
    Options<OpaqueValue> options = new Options<OpaqueValue>();
    return opaque(mapper, options);
}
 
Example #12
Source File: CassandraCqlMapState.java    From storm-cassandra-cql with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
public static StateFactory opaque(CqlRowMapper mapper, Options<OpaqueValue> opts) {
    return new CassandraCqlMapStateFactory(mapper, StateType.OPAQUE, opts);
}
 
Example #13
Source File: OpaqueMap.java    From jstorm with Apache License 2.0 4 votes vote down vote up
public static <T> MapState<T> build(IBackingMap<OpaqueValue> backing) {
    return new OpaqueMap<T>(backing);
}
 
Example #14
Source File: OpaqueMap.java    From jstorm with Apache License 2.0 4 votes vote down vote up
protected OpaqueMap(IBackingMap<OpaqueValue> backing) {
    _backing = new CachedBatchReadsMap(backing);
}