Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jur0/eid
Erlang unique id generator based on UNIX timestamp.
https://github.com/jur0/eid
erlang id-generator unique-id unix-timestamp
Last synced: about 1 month ago
JSON representation
Erlang unique id generator based on UNIX timestamp.
- Host: GitHub
- URL: https://github.com/jur0/eid
- Owner: jur0
- License: isc
- Created: 2013-05-25T12:21:30.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2019-06-25T11:21:44.000Z (over 5 years ago)
- Last Synced: 2024-12-26T12:35:35.421Z (about 1 month ago)
- Topics: erlang, id-generator, unique-id, unix-timestamp
- Language: Erlang
- Size: 82 KB
- Stars: 14
- Watchers: 3
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# eid
Erlang unique ID generator.
## ID types
An ID is composed of the UNIX timestamp in milliseconds (upper 48 bits) and a
sequence number (lower 16 bits). The sequence number is used when two UNIX
timestamp are equal. This happens when the IDs are generated quickly (more
than one ID per millisecond). The sequence number makes sure that even in this
case the IDs are unique.There are two formats of the IDs that can be generated:
* binary - upper 48 bits is UNIX timestamp in milliseconds and the lower 16
bits is the sequence number
* integer - the same as binary, but expressed as a positive integerMoreover, the generated IDs are either ascending or descending. This feature
makes it possible to sort the IDs according to when they were generated.## Usage
In order to generate IDs, the `eid` application must be started. The API
contains the following functions:* `eid:get_bin()` - returns a binary ID, the generated IDs are ascending;
* `eid:get_bin(ascend)` - the same as the previous function;
* `eid:get_bin(descend)` - returns a binary ID, the generated IDs are
descending;
* `eid:get_int()` - returns an integer ID, the generated IDs are ascending;
* `eid:get_int(ascend)` - the same as the previous function;
* `eid:get_int(descend)` - returns an integer id, the generated ids are
descending.The functions return either `{ok, Id}` or `{error, Reason}`. The error is
returned when the sequence number is exceeded (which could theoretically
happen when there are more then 2^16 IDs generated within 1 millisecond).*The IDs are unique just on one node!*
### Rebar dependency
The `deps` part of the `rebar.config` should include:
```erlang
{deps, [
...
{eid, {git, "https://github.com/jur0/eid.git", {branch, "master"}}},
...
]}.
```