|
|
|
|
|
|
| Author |
Message |
Hendrik Rusch *nix forums beginner
Joined: 03 Feb 2005
Posts: 13
|
Posted: Thu Feb 17, 2005 12:36 pm Post subject:
Re: creating ids automatically
|
|
|
Hi Mark,
thanks for your help. The combination of Recno access method for the primary
and BTree for the secondary database works well.
Regards,
Hendrik
"Mark Hayes" <mark@sleepycat.com> schrieb im Newsbeitrag
news:4213D46B.9010004@sleepycat.com...
| Quote: | Hendrik Rusch wrote:
Hi Mark,
thanks for your help. I'm afraid I forgot to mention that I'd need to
have the IDs being used for lookup, that is given a certain ID I need to
retrieve the corresponding string value.
Thus I think your solution doesn't really fit my needs, unless I'd
iterate the database searching for the specified ID, which would in my
opinion be far away from a performant solution.
In that case a secondary index is needed to lookup strings by ID. See:
http://www.sleepycat.com/docs/gsg/CXX/indexes.html
http://www.sleepycat.com/docs/ref/am/second.html
http://www.sleepycat.com/docs/api_cxx/db_associate.html
The primary database would be a map of {String, ID} and the secondary
index would be a map of {ID, String}. The secondary index can be defined
using Db:associate with a callback that returns the entire data value of
the primary record. The data of the primary record (the ID) then becomes
the key of the secondary index.
You can lookup the String by ID using the secondary index database Db:pget
method, passing the ID as the key. pget returns the primary key as well
as the primary data. The primary key is the String in this case.
You could also implement this in reverse: The primary database could be a
map of {ID, String} and the secondary index could be a map of {String,
ID}. Which technique is more efficient depends on the data access
patterns in your application.
Yet another approach is to use a RECNO or QUEUE database to hold the {ID,
String} map and a BTREE secondary index to hold the {String, ID} map.
With this approach you don't need a sequence, since RECNO or QUEUE will
automatically assign unique record IDs for you using Db::put and the
DB_APPEND flag, as you mentioned.
QUEUE provides better concurrency than RECNO, which may or may not be
important to you. But QUEUE requires a fixed record length, so you will
need to decide on a a maximum length for your strings.
Mark |
|
|
| Back to top |
|
 |
Mark Hayes *nix forums beginner
Joined: 27 May 2005
Posts: 35
|
Posted: Wed Feb 16, 2005 10:16 pm Post subject:
Re: creating ids automatically
|
|
|
Hendrik Rusch wrote:
| Quote: | Hi Mark,
thanks for your help. I'm afraid I forgot to mention that I'd need to have
the IDs being used for lookup, that is given a certain ID I need to retrieve
the corresponding string value.
Thus I think your solution doesn't really fit my needs, unless I'd iterate
the database searching for the specified ID, which would in my opinion be
far away from a performant solution.
|
In that case a secondary index is needed to lookup strings by ID. See:
http://www.sleepycat.com/docs/gsg/CXX/indexes.html
http://www.sleepycat.com/docs/ref/am/second.html
http://www.sleepycat.com/docs/api_cxx/db_associate.html
The primary database would be a map of {String, ID} and the secondary
index would be a map of {ID, String}. The secondary index can be
defined using Db:associate with a callback that returns the entire data
value of the primary record. The data of the primary record (the ID)
then becomes the key of the secondary index.
You can lookup the String by ID using the secondary index database
Db:pget method, passing the ID as the key. pget returns the primary key
as well as the primary data. The primary key is the String in this case.
You could also implement this in reverse: The primary database could be
a map of {ID, String} and the secondary index could be a map of {String,
ID}. Which technique is more efficient depends on the data access
patterns in your application.
Yet another approach is to use a RECNO or QUEUE database to hold the
{ID, String} map and a BTREE secondary index to hold the {String, ID}
map. With this approach you don't need a sequence, since RECNO or QUEUE
will automatically assign unique record IDs for you using Db::put and
the DB_APPEND flag, as you mentioned.
QUEUE provides better concurrency than RECNO, which may or may not be
important to you. But QUEUE requires a fixed record length, so you will
need to decide on a a maximum length for your strings.
Mark |
|
| Back to top |
|
 |
Hendrik Rusch *nix forums beginner
Joined: 03 Feb 2005
Posts: 13
|
Posted: Wed Feb 16, 2005 9:48 pm Post subject:
Re: creating ids automatically
|
|
|
Hi Mark,
thanks for your help. I'm afraid I forgot to mention that I'd need to have
the IDs being used for lookup, that is given a certain ID I need to retrieve
the corresponding string value.
Thus I think your solution doesn't really fit my needs, unless I'd iterate
the database searching for the specified ID, which would in my opinion be
far away from a performant solution.
Regards,
Hendrik
"Mark Hayes" <mark@sleepycat.com> schrieb im Newsbeitrag
news:4213CBD2.2030706@sleepycat.com...
| Quote: | Hendrik Rusch wrote:
Hi,
I need a database for storing strings, showing the following behavior:
i) when putting a string to db, a unique id is to be assigned
automatically (some integer value, including overflow handling)
ii) strings already contained in the db must not be stored a second time.
I experimented with type DB_RECNO using the DB_APPEND flag in Db::put,
but this didn't meet my requirements.
What is the easiest way to achieve the requirements above?
I suggest using a BTREE database where the record key is the string and
the record data is the ID. The IDs can be assigned using a sequence:
http://sleepycat.com/docs/api_cxx/seq_list.html
Lookup the string in the BTREE database using Db::get. If it's found, the
data returned is the ID. If it's not found, assign a new ID using the
sequence and call Db::put to store the new string/ID pair.
Mark |
|
|
| Back to top |
|
 |
Mark Hayes *nix forums beginner
Joined: 27 May 2005
Posts: 35
|
Posted: Wed Feb 16, 2005 9:40 pm Post subject:
Re: creating ids automatically
|
|
|
Hendrik Rusch wrote:
| Quote: | Hi,
I need a database for storing strings, showing the following behavior:
i) when putting a string to db, a unique id is to be assigned automatically
(some integer value, including overflow handling)
ii) strings already contained in the db must not be stored a second time.
I experimented with type DB_RECNO using the DB_APPEND flag in Db::put, but
this didn't meet my requirements.
What is the easiest way to achieve the requirements above?
|
I suggest using a BTREE database where the record key is the string and
the record data is the ID. The IDs can be assigned using a sequence:
http://sleepycat.com/docs/api_cxx/seq_list.html
Lookup the string in the BTREE database using Db::get. If it's found,
the data returned is the ID. If it's not found, assign a new ID using
the sequence and call Db::put to store the new string/ID pair.
Mark |
|
| Back to top |
|
 |
Hendrik Rusch *nix forums beginner
Joined: 03 Feb 2005
Posts: 13
|
Posted: Wed Feb 16, 2005 9:16 pm Post subject:
creating ids automatically
|
|
|
Hi,
I need a database for storing strings, showing the following behavior:
i) when putting a string to db, a unique id is to be assigned automatically
(some integer value, including overflow handling)
ii) strings already contained in the db must not be stored a second time.
I experimented with type DB_RECNO using the DB_APPEND flag in Db::put, but
this didn't meet my requirements.
What is the easiest way to achieve the requirements above?
Regards,
Hendrik |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Jan 08, 2009 4:40 am | All times are GMT
|
|
Web Advertising | Actress | Credit Cards | Loans | France Hotels
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|