fergiemcdowall / search-index

A persistent, network resilient, full text search library for the browser and Node.js
MIT License
1.39k stars 149 forks source link

Fix auto-generated IDs not always being unique. #530

Closed ggreer closed 3 years ago

ggreer commented 3 years ago

Due to timer coalescing and other optimizations in OSes and JavaScript engines, Date.now() can return the same value over several milliseconds. This means that if you call PUT() more than 100 times a second or so, you're likely to generate the same ID and accidentally overwrite recently-inserted data.

This change fixes the issue. Overwriting is still possible, but you'd have to close the DB, change the system's clock to an earlier time, then reopen the DB and start inserting data.

fergiemcdowall commented 3 years ago

@ggreer not sure how good github notifications are, so tagging you just in case :)

ggreer commented 3 years ago

I wasn't sure if the structure of that ID was important, so I kept the format number-dash-number. Do you want me to change this PR or do you want to make the change to master yourself?

fergiemcdowall commented 3 years ago

I see why you thought that, but the structure of the autogenerated _id field is not actually that important.

Its good to have as many contributors as possible, so if you have the time to update this PR then that would be the best for me. I also think that since you uncovered this issue that you should get credit for rectifying it.

ggreer commented 3 years ago

Thanks a lot. Could I trouble you to tag a new release? I'm using this library for some work stuff. It'd be nice if I could point my package.json to an official version instead of a sha.

fergiemcdowall commented 3 years ago

Sure- the v2.2.1 release containing your PR is here

I have also published search-index@2.2.1 to npm.