couchbase-id 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +0 -0
- data/README.md +0 -0
- data/Rakefile +0 -0
- data/couchbase-id.gemspec +0 -0
- data/lib/couchbase-id/generator.rb +116 -116
- data/lib/couchbase-id/version.rb +3 -3
- data/lib/couchbase-id.rb +2 -1
- metadata +25 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 932a8f5438d9d2ed828ff17225796f1cdcfcf346
|
4
|
+
data.tar.gz: 13077308a72f5e99338f8aa10eabb85c7b884ba9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a313427d057436fe79dc2b07a7aaad163ac9965d643a2d47787f1683f0affc7f42f41979fcc1488178a565f19e2fc7184151b4d777bd41317eca0f7ff606cbd
|
7
|
+
data.tar.gz: 115f7f4b27a1633af0a34e32045d37d04f52458275f0a177a7553cdfc68a2fc54bfd1ad2c8d0ad5b36e42b3350bb7121d643938122d1d10dfa7da427b86f2585
|
data/LICENSE
CHANGED
File without changes
|
data/README.md
CHANGED
File without changes
|
data/Rakefile
CHANGED
File without changes
|
data/couchbase-id.gemspec
CHANGED
File without changes
|
@@ -1,116 +1,116 @@
|
|
1
|
-
#
|
2
|
-
# This disables the built in generator, faster then running validations twice
|
3
|
-
# Forces models to include an ID generator
|
4
|
-
#
|
5
|
-
module Couchbase
|
6
|
-
class Model
|
7
|
-
class UUID
|
8
|
-
def initialize(*args)
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
def next(*args)
|
13
|
-
nil
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
#
|
20
|
-
# This is our id generator, runs in the before save call back
|
21
|
-
#
|
22
|
-
module CouchbaseId
|
23
|
-
|
24
|
-
# NOTE:: incr, decr, append, prepend == atomic
|
25
|
-
module Generator
|
26
|
-
|
27
|
-
|
28
|
-
# Basic compression using UTF (more efficient for ID's stored as strings)
|
29
|
-
B65 = Radix::Base.new(Radix::BASE::B62 + ['-', '_', '~'])
|
30
|
-
B10 = Radix::Base.new(10)
|
31
|
-
|
32
|
-
# The cluster id this node belongs to (avoids XDCR clashes)
|
33
|
-
CLUSTER_ID ||= ENV['COUCHBASE_CLUSTER'] || 1 # Cluster ID number
|
34
|
-
|
35
|
-
# instance method
|
36
|
-
def generate_id
|
37
|
-
if self.id.nil?
|
38
|
-
#
|
39
|
-
# Generate the id (incrementing values as required)
|
40
|
-
#
|
41
|
-
overflow = self.class.__overflow__ ||= self.class.bucket.get("#{self.class.design_document}:#{CLUSTER_ID}:overflow", :quiet => true) # Don't error if not there
|
42
|
-
count = self.class.bucket.incr("#{self.class.design_document}:#{CLUSTER_ID}:count", :create => true) # This models current id count
|
43
|
-
if count == 0 || overflow.nil?
|
44
|
-
overflow ||= 0
|
45
|
-
overflow += 1
|
46
|
-
# We shouldn't need to worry about concurrency here due to the size of count
|
47
|
-
# Would require ~18446744073709551615 concurrent writes
|
48
|
-
self.class.bucket.set("#{self.class.design_document}:#{CLUSTER_ID}:overflow", overflow)
|
49
|
-
self.class.__overflow__ = overflow
|
50
|
-
end
|
51
|
-
|
52
|
-
self.id = self.class.__class_id_generator__.call(overflow, count)
|
53
|
-
|
54
|
-
|
55
|
-
#
|
56
|
-
# So an existing id would only be present if:
|
57
|
-
# => something crashed before incrementing the overflow
|
58
|
-
# => this is another request was occurring before the overflow is incremented
|
59
|
-
#
|
60
|
-
# Basically only the overflow should be able to cause issues, we'll increment the count just to be sure
|
61
|
-
# One would hope this code only ever runs under high load during an overflow event
|
62
|
-
#
|
63
|
-
while self.class.bucket.get(self.id, :quiet => true).present?
|
64
|
-
# Set in-case we are here due to a crash (concurrency is not an issue)
|
65
|
-
# Note we are not incrementing the @__overflow__ variable
|
66
|
-
self.class.bucket.set("#{self.class.design_document}:#{CLUSTER_ID}:overflow", overflow + 1)
|
67
|
-
count = self.class.bucket.incr("#{self.class.design_document}:#{CLUSTER_ID}:count") # Increment just in case (attempt to avoid infinite loops)
|
68
|
-
|
69
|
-
# Reset the overflow
|
70
|
-
if self.class.__overflow__ == overflow
|
71
|
-
self.class.__overflow__ = nil
|
72
|
-
end
|
73
|
-
|
74
|
-
# Generate the new id
|
75
|
-
self.id = self.class.__class_id_generator__.call(overflow + 1, count)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def self.included(base)
|
81
|
-
class << base
|
82
|
-
attr_accessor :__overflow__
|
83
|
-
attr_accessor :__class_id_generator__
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
base.class_eval do
|
88
|
-
#
|
89
|
-
# Best case we have 18446744073709551615 * 18446744073709551615 model entries for each database cluster
|
90
|
-
# and we can always change the cluster id if this limit is reached
|
91
|
-
#
|
92
|
-
define_model_callbacks :save, :create
|
93
|
-
before_save :generate_id
|
94
|
-
before_create :generate_id
|
95
|
-
|
96
|
-
def self.default_class_id_generator(overflow, count)
|
97
|
-
id = Radix.convert([overflow, CLUSTER_ID].join.to_i, B10, B65) + Radix.convert(count, B10, B65)
|
98
|
-
"#{self.design_document}-#{id}"
|
99
|
-
end
|
100
|
-
|
101
|
-
#
|
102
|
-
# Override the default hashing function
|
103
|
-
#
|
104
|
-
def self.set_class_id_generator(callback = nil, &block)
|
105
|
-
callback ||= block
|
106
|
-
self.__class_id_generator__ = callback
|
107
|
-
end
|
108
|
-
|
109
|
-
#
|
110
|
-
# Configure class level variables
|
111
|
-
base.__overflow__ = nil
|
112
|
-
base.__class_id_generator__ = method(:default_class_id_generator)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end # END:: Generator
|
116
|
-
end
|
1
|
+
#
|
2
|
+
# This disables the built in generator, faster then running validations twice
|
3
|
+
# Forces models to include an ID generator
|
4
|
+
#
|
5
|
+
module Couchbase
|
6
|
+
class Model
|
7
|
+
class UUID
|
8
|
+
def initialize(*args)
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
def next(*args)
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# This is our id generator, runs in the before save call back
|
21
|
+
#
|
22
|
+
module CouchbaseId
|
23
|
+
|
24
|
+
# NOTE:: incr, decr, append, prepend == atomic
|
25
|
+
module Generator
|
26
|
+
|
27
|
+
|
28
|
+
# Basic compression using UTF (more efficient for ID's stored as strings)
|
29
|
+
B65 = Radix::Base.new(Radix::BASE::B62 + ['-', '_', '~'])
|
30
|
+
B10 = Radix::Base.new(10)
|
31
|
+
|
32
|
+
# The cluster id this node belongs to (avoids XDCR clashes)
|
33
|
+
CLUSTER_ID ||= ENV['COUCHBASE_CLUSTER'] || 1 # Cluster ID number
|
34
|
+
|
35
|
+
# instance method
|
36
|
+
def generate_id
|
37
|
+
if self.id.nil?
|
38
|
+
#
|
39
|
+
# Generate the id (incrementing values as required)
|
40
|
+
#
|
41
|
+
overflow = self.class.__overflow__ ||= self.class.bucket.get("#{self.class.design_document}:#{CLUSTER_ID}:overflow", :quiet => true) # Don't error if not there
|
42
|
+
count = self.class.bucket.incr("#{self.class.design_document}:#{CLUSTER_ID}:count", :create => true) # This models current id count
|
43
|
+
if count == 0 || overflow.nil?
|
44
|
+
overflow ||= 0
|
45
|
+
overflow += 1
|
46
|
+
# We shouldn't need to worry about concurrency here due to the size of count
|
47
|
+
# Would require ~18446744073709551615 concurrent writes
|
48
|
+
self.class.bucket.set("#{self.class.design_document}:#{CLUSTER_ID}:overflow", overflow)
|
49
|
+
self.class.__overflow__ = overflow
|
50
|
+
end
|
51
|
+
|
52
|
+
self.id = self.class.__class_id_generator__.call(overflow, count)
|
53
|
+
|
54
|
+
|
55
|
+
#
|
56
|
+
# So an existing id would only be present if:
|
57
|
+
# => something crashed before incrementing the overflow
|
58
|
+
# => this is another request was occurring before the overflow is incremented
|
59
|
+
#
|
60
|
+
# Basically only the overflow should be able to cause issues, we'll increment the count just to be sure
|
61
|
+
# One would hope this code only ever runs under high load during an overflow event
|
62
|
+
#
|
63
|
+
while self.class.bucket.get(self.id, :quiet => true).present?
|
64
|
+
# Set in-case we are here due to a crash (concurrency is not an issue)
|
65
|
+
# Note we are not incrementing the @__overflow__ variable
|
66
|
+
self.class.bucket.set("#{self.class.design_document}:#{CLUSTER_ID}:overflow", overflow + 1)
|
67
|
+
count = self.class.bucket.incr("#{self.class.design_document}:#{CLUSTER_ID}:count") # Increment just in case (attempt to avoid infinite loops)
|
68
|
+
|
69
|
+
# Reset the overflow
|
70
|
+
if self.class.__overflow__ == overflow
|
71
|
+
self.class.__overflow__ = nil
|
72
|
+
end
|
73
|
+
|
74
|
+
# Generate the new id
|
75
|
+
self.id = self.class.__class_id_generator__.call(overflow + 1, count)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.included(base)
|
81
|
+
class << base
|
82
|
+
attr_accessor :__overflow__
|
83
|
+
attr_accessor :__class_id_generator__
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
base.class_eval do
|
88
|
+
#
|
89
|
+
# Best case we have 18446744073709551615 * 18446744073709551615 model entries for each database cluster
|
90
|
+
# and we can always change the cluster id if this limit is reached
|
91
|
+
#
|
92
|
+
define_model_callbacks :save, :create
|
93
|
+
before_save :generate_id
|
94
|
+
before_create :generate_id
|
95
|
+
|
96
|
+
def self.default_class_id_generator(overflow, count)
|
97
|
+
id = Radix.convert([overflow, CLUSTER_ID].join.to_i, B10, B65) + Radix.convert(count, B10, B65)
|
98
|
+
"#{self.design_document}-#{id}"
|
99
|
+
end
|
100
|
+
|
101
|
+
#
|
102
|
+
# Override the default hashing function
|
103
|
+
#
|
104
|
+
def self.set_class_id_generator(callback = nil, &block)
|
105
|
+
callback ||= block
|
106
|
+
self.__class_id_generator__ = callback
|
107
|
+
end
|
108
|
+
|
109
|
+
#
|
110
|
+
# Configure class level variables
|
111
|
+
base.__overflow__ = nil
|
112
|
+
base.__class_id_generator__ = method(:default_class_id_generator)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end # END:: Generator
|
116
|
+
end
|
data/lib/couchbase-id/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module CouchbaseId
|
2
|
-
VERSION = '1.0.
|
3
|
-
end
|
1
|
+
module CouchbaseId
|
2
|
+
VERSION = '1.0.9'
|
3
|
+
end
|
data/lib/couchbase-id.rb
CHANGED
metadata
CHANGED
@@ -1,71 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couchbase-id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen von Takach
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: radix
|
15
|
-
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
|
-
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
23
|
requirements:
|
22
24
|
- - '>='
|
23
25
|
- !ruby/object:Gem::Version
|
24
26
|
version: '0'
|
25
|
-
prerelease: false
|
26
|
-
type: :runtime
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
|
-
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '2.14'
|
34
|
-
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
35
37
|
requirements:
|
36
38
|
- - '>='
|
37
39
|
- !ruby/object:Gem::Version
|
38
40
|
version: '2.14'
|
39
|
-
prerelease: false
|
40
|
-
type: :development
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
|
-
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.1'
|
48
|
-
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
49
51
|
requirements:
|
50
52
|
- - '>='
|
51
53
|
- !ruby/object:Gem::Version
|
52
54
|
version: '10.1'
|
53
|
-
prerelease: false
|
54
|
-
type: :development
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: yard
|
57
|
-
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
65
|
requirements:
|
64
66
|
- - '>='
|
65
67
|
- !ruby/object:Gem::Version
|
66
68
|
version: '0'
|
67
|
-
prerelease: false
|
68
|
-
type: :development
|
69
69
|
description: Overwrites the existing couchbase-model id implementation
|
70
70
|
email:
|
71
71
|
- steve@cotag.me
|
@@ -74,9 +74,9 @@ extensions: []
|
|
74
74
|
extra_rdoc_files:
|
75
75
|
- README.md
|
76
76
|
files:
|
77
|
-
- lib/couchbase-id.rb
|
78
77
|
- lib/couchbase-id/generator.rb
|
79
78
|
- lib/couchbase-id/version.rb
|
79
|
+
- lib/couchbase-id.rb
|
80
80
|
- Rakefile
|
81
81
|
- couchbase-id.gemspec
|
82
82
|
- README.md
|
@@ -85,7 +85,7 @@ homepage: https://github.com/cotag/couchbase-id
|
|
85
85
|
licenses:
|
86
86
|
- MIT
|
87
87
|
metadata: {}
|
88
|
-
post_install_message:
|
88
|
+
post_install_message:
|
89
89
|
rdoc_options: []
|
90
90
|
require_paths:
|
91
91
|
- lib
|
@@ -100,9 +100,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
- !ruby/object:Gem::Version
|
101
101
|
version: '0'
|
102
102
|
requirements: []
|
103
|
-
rubyforge_project:
|
104
|
-
rubygems_version: 2.1.
|
105
|
-
signing_key:
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.1.11
|
105
|
+
signing_key:
|
106
106
|
specification_version: 4
|
107
107
|
summary: Couchbase ID generator with XDCR support
|
108
108
|
test_files: []
|
109
|
+
has_rdoc:
|