Skip to content

Commit

Permalink
refactor(annim): better cursor format
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Sep 4, 2024
1 parent 3cfc8bb commit 68e8940
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 40 deletions.
47 changes: 24 additions & 23 deletions Cargo.lock

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

11 changes: 3 additions & 8 deletions annim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ sea-orm-migration = { version = "1.0.0", features = [
anyhow.workspace = true
thiserror.workspace = true

dotenv = "0.15.0"
tracing = { version = "0.1.37" }
tracing-subscriber = { version = "0.3.17" }
lazy_static = { version = "1.4.0" }
serde_json = "1.0"
chrono = "0.4.38"
serde = { workspace = true, features = ["derive"] }
serde.workspace = true

# Search
tantivy = "0.22.0"
Expand All @@ -52,11 +50,8 @@ lindera-tantivy = { git = "https://github.com/ProjectAnni/lindera-tantivy", feat
"compress",
] }

[dependencies.seaography]
version = "~1.0.0"

[dev-dependencies]
serde_json = { version = "1.0.103" }
rmp-serde = "1.3.0"
base64 = "0.22.1"

[features]
default = ["postgres"]
Expand Down
10 changes: 8 additions & 2 deletions annim/Readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Annim

## Debug

```bash
export ANNIM_DATABASE_URL=postgres://postgres:password@localhost:5432/annim
export ANNIM_SEARCH_DIRECTORY=/tmp/tantivy
cargo run -p annim --release
```

## Installation

```bash
Expand All @@ -10,6 +18,4 @@ cargo install seaography-cli

```bash
sea-orm-cli generate entity --database-url 'sqlite:///tmp/annim.sqlite?mode=rwc'
# --seaography --model-extra-derives async_graphql::SimpleObject
seaography-cli -f=axum ./ src/entities 'sqlite:///tmp/annim.sqlite' annim
```
93 changes: 93 additions & 0 deletions annim/src/graphql/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use base64::{prelude::BASE64_STANDARD, write::EncoderStringWriter, Engine};
use sea_orm::sea_query::ValueTuple;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub(crate) struct Cursor(Vec<CursorValue>);

impl Cursor {
pub(crate) fn new(values: Vec<sea_orm::Value>) -> Self {
let values: Vec<_> = values.into_iter().map(Into::into).collect();
Cursor(values)
}

pub(crate) fn from_str(input: &str) -> anyhow::Result<Self> {
let bytes = BASE64_STANDARD.decode(input)?;
let cursor: Cursor = rmp_serde::from_slice(&bytes)?;
Ok(cursor)
}

pub(crate) fn to_string(&self) -> String {
let mut writer = EncoderStringWriter::new(&BASE64_STANDARD);
rmp_serde::encode::write(&mut writer, self).unwrap();
writer.into_inner()
}

pub(crate) fn into_value_tuple(self) -> ValueTuple {
ValueTuple::Many(self.into_inner())
}

fn into_inner(self) -> Vec<sea_orm::Value> {
self.0.into_iter().map(Into::into).collect()
}
}

#[derive(Serialize, Deserialize)]
pub(crate) enum CursorValue {
#[serde(rename = "t")]
/// t -> tiny
TinyInt(Option<i8>),
/// m -> mini
#[serde(rename = "m")]
SmallInt(Option<i16>),
/// n -> normal
#[serde(rename = "n")]
Int(Option<i32>),
/// b -> bigint
#[serde(rename = "b")]
BigInt(Option<i64>),
#[serde(rename = "T")]
TinyUnsigned(Option<u8>),
#[serde(rename = "M")]
SmallUnsigned(Option<u16>),
#[serde(rename = "N")]
Unsigned(Option<u32>),
#[serde(rename = "B")]
BigUnsigned(Option<u64>),
/// s -> string
#[serde(rename = "s")]
String(Option<Box<String>>),
}

impl From<sea_orm::Value> for CursorValue {
fn from(value: sea_orm::Value) -> Self {
match value {
sea_orm::Value::TinyInt(value) => CursorValue::TinyInt(value),
sea_orm::Value::SmallInt(value) => CursorValue::SmallInt(value),
sea_orm::Value::Int(value) => CursorValue::Int(value),
sea_orm::Value::BigInt(value) => CursorValue::BigInt(value),
sea_orm::Value::TinyUnsigned(value) => CursorValue::TinyUnsigned(value),
sea_orm::Value::SmallUnsigned(value) => CursorValue::SmallUnsigned(value),
sea_orm::Value::Unsigned(value) => CursorValue::Unsigned(value),
sea_orm::Value::BigUnsigned(value) => CursorValue::BigUnsigned(value),
sea_orm::Value::String(value) => CursorValue::String(value),
_ => panic!("Unsupported value type"),
}
}
}

impl From<CursorValue> for sea_orm::Value {
fn from(value: CursorValue) -> Self {
match value {
CursorValue::TinyInt(value) => sea_orm::Value::TinyInt(value),
CursorValue::SmallInt(value) => sea_orm::Value::SmallInt(value),
CursorValue::Int(value) => sea_orm::Value::Int(value),
CursorValue::BigInt(value) => sea_orm::Value::BigInt(value),
CursorValue::TinyUnsigned(value) => sea_orm::Value::TinyUnsigned(value),
CursorValue::SmallUnsigned(value) => sea_orm::Value::SmallUnsigned(value),
CursorValue::Unsigned(value) => sea_orm::Value::Unsigned(value),
CursorValue::BigUnsigned(value) => sea_orm::Value::BigUnsigned(value),
CursorValue::String(value) => sea_orm::Value::String(value),
}
}
}
13 changes: 6 additions & 7 deletions annim/src/graphql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod cursor;
mod input;
pub mod types;

Expand All @@ -8,14 +9,14 @@ use async_graphql::{
connection::{Connection, Edge},
Context, EmptySubscription, Object, Schema, ID,
};
use cursor::Cursor;
use input::{AlbumsBy, MetadataIDInput};
use sea_orm::{
prelude::Uuid,
sea_query::{IntoIden, ValueTuple},
ActiveModelTrait, ActiveValue, ColumnTrait, DatabaseConnection, EntityTrait, Identity,
ModelTrait, QueryFilter, QueryOrder, QuerySelect, Related, TransactionTrait,
};
use seaography::{decode_cursor, encode_cursor};
use tantivy::{
collector::TopDocs,
indexer::NoMergePolicy,
Expand Down Expand Up @@ -124,9 +125,8 @@ impl MetadataQuery {
}

if let Some(cursor) = after {
let values = decode_cursor(&cursor)?;
let cursor_values = ValueTuple::Many(values);
query.after(cursor_values);
let cursor = Cursor::from_str(&cursor)?;
query.after(cursor.into_value_tuple());
}

let mut data = query.first(limit + 1).all(db).await.unwrap();
Expand All @@ -145,9 +145,8 @@ impl MetadataQuery {
.map(|variant| model.get(variant.clone()))
.collect();

let cursor: String = encode_cursor(values);

Edge::new(cursor, AlbumInfo(model))
let cursor = Cursor::new(values);
Edge::new(cursor.to_string(), AlbumInfo(model))
})
.collect();

Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile.annim
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ FROM alpine

COPY annim /app/

VOLUME /app/data
ENTRYPOINT ["/app/annim"]
EXPOSE 8000/tcp

0 comments on commit 68e8940

Please sign in to comment.