You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by Chris Berry <ch...@gmail.com> on 2017/02/28 03:14:36 UTC

upsert example??

Hi,

I am attempting to convert an existing application to use Apache Ignite. 

The current logic does an “upsert”  from a stream of data. And it will be very difficult to modify the existing code.

Something like this: 

public void upsertEntry(TKey key, NewInstanceFunctor<TValue> newInstFunc, UpsertFunctor<TValue> upsertFunc) {
        TValue document = getAndNewIfReqd(key, newInstFunc);
        document = upsertFunc.modifyForUpsert(document);
        getIgniteCache().put(key, document);
}

public TValue getAndNewIfReqd(TKey key, NewInstanceFunctor<TValue> newInstFunc) {
        TValue document = getEntry(key);
        if (document == null) {
            document = newInstFunc.newInstance();
            getIgniteCache().put(key, document);
        }
        return document;
}

So, in pseudo-code

1) get the document from the cache
2) if null, create a document and put it to the cache
3) modify the document
4) put the updated document to the cache
This is clearly inefficient.

I am pretty certain that I can accomplish this using a DataStreamer and a StreamTransformer??
I wonder if someone could point me at an example of how to do this??

Thanks,
— Chris 




 

 

Re: upsert example??

Posted by Andrey Mashenkov <an...@gmail.com>.
Hi Cris,

Please, take a look at EntryProcessor.
It has atomicity guarantee, but be ware it can be called more than once for
each entry.

On Tue, Feb 28, 2017 at 6:14 AM, Chris Berry <ch...@gmail.com> wrote:

> Hi,
>
> I am attempting to convert an existing application to use Apache Ignite.
>
> The current logic does an “upsert”  from a stream of data. And it will be
> very difficult to modify the existing code.
>
> Something like this:
>
> *public void *upsertEntry(TKey key, NewInstanceFunctor<TValue>
> newInstFunc, UpsertFunctor<TValue> upsertFunc) {
>         TValue document = getAndNewIfReqd(key, newInstFunc);
>         document = upsertFunc.modifyForUpsert(document);
>         getIgniteCache().put(key, document);
> }
>
> public TValue getAndNewIfReqd(TKey key, NewInstanceFunctor<TValue>
> newInstFunc) {
>         TValue document = getEntry(key);
>         if (document == null) {
>             document = newInstFunc.newInstance();
>             getIgniteCache().put(key, document);
>         }
>         return document;
> }
>
> So, in pseudo-code
> 1) get the document from the cache
> 2) if null, create a document and put it to the cache
> 3) modify the document
> 4) put the updated document to the cache
>
> This is clearly inefficient.
> I am pretty certain that I can accomplish this using a DataStreamer and a
> StreamTransformer??
> I wonder if someone could point me at an example of how to do this??
>
> Thanks,
> — Chris
>
>
>
>
>
>
>



-- 
Best regards,
Andrey V. Mashenkov