I'm not quite satisfied with the available id generators in NHibernate.
Here's an idea for a new type of generator. It should be a fairly small
self-contained project that wouldn't require to much knowledge of
Nhibernate internals, if anyone wants to have a go at implementing it.

Summary of some existing generators:

HiLo
Problem is that all clients have to agree on maxLo, both in space and over
time (possibly complicated calculations to change maxLo). Complicated to do
manual inserts.

Identity, native, etc.
Easy to understand, with low requirements on agreements between clients,
but prevents batch inserts.

Pooled with table db structure
I believe clients still need to agree on the increment value.

Pooled_lo with table db structure
Each client can have their own idea of table size, which also means that
the pool size can change over time with no coordination between clients
required. Which also means that a manual insert can easily allocate
individual numbers.
Drawback is that using the table structure on a rdbms that supports
sequences feels awkward.

pooled and pooled_lo backed by a sequence
They are based on configuring the sequence itself to have a certain
increment size. So all clients need the same value (but enforced by DB).
Simple manual insert of individual rows will cause lots of skipped
identifiers, since the sequence will skip e.g. 99 values for each inserted
row.

guid
Very simple, but for many cases they are needlessly large (which also makes
them more difficult to  compare manually for DBAs).


The idea
Here's an idea for something that should be easy to manage among multiple
clients, generate short numbers and be easy to use when doing the INSERT
manually.

The idea I have in mind:
* Backed by a sequence with increment size = 1 (where available, otherwise
fall back to table).
* For ease of use, identifiers should be the actual values retrieved from
the sequence, i.e. nothing like hilo.
* Each client should be able use their own pool size.
* When allocating new identifiers, it should emit SQL that will call
nextval() the required number of times in the same roundtrip. The result
set will contain a list of allocated numbers (which might not be
consecutive). For some rdbms there might be functions available to do this
(e.g. sp_sequence_get_range on MSSQL2012). For other dialects it should be
possible to formulate an SQL query that will call "nextval" multiple times.
* For ease of use with manual INSERT, the table can be defined to just use
the sequence nextval as default value.

If sequences are not available, it should use a table backed structure -
and we should include a stored procedure to match nextval, that can be used
with e.g. default value constraints. I suppose this would be fairly close
to the existing pooled_lo implementation.

Probably this would be implemented as an optimizer plugin for the enhanced
id generators infrastructure.

Depending on the used rdbms' ability to efficiently allocate a range from a
sequence, this might not be the absolutely fastest generator - the benefit
would be in scenarios where ease of management matters more.

/Oskar

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"nhibernate-development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to