Skip to content

Commit

Permalink
feat, test, refactor: whole lotta crap
Browse files Browse the repository at this point in the history
- use docs.rs/darling for the entity macro, because its just so much easier
- setup the example running in ci
- improve the parser a wee bit
  • Loading branch information
Fyko committed Oct 9, 2023
1 parent df8f06b commit 621a33a
Show file tree
Hide file tree
Showing 37 changed files with 737 additions and 862 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ jobs:
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov

- name: Setup 3-node Scylla cluster
run: |
sudo sh -c "echo 2097152 >> /proc/sys/fs/aio-max-nr"
docker compose -f docker-compose.ci.yml up -d --wait
- name: Run Formatter
run: cargo make format-ci

Expand All @@ -62,3 +67,64 @@ jobs:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
files: lcov.info

- name: Stop the cluster
if: ${{ always() }}
run: docker compose -f docker-compose.ci.yml stop

- name: Print the cluster logs
if: ${{ always() }}
run: docker compose -f docker-compose.ci.yml logs

- name: Remove cluster
run: docker compose -f docker-compose.ci.yml down

example:
name: Run the example
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Register Problem Matchers
uses: r7kamura/rust-problem-matchers@v1

- run: rustup toolchain install stable --profile minimal

- name: Rust Cache
uses: Swatinem/rust-cache@v2

- name: Setup Rust
uses: dtolnay/rust-toolchain@stable

- name: Setup 3-node Scylla cluster
run: |
sudo sh -c "echo 2097152 >> /proc/sys/fs/aio-max-nr"
docker compose -f ./test/docker-compose.yml up -d --wait
- name: Run Migrations
env:
RUST_LOG: debug
SCYLLA_NODES: 172.42.0.2,172.42.0.3,172.42.0.4
run: cargo run -p scyllax-cli -- migrate run

- name: Run Example
env:
RUST_LOG: debug
SCYLLA_NODES: 172.42.0.2,172.42.0.3,172.42.0.4
SCYLLA_DEFAULT_KEYSPACE: scyllax
run: cargo run -p example

- name: Run Unit Tests
run: cargo make cov-ci

- name: Stop the cluster
if: ${{ always() }}
run: docker compose -f ./test/docker-compose.yml stop

- name: Print the cluster logs
if: ${{ always() }}
run: docker compose -f ./test/docker-compose.yml logs

- name: Remove cluster
run: docker compose -f ./test/docker-compose.yml down
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ A SQLx and Discord inspired query system for Scylla.
### 1. Model definition
Before you can write any queries, you have to define a model.
```rust,ignore
#[derive(Clone, Debug, FromRow, PartialEq, ValueList, Entity)]
#[entity]
pub struct PersonEntity {
#[pk]
#[entity(primary_key)]
pub id: uuid::Uuid,
pub email: String,
pub created_at: i64,
}
```

### 2. Read queries
With the [`read_query`] attribute, it's easy to define select queries.
```rust,ignore
Expand All @@ -28,13 +29,14 @@ pub struct GetPersonById {
pub id: Uuid,
}
```

### 3. Upsert queries
With the [`upsert_query`] attribute, it's easy to define upsert queries.
```rust,ignore
#[entity]
#[upsert_query(table = "person", name = UpsertPerson)]
#[derive(Clone, Debug, FromRow, PartialEq, ValueList, Entity)]
pub struct PersonEntity {
#[pk]
#[entity(primary_key)]
pub id: uuid::Uuid,
pub email: String,
pub created_at: i64,
Expand Down
4 changes: 2 additions & 2 deletions book/src/creating_entities/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Creating a new entity is super simple. Simply use the [`scyllax::Entity`](https:
#
#[derive(Clone, Debug, PartialEq, Entity, FromRow, ValueList)]
pub struct PersonEntity {
#[pk]
#[entity(pk)]
pub id: uuid::Uuid,
pub email: String,
pub created_at: i64,
}
```
Since `id` is a partition key, it must be annotated with `#[pk]`.
Since `id` is a primary key, it must be annotated with `#[entity(pk)]`.
**Clustering columns must be treated the same way**.

This is so that, when eventually using the `upsert_query` macro, scyllax will use the column in the where clause rather than the set clause.
Expand Down
4 changes: 2 additions & 2 deletions book/src/creating_entities/camelcase_columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ If you have some column names you can't change to work with the Rust naming conv
#
#[entity]
pub struct PersonEntity {
#[pk]
#[entity(pk)]
pub id: uuid::Uuid,
pub email: String,
#[rename = "createdAt"]
#[entity(rename = "createdAt")]
pub created_at: i64,
}
```
10 changes: 5 additions & 5 deletions book/src/creating_entities/counter_columns.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Counter columns
If you have a table that uses scylla's [`counter`](https://opensource.docs.scylladb.com/stable/cql/types.html#counters) type, you can use the `#[counter]` attribute macro on an entity column along with using the [`scylla::frame::value::Counter`](https://docs.rs/scylla/latest/scylla/frame/value/struct.Counter.html) type.
If you have a table that uses scylla's [`counter`](https://opensource.docs.scylladb.com/stable/cql/types.html#counters) type, you can use the `#[entity(counter)]` attribute macro on an entity column along with using the [`scylla::frame::value::Counter`](https://docs.rs/scylla/latest/scylla/frame/value/struct.Counter.html) type.

```rust
#use scyllax::prelude::*;
#
#[entity]
pub struct PersonLoginEntity {
#[pk]
#[entity(pk)]
pub id: uuid::Uuid,
#[pk]
#[entity(pk)]
pub person_id: uuid::Uuid,
#[counter]
#[entity(counter)]
pub count: scylla::frame::value::Counter,
}
```

Similarly to `#[pk]`, the `#[counter]` attribute also tells the upsert macro how to use the column in the query.
Similarly to `#[entity(pk)]`, the `#[entity(counter)]` attribute also tells the upsert macro how to use the column in the query.
8 changes: 4 additions & 4 deletions book/src/creating_entities/json_columns.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# JSON columns
If you want something similar to postgres's [`json`](https://www.postgresql.org/docs/current/datatype-json.html) type, you can use the `#[json]` attribute macro on a struct.
If you want something similar to postgres's [`json`](https://www.postgresql.org/docs/current/datatype-json.html) type, you can use the `#[json_data]` attribute macro on a struct.

```rust
#use scyllax::prelude::*;
Expand All @@ -14,13 +14,13 @@ pub struct PersonData {

#[entity]
pub struct PersonEntity {
#[pk]
#[entity(pk)]
pub id: uuid::Uuid,
pub email: String,
pub data: Option<PersonData>
#[rename = "createdAt"]
#[entity(rename = "createdAt")]
pub created_at: i64,
}
```

`json_data` uses serde `Deserialize` and `Serialize` under the hood, so you're welcome to use any of their macros.
`json_data` uses serde `Deserialize` and `Serialize` under the hood, so you're welcome to use any Serde macro attributes.
4 changes: 2 additions & 2 deletions book/src/delete_queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Simply create a struct with the fields you want to select, and annotate it with
#
#\#[entity]
#pub struct PersonEntity {
# #[pk]
# #[entity(pk)]
# pub id: uuid::Uuid,
# pub email: String,
# #[rename = "createdAt"]
# #[entity(rename = "createdAt")]
# pub created_at: i64,
#}
#
Expand Down
2 changes: 1 addition & 1 deletion book/src/example/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Example
Are you looking for a real-life example of scyllax? You're in luck! The `exmaple` directory in our GitHub repository contains a full example of scyllax in action -- it's actually used to run tests!
Are you looking for a real-life example of scyllax? You're in luck! The `example` directory in our GitHub repository contains a full example of scyllax in action -- it's actually used to run tests!

[https://github.com/trufflehq/scyllax/tree/main/example](https://github.com/trufflehq/scyllax/tree/main/example)
4 changes: 2 additions & 2 deletions book/src/select_queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Simply create a struct with the fields you want to select, and annotate it with
#
#\#[entity]
#pub struct PersonEntity {
# #[pk]
# #[entity(pk)]
# pub id: uuid::Uuid,
# pub email: String,
# #[rename = "createdAt"]
# #[entity(rename = "createdAt")]
# pub created_at: i64,
#}
#
Expand Down
4 changes: 2 additions & 2 deletions book/src/upsert_queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Now listen up. Upsert queries with scyllax are special. You don't have to hand w
#[entity]
#[upsert_query(table = "person", name = UpsertPerson)]
pub struct PersonEntity {
#[pk]
#[entity(pk)]
pub id: uuid::Uuid,
pub email: String,
#[rename = "createdAt"]
#[entity(rename = "createdAt")]
pub created_at: i64,
}
```
Expand Down
6 changes: 5 additions & 1 deletion example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ uuid.workspace = true
default = ["integration"]
integration = []


[dev-dependencies]
pretty_assertions = "1"
criterion = { version = "0.5", features = ["html_reports", "async_tokio"] }

[[bench]]
name = "bench"
harness = false
44 changes: 44 additions & 0 deletions example/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! benches
use example::entities::person::{self, queries::PersonQueries};
use scyllax::prelude::{create_session, Executor};
use std::sync::Arc;
use tracing_subscriber::prelude::*;

async fn test_select(executor: Arc<Executor<PersonQueries>>) {
let query = person::queries::GetPersonByEmail {
email: "[email protected]".to_string(),
};

let _ = executor
.execute_read(&query)
.await
.expect("person not found");
}

const RUNS: usize = 100_000;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(tracing_subscriber::fmt::layer())
.init();

let known_nodes = std::env::var("SCYLLA_NODES").unwrap_or_else(|_| String::new());
let known_nodes = known_nodes.split(',').collect::<Vec<_>>();
let default_keyspace = std::env::var("SCYLLA_DEFAULT_KEYSPACE").ok();

let session = create_session(known_nodes, default_keyspace).await?;
let executor = Arc::new(Executor::<PersonQueries>::new(session).await?);

let start = std::time::Instant::now();
for _ in 0..RUNS {
test_select(executor.clone()).await;
}
let end = std::time::Instant::now();

println!("elapsed: {:#?}", end - start);
println!("per run: {:?}", (end - start) / RUNS as u32);

Ok(())
}
Loading

0 comments on commit 621a33a

Please sign in to comment.