orientdb icon indicating copy to clipboard operation
orientdb copied to clipboard

Support for rdbms like sequences [moved]

Open lvca opened this issue 13 years ago • 37 comments

This is Issue 367 moved from a Google Code project. Added by 2011-05-20T05:44:14.000Z by [email protected]. Please review that bug for more context and additional comments, but update this bug.

Original labels: Type-Enhancement, Priority-Low, v2.1.0

Original description

Our current use case wants to use a sequence to retrieve a unique number for every new transaction.

Thanks
Pieter

lvca avatar Dec 10 '12 15:12 lvca

How to implement it? We've so many options, this is just one:

  • add the support for sequence in Metadata, like db.getMetadata().getSequenceManager() -> OSequenceManager
  • OSequenceManager could have getSequence(name), dropSequence(name) that manages OSequence objects
  • OSequence wil have:
    • a long number that keeps the value
    • next() : long,
    • current() : long,
    • reset()
  • We could create an ad-hoc cluster "osequence" to save all the instances there
  • OSequenceManager could cache the value to stay in memory and flush to disk at every next(), but the strategy could change per-sequence
  • For remote protocol this should be managed at server-side only like indexes, so 2 clients that call next() do a rpc to the server
  • In next release we could pre-assign a block of numbers to each client to minimize network calls.

lvca avatar Dec 21 '12 11:12 lvca

Hi Luca, pay attention with the pre-assign blocks and with the in-memory-then-flush strategies. They could be cause of creating holes in the "Sequence" sequence and in some case even break the sequence order. I know it sounds crazy, but it could happen.

I had a similar experience with the Oracle sequences.

giastfader avatar Dec 21 '12 16:12 giastfader

Yeah, probably we could define a sort of autoincrement annotation at document level, so this is made by the server once the record is on it, avoiding a RPC call.

lvca avatar Dec 21 '12 16:12 lvca

A question. Why Long? why not BigDecimal?

b0c1 avatar May 08 '13 22:05 b0c1

Why BigDecimal? Usually a sequence is an integer number.

lvca avatar May 09 '13 13:05 lvca

Usually sequence is long, that's good most of the time, but some times we using BigDecimal (long running database with very small but a lot of data)

b0c1 avatar May 09 '13 13:05 b0c1

So you mean BigInteger?

lvca avatar May 09 '13 13:05 lvca

Yeah :)

b0c1 avatar May 09 '13 13:05 b0c1

However Long is 2^263-1 that is a pretty huge number!

lvca avatar May 09 '13 13:05 lvca

This feature planned to next release?

b0c1 avatar May 09 '13 13:05 b0c1

2^263 - 1? Is that correct Luca? I went back to the documentation and it says Long is +/- 2^31 (which I also feel could be incorrect, as that's the same range as Integer; should it be +/- 2^63 ?).

Mario

On Thu, May 9, 2013 at 9:27 AM, Luca Garulli [email protected]:

However Long is 2^263-1 that is a pretty huge number!

— Reply to this email directly or view it on GitHubhttps://github.com/nuvolabase/orientdb/issues/367#issuecomment-17663998 .


Mario Cormier

w3aponx avatar May 09 '13 14:05 w3aponx

2^63 is correct.

lvca avatar May 09 '13 14:05 lvca

Do you want me to correct https://github.com/nuvolabase/orientdb/wiki/Types? We are talking about the same Long right?

Mario

On Thu, May 9, 2013 at 10:57 AM, Luca Garulli [email protected]:

2^63 is correct.

— Reply to this email directly or view it on GitHubhttps://github.com/nuvolabase/orientdb/issues/367#issuecomment-17668747 .


Mario Cormier

w3aponx avatar May 09 '13 15:05 w3aponx

2^63 sorry.

Lvc@

On 9 May 2013 17:02, w3aponx [email protected] wrote:

Do you want me to correct https://github.com/nuvolabase/orientdb/wiki/Types? We are talking about the same Long right?

Mario

On Thu, May 9, 2013 at 10:57 AM, Luca Garulli [email protected]:

2^63 is correct.

— Reply to this email directly or view it on GitHub< https://github.com/nuvolabase/orientdb/issues/367#issuecomment-17668747> .


Mario Cormier

— Reply to this email directly or view it on GitHubhttps://github.com/nuvolabase/orientdb/issues/367#issuecomment-17669084 .

lvca avatar May 09 '13 15:05 lvca

This feature planned to the 1.4 release?

b0c1 avatar May 12 '13 23:05 b0c1

We can have something similar now by managing your own counter in this way (example using SQL):

create class counter
insert into counter set name='mycounter', value=0

And then every time you need a new number you can do:

update counter incr value = 1 where name = 'mycounter'

This works in a SQL batch in this way:

begin
let $counter = update counter incr value = 1 where name = 'mycounter' return after
insert into items set id = $counter.value, qty = 10, price = 1000
commit

lvca avatar Jul 18 '14 09:07 lvca

How can I get the sequence from java?

update counter incr value = 1 where name = 'mycounter' 

Will return with the new value?

b0c1 avatar Jul 18 '14 11:07 b0c1

If you work with plocal you can create a singleton instance that takes care about updating the value and storing the document, all inside a synchronized() block.

With remote access, you could have concurrent updates, so it's better to do:

public int incrementCounter( String name );
  ODocument lastDocument = db.command(new OCommandSQL("update counter incr value = 1   where name = '"+name+"' return after") ).execute();
  return (Integer) lastDocument.field("value");
}

lvca avatar Jul 18 '14 11:07 lvca

Thanks...

b0c1 avatar Jul 18 '14 11:07 b0c1

We could create a new Java API like:

long counter = db.getMetadata().getSequenceManager().
     getSequence("mycounter").increment();

Just an idea.

lvca avatar Jul 18 '14 11:07 lvca