Otto v0.1.2 Otto.Query View Source

Module to be used in Otto.Query should:

  1. use Otto.Table
  2. implement function schema(:type, field), you can simply use Ecto.Schema

Example

defmodule Example do
  use Otto.Table

  use Ecto.Schema
  import Ecto.Changeset
end

If you need to use Query module, if you need add filter when using get_row or get_range, you'd better import it. Because filter is a macro.

It implemented common actions of ots:

  • put_row
  • update_row
  • get_row
  • delete_row
  • get_range
  • batch_get
  • batch_write
  • search
  • search_iterate_all
  • start_local_transaction
  • commit_transaction
  • abort_transaction ...

For local transaction, it supports:

  • Read: get_row, get_range
  • Write: put_row, update_row, delete_row, batch_write A transaction's max ttl is 60s.

Link to this section Summary

Functions

abort_transaction can abort a started local transaction. It accepts a module name, a transaction id and opts. You can get a transaction id when you finished start_local_transaction

batch_get is used for getting multi get_row requests in a single time. If batch get success, it will return a map of responses. It accepts a list of requests, you can generate a single request with get function

batch_write is used for writing put, update, delete requests of multi-tables in a single time. It accepts a list of requests, each of which is a tuple of {module, requests}. You can generate requests of a module using write_put, write_update or write_delete

commit_transaction will commit transaction with the given id. It accepts a module name, a transaction_id and opts. You can get a transaction id when you finished start_local_transaction

Used in put_row, update_row and delete_row. contition/1 need one parameter, only 3 atoms are supported:

  • :ignore
  • :expect_exist
  • :expect_not_exist

Used in put_row, update_row and delete_row. condition/2 need two parameter:

  • the first is the existence, an atom in [:ignore, :expect_exist, :expect_not_exist]
  • the second is a filter_expr

delete_row accepts struct that contains all the primary keys. You can also pass some options to it

filter is a macro, you can pass a filter expression to it. It can be used in:

  • get_row, get_range
  • macro condition/2, work with row_existence

get is used for generating batch_get request. It accepts a module name, a map contains all the primary keys(Same as get_row). You can also pass options to it

get_range accepts a module name, and two maps contains primary keys. The second params is the start primary keys, the third is the end primary keys. If a primary key's value is infmax or infmin, you should use :__inf_max__ or `:__inf_min`

get_row accepts a module name, a map contains all the primary keys. You can also pass some options to it

put_row accepts a struct, all the not_null fields will be inserted. You can also pass some options to it

search accepts a module name, a index_name, and some options

search_iterate_all is the iterate version of search, which will continue search until next_token is nil. This function will ignore [:sort, :token, :limit] in your search_query option

start_local_transaction is use to create a local transaction. It accepts a module name, a partition key's value, and some options. A partition key is the first primary key of your table

update_row accepts a struct. The not_null fields will be inserted to the row. If you want to delete some fields, you should add them to the :delete_fields option

write_delete is used for generating batch_write request. It accepts a struct, all the not_null fields will be inserted. You can also pass some options to it

write_put is used for generating batch_write request. It accepts a struct, all the not_null fields will be inserted. You can also pass some options to it

write_update is used for generating batch_write request. It accepts a struct, all the not_null fields will be inserted. You can also pass some options to it

Link to this section Types

Link to this section Functions

Link to this function

abort_transaction(module, transaction_id, opts \\ []) View Source

abort_transaction can abort a started local transaction. It accepts a module name, a transaction id and opts. You can get a transaction id when you finished start_local_transaction.

Link to this function

batch_get(requests, opts \\ []) View Source
batch_get(list(), opts()) :: {:ok, any()} | {:error, any()}

batch_get is used for getting multi get_row requests in a single time. If batch get success, it will return a map of responses. It accepts a list of requests, you can generate a single request with get function.

Example:

batch_get([
  get(Module1, [%{pk11: "a1", pk12: "b1"}, %{pk11: "a2", pk12: "b2"}]),
  get(Module2, [%{pk21: "c1", pk22: "d1"}, %{pk21: "c2", pk22: "d2"}])
])
Link to this function

batch_write(requests, opts \\ []) View Source
batch_write(list(), opts()) :: {:ok, any()} | {:error, any()}

batch_write is used for writing put, update, delete requests of multi-tables in a single time. It accepts a list of requests, each of which is a tuple of {module, requests}. You can generate requests of a module using write_put, write_update or write_delete.

Example:

batch_write([
  {Module1, [
    write_put(%Module1{pk11: "a1", pk12: "b1", attr1: "attr1"}),
    write_put(%Module1{pk11: "a2", pk12: "b2", attr1: "attr2"}),
    write_update(%Module1{pk11: "a3", pk12: "b3", attr1: "attr3"}),
    write_delete(%Module1{pk11: "a4", pk12: "b4"})
  ]},
  {Module2, [
    write_put(%Module2{pk11: "a1", pk12: "b1", attr1: "attr1"}),
    write_put(%Module2{pk11: "a2", pk12: "b2", attr1: "attr2"}),
    write_update(%Module2{pk11: "a3", pk12: "b3", attr1: "attr3"}),
    write_delete(%Module2{pk11: "a4", pk12: "b4"})
  ]},
])
Link to this function

commit_transaction(module, transaction_id, opts \\ []) View Source

commit_transaction will commit transaction with the given id. It accepts a module name, a transaction_id and opts. You can get a transaction id when you finished start_local_transaction.

Link to this macro

condition(existence) View Source (macro)

Used in put_row, update_row and delete_row. contition/1 need one parameter, only 3 atoms are supported:

  • :ignore
  • :expect_exist
  • :expect_not_exist
Link to this macro

condition(existence, filter_expr) View Source (macro)

Used in put_row, update_row and delete_row. condition/2 need two parameter:

  • the first is the existence, an atom in [:ignore, :expect_exist, :expect_not_exist]
  • the second is a filter_expr.
Link to this function

delete_row(data, opts \\ []) View Source
delete_row(data(), opts()) :: {:ok, any()} | {:error, any()}

delete_row accepts struct that contains all the primary keys. You can also pass some options to it:

  • :condition - The macro condition/1 or condition/2
  • :return_type - an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
Link to this macro

filter(filter_expr) View Source (macro)

filter is a macro, you can pass a filter expression to it. It can be used in:

  • get_row, get_range
  • macro condition/2, work with row_existence.

Examples:

filter("age" >= 10)
filter("attr1" == "attr_value")
filter(("name[ignore_if_missing: true, latest_version_only: true]" == "hello" and "age" > 1) or ("class" == "1"))
Link to this function

get(module, datas, opts \\ []) View Source
get(module(), list(), opts()) :: {module(), String.t(), any()}

get is used for generating batch_get request. It accepts a module name, a map contains all the primary keys(Same as get_row). You can also pass options to it.

datas should be a list of data maps.

  • :filter - The macro filter/1
  • :return_type - an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
  • :columns_to_get - a string list of attrubutes you want to get.
  • :start_column - The start column string, used for widecolumn read.
  • :end_column - The end column string, used for widecolumn read.
Link to this function

get_range(module, start_pks_data, end_pks_data, opts \\ []) View Source
get_range(module(), map(), map(), opts()) ::
  {:ok, any(), any()} | {:error, any()}

get_range accepts a module name, and two maps contains primary keys. The second params is the start primary keys, the third is the end primary keys. If a primary key's value is infmax or infmin, you should use :__inf_max__ or `:__inf_min`.

If your rows are more than 5000, please use iterate_all_range.

You can also pass some options to it.

  • :direction - :forward or :backward.

  • :limit - Integer, the max rows to get.

  • :filter - The macro filter/1

  • :return_type - an atom in [:'RT_NONE', :none, :'RT_PK', :pk]

  • :columns_to_get - a string list of attrubutes you want to get.

  • :start_column - The start column string, used for widecolumn read.

  • :end_column - The end column string, used for widecolumn read.

  • :next_start_primary_key - A base64 string. If you added this, you don't need to give correct start_pks_data, it will be ignored.

  • iterate - If set true, will use execute_iterate_all_range instead of execute_get_range

Link to this function

get_row(module, data, opts \\ []) View Source
get_row(module(), map(), opts()) :: {:ok, any()} | {:error, any()}

get_row accepts a module name, a map contains all the primary keys. You can also pass some options to it.

  • :filter - The macro filter/1
  • :return_type - an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
  • :columns_to_get - a string list of attrubutes you want to get.
  • :start_column - The start column string, used for widecolumn read.
  • :end_column - The end column string, used for widecolumn read.
Link to this function

put_row(data, opts \\ []) View Source
put_row(data(), opts()) :: {:ok, any()} | {:error, any()}

put_row accepts a struct, all the not_null fields will be inserted. You can also pass some options to it:

  • :condition - The macro condition/1 or condition/2
  • :return_type - an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
Link to this function

search(module, index_name, opts \\ []) View Source
search(module(), atom(), opts()) ::
  {:ok, any(), any(), any()} | {:error, any()}
search(module(), atom(), opts()) ::
  {:ok, any(), any(), any()} | {:error, any()}

search accepts a module name, a index_name, and some options.

The full search options should can be found in ex_aliyun_ots.

If search result is ok, it will return the datas, a cursor, and total_hits number as a tuple {:ok, data, cursor, total}.

search limit is default by 10.

Link to this function

search_iterate_all(module, index_name, opts \\ []) View Source

search_iterate_all is the iterate version of search, which will continue search until next_token is nil. This function will ignore [:sort, :token, :limit] in your search_query option.

Link to this function

start_local_transaction(module, partition_key_value, opts \\ []) View Source

start_local_transaction is use to create a local transaction. It accepts a module name, a partition key's value, and some options. A partition key is the first primary key of your table.

Link to this function

update_row(data, opts \\ []) View Source
update_row(data(), opts()) :: {:ok, any()} | {:error, any()}

update_row accepts a struct. The not_null fields will be inserted to the row. If you want to delete some fields, you should add them to the :delete_fields option.

Option fields:

  • :condition - The macro condition/1 or condition/2
  • :return_type - an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
  • :delete_all - fields you want to delete when updating the row, a k-w list or string list.
Link to this function

write_delete(data, opts \\ []) View Source
write_delete(data(), opts()) :: any()

write_delete is used for generating batch_write request. It accepts a struct, all the not_null fields will be inserted. You can also pass some options to it:

  • :condition - The macro condition/1 or condition/2
  • :return_type - an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
Link to this function

write_put(data, opts \\ []) View Source
write_put(data(), opts()) :: any()

write_put is used for generating batch_write request. It accepts a struct, all the not_null fields will be inserted. You can also pass some options to it:

  • :condition - The macro condition/1 or condition/2
  • :return_type - an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
Link to this function

write_update(data, opts \\ []) View Source
write_update(data(), opts()) :: any()

write_update is used for generating batch_write request. It accepts a struct, all the not_null fields will be inserted. You can also pass some options to it:

  • :condition - The macro condition/1 or condition/2
  • :return_type - an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
  • :delete_all - fields you want to delete when updating the row, a k-w list or string list.