From 05015d49c492dfad7ddca94131c7be13990e2395 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 25 Nov 2018 12:06:27 -0400 Subject: [PATCH] Brought in SQL statements to create the tables --- src/bin/data_node.rs | 3 +- src/bin/ls.rs | 1 - src/bin/meta_data.rs | 129 ++++++++++++++++++++++++++++--------------- src/lib.rs | 3 +- 4 files changed, 88 insertions(+), 48 deletions(-) diff --git a/src/bin/data_node.rs b/src/bin/data_node.rs index e872f71..95e9372 100644 --- a/src/bin/data_node.rs +++ b/src/bin/data_node.rs @@ -2,5 +2,4 @@ extern crate a03; use a03::*; -fn main() { -} +fn main() {} diff --git a/src/bin/ls.rs b/src/bin/ls.rs index 7da3a48..e7a11a9 100644 --- a/src/bin/ls.rs +++ b/src/bin/ls.rs @@ -1,4 +1,3 @@ - fn main() { println!("Hello, world!"); } diff --git a/src/bin/meta_data.rs b/src/bin/meta_data.rs index 83ddf20..67fe136 100644 --- a/src/bin/meta_data.rs +++ b/src/bin/meta_data.rs @@ -1,61 +1,108 @@ extern crate a03; extern crate rusqlite; +#[macro_use] +extern crate serde_json; -use a03::*; +// use a03::*; use rusqlite::types::ToSql; use rusqlite::{Connection, NO_PARAMS}; fn main() { - let conn = Connection::open("dfs.db").unwrap(); + let conn = Connection::open("dfs.db").unwrap(); } #[derive(Debug)] -struct Dnode { +struct DataNode { + id: i32, + address: String, + port: i32, +} + +#[derive(Debug)] +struct INode { + id: i32, + name: String, + size: i32, +} + +#[derive(Debug)] +struct Block { id : i32, - address : String, - port : i32, + file_id : i32, + node_id : i32, + c_id : String, } -fn create_tables(conn : &Connection) { - conn.execute( - "CREATE TABLE dnode (nid INTEGER PRIMARY KEY ASC AUTOINCREMENT, address TEXT NOT NULL DEFAULT \" \", port INTEGER NOT NULL DEFAULT \"0\")", - NO_PARAMS); +fn create_tables(conn: &Connection) { + conn.execute( + "CREATE TABLE inode ( +fid INTEGER PRIMARY KEY ASC AUTOINCREMENT, +fname TEXT UNIQUE NOT NULL DEFAULT \" \", +fsize INTEGER NOT NULL default \"0\")", + NO_PARAMS, + ).unwrap(); + + conn.execute( + "CREATE TABLE dnode ( +nid INTEGER PRIMARY KEY ASC AUTOINCREMENT, +address TEXT NOT NULL DEFAULT \" \", +port INTEGER NOT NULL DEFAULT \"0\")", + NO_PARAMS, + ).unwrap(); + + conn.execute( + "CREATE TABLE block ( +bid INTEGER PRIMARY KEY ASC AUTOINCREMENT, +fid INTEGER NOT NULL DEFAULT \"0\", +nid INTEGER NOT NULL DEFAULT \"0\", +cid TEXT NOT NULL DEFAULT \"0\")", + NO_PARAMS, + ).unwrap(); + + // Create UNIQUE tuple for block + conn.execute("CREATE UNIQUE INDEX blocknc ON block(nid, cid)", NO_PARAMS) + .unwrap(); } -fn add_data_node(conn : &Connection, address : &str, port : i32) { - let dn = Dnode { - id : 0, - address : address.to_string(), - port, - }; +fn add_data_node(conn: &Connection, address: &str, port: i32) { match conn.execute( "INSERT INTO dnode (address, port) VALUES (?1, ?2)", - &[&dn.address as &ToSql, &dn.port]) { + &[&address as &ToSql, &port], + ) { Ok(n) => println!("{} rows updated", n), - Err(e) => println!("INSERT error: {}", e) - } + Err(e) => println!("INSERT error: {}", e), + }; } -fn check_node(conn : Connection, address : String, port : usize) { +fn check_node(conn: &Connection, address: &str, port: i32) -> DataNode { + let mut stmt = conn + .prepare("SELECT nid, address, port FROM dnode WHERE address=?1 AND port=?2") + .unwrap(); + stmt.query_row( + &[&address as &ToSql, &port], + |row| DataNode { + id: row.get(0), + address: row.get(1), + port: row.get(2), + }) + .unwrap() } -fn get_data_nodes(conn : Connection) { -} + fn get_data_nodes(conn: &Connection) { + let mut stmt = conn.prepare("SELECT address, port FROM dnode WHERE 1"); + } -fn insert_file(conn : Connection, fname : String, fsize : usize) { -} + fn insert_file(conn: &Connection, fname: String, fsize: usize) { + let mut stmt = conn.prepare("INSERT INTO inode (fname, fsize) VALUES (\"?1\", ?2)"); + } -fn get_file_info(conn : Connection, fname : String) { -} +// fn get_file_info(conn: &Connection, fname: String) {} -fn get_files(conn : Connection) { -} +// fn get_files(conn: &Connection) {} -fn add_block_to_inode(conn : Connection, fname : String, blocks : usize) { -} +// fn add_block_to_inode(conn: &Connection, fname: String, blocks: usize) {} -fn get_file_inode(conn : Connection, fname : String) { -} +// fn get_file_inode(conn: &Connection, fname: String) {} #[cfg(test)] mod tests { @@ -68,20 +115,14 @@ mod tests { } #[test] - fn inserts_dnode() { + fn inserts_dnode_with_correct_ip_and_port() { let conn = get_test_db(); - add_data_node(&conn, "127.0.0.1", 65533); - let mut stmt = conn.prepare("SELECT * FROM dnode").unwrap(); - let result = stmt.query_map( - NO_PARAMS, |row| Dnode { - id : row.get(0), address : row.get(1), port : row.get(2), - }).unwrap(); - for d in result { - let dnode = d.unwrap(); - assert_eq!(dnode.id, 1); - assert_eq!(dnode.address, "127.0.0.1"); - assert_eq!(dnode.port, 65533); - } + let ip = "127.0.0.1"; + add_data_node(&conn, &ip, 65533); + let dnode = check_node(&conn, &ip, 65533); + assert_eq!(dnode.id, 1); + assert_eq!(dnode.address, ip); + assert_eq!(dnode.port, 65533); } } diff --git a/src/lib.rs b/src/lib.rs index 8b13789..c9931ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ - +#[macro_use] +extern crate serde_json;