Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Advanced-java] Fast Persistence of Unique Numbers


  • From: tug@xxxxxxxxxxxx (John Wilson)
  • Subject: [Advanced-java] Fast Persistence of Unique Numbers
  • Date: Sat, 19 Jan 2002 14:26:08 -0000

----- Original Message -----
From: "Jeff Singer" <jeff.singer@xxxxxxxxxx>
To: <advanced-java@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Friday, January 18, 2002 2:10 PM
Subject: [Advanced-java] Fast Persistence of Unique Numbers


> Hi All
>
> Sorry if this has been asked before.
>
> I need to generate a unique 9 or 10 digit number. Any ideas. I do realize
> the numbers will rollover after a certain amount of time.
>
> The easy solution for us is to start a counter at 1 and increment. I am
just
> worried about persistence, if the process is terminated.
>
> Our limitation is speed. It is possible to use a database to do this quite
> easily, but I need a fast method of doing this without using a database.

Renaming a file is a reasonably atomic way of recording the last number
used. The file would be the only one in directory.

I would have a dedicated thread which renames the file asynchronously.

The scheme works as follows:

At start-up you read the file name from the directory and rename it to
number one greater (i.e. file "123" is renamed to "124")

You start allocating numbers starting with 123000, 123001, 123002, etc. and
wake up the thread which will rename the file from "124" to "125".

When you allocate number 123999 you check to see if the thread has finished
renaming the file to "125" if it has you continue allocating numbers, if not
you wait for the rename to complete.

If the program falls over and restarts then it will never re-use numbers
(unless the number rolls over) though there might be a gap in the numbers
allocated.

1,000 may not be the best block size. You need to choose a block size which
makes it very unlikely that you will ever have to wait for the rename to
complete and that's a trial and error thing.

John Wilson
The Wilson Partnership
http://www.wilson.co.uk