Skip to main content
1-Visitor
March 13, 2017
Question

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

  • March 13, 2017
  • 1 reply
  • 4455 views

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.

1 reply

1-Visitor
March 13, 2017

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.

aelgov1-VisitorAuthor
1-Visitor
March 13, 2017

I'm using postgres and it added only 1 row

1-Visitor
March 13, 2017

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