niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Berkeley DB
creating ids automatically
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
Author Message
Hendrik Rusch
*nix forums beginner


Joined: 03 Feb 2005
Posts: 13

PostPosted: Thu Feb 17, 2005 12:36 pm    Post subject: Re: creating ids automatically Reply with quote

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

PostPosted: Wed Feb 16, 2005 10:16 pm    Post subject: Re: creating ids automatically Reply with quote

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

PostPosted: Wed Feb 16, 2005 9:48 pm    Post subject: Re: creating ids automatically Reply with 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.

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

PostPosted: Wed Feb 16, 2005 9:40 pm    Post subject: Re: creating ids automatically Reply with quote

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

PostPosted: Wed Feb 16, 2005 9:16 pm    Post subject: creating ids automatically Reply with 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?

Regards,
Hendrik
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 4:40 am | All times are GMT
navigation Forum index » Databases » Berkeley DB
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Creating relational view for an ODBC result set? antilog@gmail.com Server 0 Fri Jul 21, 2006 5:56 am
No new posts Exim4 not creating and/or receiving incoming mail... jtelep@localonline.net Exim 1 Fri Jul 21, 2006 1:59 am
No new posts idea/ concept behind creating breadcrumbs crescent_au@yahoo.com PHP 3 Fri Jul 21, 2006 12:10 am
No new posts Creating a relationship between 2 tables Andyza Oracle 2 Thu Jul 20, 2006 1:11 pm
No new posts creating another database on the same box ? newhorizon Server 3 Thu Jul 20, 2006 9:18 am

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
[ Time: 0.1289s ][ Queries: 20 (0.0378s) ][ GZIP on - Debug on ]