Brought in SQL statements to create the tables
This commit is contained in:
parent
5164506cf1
commit
05015d49c4
@ -2,5 +2,4 @@ extern crate a03;
|
|||||||
|
|
||||||
use a03::*;
|
use a03::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {}
|
||||||
}
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
|
@ -1,61 +1,108 @@
|
|||||||
extern crate a03;
|
extern crate a03;
|
||||||
extern crate rusqlite;
|
extern crate rusqlite;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_json;
|
||||||
|
|
||||||
use a03::*;
|
// use a03::*;
|
||||||
use rusqlite::types::ToSql;
|
use rusqlite::types::ToSql;
|
||||||
use rusqlite::{Connection, NO_PARAMS};
|
use rusqlite::{Connection, NO_PARAMS};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let conn = Connection::open("dfs.db").unwrap();
|
let conn = Connection::open("dfs.db").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[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,
|
id : i32,
|
||||||
address : String,
|
file_id : i32,
|
||||||
port : i32,
|
node_id : i32,
|
||||||
|
c_id : String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_tables(conn : &Connection) {
|
fn create_tables(conn: &Connection) {
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"CREATE TABLE dnode (nid INTEGER PRIMARY KEY ASC AUTOINCREMENT, address TEXT NOT NULL DEFAULT \" \", port INTEGER NOT NULL DEFAULT \"0\")",
|
"CREATE TABLE inode (
|
||||||
NO_PARAMS);
|
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) {
|
fn add_data_node(conn: &Connection, address: &str, port: i32) {
|
||||||
let dn = Dnode {
|
|
||||||
id : 0,
|
|
||||||
address : address.to_string(),
|
|
||||||
port,
|
|
||||||
};
|
|
||||||
match conn.execute(
|
match conn.execute(
|
||||||
"INSERT INTO dnode (address, port) VALUES (?1, ?2)",
|
"INSERT INTO dnode (address, port) VALUES (?1, ?2)",
|
||||||
&[&dn.address as &ToSql, &dn.port]) {
|
&[&address as &ToSql, &port],
|
||||||
|
) {
|
||||||
Ok(n) => println!("{} rows updated", n),
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@ -68,20 +115,14 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn inserts_dnode() {
|
fn inserts_dnode_with_correct_ip_and_port() {
|
||||||
let conn = get_test_db();
|
let conn = get_test_db();
|
||||||
add_data_node(&conn, "127.0.0.1", 65533);
|
let ip = "127.0.0.1";
|
||||||
let mut stmt = conn.prepare("SELECT * FROM dnode").unwrap();
|
add_data_node(&conn, &ip, 65533);
|
||||||
let result = stmt.query_map(
|
let dnode = check_node(&conn, &ip, 65533);
|
||||||
NO_PARAMS, |row| Dnode {
|
assert_eq!(dnode.id, 1);
|
||||||
id : row.get(0), address : row.get(1), port : row.get(2),
|
assert_eq!(dnode.address, ip);
|
||||||
}).unwrap();
|
assert_eq!(dnode.port, 65533);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1 +1,2 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate serde_json;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user