Skip to content

Commit

Permalink
General fixes and enhancements.
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Dec 4, 2016
1 parent ed52d33 commit e82ca93
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 53 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Add `kvx` to your Mix dependencies:

```elixir
defp deps do
[{:kvx, "~> 0.1.1"}]
[{:kvx, "~> 0.1.2"}]
end
```

Expand Down Expand Up @@ -240,7 +240,7 @@ defmodule MyApp.CacheableRepo do
{:ok, schema} = rs ->
schema
|> bucket
|> Cache.del(schema.id)
|> Cache.delete(schema.id)
rs
error ->
error
Expand Down
1 change: 0 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use Mix.Config

# KVX config
config :kvx,
adapter: KVX.Bucket.Shards,
ttl: 1,
buckets: [
mybucket: [
Expand Down
30 changes: 15 additions & 15 deletions lib/kvx/adapters/shards/bucket_shards.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule KVX.Bucket.Shards do
@behaviour KVX.Bucket

@shards (Application.get_env(:kvx, :shards_mod, :shards))
@default_ttl (Application.get_env(:kvx, :ttl, 3600))
@default_ttl (Application.get_env(:kvx, :ttl, :infinity))

require Ex2ms

Expand Down Expand Up @@ -75,15 +75,12 @@ defmodule KVX.Bucket.Shards do
end

def set(bucket, key, value, ttl \\ @default_ttl) do
ttl = if is_integer(ttl) do
seconds_since_epoch(ttl)
end
true = @shards.insert(bucket, {key, value, ttl})
true = @shards.insert(bucket, {key, value, seconds_since_epoch(ttl)})
bucket
end

def mset(bucket, kv_pairs, ttl \\ @default_ttl) when is_list(kv_pairs) do
kv_pairs |> Enum.each(fn({key, value}) ->
def mset(bucket, entries, ttl \\ @default_ttl) when is_list(entries) do
entries |> Enum.each(fn({key, value}) ->
^bucket = set(bucket, key, value, ttl)
end)
bucket
Expand All @@ -94,12 +91,11 @@ defmodule KVX.Bucket.Shards do
def get(bucket, key) do
case @shards.lookup(bucket, key) do
[{^key, value, ttl}] ->
case ttl > seconds_since_epoch(0) do
true ->
value
_ ->
true = @shards.delete(bucket, key)
nil
if ttl > seconds_since_epoch(0) do
value
else
true = @shards.delete(bucket, key)
nil
end
_ ->
nil
Expand All @@ -117,7 +113,7 @@ defmodule KVX.Bucket.Shards do
end

defp do_find_all(bucket, nil) do
find_all(bucket, Ex2ms.fun do object -> object end)
do_find_all(bucket, Ex2ms.fun do object -> object end)
end
defp do_find_all(bucket, query) do
bucket
Expand Down Expand Up @@ -154,10 +150,14 @@ defmodule KVX.Bucket.Shards do

def __shards_mod__, do: @shards

def __default_ttl__, do: @default_ttl

## Private functions

defp seconds_since_epoch(diff) do
defp seconds_since_epoch(diff) when is_integer(diff) do
{mega, secs, _} = :os.timestamp()
mega * 1000000 + secs + diff
end
defp seconds_since_epoch(:infinity), do: :infinity
defp seconds_since_epoch(diff), do: raise ArgumentError, "ttl #{inspect diff} is invalid."
end
6 changes: 3 additions & 3 deletions lib/kvx/bucket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ defmodule KVX.Bucket do
@type bucket :: atom
@type key :: term
@type value :: term
@type ttl :: integer
@type ttl :: integer | :infinity

@doc false
defmacro __using__(_opts) do
quote do
@behaviour KVX.Bucket

@adapter (Application.get_env(:exshards, :adapter, KVX.Bucket.Shards))
@default_ttl (Application.get_env(:kvx, :ttl, 3600))
@adapter (Application.get_env(:kvx, :adapter, KVX.Bucket.Shards))
@default_ttl (Application.get_env(:kvx, :ttl, :infinity))

def __adapter__ do
@adapter
Expand Down
17 changes: 0 additions & 17 deletions lib/kvx/exceptions.ex
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
defmodule KVX.BucketError do
@moduledoc """
Raised at runtime when the bucket is invalid.
"""
defexception [:message]

def exception(opts) do
bucket = Keyword.fetch!(opts, :bucket)

msg = """
Unexisting bucket #{bucket}.
"""

%__MODULE__{message: msg}
end
end

defmodule KVX.ConflictError do
defexception [:message]

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule KVX.Mixfile do

def project do
[app: :kvx,
version: "0.1.1",
version: "0.1.2",
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand Down
26 changes: 13 additions & 13 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
%{"certifi": {:hex, :certifi, "0.4.0", "a7966efb868b179023618d29a407548f70c52466bf1849b9e8ebd0e34b7ea11f", [:rebar3], []},
"earmark": {:hex, :earmark, "1.0.1", "2c2cd903bfdc3de3f189bd9a8d4569a075b88a8981ded9a0d95672f6e2b63141", [:mix], []},
"ex2ms": {:hex, :ex2ms, "1.4.0", "e43b410888b45ba363ea6650db3736db3e455a0a412ec244ac633fede857bcb2", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.13.0", "aa2f8fe4c6136a2f7cfc0a7e06805f82530e91df00e2bff4b4362002b43ada65", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"excoveralls": {:hex, :excoveralls, "0.5.6", "35a903f6f78619ee7f951448dddfbef094b3a0d8581657afaf66465bc930468e", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.0", "7136cc739ace295fc74c378f33699e5145bead4fdc1b4799822d0287489136fb", [:mix], [{:jsx, "~> 2.6.2", [hex: :jsx, optional: false]}]},
"hackney": {:hex, :hackney, "1.6.1", "ddd22d42db2b50e6a155439c8811b8f6df61a4395de10509714ad2751c6da817", [:rebar3], [{:certifi, "0.4.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.0", [hex: :ssl_verify_fun, optional: false]}]},
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
"jsx": {:hex, :jsx, "2.6.2", "213721e058da0587a4bce3cc8a00ff6684ced229c8f9223245c6ff2c88fbaa5a", [:mix, :rebar], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"shards": {:hex, :shards, "0.3.1", "e8a116641d517bcf57a1aeba900dff5dfd39bcad4fbe2f213f76c45792dde9a5", [:rebar3, :make], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.0", "edee20847c42e379bf91261db474ffbe373f8acb56e9079acb6038d4e0bf414f", [:rebar, :make], []}}
%{"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []},
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
"ex2ms": {:hex, :ex2ms, "1.4.0", "e43b410888b45ba363ea6650db3736db3e455a0a412ec244ac633fede857bcb2", [], []},
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"excoveralls": {:hex, :excoveralls, "0.5.7", "5d26e4a7cdf08294217594a1b0643636accc2ad30e984d62f1d166f70629ff50", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
"hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [:mix, :rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [], []},
"jsx": {:hex, :jsx, "2.8.0", "749bec6d205c694ae1786d62cea6cc45a390437e24835fd16d12d74f07097727", [:mix, :rebar], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [], []},
"shards": {:hex, :shards, "0.3.1", "e8a116641d517bcf57a1aeba900dff5dfd39bcad4fbe2f213f76c45792dde9a5", [], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}}
5 changes: 4 additions & 1 deletion test/bucket_shards_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ defmodule KVX.Bucket.ShardsTest do
|> find_all
end
end
:ok
end

test "default config" do
Expand Down Expand Up @@ -83,6 +82,10 @@ defmodule KVX.Bucket.ShardsTest do
end

test "ttl test" do
assert_raise ArgumentError, fn ->
set(@bucket, :k1, 1, :foo)
end

@bucket
|> mset([k1: 1, k2: 2, k3: 3], 2)
|> set(:k4, 4, 3)
Expand Down

0 comments on commit e82ca93

Please sign in to comment.