user3448187
user3448187

Reputation:

IndexedDB: Indexing object values?

I have objects with keywords nested in various depths, which I want to store in an index. The keywords are formatted as so:

{
    "list": [
        {
            "keywords": ["hi", "bye"],
            "rules": "contains 1 of the following"
        },
        {
            "keywords": ["foo", "bar"],
            "rules": "contains all of the following"
        }
    ]
}

I want to search all the keywords at once, then loop back through the "list" with the matches to find similarities. I was wondering whether it was best store all the keywords separately in another table or loop through each of the list and search the keywords with a new regex each time, bare in mind there are thousands of these lists.

Upvotes: 3

Views: 1395

Answers (1)

Josh
Josh

Reputation: 18730

  • indexedDB only supports whole value comparisons: (string1 == string2, string1 >= string2, string1 <= string2). You cannot search for strings using a regex unless you plan to load all of the list/lists of words into memory in an array and loop over the list.
  • you can store values in an array as a property for each object you are storing, and then create a multi-entry index on this property. Using the multi-entry flag has the effect of taking the array and creating a row in the index for each value of the array. For example, if you have the object {prop1:value1,prop2:[value2,value3]}, and you create an index on prop2, and you use the multi-entry flag to the createIndex API method, then two rows would appear in the index, one for value2 and one for value3. You could then open a cursor on this index and iterate over its rows, first seeing value2 and then seeing value3 (depending on order). This would be the source of the array you build and load into memory and then separately, in memory, loop over using your own regex test condition.
  • alternatively you can store the words as separate objects in a single store. Each object would look like {prop:word}. You could then open a cursor on this store and iterate over all the words to build the same in memory array over which you then iterate and test with your regex.
  • looping over the values in memory and doing a test regex is going to be slow. really slow. and it will just get slower when you have more keywords. but you should maybe try to use it first, as maybe it is fast enough for your needs.
  • if you really want to speed this up, you have to learn about several advanced concepts. for example, if you would be open to imposing constraints on the types of queries (regexes) that you are able to apply to the table, you could consider using an advanced data structure like a trie (or check out this article by John Resig, jQuery's creator). using a trie has nothing to do with "how to" use indexedDB. It is something you would create yourself on top of indexedDB. But if you did it you could probably query the trie data structure extremely quickly.

Upvotes: 3

Related Questions