Skip to content

Commit

Permalink
Triton 0.1.10 - rename stream to streamed
Browse files Browse the repository at this point in the history
  • Loading branch information
weixiyen committed Mar 23, 2018
1 parent a04832a commit 27e6d6e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,40 @@ UserByEmail
|> User.one
```

## Comparison / Range Queries

Select messages created before **timestamp**

```elixir
MessagesByDate
|> select([:message_id, :text])
|> where(channel_id: 1, created: ["<=": timestamp])
|> limit(20)
|> MessagesByDate.all
```

Select messages created between **timestamp_a** and **timestamp_b**

```elixir
MessagesByDate
|> select([:message_id, :text])
|> where(channel_id: 1, created: [">=": timestamp_a], created: [<: timestamp_b])
|> MessagesByDate.all
```

## Streaming

Stream all messages

```elixir
MessagesByDate
|> select(:all)
|> where(channel_id: 1)
|> MessagesByDate.stream(page_size: 20)
```

Which returns {:ok, stream} or {:error, msg}

## Inserting, Updating, & Deleting

Again, lets import Triton.Query for the necessary macros.
Expand Down Expand Up @@ -319,3 +353,40 @@ User
|> where(user_id: 10)
|> User.save
```

## Pre-populating data

You can pre-populate data with Triton at compile time with **Triton.Setup**

```elixir
defmodule PrepopulateModule do
use Triton.Setup
import Triton.Query
require Schema.User
alias Schema.User

# create an admin user if it doesn't exist

setup do
User
|> insert(
user_id: @admin_user_id,
username: @admin_user_username,
display_name: @admin_user_display_name,
password: Bcrypt.hashpwsalt(@admin_user_password),
email: @admin_user_email,
created: @admin_user_created
) |> if_not_exists
end
end
```

## Automatic Schema Creation

Triton attempts to create your keyspace, tables, and materialized views at compile time if they do not exist.

This means that your build server will need access to your production DB if you want to automatically create your schema in prod. The alternative is simply to create your production schemas yourself.

## Consistency levels

For dev, you may want to consider running ccm with more than 1 node if you are doing queries at anything more than consistency: :one.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v 0.1.10

**Breaking** - Table.streamed renamed to Table.stream

Added more documentation for range queries and streaming to readme.

## v 0.1.9

Change supervision tree so that clusters cannot interrupt each other.
Expand Down
10 changes: 5 additions & 5 deletions lib/triton/executor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ defmodule Triton.Executor do
end
end

def streamed(query, options \\ []) do
case Triton.Executor.execute([{:streamed, true} | query], options) do
def stream(query, options \\ []) do
case Triton.Executor.execute([{:stream, true} | query], options) do
{:error, err} -> {:error, err.message}
{:ok, results} -> {:ok, transform_results(query, results)}
end
Expand Down Expand Up @@ -101,7 +101,7 @@ defmodule Triton.Executor do

defp build_cql(query) do
case Triton.Helper.query_type(query) do
:streamed -> {:ok, :streamed, Triton.CQL.Select.build(query)}
:stream -> {:ok, :stream, Triton.CQL.Select.build(query)}
:count -> {:ok, :count, Triton.CQL.Select.build(query)}
:select -> {:ok, :select, Triton.CQL.Select.build(query)}
:insert -> {:ok, :insert, Triton.CQL.Insert.build(query)}
Expand All @@ -111,14 +111,14 @@ defmodule Triton.Executor do
end
end

defp execute_cql(conn, :streamed, cql, nil, options) do
defp execute_cql(conn, :stream, cql, nil, options) do
with pages <- Xandra.stream_pages!(conn, cql, [], [pool: Xandra.Cluster] ++ options) do
results = pages
|> Stream.flat_map(fn page -> Enum.to_list(page) |> format_results end)
{:ok, results}
end
end
defp execute_cql(conn, :streamed, cql, prepared, options) do
defp execute_cql(conn, :stream, cql, prepared, options) do
with {:ok, statement} <- Xandra.prepare(conn, cql, [pool: Xandra.Cluster] ++ options),
pages <- Xandra.stream_pages!(conn, statement, atom_to_string_keys(prepared), [pool: Xandra.Cluster] ++ options)
do
Expand Down
2 changes: 1 addition & 1 deletion lib/triton/helper.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Triton.Helper do
def query_type(query) do
cond do
query[:streamed] -> :streamed
query[:stream] -> :stream
query[:count] -> :count
query[:select] -> :select
query[:insert] -> :insert
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Triton.Mixfile do
use Mix.Project

@version "0.1.9"
@version "0.1.10"
@url "https://github.com/blitzstudios/triton"
@maintainers ["Weixi Yen"]

Expand Down

0 comments on commit 27e6d6e

Please sign in to comment.