lib/libcouchbase/connection.rb in libcouchbase-1.1.1 vs lib/libcouchbase/connection.rb in libcouchbase-1.2.0

- old
+ new

@@ -257,37 +257,24 @@ defer.promise end NonJsonValue = [:append, :prepend].freeze - # These are client specific - FormatFlags = { - document: 0, - marshal: 1, - plain: 2 - } - SupportedFormats = FormatFlags.keys.freeze - FormatFlags.merge!(FormatFlags.invert) - FormatFlags.freeze - # http://docs.couchbase.com/sdk-api/couchbase-c-client-2.6.2/group__lcb-store.html # http://docs.couchbase.com/sdk-api/couchbase-c-client-2.6.2/group__lcb-durability.html - # NOTE:: first 2 bits of the flags are reserved for document format def store(key, value, defer: nil, operation: :set, expire_in: nil, expire_at: nil, ttl: nil, persist_to: 0, replicate_to: 0, - format: :document, cas: nil, flags: 0, **opts) raise 'not connected' unless @handle - raise 'format not supported' unless SupportedFormats.include?(:document) defer ||= @reactor.defer # Check if this should be a durable operation durable = (persist_to | replicate_to) != 0 if durable @@ -296,32 +283,22 @@ cmd[:replicate_to] = replicate_to else cmd = Ext::CMDSTORE.new end cmd[:operation] = operation + cmd[:flags] = flags - # Check if we are storing a string or partial value - if NonJsonValue.include?(operation) - format = :plain + if value.is_a?(String) + str_value = value else - # Preserve any application specific flags and set the format flags - flag_mask = flags & 3 - flags = flags ^ flag_mask - cmd[:flags] = flags | FormatFlags[format] + str_value = begin + JSON.generate([value])[1..-2] + rescue + value.respond_to?(:to_str) ? value.to_str : value.to_s + end end - # Move the data into the correct format for storage - if format == :marshal - str_value = Marshal.dump(value) - elsif format == :plain - # Use coercion as it was intended - str_value = value.respond_to?(:to_str) ? value.to_str : value.to_s - else - # This will raise an error if we're not storing valid json - str_value = JSON.generate([value])[1..-2] - end - req = Request.new(cmd, defer) req.value = value cmd_set_value(req, cmd, str_value) key = cmd_set_key(req, cmd, key) @@ -337,17 +314,16 @@ defer.promise end # http://docs.couchbase.com/sdk-api/couchbase-c-client-2.6.2/group__lcb-get.html - def get(key, defer: nil, lock: false, cas: nil, format: nil, **opts) + def get(key, defer: nil, lock: false, cas: nil, **opts) raise 'not connected' unless @handle defer ||= @reactor.defer cmd = Ext::CMDGET.new req = Request.new(cmd, defer) - req.value = format key = cmd_set_key(req, cmd, key) cmd[:cas] = cas if cas # exptime == the lock expire time if lock @@ -582,28 +558,17 @@ def n1ql_query(n1ql, **opts) QueryN1QL.new(self, @reactor, n1ql, **opts) end - def parse_document(raw_string, flags: 0, hint: nil) - flag_mask = flags & 3 - format = hint || FormatFlags[flag_mask] # Defaults to document + def parse_document(raw_string) val = begin - case format - when :marshal - Marshal.load(raw_string) - when :document - JSON.parse("[#{raw_string}]", DECODE_OPTIONS)[0] - else - format = :plain - raw_string - end - rescue => e - format = :plain + JSON.parse("[#{raw_string}]", DECODE_OPTIONS)[0] + rescue raw_string end - [val, format] + val end private @@ -687,11 +652,11 @@ def callback_get(handle, type, response) resp = Ext::RESPGET.new response resp_callback_common(resp, :callback_get) do |req, cb| raw_string = resp[:value].read_string(resp[:nvalue]) - val, format = parse_document(raw_string, flags: resp[:itmflags], hint: req.value) - Response.new(cb, req.key, resp[:cas], val, {format: format, flags: resp[:itmflags]}) + val = parse_document(raw_string) + Response.new(cb, req.key, resp[:cas], val, {flags: resp[:itmflags]}) end end def callback_store(handle, type, response) resp = Ext::RESPSTORE.new response