Support for rdbms like sequences [moved]
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
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.
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.
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.
A question. Why Long? why not BigDecimal?
Why BigDecimal? Usually a sequence is an integer number.
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)
So you mean BigInteger?
Yeah :)
However Long is 2^263-1 that is a pretty huge number!
This feature planned to next release?
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
2^63 is correct.
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
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 .
This feature planned to the 1.4 release?
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
How can I get the sequence from java?
update counter incr value = 1 where name = 'mycounter'
Will return with the new value?
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");
}
Thanks...
We could create a new Java API like:
long counter = db.getMetadata().getSequenceManager().
getSequence("mycounter").increment();
Just an idea.