Coder Social home page Coder Social logo

Comments (5)

RobThree avatar RobThree commented on August 18, 2024 1

The number is always a 64 bit number. Binary 00000...00001 (64 bits) can be represented in 1 decimal digit. However, the "maximum value" of 01111.....111111 (64 bits) (The first bit is never used) simply requires 20 decimal digits to represent.

If you need less digits then you can do some bitwise arithmetic to get rid of some bits of, for example, the timestamp part. You can configure IdGen to use more or less bits for any given part (timestamp/generator/sequence) and then decide which bits you want to get rid of and use some bit-shifting and/or masking to manipulate the ID to reduce the number of bits. However, you risk collisions if you're not very careful and know what you're doing. If you're going down this route then it will likely be more efficient to build a, say, 32 bit implementation that does the same thing, just in less bits.

Alternatively, you could consider using a hexadecimal representation of an ID (which would require 16 'digits' to represent the max. value) or even Base36 (which would require 13 'digits') or base 64 (which would require 11 digits).

You could even consider using something like HumanoID to encode your ID in a few easy to remember words - given a large enough dictionary. You won't easily get it to less than 12 characters, but it will be easier to type and/or remember. I'm not aware of a .Net implementation, but there's bound to be one or something similar. If not, the PHP code is easily readable and portable to .Net I think.

But, quite honestly, if you worry about the number of digits it takes to represent an ID then most likely IdGen isn't for you. It's not designed to be compact or 'human friendly'. It's designed to be low-latency, distributed, uncoordinated, (roughly) time ordered, compact and highly available.

from idgen.

RobThree avatar RobThree commented on August 18, 2024 1

Care to share why the requirement of 12 digits and/or what solution you've decided on?

from idgen.

RobThree avatar RobThree commented on August 18, 2024 1

@JohnnySanabria Please see the FAQ. Just treat the ID's as strings instead of numbers. Other than that, what I said here still stands:

Alternatively, you could consider using a hexadecimal representation of an ID (which would require 16 'digits' to represent the max. value) or even Base36 (which would require 13 'digits') or base 64 (which would require 11 'digits').

from idgen.

JohnnySanabria avatar JohnnySanabria commented on August 18, 2024

That's very informative. Thanks for the explanation.

from idgen.

JohnnySanabria avatar JohnnySanabria commented on August 18, 2024

I am developing an api with a UI that is utilizing these ids to do various operations. For example, create a duplicate of the record with a given id and change a few fields within. Unfortunately, JavaScript is incorrectly displaying the id since the id is larger than the max integer size in Javascript which then causes an invalid reference of a given id. At least, that's my understanding.

Here's an example of the IDs as shown in DB vs UI:

UI: 1097602576425173000
DB: 1097602576425172992

I don't necessarily need it to be 12 digits, I just need it to be a little shorter so that JavaScript doesn't mess up the data.
Also, the id doesn't need to be readable and human friendly which is why I thought your IdGen would work fine.

P.S. I'm not very confident in my ability to use bitwise operations.

from idgen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.