Packages

A distributed Id generator

Current section

Files

Jump to
erlflake src erlflake.erl
Raw

src/erlflake.erl

%%%---------------------------------------------------------------------------
%%% Copyright (C) 2021, Fix Solution All Rights Reserved
%%% Unauthorized copy of this file is through any medium strictly not allowed.
%%%
%%% Authors : Alpha Shaw <shawalpha5@gmail.com>
%%% Created : 20 Apr 2021 by <shawalpha5@gmail.com>
%%% Purpose :
%%%
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%---------------------------------------------------------------------------
-module(erlflake).
-author("Alpha Umaru Shaw").
-include("erlflake.hrl").
-export([new/0, split/1, hash/2, last_saved_timestamp/0]).
new() ->
Flake = generator:next_id(),
integer_to_binary(Flake).
hash(Id, Buckets) when is_binary(Id), is_integer(Buckets) ->
hash(binary_to_integer(Id), Buckets);
hash(Id, Buckets) when is_integer(Id), is_integer(Buckets) ->
Id rem Buckets.
split(Id) when is_binary(Id); is_integer(Id) ->
generator:split(Id).
last_saved_timestamp() ->
timestamp:last_timestamp().