couchbase-id 1.0.3 → 1.0.4
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/lib/couchbase-id/generator.rb +22 -15
- data/lib/couchbase-id/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76cad00122eedaa52d97d989e130faff4a9ce0eb
|
4
|
+
data.tar.gz: c1883158b04f85e3315814b54a69cb2d7db801a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62176c3dc63b501089916f75761c6a715589f026f90eac885c01873c1b1109f6dc49d2797e418faaa93aceb0741deddb8454fadc1af08ff621e1082a3f5468e1
|
7
|
+
data.tar.gz: 5ccba7537d9cb5b6cc72b4594154af643c14fc92d679b69da2fe976f7bc7da7f05b66aa38dcf37dffe10567525f0568c79a4e4c951ab7019448df4b949e5a6b7
|
@@ -38,18 +38,18 @@ module CouchbaseId
|
|
38
38
|
#
|
39
39
|
# Generate the id (incrementing values as required)
|
40
40
|
#
|
41
|
-
overflow =
|
42
|
-
count = self.class.bucket.incr("#{
|
41
|
+
overflow = self.class.__overflow__ ||= self.class.bucket.get("#{self.class.__class_name__}:#{CLUSTER_ID}:overflow", :quiet => true) # Don't error if not there
|
42
|
+
count = self.class.bucket.incr("#{self.class.__class_name__}:#{CLUSTER_ID}:count", :create => true) # This models current id count
|
43
43
|
if count == 0 || overflow.nil?
|
44
44
|
overflow ||= 0
|
45
45
|
overflow += 1
|
46
46
|
# We shouldn't need to worry about concurrency here due to the size of count
|
47
47
|
# Would require ~18446744073709551615 concurrent writes
|
48
|
-
self.class.bucket.set("#{
|
49
|
-
|
48
|
+
self.class.bucket.set("#{self.class.__class_name__}:#{CLUSTER_ID}:overflow", overflow)
|
49
|
+
self.class.__overflow__ = overflow
|
50
50
|
end
|
51
51
|
|
52
|
-
self.id =
|
52
|
+
self.id = self.class.__class_id_generator__.call(overflow, count)
|
53
53
|
|
54
54
|
|
55
55
|
#
|
@@ -63,21 +63,28 @@ module CouchbaseId
|
|
63
63
|
while self.class.bucket.get(self.id, :quiet => true).present?
|
64
64
|
# Set in-case we are here due to a crash (concurrency is not an issue)
|
65
65
|
# Note we are not incrementing the @__overflow__ variable
|
66
|
-
self.class.bucket.set("#{
|
67
|
-
count = self.class.bucket.incr("#{
|
66
|
+
self.class.bucket.set("#{self.class.__class_name__}:#{CLUSTER_ID}:overflow", overflow + 1)
|
67
|
+
count = self.class.bucket.incr("#{self.class.__class_name__}:#{CLUSTER_ID}:count") # Increment just in case (attempt to avoid infinite loops)
|
68
68
|
|
69
69
|
# Reset the overflow
|
70
|
-
if
|
71
|
-
|
70
|
+
if self.class.__overflow__ == overflow
|
71
|
+
self.class.__overflow__ = nil
|
72
72
|
end
|
73
73
|
|
74
74
|
# Generate the new id
|
75
|
-
self.id =
|
75
|
+
self.id = self.class.__class_id_generator__.call(overflow + 1, count)
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
80
|
def self.included(base)
|
81
|
+
class << base
|
82
|
+
attr_accessor :__overflow__
|
83
|
+
attr_accessor :__class_id_generator__
|
84
|
+
attr_accessor :__class_name__
|
85
|
+
end
|
86
|
+
|
87
|
+
|
81
88
|
base.class_eval do
|
82
89
|
#
|
83
90
|
# Best case we have 18446744073709551615 * 18446744073709551615 model entries for each database cluster
|
@@ -89,7 +96,7 @@ module CouchbaseId
|
|
89
96
|
|
90
97
|
def self.default_class_id_generator(overflow, count)
|
91
98
|
id = Radix.convert([overflow, CLUSTER_ID].join.to_i, B10, B65) + Radix.convert(count, B10, B65)
|
92
|
-
"#{
|
99
|
+
"#{self.__class_name__}-#{id}"
|
93
100
|
end
|
94
101
|
|
95
102
|
#
|
@@ -97,14 +104,14 @@ module CouchbaseId
|
|
97
104
|
#
|
98
105
|
def self.set_class_id_generator(callback = nil, &block)
|
99
106
|
callback ||= block
|
100
|
-
|
107
|
+
self.__class_id_generator__ = callback
|
101
108
|
end
|
102
109
|
|
103
110
|
#
|
104
111
|
# Configure class level variables
|
105
|
-
|
106
|
-
|
107
|
-
|
112
|
+
base.__overflow__ = nil
|
113
|
+
base.__class_id_generator__ = method(:default_class_id_generator)
|
114
|
+
base.__class_name__ = self.name.underscore.gsub(/\/|_/, '-') # The included classes name
|
108
115
|
end
|
109
116
|
end
|
110
117
|
end # END:: Generator
|
data/lib/couchbase-id/version.rb
CHANGED