Otto v0.1.2 Otto.Query View Source
Module to be used in Otto.Query should:
- use Otto.Table
- 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
data()
View Source
data() :: struct()
data() :: struct()
opts()
View Source
opts() :: keyword()
opts() :: keyword()
Link to this section Functions
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
.
batch_get(requests, opts \\ []) View Source
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"}])
])
batch_write(requests, opts \\ []) View Source
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"})
]},
])
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
.
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
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.
delete_row(data, opts \\ []) View Source
delete_row
accepts struct that contains all the primary keys.
You can also pass some options to it:
:condition
- The macrocondition/1
orcondition/2
:return_type
- an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
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"))
get(module, datas, opts \\ []) View Source
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 macrofilter/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.
get_range(module, start_pks_data, end_pks_data, opts \\ []) View Source
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 macrofilter/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
get_row(module, data, opts \\ []) View Source
get_row
accepts a module name, a map contains all the primary keys. You can
also pass some options to it.
:filter
- The macrofilter/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.
put_row(data, opts \\ []) View Source
put_row
accepts a struct, all the not_null fields will be inserted.
You can also pass some options to it:
:condition
- The macrocondition/1
orcondition/2
:return_type
- an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
search(module, index_name, opts \\ []) View Source
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.
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.
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.
update_row(data, opts \\ []) View Source
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 macrocondition/1
orcondition/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.
write_delete(data, opts \\ []) View Source
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 macrocondition/1
orcondition/2
:return_type
- an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
write_put(data, opts \\ []) View Source
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 macrocondition/1
orcondition/2
:return_type
- an atom in [:'RT_NONE', :none, :'RT_PK', :pk]
write_update(data, opts \\ []) View Source
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 macrocondition/1
orcondition/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.