From c66367970203932156d288d13336c0e85c21611c Mon Sep 17 00:00:00 2001 From: MCRedJay Date: Tue, 18 Mar 2014 15:47:05 -0500 Subject: [PATCH 1/3] tst commit --- lib/couchie.ex | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/couchie.ex b/lib/couchie.ex index a0bc466..87d504f 100644 --- a/lib/couchie.ex +++ b/lib/couchie.ex @@ -168,6 +168,17 @@ defmodule Couchie do end + @doc """ + Query the database, getting a list of documents. + ## Example + + Couchie.delete(:connection, "test_key") + """ + def query(host, parameters) do +# + end + +#delete the old stuff below when above is decent @doc """ Simple synchronous fetch view for when you have the specific url & parameters built. From 476cb2f76f67e46f3256a431071d046bf2be96fb Mon Sep 17 00:00:00 2001 From: MCRedJay Date: Tue, 18 Mar 2014 15:50:53 -0500 Subject: [PATCH 2/3] WTF, can I not trust github? --- lib/couchie.ex | 89 ++------------------------------------------------ 1 file changed, 3 insertions(+), 86 deletions(-) mode change 100644 => 100755 lib/couchie.ex diff --git a/lib/couchie.ex b/lib/couchie.ex old mode 100644 new mode 100755 index 87d504f..b379c06 --- a/lib/couchie.ex +++ b/lib/couchie.ex @@ -92,20 +92,9 @@ defmodule Couchie do Couchie.set(:default, "key", "document data", 0) """ def set(connection, key, document, expiration) do - doc2 = Couchie.preprocess document - :cberl.set(connection, key, expiration, doc2) # NOTE: cberl parameter order is different! + :cberl.set(connection, key, expiration, document) # NOTE: cberl parameter order is different! end - @doc """ - Turn Dict into list for JSON conversion. Pass binaries along unmolested. - """ - def preprocess(document) do - case document do - document when is_list(document) -> document #pass on lists unmolested - document when is_binary(document) -> document #pass on binaries unmolested - document -> {Dict.to_list document} #If not a list or binary, it's a hashdict. - end - end @doc """ Get document. Keys should be binary. @@ -115,11 +104,7 @@ defmodule Couchie do #=> {"test_key" 1234567890, "value"} # The middle figure is the CAS for this document. """ def get(connection, key) do - result = :cberl.get(connection, key) - case result do - {_, {:error, _}} -> result - _ -> postprocess(result) - end + :cberl.get(connection, key) end @doc """ @@ -129,32 +114,7 @@ defmodule Couchie do Couchie.mget(:connection, ["test_key", "another key"]) """ def mget(connection, keys) do - results = :cberl.mget(connection, keys) - case results do - {_, {:error, _}} -> results - _ -> Enum.map results, &Couchie.postprocess/1 - end - end - - @doc """ - Remove the envelope around JSON results. Turn JSON structure into HashDict - """ - def postprocess({_, {:error, _}}=result) do - result - end - def postprocess({key,cas,value}) do - case value do - {[h|_]} when is_tuple(h) -> #If the first item is a tuple, we figure its a proplist. - proplist_to_dict(key, cas, value) - value when is_binary(value) -> {key, cas, value} #just pass on binaries. - _ -> {key, cas, value} # anything else (Eg: straight list) we pass on unmolested - end - end - - defp proplist_to_dict(key, cas, value) do - {value2} = value # remove enclosing tuple, get list. - value3 = HashDict.new value2 - {key, cas, value3} + :cberl.mget(connection, keys) end @doc """ @@ -166,50 +126,7 @@ defmodule Couchie do def delete(connection, key) do :cberl.remove(connection, key) end - - - @doc """ - Query the database, getting a list of documents. - ## Example - - Couchie.delete(:connection, "test_key") - """ - def query(host, parameters) do -# - end - -#delete the old stuff below when above is decent - - @doc """ - Simple synchronous fetch view for when you have the specific url & parameters built. - ## Example - - Couchie.view("http://example.com:port/_design/foo/_view/bar") - """ -## def view(url) do -## case :ibrowse.send_req(url, [], :get) do -## {:error, reason} -> {:error, reason} -## {:ok, return_code, headers, body} -> view_process(return_code, headers, body) -## end -## end -## -## def view_process(return_code, headers, body) do -## headers_dict = HashDict.new headers -## this is not finished! views not implemented yet, as I explore a different direction. - -## end - - - - - - - - end -# -# -# From 93f65eda846aa23bf7276a36c468d0c44a6156a8 Mon Sep 17 00:00:00 2001 From: MCRedJay Date: Thu, 20 Mar 2014 16:11:33 -0500 Subject: [PATCH 3/3] view support --- README.md | 3 +++ lib/couchie.ex | 31 +++++++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a3a9ed6..ba20a83 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,9 @@ Note: to build, you need to have libcouchbase installed. iex(17)> Couchie.get(:default, "bar") {"bar", 983466356890402816, #HashDict<[{"key", "value"}]>} +## Simple support for cberl views" +Couchie.open(:beer, 10, 'cb.server.w.beer.sample:8091', 'beer-sample', 'beer-sample', '') +Couchie.query(:beer, 'beer', 'brewery_beers', [{:limit, 10}]) ##Current functionality - Basic commands: Set, Get, MGet, Delete diff --git a/lib/couchie.ex b/lib/couchie.ex index b379c06..7f708ca 100755 --- a/lib/couchie.ex +++ b/lib/couchie.ex @@ -38,10 +38,11 @@ defmodule Couchie do open(name, size, 'localhost:8091') end - def open(name, size, host) do - IO.puts "Opening #{name}, #{size}, #{host}" - :cberl.start_link(name, size, host) - end + def open(name, size, host) do + IO.puts "Opening #{name}, #{size}, #{host}" + :cberl.start_link(name, size, host) + IO.puts "Opened #{name}, #{size}, #{host}" + end def open(name, size, host, bucket) do # assume the bucket user and pass are the same as bucket name open(name, size, host, bucket, bucket, bucket) @@ -51,13 +52,10 @@ defmodule Couchie do open(name, size, host, bucket, bucket, password) end - def open(name, size, host, bucket, username, pass) do #currently usernames are set to bucket names in this interface. - IO.puts "Opening #{name}, #{size}, #{host}, #{username}, #{pass}, #{bucket} " - :cberl.start_link(name, size, host, username, pass, bucket) - end -#start_link(PoolName, NumCon, Host, Username, Password, BucketName) -> -#cberl:start_link(PoolName, NumCon, Host, Username, Password, BucketName, Transcoder) -> - + def open(name, size, host, bucket, username, pass) do #currently usernames are set to bucket names in this interface. + IO.puts "Opening #{name}, #{size}, #{host}, #{username}, #{pass}, #{bucket} " + :cberl.start_link(name, size, host, username, pass, bucket) + end @doc """ Shutdown the connection to a particular bucket @@ -87,7 +85,7 @@ defmodule Couchie do If you want the document to be purged after a period of time, use the Expiration. Set expiration to zero for permanent storage (or use set/3) - ## Example + ## Example Couchie.set(:default, "key", "document data", 0) """ @@ -127,6 +125,15 @@ defmodule Couchie do :cberl.remove(connection, key) end + @doc """ + Delete document. Key should be binary. + ## Example + + Couchie.delete(:connection, "test_key") + """ + def query(connection, doc, view, args) do + :cberl.view(connection, doc, view, args) + end end