Class: VIN::Config
- Inherits:
-
Object
- Object
- VIN::Config
- Defined in:
- lib/vin/config.rb
Instance Attribute Summary collapse
-
#custom_epoch ⇒ Object
readonly
Expressed in milliseconds.
-
#data_type_bits ⇒ Object
readonly
For instance, 9 bits gives us 512 different data types.
-
#logical_shard_id_bits ⇒ Object
readonly
For instance, 3 bits gives us 8 logical shards, which means we can have 8 different servers generating ids.
-
#logical_shard_id_range ⇒ Object
readonly
Defaults to allowing all logical shard ids to be generated by this server.
-
#sequence_bits ⇒ Object
readonly
For instance, 11 bits gives us 2048 ids per millisecond per logical shard.
-
#timestamp_bits ⇒ Object
readonly
For instance, 40 bits gives us 1099511627776 milliseconds, or 34.8 years.
Instance Method Summary collapse
- #data_type_allowed_range ⇒ Object
- #data_type_shift ⇒ Object
- #fetch_allowed_range! ⇒ Object
-
#initialize(custom_epoch: nil, timestamp_bits: nil, logical_shard_id_bits: nil, data_type_bits: nil, sequence_bits: nil, logical_shard_id_range: nil) ⇒ Config
constructor
A new instance of Config.
- #logical_shard_id_allowed_range ⇒ Object
- #logical_shard_id_shift ⇒ Object
- #max_data_type ⇒ Object
- #max_logical_shard_id ⇒ Object
- #max_sequence ⇒ Object
- #min_data_type ⇒ Object
- #min_logical_shard_id ⇒ Object
- #sequence_shift ⇒ Object
- #timestamp_shift ⇒ Object
Constructor Details
#initialize(custom_epoch: nil, timestamp_bits: nil, logical_shard_id_bits: nil, data_type_bits: nil, sequence_bits: nil, logical_shard_id_range: nil) ⇒ Config
Returns a new instance of Config.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/vin/config.rb', line 21 def initialize( custom_epoch: nil, timestamp_bits: nil, logical_shard_id_bits: nil, data_type_bits: nil, sequence_bits: nil, logical_shard_id_range: nil ) @custom_epoch = custom_epoch || ENV.fetch("VIN_CUSTOM_EPOCH").to_i @timestamp_bits = || ENV.fetch("VIN_TIMESTAMP_BITS").to_i @logical_shard_id_bits = logical_shard_id_bits || ENV.fetch("VIN_LOGICAL_SHARD_ID_BITS").to_i @data_type_bits = data_type_bits || ENV.fetch("VIN_DATA_TYPE_BITS").to_i @sequence_bits = sequence_bits || ENV.fetch("VIN_SEQUENCE_BITS").to_i @logical_shard_id_range = logical_shard_id_range || fetch_allowed_range! end |
Instance Attribute Details
#custom_epoch ⇒ Object (readonly)
Expressed in milliseconds.
4 5 6 |
# File 'lib/vin/config.rb', line 4 def custom_epoch @custom_epoch end |
#data_type_bits ⇒ Object (readonly)
For instance, 9 bits gives us 512 different data types.
13 14 15 |
# File 'lib/vin/config.rb', line 13 def data_type_bits @data_type_bits end |
#logical_shard_id_bits ⇒ Object (readonly)
For instance, 3 bits gives us 8 logical shards, which means we can have 8 different servers generating ids.
10 11 12 |
# File 'lib/vin/config.rb', line 10 def logical_shard_id_bits @logical_shard_id_bits end |
#logical_shard_id_range ⇒ Object (readonly)
Defaults to allowing all logical shard ids to be generated by this server.
19 20 21 |
# File 'lib/vin/config.rb', line 19 def logical_shard_id_range @logical_shard_id_range end |
#sequence_bits ⇒ Object (readonly)
For instance, 11 bits gives us 2048 ids per millisecond per logical shard.
16 17 18 |
# File 'lib/vin/config.rb', line 16 def sequence_bits @sequence_bits end |
#timestamp_bits ⇒ Object (readonly)
For instance, 40 bits gives us 1099511627776 milliseconds, or 34.8 years. Enough time to last us until 2057, enough time for any of us to retire.
7 8 9 |
# File 'lib/vin/config.rb', line 7 def @timestamp_bits end |
Instance Method Details
#data_type_allowed_range ⇒ Object
57 58 59 |
# File 'lib/vin/config.rb', line 57 def data_type_allowed_range @data_type_allowed_range ||= (min_data_type..max_data_type) end |
#data_type_shift ⇒ Object
69 70 71 |
# File 'lib/vin/config.rb', line 69 def data_type_shift @data_type_shift ||= sequence_bits end |
#fetch_allowed_range! ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/vin/config.rb', line 81 def fetch_allowed_range! range = Range.new( ENV.fetch("VIN_LOGICAL_SHARD_ID_RANGE_MIN", 0).to_i, ENV.fetch("VIN_LOGICAL_SHARD_ID_RANGE_MAX", 0).to_i, ) # rubocop:disable Style/BitwisePredicate unless (logical_shard_id_allowed_range.to_a & range.to_a) == range.to_a raise(ArgumentError, "VIN_LOGICAL_SHARD_ID_RANGE_MIN and VIN_LOGICAL_SHARD_ID_RANGE_MAX env vars compose a range outside the allowed range of #{logical_shard_id_allowed_range} defined by the number of bits in VIN_LOGICAL_SHARD_ID_BITS env var.") end # rubocop:enable Style/BitwisePredicate range end |
#logical_shard_id_allowed_range ⇒ Object
45 46 47 |
# File 'lib/vin/config.rb', line 45 def logical_shard_id_allowed_range @logical_shard_id_allowed_range ||= (min_logical_shard_id..max_logical_shard_id) end |
#logical_shard_id_shift ⇒ Object
73 74 75 |
# File 'lib/vin/config.rb', line 73 def logical_shard_id_shift @logical_shard_id_shift ||= (sequence_bits + data_type_bits) end |
#max_data_type ⇒ Object
53 54 55 |
# File 'lib/vin/config.rb', line 53 def max_data_type @max_data_type ||= ~(-1 << data_type_bits) end |
#max_logical_shard_id ⇒ Object
41 42 43 |
# File 'lib/vin/config.rb', line 41 def max_logical_shard_id @max_logical_shard_id ||= ~(-1 << logical_shard_id_bits) end |
#max_sequence ⇒ Object
61 62 63 |
# File 'lib/vin/config.rb', line 61 def max_sequence @max_sequence ||= ~(-1 << sequence_bits) end |
#min_data_type ⇒ Object
49 50 51 |
# File 'lib/vin/config.rb', line 49 def min_data_type 0 end |
#min_logical_shard_id ⇒ Object
37 38 39 |
# File 'lib/vin/config.rb', line 37 def min_logical_shard_id 0 end |
#sequence_shift ⇒ Object
65 66 67 |
# File 'lib/vin/config.rb', line 65 def sequence_shift 0 end |
#timestamp_shift ⇒ Object
77 78 79 |
# File 'lib/vin/config.rb', line 77 def @timestamp_shift ||= (sequence_bits + data_type_bits + logical_shard_id_bits) end |