require 'couchbase' require 'map' require "couchbase_doc_store/version" require "couchbase_doc_store/railtie" if defined?(Rails) module CouchbaseDocStore #### INSTANCE METHODS # Check if a key/document exists def document_exists?(key) return nil unless key CouchbaseDocStore.document_exists?(key) end # Initialize a document, if it doesn't exist, create it, if it exists, ignore the call def initialize_document(key, value, args={}) return nil unless key CouchbaseDocStore.initialize_document(key, value, args) end # Create a new document (Couchbase#add) def create_document(key, value, args={}) return nil unless key CouchbaseDocStore.create_document(key, value, args) # => if !quiet, Generates Couchbase::Error::KeyExists if key already exists end # Replace a new document (Couchbase#replace), throws Couchbase::Error:: if it doesn't exist def replace_document(key, value, args = {}) return nil unless key CouchbaseDocStore.replace_document(key, value, args) end # def get_document(key, args = {}) return nil unless key CouchbaseDocStore.get_document(key, args) end def get_documents(keys = [], args = {}) return nil unless keys || keys.empty? CouchbaseDocStore.get_documents(keys, args) end def delete_document(key, args={}) return nil unless key CouchbaseDocStore.delete_document(key, args) end # @param args :amount => Fixnum||Integer, increases by that def increase_atomic_count(key, args={}) return nil unless key CouchbaseDocStore.increase_atomic_count(key, args) end def decrease_atomic_count(key, args={}) return nil unless key CouchbaseDocStore.decrease_atomic_count(key, args) end # preferred way is to use create/replace to make sure there are no collisions def force_set_document(key, value, args={}) return nil unless key CouchbaseDocStore.force_set_document(key, value, args) end # end Instance Methods ##################################################################### #### CLASS METHODS class << self def connection $cb end def connect!(setting_hash = {hostname: "127.0.0.1", bucket: "default"}) puts "---------------------------------------------------" if defined? (CouchbaseSetting) puts "CouchbaseSetting found..." if (CouchbaseSetting.respond_to?("servers") && CouchbaseSetting.servers && !CouchbaseSetting.servers.empty?) setting_hash[:node_list] = CouchbaseSetting.servers elsif CouchbaseSetting.respond_to?("server") setting_hash[:hostname] = CouchbaseSetting.server else raise ArgumentError, "You didn't set Couchbase Server(s)/Bucket in your /config/couchbase.yml file!" end setting_hash[:pool] = "default" setting_hash[:bucket] = CouchbaseSetting.bucket setting_hash[:port] = 8091 if (CouchbaseSetting.respond_to?("password") && CouchbaseSetting.password && !CouchbaseSetting.password.blank?) setting_hash[:username] = CouchbaseSetting.bucket setting_hash[:password] = CouchbaseSetting.password end end $cb = Couchbase.connect(setting_hash) puts "CouchbaseDocStore connected..." puts $cb.inspect puts "---------------------------------------------------" end def delete_all_documents! $cb.flush end def document_exists?(key) return nil unless key # Save quiet setting tmp = $cb.quiet # Set quiet to be sure $cb.quiet = true doc = $cb.get(key) # Restore quiet setting $cb.quiet = tmp !doc.nil? end def initialize_document(key, value, args={}) return nil unless key $cb.quiet = true doc = CouchbaseDocStore.get_document( key ) (value.is_a?(Fixnum) || value.is_a?(Integer) ? $cb.set( key, value ) : $cb.add( key, value )) unless doc end def create_document(key, value, args={}) return nil unless key $cb.quiet = args[:quiet] || true $cb.add(key, value, args) # => if !quiet, Generates Couchbase::Error::KeyExists if key already exists end def replace_document(key, value, args = {}) return nil unless key $cb.quiet = args[:quiet] || true $cb.replace(key, value) # => if !quiet, Generates Couchbase::Error::NotFound if key doesn't exist end def get_document(key, args = {}) return nil unless key $cb.quiet = args[:quiet] || true doc = $cb.get(key, args) doc.is_a?(Hash) ? Map.new(doc) : doc end def get_documents(keys = [], args = {}) return nil unless keys || keys.empty? values = $cb.get(keys, args) if values.is_a? Hash tmp = [] tmp[0] = values values = tmp end # convert hashes to Map (subclass of Hash with *better* indifferent access) values.each_with_index do |v, i| values[i] = Map.new(v) if v.is_a? Hash end values end def delete_document(key, args={}) return nil unless key $cb.quiet = args[:quiet] || true $cb.delete(key) end def increase_atomic_count(key, args={} ) return nil unless key $cb.quiet = args[:quiet] || true $cb.incr(key, args[:amount] || 1) end def decrease_atomic_count(key, args={}) return nil unless key $cb.quiet = args[:quiet] || true $cb.decr(key, args[:amount] || 1) end # preferred way is to use create/replace instead of this to make sure there are no collisions def force_set_document(key, value, args={}) return nil unless key $cb.quiet = args[:quiet] || true $cb.set(key, value, args) end end# end ClassMethods ##################################################################### end