bucketier v0.1.4 Bucketier.Bucket
The Bucket is an Agent
holding the state of a Map
as its content.
Buckets are started automatically if not found in the Registry
.
The DynamicSupervisor
named Bucketier.BucketSupervisor
, started
in Bucketier.Application
, starts new Buckets and supervises them.
Link to this section Summary
Functions
Find a bucket with the given pid
or name
and return its state.
A new name
will instantiate a new Bucketier.Bucket
and returns an empty state
Returns a specification to start this module under a supervisor
Commit the state of the bucket
, thus the next request to bucket/1
will return this state
Drop an entry from a bucket’s data-struct
Drops all buckets! This function is used mainly from tests but there maybe some use-cases where you want to get rid of all data
Get an entry back from the bucket
Get all keys in a bucket
Put a new key/value-pair into a bucket-struct
start_link
is called by the Bucketier.BucketSupervisor
through
Update data set of a given entity
Get all values from a bucket
Link to this section Types
Link to this section Functions
Find a bucket with the given pid
or name
and return its state.
A new name
will instantiate a new Bucketier.Bucket
and returns an empty state.
Examples:
iex> Bucketier.Bucket.bucket(:shopping_list)
%Bucketier.Bucket{ name: :shopping_list, data: %{} }
If you pass a pid
instead of name
the function will return the
state of this bucket. If a Bucket with this pid does not exists, the function
will return {:error, :bucket_not_alive}
Examples:
iex> Bucketier.Bucket.bucket(:shopping_list)
iex> [{pid,_}] = Registry.lookup(Bucketier.Registry, :shopping_list)
iex> Bucketier.Bucket.bucket(pid)
%Bucketier.Bucket{ name: :shopping_list, data: %{} }
iex> Bucketier.Bucket.bucket(:shopping_list)
iex> [{pid,_}] = Registry.lookup(Bucketier.Registry, :shopping_list)
iex> Agent.stop(pid,:shutdown)
iex> Bucketier.Bucket.bucket(pid)
{ :error, :bucket_not_alive }
Returns a specification to start this module under a supervisor.
See Supervisor
.
Commit the state of the bucket
, thus the next request to bucket/1
will return this state.
Example:
iex> %Bucketier.Bucket{ name: "B1", data: %{} }
iex> |> Bucketier.Bucket.put( :some_key, "some value" )
iex> |> Bucketier.Bucket.commit
iex> Bucketier.Bucket.bucket("B1")
%Bucketier.Bucket{ name: "B1", data: %{ some_key: "some value" } }
Drop an entry from a bucket’s data-struct.
Note: This updates the struct im memory only. No Bucket on the server
will actually be uptdated! If you want your changes to be persistent,
please see: commit/1
.
Example:
iex> %Bucketier.Bucket{ name: "B1", data: %{ s1: "One", s2: "Two"} }
iex> |> Bucketier.Bucket.drop!(:s1)
%Bucketier.Bucket{data: %{s2: "Two"}, name: "B1"}
Drops all buckets! This function is used mainly from tests but there maybe some use-cases where you want to get rid of all data.
Get an entry back from the bucket.
Examples:
iex> Bucketier.Bucket.bucket("my list")
iex> |> Bucketier.Bucket.put( 1, "One" )
iex> |> Bucketier.Bucket.put( 2, "Two" )
iex> |> Bucketier.Bucket.put( 3, "Three" )
iex> |> Bucketier.Bucket.commit
iex> Bucketier.Bucket.get("my list", 2)
"Two"
iex> Bucketier.Bucket.bucket("my list")
iex> |> Bucketier.Bucket.put( 1, "One" )
iex> |> Bucketier.Bucket.put( 2, "Two" )
iex> |> Bucketier.Bucket.put( 3, "Three" )
iex> |> Bucketier.Bucket.commit
iex> Bucketier.Bucket.get("unknown bucket", :unknown_key)
{:error, :bucket_not_found}
iex> Bucketier.Bucket.bucket("my list")
iex> |> Bucketier.Bucket.put( 1, "One" )
iex> |> Bucketier.Bucket.put( 2, "Two" )
iex> |> Bucketier.Bucket.put( 3, "Three" )
iex> |> Bucketier.Bucket.commit
iex> Bucketier.Bucket.get("my list", :unknown_key)
{:error, :key_not_found}
Get all keys in a bucket.
Example:
iex> Bucketier.Bucket.bucket("my list")
iex> |> Bucketier.Bucket.put( 1, "One" )
iex> |> Bucketier.Bucket.put( 2, "Two" )
iex> |> Bucketier.Bucket.put( 3, "Three" )
iex> |> Bucketier.Bucket.commit
iex> Bucketier.Bucket.keys("my list")
[1,2,3]
iex> Bucketier.Bucket.keys("not here")
{:error, :bucket_not_found}
iex> Bucketier.Bucket.bucket("empty list")
iex> Bucketier.Bucket.values("empty list")
[]
Put a new key/value-pair into a bucket-struct.
Note: This updates the struct im memory only. No Bucket on the server
will actually be uptdated! If you want your changes to be persistent,
please see: commit/1
.
Example:
iex> %Bucketier.Bucket{ name: "B1", data: %{} }
iex> |> Bucketier.Bucket.put( :some_key, "some value" )
%Bucketier.Bucket{ name: "B1", data: %{ some_key: "some value" } }
iex> %Bucketier.Bucket{ name: "B1", data: %{} }
iex> |> Bucketier.Bucket.put( :some_key, "some value" )
iex> |> Bucketier.Bucket.commit
iex> Bucketier.Bucket.bucket("B1")
iex> |> Bucketier.Bucket.put( :some_key, "some updated value" )
iex> |> Bucketier.Bucket.commit
iex> Bucketier.Bucket.bucket("B1")
%Bucketier.Bucket{
data: %{some_key: "some updated value"},
name: "B1"
}
start_link
is called by the Bucketier.BucketSupervisor
through
DynamicSupervisor.start_child(Bucketier.BucketSupervisor, {Bucketier.Bucket, name})
You don’t have to call this start_link
on your own. Bucketier.Bucket.bucket/1
will
initiate the start of the new Bucket as a child of the supervisor.
Update data set of a given entity
Example:
iex> Bucketier.Bucket.bucket("my list")
iex> |> Bucketier.Bucket.put( 1, %{ value: "one"} )
iex> |> Bucketier.Bucket.commit()
iex> Bucketier.Bucket.bucket("my list")
iex> |> Bucketier.Bucket.update( 1, :additional_value, "one.one" )
iex> |> Bucketier.Bucket.commit()
iex> Bucketier.Bucket.bucket("my list")
%Bucketier.Bucket{
data: %{1 => %{additional_value: "one.one", value: "one"}},
name: "my list"
}
Get all values from a bucket.
Example:
iex> Bucketier.Bucket.bucket("my list")
iex> |> Bucketier.Bucket.put( 1, "One" )
iex> |> Bucketier.Bucket.put( 2, "Two" )
iex> |> Bucketier.Bucket.put( 3, "Three" )
iex> |> Bucketier.Bucket.commit
iex> Bucketier.Bucket.values("my list")
["One", "Two", "Three"]
iex> Bucketier.Bucket.values("not here")
{:error, :bucket_not_found}
iex> Bucketier.Bucket.bucket("empty list")
iex> Bucketier.Bucket.values("empty list")
[]