couchbase-id 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/couchbase-id/generator.rb +27 -33
- data/lib/couchbase-id/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9824a439eb1b63a72d1392d31363bf64a30c4a45
|
4
|
+
data.tar.gz: 91b24dcc39a0c1a68d156d749be3679fdefcbfa5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd7853709e57a9db2f8c6d7ce5af4271084a6d8e81e174eb4791a5e9e6733bc152426e40a5de5cf9041d63af64bd623b20105e2d1f80c70988598dd436951bfd
|
7
|
+
data.tar.gz: c639955b773352bc788769618004fa088d9b9f37397beccc747326680510439c37333f5a106fd58bf93a82068f1e1ed33435dadceda8f2d82e9e9945bf273006
|
@@ -34,24 +34,22 @@ module CouchbaseId
|
|
34
34
|
|
35
35
|
# instance method
|
36
36
|
def generate_id
|
37
|
-
if self.id.nil?
|
38
|
-
name = "#{self.class.name.underscore.gsub!(/\/|_/, '-')}" # The included classes name
|
39
|
-
|
37
|
+
if self.id.nil?
|
40
38
|
#
|
41
39
|
# Generate the id (incrementing values as required)
|
42
40
|
#
|
43
|
-
overflow = @@__overflow__ ||= self.class.bucket.get("#{
|
44
|
-
count = self.class.bucket.incr("#{
|
41
|
+
overflow = @@__overflow__ ||= self.class.bucket.get("#{@@__class_name__}:#{CLUSTER_ID}:overflow", :quiet => true) # Don't error if not there
|
42
|
+
count = self.class.bucket.incr("#{@@__class_name__}:#{CLUSTER_ID}:count", :create => true) # This models current id count
|
45
43
|
if count == 0 || overflow.nil?
|
46
44
|
overflow ||= 0
|
47
45
|
overflow += 1
|
48
46
|
# We shouldn't need to worry about concurrency here due to the size of count
|
49
47
|
# Would require ~18446744073709551615 concurrent writes
|
50
|
-
self.class.bucket.set("#{
|
48
|
+
self.class.bucket.set("#{@@__class_name__}:#{CLUSTER_ID}:overflow", overflow)
|
51
49
|
@@__overflow__ = overflow
|
52
50
|
end
|
53
51
|
|
54
|
-
self.id = @@__class_id_generator__.call(
|
52
|
+
self.id = @@__class_id_generator__.call(overflow, count)
|
55
53
|
|
56
54
|
|
57
55
|
#
|
@@ -65,8 +63,8 @@ module CouchbaseId
|
|
65
63
|
while self.class.bucket.get(self.id, :quiet => true).present?
|
66
64
|
# Set in-case we are here due to a crash (concurrency is not an issue)
|
67
65
|
# Note we are not incrementing the @__overflow__ variable
|
68
|
-
self.class.bucket.set("#{
|
69
|
-
count = self.class.bucket.incr("#{
|
66
|
+
self.class.bucket.set("#{@@__class_name__}:#{CLUSTER_ID}:overflow", overflow + 1)
|
67
|
+
count = self.class.bucket.incr("#{@@__class_name__}:#{CLUSTER_ID}:count") # Increment just in case (attempt to avoid infinite loops)
|
70
68
|
|
71
69
|
# Reset the overflow
|
72
70
|
if @@__overflow__ == overflow
|
@@ -74,36 +72,13 @@ module CouchbaseId
|
|
74
72
|
end
|
75
73
|
|
76
74
|
# Generate the new id
|
77
|
-
self.id = @@__class_id_generator__.call(
|
75
|
+
self.id = @@__class_id_generator__.call(overflow + 1, count)
|
78
76
|
end
|
79
77
|
end
|
80
78
|
end
|
81
79
|
|
82
|
-
module ClassMethods
|
83
|
-
def default_class_id_generator(name, overflow, count)
|
84
|
-
id = Radix.convert([overflow, CLUSTER_ID].join.to_i, B10, B65) + Radix.convert(count, B10, B65)
|
85
|
-
"#{name}-#{id}"
|
86
|
-
end
|
87
|
-
|
88
|
-
#
|
89
|
-
# Override the default hashing function
|
90
|
-
#
|
91
|
-
def set_class_id_generator(callback = nil, &block)
|
92
|
-
callback ||= block
|
93
|
-
@@__class_id_generator__ = callback
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
80
|
def self.included(base)
|
98
|
-
base.extend(ClassMethods)
|
99
|
-
|
100
81
|
base.class_eval do
|
101
|
-
#
|
102
|
-
# Configure class level variables
|
103
|
-
@@__overflow__ ||= nil
|
104
|
-
@@__class_id_generator__ ||= method(:default_class_id_generator)
|
105
|
-
|
106
|
-
|
107
82
|
#
|
108
83
|
# Best case we have 18446744073709551615 * 18446744073709551615 model entries for each database cluster
|
109
84
|
# and we can always change the cluster id if this limit is reached
|
@@ -111,6 +86,25 @@ module CouchbaseId
|
|
111
86
|
define_model_callbacks :save, :create
|
112
87
|
before_save :generate_id
|
113
88
|
before_create :generate_id
|
89
|
+
|
90
|
+
def self.default_class_id_generator(overflow, count)
|
91
|
+
id = Radix.convert([overflow, CLUSTER_ID].join.to_i, B10, B65) + Radix.convert(count, B10, B65)
|
92
|
+
"#{@@__class_name__}-#{id}"
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Override the default hashing function
|
97
|
+
#
|
98
|
+
def self.set_class_id_generator(callback = nil, &block)
|
99
|
+
callback ||= block
|
100
|
+
@@__class_id_generator__ = callback
|
101
|
+
end
|
102
|
+
|
103
|
+
#
|
104
|
+
# Configure class level variables
|
105
|
+
@@__overflow__ = nil
|
106
|
+
@@__class_id_generator__ = method(:default_class_id_generator)
|
107
|
+
@@__class_name__ = self.name.underscore.gsub(/\/|_/, '-') # The included classes name
|
114
108
|
end
|
115
109
|
end
|
116
110
|
end # END:: Generator
|
data/lib/couchbase-id/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen von Takach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: couchbase-model
|