Hashing strings in a View


#1

Hello,

I’m just beginning to learn Couchbase, so if my question seems like I’m fundamentally misunderstanding something, please correct me!

I have a set of documents containing lines of text. Each line is four or five words long.

I want to create a view where the ID of each result is a hash of a line and who’s data contains the text of the line, as well as the hash of the line before and after. Something like this:

Key: 742929dcb631403d7c1c1efad2ca2700,
Value: {
Text: ‘chicken’,
PrevKey: d9f4b96fcf222c8fd607c101756b2303,
NextKey: c2e055acd7ea39b9762acfa672a74136
}

I have a few questions about this:

  • First, I’m no stranger to Javascript, but I’m not sure the right way to make a hash of a string. Should I write a function in the body of the Map, or is there a way to import a hashing library?
  • Second, Lets say the same line of text occurs multiple times. Can I have multiple results in a view with the same ID? Is that antipattern?

Thanks very much!

-Dave


#2

Hi There,

  • There is no way to import libraries.
  • you can certainly email multiple values and keys in the body, that is a common pattern.
    thanks
    -cihan

#3

You can add the function in the body of the map. That’s the best way to do things at the moment and it works quite well. I once saw someone bring a javascript PHP serializer in. Of course, you’re better off if you can a hash function that’s in the platform in many cases. I don’t know what v8’s javascript has out of the box of the top of my head.

You can have multiple results in a view with the same document key/ID. The view results will be ordered by key (unicode collated), value (unicode collated), document key/id (value comparison, effectively memcmp()).


#4

Thank you for the responses!

ingenthr: Does Couchbase use V8 as it’s JS interpreter? I’ve been trying to find some documentation describing the javascript programming environment inside the Couchbase View system.


#5

Yes, it’s v8. The best documentation on the programming model is in the Views Section of the admin guide.

If you find anything missing or confusing, please post an issue. There is a “docs” component. Thanks!