Brought in SQL statements to create the tables

This commit is contained in:
Joseph Ferano 2018-11-25 12:06:27 -04:00
parent 5164506cf1
commit 05015d49c4
4 changed files with 88 additions and 48 deletions

View File

@ -2,5 +2,4 @@ extern crate a03;
use a03::*;
fn main() {
}
fn main() {}

View File

@ -1,4 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -1,7 +1,9 @@
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};
@ -10,52 +12,97 @@ fn main() {
}
#[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) {
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);
"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: &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) {
let mut stmt = conn.prepare("SELECT address, port FROM dnode WHERE 1");
}
}
fn check_node(conn : Connection, address : String, port : 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_data_nodes(conn : Connection) {
}
// fn get_file_info(conn: &Connection, fname: String) {}
fn insert_file(conn : Connection, fname : String, fsize : usize) {
}
// fn get_files(conn: &Connection) {}
fn get_file_info(conn : Connection, fname : String) {
}
// fn add_block_to_inode(conn: &Connection, fname: String, blocks: usize) {}
fn get_files(conn : Connection) {
}
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();
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, "127.0.0.1");
assert_eq!(dnode.address, ip);
assert_eq!(dnode.port, 65533);
}
}
}

View File

@ -1 +1,2 @@
#[macro_use]
extern crate serde_json;