Closed rupamking1 closed 3 years ago
Multiple solutions exist for this. Depends on several factors: how do you want your keys to be sorted, should they be globally unique, are your writes random or sequential, do you delete keys and if so do you want to allow reuse, etc. This is all up to you.
Can you be more specific?
@vweevers I want globally unique & sequential, it may delete and reuse. Can you please send an example?
Using ulid
as keys may work for you. E.g.:
const ulid = require('ulid').monotonicFactory()
await db.put(ulid(), 'example')
ulid() will this package never genarate same id, every time will it genarate unique id?
Can i use uniqid/nanoid/uuid package for Maintaining Global Uniqueness?
ulid() will this package never genarate same id, every time will it genarate unique id?
It has 80 bits of randomness (see spec). Should be sufficiently unique, but that's your call.
Can i use uniqid/nanoid/uuid package for Maintaining Global Uniqueness?
The benefit of ulid
is that the generated ids sort lexicographically and increase monotonically. If you stream or iterate keys from your db, you get them back in insertion order.
Yes, I understood but this repository not upgrade till 2019, Can you please a recently upgraded repository?
leveldb performance is High in sequential key or in random key, which one make leveldb performance better?
Sequential, for sure.
@vweevers Please answer my questions
- Yes, I understood but this repository not upgrade till 2019, Can you please a recently upgraded repository?
- leveldb performance is High in sequential key or in random key, which one make leveldb performance better?
Your tone is too demanding - also in https://github.com/ulid/javascript/issues/88 - and I frankly don't feel like explaining why that is an issue. So I'm just gonna leave this thread until you realize it yourself. Good luck!
@vweevers Extremely sorry for my word, you are so helpful, Thank You for Helping me.
Thank you! To answer your first question:
Yes, I understood but this repository not upgrade till 2019, Can you please a recently upgraded repository?
ulid
is stable as far as I know, and dependency-free, so naturally it's light on maintenance. I don't know of other UUID generators that sort in the same way, but I'm sure they can be found.
For inspiration, you might want to look at flakeid
(written in Go but conceptually relevant), monotonic-lexicographic-timestamp
(only unique per-machine, not globally) and modules listed in awesome.
Lastly, you can of course write your own solution, which has the benefit that you can optimize it for your specific use case. For example, let's say you have a cluster of machines and you already have a known distinct name for each machine, then you could construct keys in the form of <timestamp>~<name>
or <sequence number>~<name>
.
if anyone save data generate automatically unique key, is that now possible with leveldb?