dain / leveldb

Port of LevelDB to Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creating a seconday Index?

hhimanshu opened this issue · comments

Hey @dain
I have a usecase where I wanted a BiDirectional Map, where I can search on both key and value (they both are unique). Currently, I store them twice in 2 different databases

String key = // generated from somewhere
String hashedKey = // encrypt key and save

mapDb.put(key, hashedKey)
decryptMapDb(hashKey, key)

Our key and hashedKey both are unique

I was reading on leveldb google groups, where someone recommended to create secondary index. The link is here

What you have is a kind of secondary index, so you may be all set. In the linked discussion, they are referring to a design where you would store both directions in the same table, using a prefix to distinguish between the two. You might have something like this:

prefixes: a2b, b2a

key structure:
a2b:keyForDirectionA:KeyForDirectionB

with a value of new byte[0].

The advantage to putting both 'keys' in the the table key is that you can now support indexes that are not unique. Of course, you have to parse out the values you are interested in.

@lwhite1 that is exactly what I was going to suggest.

@lwhite1, @dain , my apologies but I am confused. Let us say my two indexes are

keyToHashKey and hashKeyToKey

How am I storing them?

keyToHashKey:key:hashedKey
hashKeyToKey:hashedKey:key

Is that what you were saying?
If yes, then I am not saving on space at all, right? so what I do currently is better, isn't it?

As I said, what you're doing is fine. This approach is not intended to save space, but to enable non-unique secondary indexes, which you don't need.