Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move generated google APIs to seperate crate #12

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "googleapis"]
path = googleapis
path = crates/googleapis/include
url = https://github.com/googleapis/googleapis.git
19 changes: 14 additions & 5 deletions Cargo.lock

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

16 changes: 9 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[workspace]

[workspace.dependencies]
tokio = { version = "1.36.0" }
tonic = { version = "0.11", features = ["gzip"] }

[package]
name = "firestore-emulator"
version = "0.1.0"
Expand All @@ -22,24 +28,20 @@ color-eyre = "0.6.2"
console = { version = "0.15.8", optional = true }
console-subscriber = { version = "0.2.0", optional = true }
futures = "0.3.30"
googleapis = { path = "crates/googleapis" }
itertools = "0.12.1"
prost = "0.12"
prost-types = "0.12"
string_cache = "0.8.7"
tikv-jemallocator = "0.5.4"
time = { version = "0.3.34", features = [
"formatting",
"macros",
], optional = true }
tokio = { version = "1.36.0", features = ["full"] }
tokio = { workspace = true, features = ["full"] }
tokio-stream = { version = "0.1.14", features = ["full"] }
tonic = { version = "0.11", features = ["gzip"] }
tonic = { workspace = true }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", optional = true }

[build-dependencies]
tonic-build = "0.11"

[profile.release]
codegen-unit = 1
lto = "fat"
Expand Down
6 changes: 6 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- common-document-store-backend
- UNIMPLEMENTED: run_aggregation_query is not supported yet
- wao-claim-appengine-backend
- UNIMPLEMENTED: mask is not supported yet
- UNIMPLEMENTED: target_id should always be 1 is not supported yet
- polis-soc-reg-backend - ???
12 changes: 12 additions & 0 deletions crates/googleapis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "googleapis"
edition = "2021"

[dependencies]
prost = "0.12"
prost-types = "0.12"
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tonic = { workspace = true, features = ["gzip"] }

[build-dependencies]
tonic-build = "0.11"
4 changes: 2 additions & 2 deletions build.rs → crates/googleapis/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ fn main() {
.build_client(false)
.include_file("googleapis.rs")
.compile(
&["googleapis/google/firestore/v1/firestore.proto"],
&["googleapis"],
&["include/google/firestore/v1/firestore.proto"],
&["include"],
)
.unwrap()
}
1 change: 1 addition & 0 deletions crates/googleapis/include
Submodule include added at 324b28
9 changes: 9 additions & 0 deletions crates/googleapis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub use googleapis::*;
pub use prost_types::Timestamp;
pub use timestamp_utils::*;

mod googleapis {
tonic::include_proto!("googleapis");
}
mod timestamp_utils;
mod value_ext;
54 changes: 54 additions & 0 deletions crates/googleapis/src/timestamp_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::{sync::Mutex, time::SystemTime};

use prost_types::Timestamp;
use tonic::Status;

use crate::google::firestore::v1::target;

const NANOS_PER_SECOND: i128 = 1_000_000_000;

pub fn timestamp_nanos(ts: &Timestamp) -> i128 {
ts.seconds as i128 * NANOS_PER_SECOND + ts.nanos as i128
}

pub fn timestamp_from_nanos(nanos: i128) -> Timestamp {
Timestamp {
seconds: (nanos / NANOS_PER_SECOND) as _,
nanos: (nanos % NANOS_PER_SECOND) as _,
}
}

/// Returns the current time as a `Timestamp` with the added guarantee that all calls will return a
/// strictly higher timestamp than all calls before that (for the execution of the program).
pub fn timestamp() -> Timestamp {
static LAST: Mutex<Timestamp> = Mutex::new(Timestamp {
seconds: 0,
nanos: 0,
});
let mut last = LAST.lock().unwrap();
let mut timestamp = SystemTime::now().into();
if timestamp_nanos(&timestamp) <= timestamp_nanos(&last) {
timestamp = Timestamp {
seconds: last.seconds,
nanos: last.nanos + 1,
}
}
last.clone_from(&timestamp);
timestamp
}

impl TryFrom<target::ResumeType> for Timestamp {
type Error = Status;

fn try_from(value: target::ResumeType) -> Result<Self, Self::Error> {
match value {
target::ResumeType::ResumeToken(token) => {
let token = token
.try_into()
.map_err(|_| Status::invalid_argument("invalid resume token"))?;
Ok(timestamp_from_nanos(i128::from_ne_bytes(token)))
}
target::ResumeType::ReadTime(time) => Ok(time),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
firestore::v1::{value::ValueType, ArrayValue, MapValue},
r#type::LatLng,
},
utils::timestamp_nanos,
timestamp_nanos,
};

impl Value {
Expand Down
1 change: 0 additions & 1 deletion googleapis
Submodule googleapis deleted from f6b54c
11 changes: 11 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set quiet := true

[private]
default:
just --choose --justfile {{ justfile() }}

alias t:= test

test:
cargo nextest run
cargo test --doc
16 changes: 8 additions & 8 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ use std::{
sync::{Arc, Weak},
};

use googleapis::{
google::firestore::v1::{
document_transform::field_transform::{ServerValue, TransformType},
*,
},
timestamp, Timestamp,
};
use itertools::Itertools;
use prost_types::Timestamp;
use string_cache::DefaultAtom;
use tokio::sync::{
broadcast::{self, Receiver},
Expand All @@ -25,12 +31,7 @@ use self::{
transaction::{RunningTransactions, Transaction, TransactionId},
};
use crate::{
googleapis::google::firestore::v1::{
document_transform::field_transform::{ServerValue, TransformType},
*,
},
unimplemented, unimplemented_collection, unimplemented_option,
utils::{timestamp, RwLockHashMapExt},
unimplemented, unimplemented_collection, unimplemented_option, utils::RwLockHashMapExt,
};

mod collection;
Expand All @@ -40,7 +41,6 @@ mod field_path;
mod listener;
mod query;
mod transaction;
mod value;

const MAX_EVENT_BACKLOG: usize = 1024;

Expand Down
9 changes: 4 additions & 5 deletions src/database/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::{
};

use futures::Future;
use prost_types::Timestamp;
use googleapis::{
google::firestore::v1::{precondition, Document, Value},
timestamp_nanos, Timestamp,
};
use string_cache::DefaultAtom;
use tokio::{
sync::{
Expand All @@ -20,10 +23,6 @@ use tonic::{Code, Result, Status};
use tracing::{instrument, trace, Level};

use super::ReadConsistency;
use crate::{
googleapis::google::firestore::v1::{precondition, Document, Value},
utils::timestamp_nanos,
};

const WAIT_LOCK_TIMEOUT: Duration = Duration::from_secs(30);

Expand Down
2 changes: 1 addition & 1 deletion src/database/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use prost_types::Timestamp;
use googleapis::Timestamp;
use string_cache::DefaultAtom;

use super::document::DocumentVersion;
Expand Down
2 changes: 1 addition & 1 deletion src/database/field_path.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{borrow::Cow, collections::HashMap, convert::Infallible, mem::take, ops::Deref};

use googleapis::google::firestore::v1::*;
use tonic::{Result, Status};

use super::document::StoredDocumentVersion;
use crate::googleapis::google::firestore::v1::*;

/// The virtual field-name that represents the document-name.
pub const DOC_NAME: &str = "__name__";
Expand Down
40 changes: 12 additions & 28 deletions src/database/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ use std::{
},
};

use googleapis::{
google::firestore::v1::{
listen_request,
listen_response::ResponseType,
target::{self, query_target},
target_change::TargetChangeType,
DocumentChange, DocumentDelete, ListenRequest, ListenResponse, Target, TargetChange,
},
timestamp, timestamp_nanos, Timestamp,
};
use itertools::Itertools;
use prost_types::Timestamp;
use string_cache::DefaultAtom;
use tokio::sync::{broadcast::error::RecvError, mpsc};
use tokio_stream::{wrappers::ReceiverStream, StreamExt};
Expand All @@ -18,17 +27,8 @@ use super::{
document::DocumentVersion, event::DatabaseEvent, query::Query, target_change, Database,
};
use crate::{
database::ReadConsistency,
googleapis::google::firestore::v1::{
listen_request,
listen_response::ResponseType,
target::{self, query_target},
target_change::TargetChangeType,
DocumentChange, DocumentDelete, ListenRequest, ListenResponse, Target, TargetChange,
},
required_option, unimplemented, unimplemented_bool, unimplemented_collection,
unimplemented_option,
utils::{timestamp, timestamp_from_nanos, timestamp_nanos},
database::ReadConsistency, required_option, unimplemented, unimplemented_bool,
unimplemented_collection, unimplemented_option,
};

const TARGET_ID: i32 = 1;
Expand Down Expand Up @@ -481,19 +481,3 @@ fn show_response_type(rt: &ResponseType) -> &'static str {
ResponseType::Filter(_) => "Filter",
}
}

impl TryFrom<target::ResumeType> for Timestamp {
type Error = Status;

fn try_from(value: target::ResumeType) -> Result<Self, Self::Error> {
match value {
target::ResumeType::ResumeToken(token) => {
let token = token
.try_into()
.map_err(|_| Status::invalid_argument("invalid resume token"))?;
Ok(timestamp_from_nanos(i128::from_ne_bytes(token)))
}
target::ResumeType::ReadTime(time) => Ok(time),
}
}
}
2 changes: 1 addition & 1 deletion src/database/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{cmp, collections::HashMap, ops::Deref, sync::Arc};

use googleapis::google::firestore::v1::{structured_query::CollectionSelector, *};
use itertools::Itertools;
use string_cache::DefaultAtom;
use tonic::{Result, Status};
Expand All @@ -9,7 +10,6 @@ use super::{
collection::Collection, document::StoredDocumentVersion, field_path::FieldReference, Database,
ReadConsistency,
};
use crate::googleapis::google::firestore::v1::{structured_query::CollectionSelector, *};

mod filter;

Expand Down
4 changes: 2 additions & 2 deletions src/database/query/filter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::ops::Deref;

use googleapis::google::firestore::v1::{structured_query, Value};
use itertools::Itertools;
use tonic::{Result, Status};

use crate::{
database::{document::StoredDocumentVersion, field_path::FieldReference, value::Value},
googleapis::google::firestore::v1::structured_query,
database::{document::StoredDocumentVersion, field_path::FieldReference},
unimplemented,
};

Expand Down
Loading
Loading