Comments (5)
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.
Care to share why the requirement of 12 digits and/or what solution you've decided on?
from idgen.
@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 require13
'digits') or base 64 (which would require11
'digits').
from idgen.
That's very informative. Thanks for the explanation.
from idgen.
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)
- Drop support for netstandard 1.1 HOT 5
- Non-generic IIdGenerator HOT 5
- Avoid spending a sign bit by using ulongs HOT 13
- Tag 3.0.3 is missing HOT 1
- Documentation about generatorId collisions when id structure changes HOT 9
- IdGen is generating 17bit Id instead of 16bit? What should we change in the method so that it generate 16 bit Id instead of 17 bit now and in the future it continuous to generate 16 bit Id? HOT 11
- Handling uniqueness ID across multiple instance of same service HOT 6
- IdGenerator does not contain a definition for 'Take' HOT 1
- Consider adding IdGenerator#CreateFromDateTime HOT 5
- Public Method Comments HOT 1
- SpinWait drastically slows continous ID generation rate for small tick length HOT 10
- Could we log how often we overflow sequence IDs and have to spinwait? HOT 1
- Concurrency issues with instances scale up HOT 2
- Use dependabot to keep references up to date HOT 1
- Sequence input for CreateId() HOT 3
- IIdGenerator is registered as a singleton, but duplicate IDs are still generated HOT 4
- Debugging InvalidSystemClockExceptions HOT 15
- Is there a way to generate id from the past HOT 1
- How to use this in distributed computing ? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from idgen.