Skip to content

Commit

Permalink
Use shorter strings for Postgres db example
Browse files Browse the repository at this point in the history
  • Loading branch information
fkettelhoit committed Apr 24, 2024
1 parent 8f8a909 commit a40bd8b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ jobs:
working-directory: ./examples/http-single-server-channels
- run: cargo test -- --nocapture
working-directory: ./examples/iroh-p2p-channels
- run: docker compose -f docker-compose.yml up -d && cargo test -- --nocapture
- run: docker compose -f docker-compose.yml up -d && cargo test --release -- --nocapture
working-directory: ./examples/postgres-integration
8 changes: 4 additions & 4 deletions examples/postgres-integration/.example.garble.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
enum Row0 {
None,
Some(i32, [u8; 16], i32),
Some(i32, [u8; 6], i32),
}

enum Row1 {
None,
Some(i32, [u8; 16], bool),
Some(i32, [u8; 6], bool),
}

pub fn main(residents: [Row0; 4], insurance: [Row1; 4], age_threshold: u32) -> [[u8; 16]; 4] {
let mut result = [[0u8; 16]; 4];
pub fn main(residents: [Row0; 4], insurance: [Row1; 4], age_threshold: u32) -> [[u8; 6]; 4] {
let mut result = [[0u8; 6]; 4];
let mut i = 0usize;
for resident in residents {
for insurance in insurance {
Expand Down
1 change: 1 addition & 0 deletions examples/postgres-integration/policies0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"input": "SELECT id, name, age FROM residents",
"input_db": "postgres://postgres:test@localhost:5550/postgres",
"max_rows": 4,
"max_str_bytes": 6,
"setup": "TRUNCATE results",
"output": "INSERT INTO results (name) VALUES ($1)",
"output_db": "postgres://postgres:test@localhost:5555/postgres"
Expand Down
3 changes: 2 additions & 1 deletion examples/postgres-integration/policies1.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"party": 1,
"input": "SELECT id, name, disabled FROM insurance",
"input_db": "postgres://postgres:test@localhost:5551/postgres",
"max_rows": 4
"max_rows": 4,
"max_str_bytes": 6
}
]
}
4 changes: 2 additions & 2 deletions examples/postgres-integration/seeds/db0/residents.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Id,Name,Age,Address
0,"John Doe",22,"Parkstraße 1, 12345 Berlin"
1,"Jane Doe",33,"Schlossallee 2, 12345 Berlin"
2,"Max Mustermann",66,"Parkstraße 1, 12345 Berlin"
3,"Erika Musterfrau",77,"Schlossallee 2, 12345 Berlin"
2,"Max Doe",66,"Parkstraße 1, 12345 Berlin"
3,"Bob Doe",77,"Schlossallee 2, 12345 Berlin"
4 changes: 2 additions & 2 deletions examples/postgres-integration/seeds/db1/insurance.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Id,Name,Disabled,Address
0,"Max Mustermann",true,"Parkstraße 1, 12345 Berlin"
1,"Erika Musterfrau",false,"Schlossallee 2, 12345 Berlin"
0,"Max Doe",true,"Parkstraße 1, 12345 Berlin"
1,"Bob Doe",false,"Schlossallee 2, 12345 Berlin"
15 changes: 11 additions & 4 deletions examples/postgres-integration/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use url::Url;

const TIME_BETWEEN_EXECUTIONS: Duration = Duration::from_secs(30);
const DEFAULT_MAX_ROWS: usize = 10;
const STR_LEN_BYTES: usize = 16;
const DEFAULT_MAX_STR_BYTES: usize = 8;

/// A CLI for Multi-Party Computation using the Parlay engine.
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -68,6 +68,7 @@ struct Policy {
input: String,
input_db: Option<String>,
max_rows: Option<usize>,
max_str_bytes: Option<usize>,
setup: Option<String>,
output: Option<String>,
output_db: Option<String>,
Expand Down Expand Up @@ -304,6 +305,7 @@ async fn execute_mpc(
input,
input_db: db,
max_rows,
max_str_bytes,
setup: _setup,
output: _output,
output_db: _output_db,
Expand All @@ -320,6 +322,7 @@ async fn execute_mpc(
let rows = sqlx::query(input).fetch_all(&pool).await?;
info!("'{input}' returned {} rows from {db}", rows.len());
let max_rows = max_rows.unwrap_or(DEFAULT_MAX_ROWS);
let max_str_bytes = max_str_bytes.unwrap_or(DEFAULT_MAX_STR_BYTES);
let mut rows_as_literals = vec![
Literal::Enum(
format!("Row{party}"),
Expand All @@ -333,12 +336,16 @@ async fn execute_mpc(
for c in 0..row.len() {
let field = if let Ok(s) = row.try_get::<String, _>(c) {
let mut fixed_str =
vec![Literal::NumUnsigned(0, UnsignedNumType::U8); STR_LEN_BYTES];
vec![Literal::NumUnsigned(0, UnsignedNumType::U8); max_str_bytes];
for (i, b) in s.as_bytes().iter().enumerate() {
if i < STR_LEN_BYTES {
if i < max_str_bytes {
fixed_str[i] = Literal::NumUnsigned(*b as u64, UnsignedNumType::U8);
} else {
bail!("String is longer than {STR_LEN_BYTES} bytes: '{s}'");
warn!(
"String is longer than {max_str_bytes} bytes: '{s}', dropping '{}'",
&s[i..]
);
break;
}
}
Literal::Array(fixed_str)
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres-integration/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

#[test]
fn simulate() {
let millis = 1000;
let millis = 2000;
println!("\n\n--- {millis}ms ---\n\n");
let mut children = vec![];
let mut cmd = Command::new("cargo")
Expand Down

0 comments on commit a40bd8b

Please sign in to comment.