This repository has been archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chibicco
committed
Jul 26, 2019
1 parent
e7f4f07
commit 779cbc2
Showing
20 changed files
with
666 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language: ruby | ||
rvm: | ||
- 2.5.5 | ||
bundler_args: --jobs=2 | ||
script: bundle exec rake spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# CHANGELOG | ||
|
||
## 0.0.1 - First Release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in nagoriyuki.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
nagoriyuki (0.0.1) | ||
activesupport | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
activesupport (5.2.3) | ||
concurrent-ruby (~> 1.0, >= 1.0.2) | ||
i18n (>= 0.7, < 2) | ||
minitest (~> 5.1) | ||
tzinfo (~> 1.1) | ||
chronic (0.10.2) | ||
coderay (1.1.2) | ||
concurrent-ruby (1.1.5) | ||
coveralls (0.8.23) | ||
json (>= 1.8, < 3) | ||
simplecov (~> 0.16.1) | ||
term-ansicolor (~> 1.3) | ||
thor (>= 0.19.4, < 2.0) | ||
tins (~> 1.6) | ||
delorean (2.1.0) | ||
chronic | ||
diff-lcs (1.3) | ||
docile (1.3.2) | ||
ffi (1.11.1) | ||
formatador (0.2.5) | ||
guard (2.15.0) | ||
formatador (>= 0.2.4) | ||
listen (>= 2.7, < 4.0) | ||
lumberjack (>= 1.0.12, < 2.0) | ||
nenv (~> 0.1) | ||
notiffany (~> 0.0) | ||
pry (>= 0.9.12) | ||
shellany (~> 0.0) | ||
thor (>= 0.18.1) | ||
guard-compat (1.2.1) | ||
guard-rspec (4.7.3) | ||
guard (~> 2.1) | ||
guard-compat (~> 1.1) | ||
rspec (>= 2.99.0, < 4.0) | ||
i18n (1.6.0) | ||
concurrent-ruby (~> 1.0) | ||
json (2.2.0) | ||
listen (3.1.5) | ||
rb-fsevent (~> 0.9, >= 0.9.4) | ||
rb-inotify (~> 0.9, >= 0.9.7) | ||
ruby_dep (~> 1.2) | ||
lumberjack (1.0.13) | ||
method_source (0.9.2) | ||
minitest (5.11.3) | ||
nenv (0.3.0) | ||
notiffany (0.1.1) | ||
nenv (~> 0.1) | ||
shellany (~> 0.0) | ||
pry (0.12.2) | ||
coderay (~> 1.1.0) | ||
method_source (~> 0.9.0) | ||
rake (12.3.3) | ||
rb-fsevent (0.10.3) | ||
rb-inotify (0.10.0) | ||
ffi (~> 1.0) | ||
rspec (3.5.0) | ||
rspec-core (~> 3.5.0) | ||
rspec-expectations (~> 3.5.0) | ||
rspec-mocks (~> 3.5.0) | ||
rspec-core (3.5.4) | ||
rspec-support (~> 3.5.0) | ||
rspec-expectations (3.5.0) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.5.0) | ||
rspec-its (1.3.0) | ||
rspec-core (>= 3.0.0) | ||
rspec-expectations (>= 3.0.0) | ||
rspec-mocks (3.5.0) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.5.0) | ||
rspec-support (3.5.0) | ||
ruby_dep (1.5.0) | ||
shellany (0.0.1) | ||
simplecov (0.16.1) | ||
docile (~> 1.1) | ||
json (>= 1.8, < 3) | ||
simplecov-html (~> 0.10.0) | ||
simplecov-html (0.10.2) | ||
term-ansicolor (1.7.1) | ||
tins (~> 1.0) | ||
thor (0.20.3) | ||
thread_safe (0.3.6) | ||
tins (1.21.1) | ||
tzinfo (1.2.5) | ||
thread_safe (~> 0.1) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
bundler (~> 2.0.0) | ||
coveralls | ||
delorean | ||
guard-rspec | ||
nagoriyuki! | ||
pry | ||
rake | ||
rspec (~> 3.5.0) | ||
rspec-its | ||
|
||
BUNDLED WITH | ||
2.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# A sample Guardfile | ||
# More info at https://github.com/guard/guard#readme | ||
|
||
guard :rspec, | ||
cmd: "bundle exec rspec", | ||
all_after_pass: true, | ||
all_on_start: true do | ||
watch(%r{^spec/.+_spec\.rb$}) | ||
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } | ||
watch('spec/spec_helper.rb') { "spec" } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,84 @@ | ||
# cherryblossom | ||
# Nagoriyuki | ||
|
||
[![Gem Version](https://badge.fury.io/rb/nagoriyuki.svg)](http://badge.fury.io/rb/nagoriyuki) | ||
[![Coverage Status](https://img.shields.io/coveralls/chibicco/nagoriyuki.svg)](https://coveralls.io/r/chibicco/nagoriyuki) | ||
[![Build Status](https://travis-ci.org/chibicco/nagoriyuki.svg)](https://travis-ci.org/chibicco/nagoriyuki) | ||
|
||
nagoriyuki(なごり雪) is a twitter/snowflake like ID generator. | ||
|
||
This is the extraction of the minimum functionality with referred to the code of [drecom/barrage](https://github.com/drecom/barrage). | ||
|
||
And there are only a few feature additions. | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'nagoriyuki' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install nagoriyuki | ||
|
||
## Usage | ||
|
||
### Example | ||
|
||
```ruby | ||
# 39bit: msec (17.4 years from offset_epoch) | ||
# 16bit: pid | ||
# 9bit: sequence | ||
require 'nagoriyuki' | ||
|
||
nagoriyuki = Nagoriyuki.new( | ||
"generators" => [ | ||
{"name" => "msec", "length" => 39, "offset_epoch" => 1546268400000}, | ||
{"name" => "pid", "length" => 16}, | ||
{"name" => "sequence", "length" => 9} | ||
] | ||
) | ||
|
||
id = nagoriyuki.next | ||
# => 596299014690041345 | ||
|
||
ts = nagoriyuki.timestamp(id) | ||
# => 1564039495 | ||
|
||
Time.at(ts) | ||
# => 2019-07-25 16:24:55 +0900 | ||
``` | ||
|
||
### Generators | ||
|
||
#### msec | ||
|
||
#### pid | ||
|
||
#### sequence | ||
|
||
### Creating your own generator | ||
|
||
```ruby | ||
module Nagoriyuki::Generators | ||
class YourOwnGenerator < Base | ||
self.required_options += %w(your_option_value) | ||
def generate | ||
# generated code | ||
end | ||
|
||
def your_option_value | ||
options["your_option_value"] | ||
end | ||
end | ||
end | ||
|
||
nagoriyuki = Nagoriyuki.new("generators" => [{"name"=>"your_own", "length" => 8, "your_option_value"=>"xxx"}]) | ||
``` | ||
|
||
## License | ||
|
||
[Copyright (c) 2014 Drecom Co., Ltd.](https://github.com/drecom/barrage/blob/master/LICENSE.txt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task :default => :spec | ||
|
||
desc "pry console" | ||
task :console do | ||
require "pry" | ||
require "nagoriyuki" | ||
binding.pry | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require "nagoriyuki/version" | ||
require "nagoriyuki/generators" | ||
require "nagoriyuki/generators/base" | ||
require "nagoriyuki/generators/msec" | ||
require "nagoriyuki/generators/pid" | ||
require "nagoriyuki/generators/sequence" | ||
require "active_support/core_ext/string/inflections" | ||
|
||
class Nagoriyuki | ||
class InvalidOption < StandardError; end | ||
attr_reader :generators | ||
|
||
# | ||
# generators: | ||
# - name: msec | ||
# length: 40 # (2 ** 40 - 1) / 1000 / 365 / 24 / 60 / 60.0 | ||
# start_at: 1546268400000 # 2019-01-01 00:00:00 +0900 (msec) | ||
# - name: pid | ||
# length: 16 | ||
# - name: sequence | ||
# length: 8 | ||
def initialize(options = {}) | ||
@options = options | ||
@lock = Mutex.new | ||
@generators = @options["generators"].map do |h| | ||
generator_name = h["name"] | ||
"Nagoriyuki::Generators::#{generator_name.classify}".constantize.new(h) | ||
end | ||
end | ||
|
||
def generate | ||
@lock.synchronize do | ||
shift_size = length | ||
@generators.inject(0) do |result, generator| | ||
shift_size = shift_size - generator.length | ||
result += generator.generate << shift_size | ||
end | ||
end | ||
end | ||
alias_method :next, :generate | ||
|
||
def current | ||
@lock.synchronize do | ||
shift_size = length | ||
@generators.inject(0) do |result, generator| | ||
shift_size = shift_size - generator.length | ||
result += generator.current << shift_size | ||
end | ||
end | ||
end | ||
|
||
def timestamp(id) | ||
generator = @generators.detect {|g| g.name == "msec" } | ||
shift_size = length - generator.length | ||
((id >> shift_size) + generator.offset_epoch) / 1000 | ||
end | ||
|
||
def smallest(timestamp) | ||
@lock.synchronize do | ||
shift_size = length | ||
@generators.inject(0) do |result, generator| | ||
shift_size = shift_size - generator.length | ||
result += generator.smallest(timestamp) << shift_size | ||
end | ||
end | ||
end | ||
|
||
def biggest(timestamp) | ||
@lock.synchronize do | ||
shift_size = length | ||
@generators.inject(0) do |result, generator| | ||
shift_size = shift_size - generator.length | ||
result += generator.biggest(timestamp) << shift_size | ||
end | ||
end | ||
end | ||
|
||
def length | ||
@generators.inject(0) {|sum, g| sum += g.length } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Nagoriyuki | ||
module Generators | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require "nagoriyuki/generators" | ||
require "active_support/core_ext/class/attribute" | ||
|
||
class Nagoriyuki | ||
module Generators | ||
class Base | ||
attr_reader :options | ||
class_attribute :required_options, :available_options | ||
self.required_options = %w(length) | ||
self.available_options = [] | ||
|
||
def initialize(options = {}) | ||
if (missing = missing_required_options(options)) && !missing.empty? | ||
raise ArgumentError, "Missing Required options: #{missing.join(', ')}" | ||
end | ||
@options = options | ||
end | ||
|
||
def name | ||
options["name"] | ||
end | ||
|
||
def length | ||
options["length"] | ||
end | ||
|
||
def generate | ||
raise NotImplemented, "Please Override" | ||
end | ||
|
||
def current | ||
raise NotImplemented, "Please Override" | ||
end | ||
|
||
def smallest(timestamp) | ||
raise NotImplemented, "Please Override" | ||
end | ||
|
||
def biggest(timestamp) | ||
raise NotImplemented, "Please Override" | ||
end | ||
|
||
private | ||
|
||
def bit_fill | ||
(2 ** length - 1) | ||
end | ||
|
||
def missing_required_options(given_options) | ||
required_options.reject { |k| given_options.has_key?(k) } | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.