![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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. |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
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? |
![]() |
| Thread Tools | |
| Display Modes | |
| |