Search in sources :

Example 1 with PatchingRecord

use of org.nustaq.reallive.records.PatchingRecord in project kontraktor by RuedigerMoeller.

the class StorageDriver method atomicQuery.

/**
 * apply the function to the record with given key and return the result inside a promise
 *
 * changes to the record inside the function are applied to the real record and a change message
 * is generated.
 *
 * In case the function returns a changemessage (add,putRecord,remove ..), the change message is applied
 * to the original record and the change is broadcasted
 *
 * @param key
 * @param action
 * @return the result of function.
 */
public IPromise atomicQuery(String key, RLFunction<Record, Object> action) {
    Record rec = getStore().get(key);
    if (rec == null) {
        final Object apply = action.apply(rec);
        if (apply instanceof ChangeMessage) {
            receive((ChangeMessage) apply);
        }
        return new Promise(apply);
    } else {
        PatchingRecord pr = new PatchingRecord(rec);
        final Object res = action.apply(pr);
        if (res instanceof ChangeMessage) {
            receive((ChangeMessage) res);
        } else {
            UpdateMessage updates = pr.getUpdates();
            if (updates != null) {
                receive(updates);
            }
        }
        return new Promise(res);
    }
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) PatchingRecord(org.nustaq.reallive.records.PatchingRecord) PatchingRecord(org.nustaq.reallive.records.PatchingRecord)

Example 2 with PatchingRecord

use of org.nustaq.reallive.records.PatchingRecord in project kontraktor by RuedigerMoeller.

the class StorageDriver method atomicUpdate.

public void atomicUpdate(RLPredicate<Record> filter, RLFunction<Record, Boolean> action) {
    store.forEach(filter, (r, e) -> {
        if (r != null) {
            PatchingRecord pr = new PatchingRecord(r);
            Boolean res = action.apply(pr);
            if (res == Boolean.FALSE) {
                receive(RLUtil.get().remove(pr.getKey()));
            } else {
                UpdateMessage updates = pr.getUpdates();
                if (updates != null) {
                    receive(updates);
                }
            }
        }
    });
}
Also used : PatchingRecord(org.nustaq.reallive.records.PatchingRecord)

Example 3 with PatchingRecord

use of org.nustaq.reallive.records.PatchingRecord in project kontraktor by RuedigerMoeller.

the class FilterSpore method remote.

@Override
public void remote(Record input) {
    if (modifiesResult) {
        final PatchingRecord patchingRecord = rec.get();
        patchingRecord.reset(input);
        if (filter.test(patchingRecord)) {
            stream(patchingRecord.unwrapOrCopy());
        }
    } else {
        if (filter.test(input)) {
            stream(input);
        }
    }
}
Also used : PatchingRecord(org.nustaq.reallive.records.PatchingRecord)

Aggregations

PatchingRecord (org.nustaq.reallive.records.PatchingRecord)3 IPromise (org.nustaq.kontraktor.IPromise)1 Promise (org.nustaq.kontraktor.Promise)1