Level / level

Universal abstract-level database for Node.js and browsers.
MIT License
1.55k stars 106 forks source link

Auto Generated Unique Key #199

Closed rupamking1 closed 3 years ago

rupamking1 commented 3 years ago

if anyone save data generate automatically unique key, is that now possible with leveldb?

vweevers commented 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?

rupamking1 commented 3 years ago

@vweevers I want globally unique & sequential, it may delete and reuse. Can you please send an example?

vweevers commented 3 years ago

Using ulid as keys may work for you. E.g.:

const ulid = require('ulid').monotonicFactory()

await db.put(ulid(), 'example')
rupamking1 commented 3 years ago

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?

vweevers commented 3 years ago

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.

rupamking1 commented 3 years ago
  1. Yes, I understood but this repository not upgrade till 2019, Can you please a recently upgraded repository?

  2. leveldb performance is High in sequential key or in random key, which one make leveldb performance better?

vweevers commented 3 years ago

Sequential, for sure.

rupamking1 commented 3 years ago

@vweevers Please answer my questions

  1. Yes, I understood but this repository not upgrade till 2019, Can you please a recently upgraded repository?
  2. leveldb performance is High in sequential key or in random key, which one make leveldb performance better?
vweevers commented 3 years ago

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!

rupamking1 commented 3 years ago

@vweevers Extremely sorry for my word, you are so helpful, Thank You for Helping me.

vweevers commented 3 years ago

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>.