|
|
@ -4,9 +4,14 @@ use serde::{Deserialize, Serialize}; |
|
|
|
|
|
|
|
|
|
|
|
use std::sync::{Arc, Mutex}; |
|
|
|
use std::sync::{Arc, Mutex}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::iter::repeat_with; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ID_LENGTH: usize = 20; |
|
|
|
|
|
|
|
pub type SegmentId = String; |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)] |
|
|
|
#[derive(Debug, Serialize, Deserialize)] |
|
|
|
pub struct Segment { |
|
|
|
pub struct Segment { |
|
|
|
pub id: Option<u64>, |
|
|
|
pub id: Option<SegmentId>, |
|
|
|
pub from: u64, |
|
|
|
pub from: u64, |
|
|
|
pub to: u64, |
|
|
|
pub to: u64, |
|
|
|
} |
|
|
|
} |
|
|
@ -23,7 +28,7 @@ impl DataService { |
|
|
|
let conn = Connection::open("./data.db")?; |
|
|
|
let conn = Connection::open("./data.db")?; |
|
|
|
conn.execute( |
|
|
|
conn.execute( |
|
|
|
"CREATE TABLE IF NOT EXISTS segment ( |
|
|
|
"CREATE TABLE IF NOT EXISTS segment ( |
|
|
|
id INTEGER PRIMARY KEY, |
|
|
|
id TEXT PRIMARY KEY, |
|
|
|
start INTEGER NOT NULL, |
|
|
|
start INTEGER NOT NULL, |
|
|
|
end INTEGER NOT NULL |
|
|
|
end INTEGER NOT NULL |
|
|
|
)", |
|
|
|
)", |
|
|
@ -35,19 +40,23 @@ impl DataService { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn insert_segment(&self, segment: &Segment) -> anyhow::Result<u64> { |
|
|
|
// returns id of created segment
|
|
|
|
|
|
|
|
pub fn insert_segment(&self, segment: &Segment) -> anyhow::Result<SegmentId> { |
|
|
|
|
|
|
|
let id: SegmentId = repeat_with(fastrand::alphanumeric) |
|
|
|
|
|
|
|
.take(ID_LENGTH) |
|
|
|
|
|
|
|
.collect(); |
|
|
|
// TODO: merge with other segments
|
|
|
|
// TODO: merge with other segments
|
|
|
|
self.connection.lock().unwrap().execute( |
|
|
|
self.connection.lock().unwrap().execute( |
|
|
|
"INSERT INTO segment (start, end) VALUES (?1, ?2)", |
|
|
|
"INSERT INTO segment (id, start, end) VALUES (?1, ?2, ?3)", |
|
|
|
params![segment.from, segment.to], |
|
|
|
params![id, segment.from, segment.to], |
|
|
|
)?; |
|
|
|
)?; |
|
|
|
Ok(10) |
|
|
|
Ok(id) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_segments(&self, from: u64, to: u64) -> anyhow::Result<Vec<Segment>> { |
|
|
|
pub fn get_segments(&self, from: u64, to: u64) -> anyhow::Result<Vec<Segment>> { |
|
|
|
let conn = self.connection.lock().unwrap(); |
|
|
|
let conn = self.connection.lock().unwrap(); |
|
|
|
let mut stmt = |
|
|
|
let mut stmt = |
|
|
|
conn.prepare("SELECT id, start, end FROM person WHERE ?1 < start AND end < ?2")?; |
|
|
|
conn.prepare("SELECT id, start, end FROM segment WHERE ?1 < start AND end < ?2")?; |
|
|
|
|
|
|
|
|
|
|
|
let res = stmt |
|
|
|
let res = stmt |
|
|
|
.query_map(params![from, to], |row| { |
|
|
|
.query_map(params![from, to], |row| { |
|
|
|