0

Is it possible to compare the function of an existing index with an ordinary native function in JavaScript?

For instance, I might create an index with the following code:

r.table('Table').indexCreate('index', document => document.hasFields('field'));

I might then, later, wish to determine whether the index has the same function:

document => document.hasFields('field')

Using indexStatus(), two properties may be able to help with this.

First, function is a Buffer representing the function, and can be compared with Buffers obtained from other indexStatus() objects. However, it's not clear how this could be compared with a native JavaScript function. A new index could be created, and then its Buffer compared with the Buffer of the existing index, but this would be a messy and performance-impacting workaround.

Second, query is a string containing a function resembling that which was provided to indexCreate(). However, this property seems to be undocumented, and the value is not always exactly the same as the function provided to indexCreate(), with changes to variable names and the transformation of arrow functions to function expressions. I've written some rough code which tries to work with this approach, although it's imperfect, given the opaque nature of the rules by which the query value is generated by RethinkDB.

Chris Talman
  • 1,059
  • 2
  • 14
  • 24
  • why don't you set a unique name for that index, like "index_field_present"? That way you could just call table.indexList() and instantly see whether it's there. – taygetos Jan 10 '19 at 14:21
  • Hey @taygetos. The context is that I've written a module which guarantees that databases, tables, and indexes are created, according to a config file. The name of each index is specified by the user in the config file. The user can also change the function at any time, without changing the name. It's for this reason that the module needs to work out whether the index function is, in a sense, up-to-date. If it is, the index can be left as it is, but otherwise the index must be recreated. And, obviously, you don't want to recreate the index if the function is unchanged. – Chris Talman Jan 10 '19 at 16:01

0 Answers0