cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

check if stream entry exist while inserting for the 2nd time

aelgov
1-Newbie

check if stream entry exist while inserting for the 2nd time

Hi,

I'm adding stream entries using this snippet:

​var params = {

  sourceType: undefined /* STRING */,

  values: values /* INFOTABLE*/,

  location: undefined /* LOCATION */,

  source: undefined /* STRING */,

timestamp: myDate /* DATETIME */,

  tags: undefined /* TAGS */

};

me.AddStreamEntry(params);

If I try to add the same row twice:

me.AddStreamEntry(params);

me.AddStreamEntry(params);


It will not add the 2nd time - OK!


I just want to know if this row already exist, I tried to add try and catch but there is no exception thrown.

What is the BEST way to do that instead of checking with getStreamEntry as this will require to check for every entry that I try to add.


*My key is the timestamp + another column (String) - I don't use the ms part of the time stamp, so if the entry is not exist but still have the same timestamp I will add 1 millisecond to the timestamp and insert it to the stream.


Thanks in advance.

7 REPLIES 7

Hi Assaf,

If you are on Neo4j it will create two entries for the same timestamp, if you do it with any other Backend database storage it will upsert.

If you want to prevent a new entry to overwrite an existing one, you will have to check before inserting it, but take care as if you do it too fast maybe the first entry it's still not persisted ( as AddStreamEntry it's an Async operation ).

Best Regards,

Carles.

I'm using postgres and it added only 1 row

Then second write it's the one that will be persisted.

Not good.

I hoped that there is add AND addorupdate

Is there any smart way to do that or just to check before every insert?

check before, as I where until last week on Neo4J it had to check before insert almost always to prevent duplicates.

"as I where until last week"

What change did you make to stop doing this check?

I was on Neo4j, then to prevent duplicates I had to check always if the entry exists, now only when it's really needed.

Top Tags