switchman 1.15.0 → 1.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/switchman/shard.rb +39 -17
- data/lib/switchman/active_record/migration.rb +9 -0
- data/lib/switchman/engine.rb +1 -0
- data/lib/switchman/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bfb336093e3167cc3f353d50e6dcb60ec44da9d03fd8b30d1ec51adf570d702
|
4
|
+
data.tar.gz: ae40dd725814601f125ef10f7c0ae835abc8ae22c38259ec82abb9bfb4046b95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8e0dd6e60c8454eb99ad2035b6dfb95a5596c46dbcecf6b77dd422c7a988730ea63f32a78de5d5e742e9aa3caff0f8ff5576af0f3dac92b6b40677f5ca06205
|
7
|
+
data.tar.gz: 810631877453b90f8cbeda22eb180b50e575b3f49f2965006292e20c0f4f35085417f5ac0f4d0ead2c291abbec82c82943332b46c81669b9068e40a947a03c29
|
@@ -425,6 +425,19 @@ module Switchman
|
|
425
425
|
end
|
426
426
|
end
|
427
427
|
|
428
|
+
# it's tedious to hold onto this same
|
429
|
+
# kind of sign state and transform the
|
430
|
+
# result in multiple places, so
|
431
|
+
# here we can operate on the absolute value
|
432
|
+
# in a provided block and trust the sign will
|
433
|
+
# stay as provided. This assumes no consumer
|
434
|
+
# will return a nil value from the block.
|
435
|
+
def signed_id_operation(input_id)
|
436
|
+
sign = input_id < 0 ? -1 : 1
|
437
|
+
output = yield input_id.abs
|
438
|
+
output * sign
|
439
|
+
end
|
440
|
+
|
428
441
|
# converts an AR object, integral id, string id, or string short-global-id to a
|
429
442
|
# integral id. nil if it can't be interpreted
|
430
443
|
def integral_id_for(any_id)
|
@@ -437,12 +450,13 @@ module Switchman
|
|
437
450
|
case any_id
|
438
451
|
when ::ActiveRecord::Base
|
439
452
|
any_id.id
|
440
|
-
when /^(\d+)~(
|
453
|
+
when /^(\d+)~(-?\d+)$/
|
441
454
|
local_id = $2.to_i
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
455
|
+
signed_id_operation(local_id) do |id|
|
456
|
+
return nil if id > IDS_PER_SHARD
|
457
|
+
$1.to_i * IDS_PER_SHARD + id
|
458
|
+
end
|
459
|
+
when Integer, /^-?\d+$/
|
446
460
|
any_id.to_i
|
447
461
|
else
|
448
462
|
nil
|
@@ -457,13 +471,17 @@ module Switchman
|
|
457
471
|
def local_id_for(any_id)
|
458
472
|
id = integral_id_for(any_id)
|
459
473
|
return NIL_NIL_ID unless id
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
474
|
+
return_shard = nil
|
475
|
+
local_id = signed_id_operation(id) do |abs_id|
|
476
|
+
if abs_id < IDS_PER_SHARD
|
477
|
+
abs_id
|
478
|
+
elsif return_shard = lookup(abs_id / IDS_PER_SHARD)
|
479
|
+
abs_id % IDS_PER_SHARD
|
480
|
+
else
|
481
|
+
return NIL_NIL_ID
|
482
|
+
end
|
466
483
|
end
|
484
|
+
[local_id, return_shard]
|
467
485
|
end
|
468
486
|
|
469
487
|
# takes an id-ish, and returns an integral id relative to
|
@@ -494,11 +512,13 @@ module Switchman
|
|
494
512
|
def global_id_for(any_id, source_shard = nil)
|
495
513
|
id = integral_id_for(any_id)
|
496
514
|
return any_id unless id
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
515
|
+
signed_id_operation(id) do |abs_id|
|
516
|
+
if abs_id >= IDS_PER_SHARD
|
517
|
+
abs_id
|
518
|
+
else
|
519
|
+
source_shard ||= Shard.current
|
520
|
+
source_shard.global_id_for(abs_id)
|
521
|
+
end
|
502
522
|
end
|
503
523
|
end
|
504
524
|
|
@@ -671,7 +691,9 @@ module Switchman
|
|
671
691
|
# takes an id local to this shard, and returns a global id
|
672
692
|
def global_id_for(local_id)
|
673
693
|
return nil unless local_id
|
674
|
-
|
694
|
+
self.class.signed_id_operation(local_id) do |abs_id|
|
695
|
+
abs_id + self.id * IDS_PER_SHARD
|
696
|
+
end
|
675
697
|
end
|
676
698
|
|
677
699
|
# skip global_id.hash
|
@@ -33,5 +33,14 @@ module Switchman
|
|
33
33
|
::ActiveRecord::Migrator::MIGRATOR_SALT * shard_name_hash
|
34
34
|
end
|
35
35
|
end
|
36
|
+
|
37
|
+
module MigrationContext
|
38
|
+
def migrations
|
39
|
+
return @migrations if instance_variable_defined?(:@migrations)
|
40
|
+
migrations_cache = Thread.current[:migrations_cache] ||= {}
|
41
|
+
key = Digest::MD5.hexdigest(migration_files.sort.join(','))
|
42
|
+
@migrations = migrations_cache[key] ||= super
|
43
|
+
end
|
44
|
+
end
|
36
45
|
end
|
37
46
|
end
|
data/lib/switchman/engine.rb
CHANGED
@@ -122,6 +122,7 @@ module Switchman
|
|
122
122
|
::ActiveRecord::LogSubscriber.prepend(ActiveRecord::LogSubscriber)
|
123
123
|
::ActiveRecord::Migration.prepend(ActiveRecord::Migration)
|
124
124
|
::ActiveRecord::Migration::Compatibility::V5_0.prepend(ActiveRecord::Migration::Compatibility::V5_0)
|
125
|
+
::ActiveRecord::MigrationContext.prepend(ActiveRecord::MigrationContext) if ::Rails.version >= '5.2'
|
125
126
|
::ActiveRecord::Migrator.prepend(ActiveRecord::Migrator)
|
126
127
|
|
127
128
|
::ActiveRecord::Reflection::AbstractReflection.include(ActiveRecord::Reflection::AbstractReflection)
|
data/lib/switchman/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switchman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.15.
|
4
|
+
version: 1.15.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cody Cutrer
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2020-
|
13
|
+
date: 2020-06-18 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: railties
|