dbTalk Databases Forums  

Berkeley DB Java Edition - Question on searching the data

comp.databases.berkeley-db comp.databases.berkeley-db


Discuss Berkeley DB Java Edition - Question on searching the data in the comp.databases.berkeley-db forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Narayanan A R
 
Posts: n/a

Default Berkeley DB Java Edition - Question on searching the data - 09-06-2006 , 07:24 AM






Hi,

I have a query in Java edition with respect to searching data stored in
the form of name-value pairs. To explain in detail, I have a class like
the one given below.

class Props {
private String key; // This will be key for the record
private Properties data;
}

Example data:
[Key:1, Data: [name=John, age=25, city=New York]]
[Key:2, Data: [desc=Google URL, url=www.google.com]]

I want to store this data and search based on the name value pairs. Is
there a way to achieve this using Berkeley DB Java edition.

Thanks for the help.


Reply With Quote
  #2  
Old   
Narayanan A R
 
Posts: n/a

Default Re: Berkeley DB Java Edition - Question on searching the data - 09-06-2006 , 08:47 AM






I guess I will have to traverse the entire database to find the
matching records. Please suggest if there is an efficient way.

Narayanan A R wrote:
Quote:
Hi,

I have a query in Java edition with respect to searching data stored in
the form of name-value pairs. To explain in detail, I have a class like
the one given below.

class Props {
private String key; // This will be key for the record
private Properties data;
}

Example data:
[Key:1, Data: [name=John, age=25, city=New York]]
[Key:2, Data: [desc=Google URL, url=www.google.com]]

I want to store this data and search based on the name value pairs. Is
there a way to achieve this using Berkeley DB Java edition.

Thanks for the help.


Reply With Quote
  #3  
Old   
mark.hayes@oracle.com
 
Posts: n/a

Default Re: Berkeley DB Java Edition - Question on searching the data - 09-07-2006 , 10:08 AM



Hello Narayanan,

To do lookups by key-value in your properties object, you can create a
secondary index. One way to do this is to create a secondary index for
each possible property name. This works well if the set of all
possible property names is fixed. A second way is to embed the
property name in the secondary key. This allows any property names to
be used -- there does not have to be a fixed set of names. I'll
describe this second approach below.

In this approach you create a single secondary index. The key of that
index, the secondary key, would be a two part value. For simplicity
I'll use a string with two parts separated by a colon as follows:

NAME:VALUE

So in your example the secondary keys (Strings) would look like this:

name:John
age:25
city:New York
desc:Google URL
url:www.google.com

If you are using the Base API, you would implement a
SecondaryMultiKeyCreator. It would enumerate your Properties object
and return a DatabaseEntry key for each property. The key returned
would be the NAME:VALUE as described above. You could use a
StringBinding (in com.sleepycat.bind.tuple) to translate secondary key
Strings to/from DatabaseEntry objects.

If you are using the DPL (Direct Persistence Layer), you would define a
ONE_TO_MANY key field that contains the NAME:VALUE strings. You could
use this field instead of your Properties object, and translate to/from
a Properties object when needed. For example:

@Entity
class Props {

@PrimaryKey
private String key; // This will be key for the record

@SecondaryKey(relate=ONE_TO_MANY)
private Set<String> data;

Properties getData() { /* translate data NAME:VALUE strings to
Properties */ }
void setData(Properties props) { /* translate properties to
NAME:VALUE strings */ }
}

To do a lookup by a property value, you supply the NAME:VALUE secondary
key to a get() method of the secondary index.

Using the Base API, call SecondaryDatabase.get or
SecondaryCursor.getSearchKey. You'll need to translate the NAME:VALUE
string to a DatabaseEntry using a StringBinding.

Using the DPL, call SecondaryIndex.get, passing the NAME:VALUE string.
For example:

SecondaryIndex<String,String,Props> index = ...;
String key = "name:John";
Props entity = index.get(key);

Mark


Reply With Quote
  #4  
Old   
Narayanan A R
 
Posts: n/a

Default Re: Berkeley DB Java Edition - Question on searching the data - 09-17-2006 , 04:34 AM



Hi,

Thanks for the detailed note. I think it will help.

A quick follow up question. What if want to search using wild-cards.
Like for example, names starting with 'Mark*'.

Will I need to iterate all the secondary keys defined for the
properties?

Thanks,
Narayanan A R

mark.hayes (AT) oracle (DOT) com wrote:
Quote:
Hello Narayanan,

To do lookups by key-value in your properties object, you can create a
secondary index. One way to do this is to create a secondary index for
each possible property name. This works well if the set of all
possible property names is fixed. A second way is to embed the
property name in the secondary key. This allows any property names to
be used -- there does not have to be a fixed set of names. I'll
describe this second approach below.

In this approach you create a single secondary index. The key of that
index, the secondary key, would be a two part value. For simplicity
I'll use a string with two parts separated by a colon as follows:

NAME:VALUE

So in your example the secondary keys (Strings) would look like this:

name:John
age:25
city:New York
desc:Google URL
url:www.google.com

If you are using the Base API, you would implement a
SecondaryMultiKeyCreator. It would enumerate your Properties object
and return a DatabaseEntry key for each property. The key returned
would be the NAME:VALUE as described above. You could use a
StringBinding (in com.sleepycat.bind.tuple) to translate secondary key
Strings to/from DatabaseEntry objects.

If you are using the DPL (Direct Persistence Layer), you would define a
ONE_TO_MANY key field that contains the NAME:VALUE strings. You could
use this field instead of your Properties object, and translate to/from
a Properties object when needed. For example:

@Entity
class Props {

@PrimaryKey
private String key; // This will be key for the record

@SecondaryKey(relate=ONE_TO_MANY)
private Set<String> data;

Properties getData() { /* translate data NAME:VALUE strings to
Properties */ }
void setData(Properties props) { /* translate properties to
NAME:VALUE strings */ }
}

To do a lookup by a property value, you supply the NAME:VALUE secondary
key to a get() method of the secondary index.

Using the Base API, call SecondaryDatabase.get or
SecondaryCursor.getSearchKey. You'll need to translate the NAME:VALUE
string to a DatabaseEntry using a StringBinding.

Using the DPL, call SecondaryIndex.get, passing the NAME:VALUE string.
For example:

SecondaryIndex<String,String,Props> index = ...;
String key = "name:John";
Props entity = index.get(key);

Mark


Reply With Quote
  #5  
Old   
mark.hayes@oracle.com
 
Posts: n/a

Default Re: Berkeley DB Java Edition - Question on searching the data - 09-18-2006 , 10:43 AM



Narayanan A R wrote:
Quote:
Hi,

Thanks for the detailed note. I think it will help.
You're welcome.

Quote:
A quick follow up question. What if want to search using wild-cards.
Like for example, names starting with 'Mark*'.

Will I need to iterate all the secondary keys defined for the
properties?
No, you don't have to iterate all properties. Berkeley DB can search
for keys that are greater or equal to a specified key -- this is called
a range search. For example, you could specify "name:Mark" and search
for keys greater or equal to it. Depending on which of our APIs you
use, a range search is done using one of the following in the

Base API: com.sleepycat.je.Cursor.getSearchKeyRange
Direct Persistence Layer:
com.sleepycat.persist.SecondaryIndex.entities(from Key, ...)
Collections API: com.sleepycat.collections.StoredMap.subMap, headMap,
tailMap

Mark



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.