View Source FDBC.Subspace (fdbc v0.1.4)

Subspaces provide the recommended way to define namespaces for different categories of data. As a best practice, you should always use at least one subspace as a namespace for your application data.

An instance of the class stores a prefix used to identify the namespace and automatically prepends it when encoding tuples into keys. Likewise, it removes the prefix when decoding keys.

For example, suppose your application tracks profile data for your users. You could store the data in a user_space subspace that would make 'user' the first element of each tuple. A back-end function might have the form:

defmodule Example do
  @user_space FDBC.Subspace(["user"])

  def set_user_data(db_or_tr, key, value) do
    FDBC.transact(db_or_tr, fn tr ->
      FDBC.Transaction.set(tr, FDBC.Subspace.pack(@user_space, [key]), value)
    end)
  end
end

Summary

Functions

Returns a new subspace that is the concatination of two subspaces.

Returns true if the subspace is the logical prefix of the given key.

Creates a new subspace with the given prefix.

Returns a key encoding the specified tuple prefixed by the subspace.

Returns the start and stop keys in the form of a tuple pair representing all keys in the subspace that encode tuples strictly starting with the specifed tuple.

Returns the tuple that encoded into the given key with the subspace prefix removed.

Types

t()

@type t() :: %FDBC.Subspace{key: binary()}

Functions

concat(s1, s2)

@spec concat(t(), t() | [any()]) :: t()

Returns a new subspace that is the concatination of two subspaces.

The second subspace is appended to the first subspace.

contains?(subspace, key)

@spec contains?(t(), binary()) :: boolean()

Returns true if the subspace is the logical prefix of the given key.

new(prefix)

@spec new(binary() | [any()]) :: t()

Creates a new subspace with the given prefix.

If the prefix is a binary it is encoded raw, otherwise a tuple must be provided.

pack(subspace, tuple, opts \\ [])

@spec pack(t(), [any()], keyword()) :: binary()

Returns a key encoding the specified tuple prefixed by the subspace.

Options

  • :extension - a module that implements the behaviour FDBC.Tuple.Extension allowing the Tuple layer to be extended with user type codes.

  • :keyed - if present, then the tuple is returned as a keyword list, such as [{string: "flip"}, {string: "flop"}].

  • :versionstamp - if present, then the tuple must contain one incomplete versionstamp whose postition will be encoded on the end of the key to enable compatability with versionstamp operations; An ArgumentError will be raised if no incomplete versionstamp is found or if more than one is found. By default packing a tuple with an incomplete versionstamp will raise an ArgumentError.

range(subspace, tuple \\ [], opts \\ [])

@spec range(t(), [any()], keyword()) :: {binary(), binary()}

Returns the start and stop keys in the form of a tuple pair representing all keys in the subspace that encode tuples strictly starting with the specifed tuple.

unpack(subspace, key, opts \\ [])

Returns the tuple that encoded into the given key with the subspace prefix removed.

Raises ArgumentError if the key is not in the subspace.

Options

  • :extension - a module that implements the behaviour FDBC.Tuple.Extension allowing the Tuple layer to be extended with user type codes.

  • :keyed - if present, then the tuple is returned as a keyword list, such as [{string: "flip"}, {string: "flop"}].