-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from infinitered/31-stream-callback
[#31] Base Store behaviour on streams
- Loading branch information
Showing
7 changed files
with
57 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,36 @@ | ||
defmodule Elasticsearch.Store do | ||
@moduledoc """ | ||
A behaviour for fetching data to index. Used by `mix elasticsearch.build`. | ||
A behaviour for fetching data to index using a streaming strategy. | ||
""" | ||
|
||
@typedoc """ | ||
A data source. For example, `Post`, where `Post` is an `Ecto.Schema`. | ||
Each datum returned must implement `Elasticsearch.Document`. | ||
""" | ||
@type source :: any | ||
|
||
@typedoc """ | ||
Instances of the data source. For example, `%Post{}` structs. | ||
""" | ||
@type data :: any | ||
@doc """ | ||
Returns a stream of the given datasource. | ||
@typedoc """ | ||
The current offset for the query. | ||
""" | ||
@type offset :: integer | ||
## Example | ||
@typedoc """ | ||
A limit on the number of elements to return. | ||
def stream(Post) do | ||
Repo.stream(Post) | ||
end | ||
""" | ||
@type limit :: integer | ||
@callback stream(any) :: Stream.t() | ||
|
||
@doc """ | ||
Loads data based on the given source, offset, and limit. | ||
Returns a transaction wrapper to execute the stream returned by `stream/1` | ||
within. This is required when using Ecto. | ||
## Example | ||
def load(Post, offset, limit) do | ||
Post | ||
|> offset(^offset) | ||
|> limit(^limit) | ||
|> Repo.all() | ||
def transaction(fun) do | ||
{:ok, result} = Repo.transaction(fun, timeout: :infinity) | ||
result | ||
end | ||
If you are not using Ecto and do not require transactions, simply call the | ||
function passed as a parameter. | ||
def transaction(fun) do | ||
fun.() | ||
end | ||
""" | ||
@callback load(source, offset, limit) :: [data] | ||
@callback transaction(fun) :: any | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters