storm.trident.state.TransactionalValue Java Examples

The following examples show how to use storm.trident.state.TransactionalValue. 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: 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<TransactionalValue<T>> mapState = new ESIndexMapState<>(clientFactory.makeClient(conf), serializer, new BulkResponseHandler.LoggerResponseHandler(), options.reportError());
    MapState<T> ms  = TransactionalMap.build(new CachedMap(mapState, options.getCachedMapSize()));
    Values snapshotKey = new Values(options.getGlobalKey());
    return new SnapshottableMap<>(ms, snapshotKey);
}
 
Example #2
Source File: ValueSerializerTest.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeTransactionValue( ) throws IOException {
    TransactionalValueSerializer<FooDocument> serializer = new TransactionalValueSerializer<>(FooDocument.class);
    byte[] value = serializer.serialize(new TransactionalValue<>(1L, new FooDocument("foo")));

    TransactionalValue<FooDocument> actual = serializer.deserialize(value);
    Assert.assertNotNull(actual);
    Assert.assertEquals(1L, (long)actual.getTxid());
    Assert.assertEquals("foo", actual.getVal().value);
}
 
Example #3
Source File: HazelCastState.java    From trident-tutorial with Apache License 2.0 5 votes vote down vote up
@Override
public void multiPut(List<List<Object>> keys, List<TransactionalValue<T>> vals) {
    for (int i = 0; i < keys.size(); i++) {
        String key = getKey(keys.get(i));
        T value = vals.get(i).getVal();
        addKeyValue(key, value);
    }
}
 
Example #4
Source File: HazelCastState.java    From trident-tutorial with Apache License 2.0 5 votes vote down vote up
public List<TransactionalValue<T>> multiGet(List<List<Object>> keys) {
    List<TransactionalValue<T>> result = new ArrayList<TransactionalValue<T>>(keys.size());
    for (int i = 0; i < keys.size(); i++) {
        String key = getKey(keys.get(i));
        result.add(new TransactionalValue<T>(0L, (T)(handler.getState().get(key))));
    }
    return result;
}
 
Example #5
Source File: TransactionalMap.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public List<T> multiGet(List<List<Object>> keys) {
    List<CachedBatchReadsMap.RetVal<TransactionalValue>> vals = _backing.multiGet(keys);
    List<T> ret = new ArrayList<T>(vals.size());
    for(CachedBatchReadsMap.RetVal<TransactionalValue> retval: vals) {
        TransactionalValue v = retval.val;
        if(v!=null) {
            ret.add((T) v.getVal());
        } else {
            ret.add(null);
        }
    }
    return ret;
}
 
Example #6
Source File: TransactionalMap.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<TransactionalValue>> curr = _backing.multiGet(keys);
    List<TransactionalValue> newVals = new ArrayList<TransactionalValue>(curr.size());
    List<List<Object>> newKeys = new ArrayList();
    List<T> ret = new ArrayList<T>();
    for(int i=0; i<curr.size(); i++) {
        CachedBatchReadsMap.RetVal<TransactionalValue> retval = curr.get(i);
        TransactionalValue<T> val = retval.val;
        ValueUpdater<T> updater = updaters.get(i);
        TransactionalValue<T> newVal;
        boolean changed = false;
        if(val==null) {
            newVal = new TransactionalValue<T>(_currTx, updater.update(null));
            changed = true;
        } else {
            if(_currTx!=null && _currTx.equals(val.getTxid()) && !retval.cached) {
                newVal = val;
            } else {
                newVal = new TransactionalValue<T>(_currTx, updater.update(val.getVal()));
                changed = true;
            }
        }
        ret.add(newVal.getVal());
        if(changed) {
            newVals.add(newVal);
            newKeys.add(keys.get(i));
        }
    }
    if(!newKeys.isEmpty()) {
        _backing.multiPut(newKeys, newVals);
    }
    return ret;
}
 
Example #7
Source File: TransactionalMap.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public void multiPut(List<List<Object>> keys, List<T> vals) {
    List<TransactionalValue> newVals = new ArrayList<TransactionalValue>(vals.size());
    for(T val: vals) {
        newVals.add(new TransactionalValue<T>(_currTx, val));
    }
    _backing.multiPut(keys, newVals);
}
 
Example #8
Source File: ESIndexMapState.java    From storm-trident-elasticsearch with Apache License 2.0 4 votes vote down vote up
public static <T> Factory<TransactionalValue<T>> transactional(ClientFactory client, Class<T> type) {
    return new TransactionalFactory<>(client, StateType.TRANSACTIONAL, new TransactionalValueSerializer<>(type));
}
 
Example #9
Source File: ESIndexMapState.java    From storm-trident-elasticsearch with Apache License 2.0 4 votes vote down vote up
public TransactionalFactory(ClientFactory clientFactory, StateType stateType, ValueSerializer<TransactionalValue<T>> serializer) {
    super(clientFactory, stateType, serializer);
}
 
Example #10
Source File: ValueSerializer.java    From storm-trident-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public TransactionalValue<T> deserialize(byte[] value) throws IOException {
    ObjectNode node = mapper.readValue(value, ObjectNode.class);
    byte[] bytes = mapper.writeValueAsBytes(node.get(FIELD_VAL));
    return new TransactionalValue<>(node.get(FIELD_TXID).asLong(), mapper.readValue(bytes, type));
}
 
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 transactional(CqlRowMapper mapper) {
    Options<TransactionalValue> options = new Options<TransactionalValue>();
    return transactional(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 transactional(CqlRowMapper mapper, Options<TransactionalValue> opts) {
    return new CassandraCqlMapStateFactory(mapper, StateType.TRANSACTIONAL, opts);
}
 
Example #13
Source File: HazelCastStateFactory.java    From trident-tutorial with Apache License 2.0 4 votes vote down vote up
@Override
public MapState<T> makeState(Map conf, IMetricsContext metrics, int partitionIndex, int numPartitions) {
    IBackingMap im = new HazelCastState<T>(new HazelCastHandler<TransactionalValue<T>>());
    return TransactionalMap.build(im);
}
 
Example #14
Source File: TransactionalMap.java    From jstorm with Apache License 2.0 4 votes vote down vote up
public static <T> MapState<T> build(IBackingMap<TransactionalValue> backing) {
    return new TransactionalMap<T>(backing);
}
 
Example #15
Source File: TransactionalMap.java    From jstorm with Apache License 2.0 4 votes vote down vote up
protected TransactionalMap(IBackingMap<TransactionalValue> backing) {
    _backing = new CachedBatchReadsMap(backing);
}