Cassandrax.Keyspace behaviour (Cassandrax v0.4.0) View Source

Defines a Keyspace.

A keyspace acts as a repository, wrapping an underlying keyspace in CassandraDB.

Setup

test_conn_attrs = [ nodes: ["127.0.0.1:9043"], username: "cassandra", password: "cassandra" ]

child = Cassandrax.Supervisor.child_spec(Cassandrax.MyConn, test_conn_attrs) Cassandrax.start_link([child])

Defining a new keyspace module.

defmodule MyKeyspace do
  use Cassandrax.Keyspace, cluster: Cassandrax.MyConn, name: "my_keyspace"
end

Creating a keyspace.

statement = """
CREATE KEYSPACE IF NOT EXISTS my_keyspace
WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1}
"""

Cassandrax.cql(Cassandrax.MyConn, statement)

Creating a table in the Keyspace.

statement = [
  "CREATE TABLE IF NOT EXISTS ",
  "my_keyspace.user(",
  "id integer, ",
  "user_name text, ",
  "svalue set<text>, ",
  "PRIMARY KEY (id))"
]

{:ok, _result} = Cassandrax.cql(Cassandrax.MyConn, statement)

Link to this section Summary

Callbacks

Accesses the cluster that was setup in the runtime configuration.

Accesses the consistency level that manages availability versus data accuracy. Consistency level is configured for per individual read or write operation. Pass :read or :write to access the consistency level (eg. [consistency: :one]).

Accesses the name of the Keyspace.

Fetches all entries from the data store that matches the given query.

Runs batch queries.

Adds a DELETE query to the given batch.

Adds an INSERT query to the given batch.

Adds an UPDATE query to the given batch.

Runs plain CQL Statements.

Deletes a struct using its primary key.

Same as delete/2 but returns the struct or raises if the changeset is invalid.

Deletes all entries from the data store that matches the given query.

Example

MyKeyspace.get(User, 2)

A callback executed when the keyspace starts or when configuration is read.

Inserts a struct defined in Cassandrax.Schema or a changeset.

Same as insert/2 but returns the struct or raises if the changeset is invalid.

Fetches a single record from the query.

Streams all entries from the data store that matches the given query.

Updates a changeset using its primary key.

Same as update/2 but returns the struct or raises if the changeset is invalid.

Link to this section Callbacks

Specs

__conn__() :: Cassandrax.Connection

Accesses the cluster that was setup in the runtime configuration.

Example

Cassandrax.MyConn = MyKeyspace.__conn__()
Link to this callback

__default_options__(atom)

View Source

Specs

__default_options__(atom :: :read | :write) :: list() | nil

Accesses the consistency level that manages availability versus data accuracy. Consistency level is configured for per individual read or write operation. Pass :read or :write to access the consistency level (eg. [consistency: :one]).

Example

[consistency: :one] = MyKeyspace.__default_options__(:read)

Specs

__keyspace__() :: String.t()

Accesses the name of the Keyspace.

Example

"my_keyspace" = MyKeyspace.__keyspace__()

Specs

all(queryable :: Cassandrax.Queryable.t(), opts :: Keyword.t()) :: [
  Cassandrax.Schema.t()
]

Fetches all entries from the data store that matches the given query.

May raise Xandra.Error if query validation fails.

Example

query = where(User, id: 1)
MyKeyspace.all(query)

Specs

batch(opts :: Keyword.t(), (... -> any())) :: :ok | {:error, any()}

Runs batch queries.

Can be used to group and execute queries as Cassandra BATCH query.

Options

:logged is the default behavior in Cassandrax. Logged batch acts like a lightweight transaction around a batch operation. It enforces atomicity, and fails the batch if any of the queries fail. Cassandra doesn't enforce any other transactional properties at batch level.

:unlogged consider it when there are multiple inserts and updates for the same partition key. Unlogged batching will give a warning if too many operations or too many partitions are involved.

Read the CassandraDB documents for more information logged and unlogged batch operations.

Example

user = MyKeyspace.get(User, id: 1)
changeset = Ecto.Changeset.change(user, user_name: "trent")

MyKeyspace.batch(fn batch ->
  batch
  |> MyKeyspace.batch_insert(%User{id: 3, user_name: "eve"})
  |> MyKeyspace.batch_insert(%User{id: 4, user_name: "mallory"})
  |> MyKeyspace.batch_update(changeset)
  |> MyKeyspace.batch_delete(user)
end)
Link to this callback

batch_delete(batch, arg2)

View Source

Specs

batch_delete(batch :: Cassandrax.Keyspace.Batch.t(), Cassandrax.Schema.t()) ::
  :ok

Adds a DELETE query to the given batch.

Link to this callback

batch_insert(batch, arg2)

View Source

Specs

batch_insert(batch :: Cassandrax.Keyspace.Batch.t(), Cassandrax.Schema.t()) ::
  :ok

Adds an INSERT query to the given batch.

Link to this callback

batch_update(batch, arg2)

View Source

Specs

batch_update(batch :: Cassandrax.Keyspace.Batch.t(), Cassandrax.Schema.t()) ::
  :ok

Adds an UPDATE query to the given batch.

Link to this callback

cql(statement, values, opts)

View Source

Specs

cql(statement :: String.t() | list(), values :: list(), opts :: Keyword.t()) ::
  {:ok, map()} | {:error, map()}

Runs plain CQL Statements.

Returns {:ok, map} if the CQL is successfully run or {:error, message} if there was a validation or a known constraint error.

Example

statement = """
SELECT * my_keyspace.user
"""

Cassandrax.cql(MyConn, statement)
Link to this callback

delete( struct_or_changeset, opts )

View Source

Specs

delete(
  struct_or_changeset :: Ecto.Schema.t() | Ecto.Changeset.t(),
  opts :: Keyword.t()
) :: {:ok, Cassandrax.Schema.t()} | {:error, any()}

Deletes a struct using its primary key.

If the struct has no primary key, Xandra.Error will be raised.

If the struct has been removed from db prior to call, it will still return {:ok, Cassandrax.Schema.t()}

It returns {:ok, struct} if the struct has been successfully deleted or {:error, message} if there was a validation or a known constraint error.

Example

MyKeyspace.delete(%User(id: 1, user_name: "bob"))
Link to this callback

delete!( struct_or_changeset, opts )

View Source

Specs

delete!(
  struct_or_changeset :: Ecto.Schema.t() | Ecto.Changeset.t(),
  opts :: Keyword.t()
) :: Cassandrax.Schema.t()

Same as delete/2 but returns the struct or raises if the changeset is invalid.

Link to this callback

delete_all(queryable, opts)

View Source

Specs

delete_all(queryable :: Cassandrax.Queryable.t(), opts :: Keyword.t()) ::
  :ok | {:error, any()}

Deletes all entries from the data store that matches the given query.

May raise Xandra.Error if query validation fails.

Example

query = where(User, status: "deactivated")
MyKeyspace.delete_all(query)
Link to this callback

get(queryable, id, opts)

View Source

Specs

get(queryable :: Cassandrax.Queryable.t(), id :: term(), opts :: Keyword.t()) ::
  Cassandrax.Schema.t() | nil

Example

MyKeyspace.get(User, 2)
Link to this callback

init(context, config)

View Source (optional)

Specs

init(context :: :supervisor | :runtime, config :: Keyword.t()) ::
  {:ok, Keyword.t()} | :ignore

A callback executed when the keyspace starts or when configuration is read.

The first argument is the context the callback is being invoked. If it is called because the Keyspace supervisor is starting, it will be :supervisor. It will be :runtime if it is called for reading configuration without actually starting a process.

The second argument is the keyspace configuration as stored in the application environment. It must return {:ok, keyword} with the updated list of configuration or :ignore (only in the :supervisor case).

Link to this callback

insert( struct_or_changeset, opts )

View Source

Specs

insert(
  struct_or_changeset :: Ecto.Changeset.t() | Cassandrax.Schema,
  opts :: Keyword.t()
) :: {:ok, Cassandrax.Schema.t()} | {:error, any()}

Inserts a struct defined in Cassandrax.Schema or a changeset.

If a struct is given, the struct is converted into a changeset with all non-nil fields.

Example

{:ok, user} = MyKeyspace.insert(%User{id: 1, user_name: "bob"})
Link to this callback

insert!( struct_or_changeset, opts )

View Source

Specs

insert!(
  struct_or_changeset :: Ecto.Changeset.t() | Cassandrax.Schema,
  opts :: Keyword.t()
) :: Cassandrax.Schema.t()

Same as insert/2 but returns the struct or raises if the changeset is invalid.

Specs

one(queryable :: Cassandrax.Queryable.t(), opts :: Keyword.t()) ::
  Cassandrax.Schema.t() | nil

Fetches a single record from the query.

Returns nil if no records were found. May raise Cassandrax.MultipleResultsError, if query returns more than one entry.

Example

query = where(User, id: 1)
MyKeyspace.one(query)

Specs

stream(queryable :: Cassandrax.Queryable.t(), opts :: Keyword.t()) :: [
  Cassandrax.Schema.t()
]

Streams all entries from the data store that matches the given query.

May raise Xandra.Error if query validation fails.

Example

query = where(User, id: 1)
MyKeyspace.stream(query)

Specs

update(changeset :: Ecto.Changeset.t(), opts :: Keyword.t()) ::
  {:ok, Cassandrax.Schema.t()} | {:error, any()}

Updates a changeset using its primary key.

Requires a changeset as it is the only way to track changes.

If the struct has no primary key, Xandra.Error will be raised. In CassandraDB, UPDATE is also an upsert. If the struct cannot be found, a new entry will be created.

It returns {:ok, struct} if the struct has been successfully updated or {:error, message} if there was a validation or a known constraint error.

Example

user = MyKeyspace.get(User, 1)
changeset = Ecto.Changeset.change(user, user_name: "tom")
MyKeyspace.update(changeset)
Link to this callback

update!(changeset, opts)

View Source

Specs

update!(changeset :: Ecto.Changeset.t(), opts :: Keyword.t()) ::
  Cassandrax.Schema.t()

Same as update/2 but returns the struct or raises if the changeset is invalid.