Skip to content

Commit

Permalink
Merge pull request #139 from naomijub/issue-118-json
Browse files Browse the repository at this point in the history
Issue 118 json
  • Loading branch information
naomijub committed Mar 25, 2021
2 parents 4f85133 + e88c5e3 commit fc2b8c3
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 56 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ jobs:
cargo test --features test_read io::read::test::local_data_test
cargo test --features test_read io::read::test::offset_test
cargo test --features test_read io::read::unique_data_test
build_serde_tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: DB-json-edn-tests
run: |
rm -rf data/*.log
cargo test --features "history json" -- controllers::json_history_test::test_history_ok
rm -rf data/*.log
cargo test --release --features "history json" -- --ignored controllers::json_history_test::query_and_tx_with_token
build_entity_history_tests:
runs-on: ubuntu-latest
Expand Down
58 changes: 9 additions & 49 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
test_read = []
history = []
json = ["serde_json"]

[[bin]]
name = "wooridb"
Expand All @@ -25,7 +27,6 @@ actix-rt = "1.1.1"
actix-http = "2.2.0"
uuid = { version = "0.8.2", features = ["serde", "v4"] }
serde = { version = "1.0.121", features = ["derive"] }
serde_json = "1.0.61"
serde_derive = "1.0.121"
rayon = "1.5"
num_cpus = "1.13"
Expand All @@ -40,12 +41,13 @@ glob = "0.3.0"
zstd = "0.6.0+zstd.1.4.8"
bcrypt = "0.8"
actix-web-httpauth = "0.5.0"
serde_json = { version = "1.0", optional = true }

[dev-dependencies]
bytes = "1.0.1"
criterion = "0.3"
rand = "*"
edn-rs = "*"
rand = "0.7"
edn-rs = "0.16"

# [[bench]]
# name = "tx"
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ debug:
cargo package --manifest-path wql/Cargo.toml
cargo run --manifest-path woori-db/Cargo.toml

json:
cargo package --manifest-path wql/Cargo.toml
cargo run --manifest-path woori-db/Cargo.toml --release --features json

history:
cargo package --manifest-path wql/Cargo.toml
cargo run --manifest-path woori-db/Cargo.toml --release --features history

push:
docker build -t naomijubs/wooridb:$(tag) .
docker push naomijubs/wooridb:$(tag)
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
- [Entity History](./sec-8-history.md)
- [Relation Algebra](./sec-9-algebra.md)
- [Errors Messages](./sec-10-errors.md)
- [Using Json](./sec-11-json.md)
35 changes: 35 additions & 0 deletions book/src/sec-11-json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# using Json

WoorDB is able to support Json requests and responses by using the flag `features`. To execute WooriDB with `json` feature enable execute:

- `Make json`, or;
- `cargo run --manifest-path woori-db/Cargo.toml --release --features json`

> * Remember that Json doesn't have trailing commas while ron has them.
## Example request:
For `/auth/createUser`.

```json
{
"admin_id": "your_admin",
"admin_password": "your_password",
"user_info": {
"user_password": "my_password",
"role": ["User"]
}
}
```

## Example response:
For a `SELECT * FROM entity`.

```json
{
"38d52c95-b6f6-403a-a0b2-447b8fa15784": {
"a": Integer(123),
"tx_time": DateTime("2021-03-24T23:56:45.179008791Z"),
},
}
```

8 changes: 7 additions & 1 deletion book/src/sec-8-history.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Entity History

Entity history is one of the main features of WooriDB. It receives an `entity_key` name and an `entity_id` which will return the whole history of `(DateTime<Utc>, entity_map)` for the `entity_id` in the entity tree for key `entity_key`. This is done by sending a `POST` request to endpoint `<ip>:1438/entity-history`. An example request would be `curl -X POST -H "Content-Type: application/wql" <ip>:1438/entity-history -d '(entity_key: "entity_tree_key", entity_id: "<some-Uuid>",)'`. In `release mode` it is necessary to use header `Authorization: Bearer <your session token>` for this endpoint.
Entity history is one of the main features of WooriDB. You can run it by using the `features` flag. To execute WooriDB with `history` feature enable execute:

- `make history`, or;
- `cargo run --manifest-path woori-db/Cargo.toml --release --features history`


It receives an `entity_key` name and an `entity_id` which will return the whole history of `(DateTime<Utc>, entity_map)` for the `entity_id` in the entity tree for key `entity_key`. This is done by sending a `POST` request to endpoint `<ip>:1438/entity-history`. An example request would be `curl -X POST -H "Content-Type: application/wql" <ip>:1438/entity-history -d '(entity_key: "entity_tree_key", entity_id: "<some-Uuid>",)'`. In `release mode` it is necessary to use header `Authorization: Bearer <your session token>` for this endpoint.

Example request:
```ron
Expand Down
3 changes: 2 additions & 1 deletion woori-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ edition = "2018"
[features]
test_read = []
history = []
json = ["serde_json"]

[dependencies]
actix = "0.10.0"
Expand All @@ -22,7 +23,7 @@ actix-rt = "1.1.1"
actix-http = "2.2.0"
uuid = { version = "0.8.2", features = ["serde", "v4"] }
serde = { version = "1.0.121", features = ["derive"] }
serde_json = "1.0.61"
serde_json = { version = "1.0", optional = true }
serde_derive = "1.0.121"
rayon = "1.5"
num_cpus = "1.13"
Expand Down
4 changes: 4 additions & 0 deletions woori-db/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ test:
cargo test -- --ignored controllers::algebra_test::test_select_all_group_by_count_ok
rm -rf data/*.log
cargo test -- --ignored controllers::algebra_test::test_select_all_count_ok
rm -rf data/*.log
cargo test --features "history json" -- controllers::json_history_test::test_history_ok
rm -rf data/*.log
cargo test --release --features "history json" -- --ignored controllers::json_history_test::query_and_tx_with_token

full-test: test
rm -rf data/*.log
Expand Down
40 changes: 38 additions & 2 deletions woori-db/src/auth/controllers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ use super::{
};

pub async fn create_user(body: String, admin: web::Data<AdminInfo>) -> impl Responder {
#[cfg(feature = "json")]
let credentials: Result<CreateUserWithAdmin, Error> = match serde_json::from_str(&body) {
Ok(x) => Ok(x),
Err(e) => Err(Error::SerdeJson(e)),
};
#[cfg(not(feature = "json"))]
let credentials: Result<CreateUserWithAdmin, Error> = match from_str(&body) {
Ok(x) => Ok(x),
Err(e) => Err(Error::Ron(e)),
Expand All @@ -33,6 +39,13 @@ pub async fn create_user(body: String, admin: web::Data<AdminInfo>) -> impl Resp
let user_response = UserId {
user_id: new_user_id,
};
#[cfg(feature = "json")]
match serde_json::to_string(&user_response) {
Ok(ron) => HttpResponse::Created().body(ron),
Err(_) => HttpResponse::ServiceUnavailable()
.body(Error::FailedToCreateUser.to_string()),
}
#[cfg(not(feature = "json"))]
match ron::ser::to_string_pretty(&user_response, pretty_config_output()) {
Ok(ron) => HttpResponse::Created().body(ron),
Err(_) => HttpResponse::ServiceUnavailable()
Expand All @@ -55,6 +68,12 @@ pub async fn create_user(body: String, admin: web::Data<AdminInfo>) -> impl Resp
}

pub async fn delete_users(body: String, admin: web::Data<AdminInfo>) -> impl Responder {
#[cfg(feature = "json")]
let credentials: Result<DeleteUsersWithAdmin, Error> = match serde_json::from_str(&body) {
Ok(x) => Ok(x),
Err(e) => Err(Error::SerdeJson(e)),
};
#[cfg(not(feature = "json"))]
let credentials: Result<DeleteUsersWithAdmin, Error> = match from_str(&body) {
Ok(x) => Ok(x),
Err(e) => Err(Error::Ron(e)),
Expand All @@ -63,10 +82,17 @@ pub async fn delete_users(body: String, admin: web::Data<AdminInfo>) -> impl Res
if let Ok(cred) = credentials {
if admin.is_valid_hash(&cred.admin_password, &cred.admin_id) {
if io::remove_users_from_log(&cred.users_ids).is_ok() {
#[cfg(feature = "json")]
match serde_json::to_string(&cred.users_ids) {
Ok(ron) => HttpResponse::Created().body(ron),
Err(_) => HttpResponse::ServiceUnavailable()
.body(Error::FailedToCreateUser.to_string()),
}
#[cfg(not(feature = "json"))]
match ron::ser::to_string_pretty(&cred.users_ids, pretty_config_output()) {
Ok(ron) => HttpResponse::Ok().body(ron),
Ok(ron) => HttpResponse::Created().body(ron),
Err(_) => HttpResponse::ServiceUnavailable()
.body(Error::FailedToDeleteUsers.to_string()),
.body(Error::FailedToCreateUser.to_string()),
}
} else {
HttpResponse::ServiceUnavailable().body(Error::FailedToDeleteUsers.to_string())
Expand All @@ -85,10 +111,20 @@ pub async fn put_user_session(
body: String,
session_context: web::Data<Arc<Mutex<SessionContext>>>,
) -> impl Responder {
#[cfg(feature = "json")]
let ok_user: Result<super::schemas::User, Error> = match serde_json::from_str(&body) {
Ok(x) => Ok(x),
Err(e) => {
println!("{:?}", e);
Err(Error::Unknown)
}
};
#[cfg(not(feature = "json"))]
let ok_user: Result<super::schemas::User, Error> = match ron::de::from_str(&body) {
Ok(u) => Ok(u),
Err(_) => Err(Error::Unknown),
};

if let Ok(user) = ok_user {
let user_registry = io::find_user(user.clone()).await;
if let Ok(reg) = user_registry {
Expand Down
7 changes: 7 additions & 0 deletions woori-db/src/controllers/entity_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub async fn history_controller(
local_data: DataLocalContext,
actor: DataExecutor,
) -> Result<String, Error> {
#[cfg(feature = "json")]
let info: EntityHistoryInfo = serde_json::from_str(&body)?;
#[cfg(not(feature = "json"))]
let info: EntityHistoryInfo = ron::de::from_str(&body)?;

let registry = {
Expand Down Expand Up @@ -97,6 +100,10 @@ pub async fn history_controller(
})
.collect::<BTreeMap<DateTime<Utc>, HashMap<String, Types>>>();

#[cfg(feature = "json")]
return Ok(serde_json::to_string(&filtered_tree)?);

#[cfg(not(feature = "json"))]
Ok(ron::ser::to_string_pretty(
&filtered_tree,
pretty_config_output(),
Expand Down
Loading

0 comments on commit fc2b8c3

Please sign in to comment.