def generate_id
            if self.id.nil?                
                #
                # Generate the id (incrementing values as required)
                #
                overflow = self.class.__overflow__ ||= self.class.bucket.get("#{self.class.design_document}:#{CLUSTER_ID}:overflow", :quiet => true) # Don't error if not there
                count = self.class.bucket.incr("#{self.class.design_document}:#{CLUSTER_ID}:count", :create => true)     # This models current id count
                if count == 0 || overflow.nil?
                    overflow ||= 0
                    overflow += 1
                    # We shouldn't need to worry about concurrency here due to the size of count
                    # Would require ~18446744073709551615 concurrent writes
                    self.class.bucket.set("#{self.class.design_document}:#{CLUSTER_ID}:overflow", overflow)
                    self.class.__overflow__ = overflow
                end
                
                self.id = self.class.__class_id_generator__.call(overflow, count)
                
                
                #
                # So an existing id would only be present if:
                # => something crashed before incrementing the overflow
                # => this is another request was occurring before the overflow is incremented
                #
                # Basically only the overflow should be able to cause issues, we'll increment the count just to be sure
                # One would hope this code only ever runs under high load during an overflow event
                #
                while self.class.bucket.get(self.id, :quiet => true).present?
                    # Set in-case we are here due to a crash (concurrency is not an issue)
                    # Note we are not incrementing the @__overflow__ variable
                    self.class.bucket.set("#{self.class.design_document}:#{CLUSTER_ID}:overflow", overflow + 1)
                    count = self.class.bucket.incr("#{self.class.design_document}:#{CLUSTER_ID}:count")               # Increment just in case (attempt to avoid infinite loops)
                    
                    # Reset the overflow
                    if self.class.__overflow__ == overflow
                        self.class.__overflow__ = nil
                    end

                    # Generate the new id
                    self.id = self.class.__class_id_generator__.call(overflow + 1, count)
                end
            end
        end
def generate_id
            if self.id.nil?                
                #
                # Generate the id (incrementing values as required)
                #
                overflow = self.class.__overflow__ ||= self.class.bucket.get("#{self.class.design_document}:#{CLUSTER_ID}:overflow", :quiet => true) # Don't error if not there
                count = self.class.bucket.incr("#{self.class.design_document}:#{CLUSTER_ID}:count", :create => true)     # This models current id count
                if count == 0 || overflow.nil?
                    overflow ||= 0
                    overflow += 1
                    # We shouldn't need to worry about concurrency here due to the size of count
                    # Would require ~18446744073709551615 concurrent writes
                    self.class.bucket.set("#{self.class.design_document}:#{CLUSTER_ID}:overflow", overflow)
                    self.class.__overflow__ = overflow
                end
                
                self.id = self.class.__class_id_generator__.call(overflow, count)
                
                
                #
                # So an existing id would only be present if:
                # => something crashed before incrementing the overflow
                # => this is another request was occurring before the overflow is incremented
                #
                # Basically only the overflow should be able to cause issues, we'll increment the count just to be sure
                # One would hope this code only ever runs under high load during an overflow event
                #
                while self.class.bucket.get(self.id, :quiet => true).present?
                    # Set in-case we are here due to a crash (concurrency is not an issue)
                    # Note we are not incrementing the @__overflow__ variable
                    self.class.bucket.set("#{self.class.design_document}:#{CLUSTER_ID}:overflow", overflow + 1)
                    count = self.class.bucket.incr("#{self.class.design_document}:#{CLUSTER_ID}:count")               # Increment just in case (attempt to avoid infinite loops)
                    
                    # Reset the overflow
                    if self.class.__overflow__ == overflow
                        self.class.__overflow__ = nil
                    end

                    # Generate the new id
                    self.id = self.class.__class_id_generator__.call(overflow + 1, count)
                end
            end
        end
Cluster ID number
 instance method