Skip to content

Commit

Permalink
issue-128-enable-grpc-and-gql-usage (#137)
Browse files Browse the repository at this point in the history
* Select queries dont return strings anymore, and data is stringified by the responsible function
* generalize count for serialization
  • Loading branch information
naomijub authored Mar 23, 2021
1 parent 16ee557 commit 8517547
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 114 deletions.
14 changes: 7 additions & 7 deletions woori-db/src/actors/encrypts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
use wql::Types;

use crate::{actors::wql::Executor, model::error::Error, repository::local::EncryptContext};
use crate::{
actors::wql::Executor, model::error::Error, repository::local::EncryptContext,
schemas::query::Response as QueryResponse,
};

#[derive(Debug, Serialize, Deserialize)]
pub struct WriteWithEncryption {
Expand Down Expand Up @@ -137,11 +140,11 @@ impl VerifyEncryption {
}

impl Message for VerifyEncryption {
type Result = Result<String, Error>;
type Result = Result<QueryResponse, Error>;
}

impl Handler<VerifyEncryption> for Executor {
type Result = Result<String, Error>;
type Result = Result<QueryResponse, Error>;

fn handle(&mut self, msg: VerifyEncryption, _: &mut Self::Context) -> Self::Result {
let type_nil = Types::Nil;
Expand All @@ -160,10 +163,7 @@ impl Handler<VerifyEncryption> for Executor {
(k, result)
})
.collect::<HashMap<String, bool>>();
let encrypt_log =
to_string_pretty(&results, pretty_config()).map_err(Error::Serialization)?;

Ok(encrypt_log)
Ok(results.into())
}
}

Expand Down
3 changes: 2 additions & 1 deletion woori-db/src/controllers/clauses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use wql::{Algebra, Clause, ToSelect, Types, Value};
use crate::{
core::registry::get_registries,
model::{error::Error, DataLocalContext, DataRegister},
schemas::query::Response as QueryResponse,
};

use crate::core::query::{dedup_states, get_limit_offset_count, get_result_after_manipulation};
Expand All @@ -18,7 +19,7 @@ pub async fn select_where_controller(
clauses: Vec<Clause>,
local_data: DataLocalContext,
functions: HashMap<String, wql::Algebra>,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
let states = select_where(entity, args_to_select, clauses, local_data, &functions);
let count = if let Some(Algebra::Count) = functions.get("COUNT") {
true
Expand Down
60 changes: 27 additions & 33 deletions woori-db/src/controllers/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{

use actix_web::{HttpResponse, Responder};
use rayon::prelude::*;
use ron::ser::to_string_pretty;
use uuid::Uuid;
use wql::{ToSelect, Types, Wql};

Expand All @@ -15,17 +14,15 @@ use crate::{
state::State,
when::{ReadEntitiesAt, ReadEntityIdAt, ReadEntityRange},
},
core::{
pretty_config_output,
query::{
dedup_option_states, dedup_states, get_limit_offset_count,
get_result_after_manipulation, get_result_after_manipulation_for_options,
},
core::query::{
dedup_option_states, dedup_states, get_limit_offset_count, get_result_after_manipulation,
get_result_after_manipulation_for_options,
},
model::{
error::{error_to_http, Error},
DataEncryptContext, DataExecutor, DataLocalContext, DataRegister,
},
schemas::query::Response as QueryResponse,
};

use super::clauses::select_where_controller;
Expand Down Expand Up @@ -84,7 +81,10 @@ pub async fn wql_handler(

match response {
Err(e) => error_to_http(e),
Ok(resp) => HttpResponse::Ok().body(resp),
Ok(resp) => match resp.to_string() {
Ok(body) => HttpResponse::Ok().body(body),
Err(e) => error_to_http(e),
},
}
}

Expand All @@ -95,7 +95,7 @@ pub async fn check_value_controller(
local_data: DataLocalContext,
encryption: DataEncryptContext,
actor: DataExecutor,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
if let Ok(guard) = encryption.lock() {
if guard.contains_key(&entity) {
let encrypts = guard.get(&entity).unwrap();
Expand Down Expand Up @@ -146,7 +146,7 @@ async fn select_all_when_range_controller(
start_date: String,
end_date: String,
actor: DataExecutor,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
use chrono::{DateTime, Utc};
let start_date: DateTime<Utc> = start_date
.parse::<DateTime<Utc>>()
Expand All @@ -165,13 +165,13 @@ async fn select_all_when_range_controller(
))
.await??;

Ok(to_string_pretty(&result, pretty_config_output())?)
Ok(result.into())
}
async fn select_all_when_controller(
entity: String,
date: String,
actor: DataExecutor,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
use chrono::{DateTime, Utc};
let date = date
.parse::<DateTime<Utc>>()
Expand All @@ -182,15 +182,15 @@ async fn select_all_when_controller(
let date_log = date.format("data/%Y_%m_%d.log").to_string();
let result = actor.send(ReadEntitiesAt::new(&entity, date_log)).await??;

Ok(to_string_pretty(&result, pretty_config_output())?)
Ok(result.into())
}

async fn select_all_id_when_controller(
entity: String,
date: String,
uuid: Uuid,
actor: DataExecutor,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
use chrono::{DateTime, Utc};
let date = date
.parse::<DateTime<Utc>>()
Expand All @@ -203,7 +203,7 @@ async fn select_all_id_when_controller(
.send(ReadEntityIdAt::new(&entity, uuid, date_log))
.await??;

Ok(to_string_pretty(&result, pretty_config_output())?)
Ok(result.into())
}

async fn select_keys_id_when_controller(
Expand All @@ -212,7 +212,7 @@ async fn select_keys_id_when_controller(
keys: Vec<String>,
uuid: Uuid,
actor: DataExecutor,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
use chrono::{DateTime, Utc};
let date = date
.parse::<DateTime<Utc>>()
Expand All @@ -229,15 +229,15 @@ async fn select_keys_id_when_controller(
.filter(|(k, _)| keys.contains(k))
.collect::<HashMap<String, Types>>();

Ok(to_string_pretty(&result, pretty_config_output())?)
Ok(result.into())
}

async fn select_keys_when_controller(
entity: String,
date: String,
keys: Vec<String>,
actor: DataExecutor,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
use chrono::{DateTime, Utc};
let date = date
.parse::<DateTime<Utc>>()
Expand All @@ -260,14 +260,14 @@ async fn select_keys_when_controller(
})
.collect::<HashMap<String, HashMap<String, Types>>>();

Ok(to_string_pretty(&result, pretty_config_output())?)
Ok(result.into())
}

async fn select_all_with_id(
entity: String,
uuid: Uuid,
local_data: DataLocalContext,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
let registry = {
let local_data = if let Ok(guard) = local_data.lock() {
guard
Expand All @@ -292,18 +292,15 @@ async fn select_all_with_id(
.into_par_iter()
.filter(|(_, v)| !v.is_hash())
.collect::<HashMap<String, Types>>();
Ok(ron::ser::to_string_pretty(
&filterd_state,
pretty_config_output(),
)?)
Ok(filterd_state.into())
}

async fn select_all_with_ids(
entity: String,
uuids: Vec<Uuid>,
local_data: DataLocalContext,
functions: HashMap<String, wql::Algebra>,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
let (limit, offset, count) = get_limit_offset_count(&functions);
let registries = {
let local_data = if let Ok(guard) = local_data.lock() {
Expand Down Expand Up @@ -360,7 +357,7 @@ async fn select_keys_with_id(
uuid: Uuid,
keys: Vec<String>,
local_data: DataLocalContext,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
let keys = keys.into_par_iter().collect::<HashSet<String>>();
let registry = {
let local_data = if let Ok(guard) = local_data.lock() {
Expand All @@ -387,10 +384,7 @@ async fn select_keys_with_id(
.filter(|(k, _)| keys.contains(k))
.filter(|(_, v)| !v.is_hash())
.collect();
Ok(ron::ser::to_string_pretty(
&filtered,
pretty_config_output(),
)?)
Ok(filtered.into())
}

async fn select_keys_with_ids(
Expand All @@ -399,7 +393,7 @@ async fn select_keys_with_ids(
uuids: Vec<Uuid>,
local_data: DataLocalContext,
functions: HashMap<String, wql::Algebra>,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
let (limit, offset, count) = get_limit_offset_count(&functions);
let registries = {
let local_data = if let Ok(guard) = local_data.lock() {
Expand Down Expand Up @@ -456,7 +450,7 @@ async fn select_all(
entity: String,
local_data: DataLocalContext,
functions: HashMap<String, wql::Algebra>,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
let (limit, offset, count) = get_limit_offset_count(&functions);

let registries = {
Expand Down Expand Up @@ -493,7 +487,7 @@ async fn select_args(
keys: Vec<String>,
local_data: DataLocalContext,
functions: HashMap<String, wql::Algebra>,
) -> Result<String, Error> {
) -> Result<QueryResponse, Error> {
let (limit, offset, count) = get_limit_offset_count(&functions);
let keys = keys.into_par_iter().collect::<HashSet<String>>();
let registries = {
Expand Down
Loading

0 comments on commit 8517547

Please sign in to comment.