summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2018-02-22 14:07:51 +0100
committerJustus Winter <justus@sequoia-pgp.org>2018-02-22 14:27:17 +0100
commitec8c7984f72debd3bca2a5e76b5c8292c81772f2 (patch)
tree50f15367cb97ae976d72dbe836f5be8228dee0e7 /store
parentd949f4fa891f76115aa1ba208260b73464c4d310 (diff)
store: Refactor database support code.
- Move types for working with rusqlite into a new module and make them public so that they can be reused.
Diffstat (limited to 'store')
-rw-r--r--store/src/backend/mod.rs97
-rw-r--r--store/src/backend/support.rs95
-rw-r--r--store/src/lib.rs3
3 files changed, 104 insertions, 91 deletions
diff --git a/store/src/backend/mod.rs b/store/src/backend/mod.rs
index cf29bc52..bb03f33b 100644
--- a/store/src/backend/mod.rs
+++ b/store/src/backend/mod.rs
@@ -4,9 +4,8 @@ use failure;
use std::cmp;
use std::fmt;
use std::io;
-use std::ops::{Add, Sub};
use std::rc::Rc;
-use time::{Timespec, Duration, now_utc};
+use time::Duration;
use capnp::capability::Promise;
use capnp;
@@ -17,7 +16,6 @@ use futures::future::{self, loop_fn, Loop};
use rand::distributions::{IndependentSample, Range};
use rand::thread_rng;
use rusqlite::Connection;
-use rusqlite::types::{ToSql, ToSqlOutput, FromSql, FromSqlResult, ValueRef};
use rusqlite;
use tokio_core::reactor::{Handle, Timeout};
use tokio_core;
@@ -33,6 +31,10 @@ use store_protocol_capnp::node;
use super::Result;
+// Data types for working with `rusqlite`.
+pub mod support;
+use self::support::{ID, Timestamp};
+
// Logging.
mod log;
@@ -211,7 +213,7 @@ impl Query for StoreServer {
format!("{}:{}", row.get::<_, String>(0), row.get::<_, String>(1))
})
.unwrap_or(
- format!("{}::{}", Self::table_name(), self.id().0)
+ format!("{}::{}", Self::table_name(), self.id())
)
}
}
@@ -416,7 +418,7 @@ impl Query for BindingServer {
row.get(0)
})
.unwrap_or(
- format!("{}::{}", Self::table_name(), self.id().0)
+ format!("{}::{}", Self::table_name(), self.id())
)
}
}
@@ -841,7 +843,7 @@ impl Query for KeyServer {
.and_then(|fp| Fingerprint::from_hex(&fp))
.map(|fp| fp.to_keyid().to_string())
.unwrap_or(
- format!("{}::{}", Self::table_name(), self.id().0)
+ format!("{}::{}", Self::table_name(), self.id())
)
}
}
@@ -1254,89 +1256,6 @@ CREATE TABLE log (
FOREIGN KEY (key) REFERENCES keys(id) ON DELETE SET NULL);
";
-/// Represents a row id.
-///
-/// This is used to represent handles to stored objects.
-#[derive(Copy, Clone, PartialEq)]
-pub struct ID(i64);
-
-impl ID {
- /// Returns ID(0).
- ///
- /// This is smaller than all valid ids.
- fn null() -> Self {
- ID(0)
- }
-
- /// Returns the largest id.
- fn max() -> Self {
- ID(::std::i64::MAX)
- }
-}
-
-impl From<i64> for ID {
- fn from(id: i64) -> Self {
- ID(id)
- }
-}
-
-impl ToSql for ID {
- fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> {
- Ok(ToSqlOutput::from(self.0))
- }
-}
-
-impl FromSql for ID {
- fn column_result(value: ValueRef) -> FromSqlResult<Self> {
- value.as_i64().map(|id| id.into())
- }
-}
-
-/* Timestamps. */
-
-/// A serializable system time.
-#[derive(Clone, Copy, PartialEq, PartialOrd)]
-struct Timestamp(Timespec);
-
-impl Timestamp {
- fn now() -> Self {
- Timestamp(now_utc().to_timespec())
- }
-
- /// Converts to unix time.
- fn unix(&self) -> i64 {
- self.0.sec
- }
-}
-
-impl ToSql for Timestamp {
- fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> {
- Ok(ToSqlOutput::from(self.0.sec))
- }
-}
-
-impl FromSql for Timestamp {
- fn column_result(value: ValueRef) -> FromSqlResult<Self> {
- value.as_i64().map(|t| Timestamp(Timespec::new(t, 0)))
- }
-}
-
-impl Add<Duration> for Timestamp {
- type Output = Timestamp;
-
- fn add(self, other: Duration) -> Timestamp {
- Timestamp(self.0 + other)
- }
-}
-
-impl Sub<Timestamp> for Timestamp {
- type Output = Duration;
-
- fn sub(self, other: Self) -> Self::Output {
- self.0 - other.0
- }
-}
-
/* Miscellaneous. */
impl<'a> From<&'a core::NetworkPolicy> for node::NetworkPolicy {
diff --git a/store/src/backend/support.rs b/store/src/backend/support.rs
new file mode 100644
index 00000000..f55bed40
--- /dev/null
+++ b/store/src/backend/support.rs
@@ -0,0 +1,95 @@
+//! Data types for working with `rusqlite`.
+
+use rusqlite;
+use rusqlite::types::{ToSql, ToSqlOutput, FromSql, FromSqlResult, ValueRef};
+use std::fmt;
+use std::ops::{Add, Sub};
+use time::{Timespec, Duration, now_utc};
+
+/// Represents a row id.
+///
+/// This is used to represent handles to stored objects.
+#[derive(Copy, Clone, PartialEq)]
+pub struct ID(i64);
+
+impl fmt::Display for ID {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.0)
+ }
+}
+
+impl ID {
+ /// Returns ID(0).
+ ///
+ /// This is smaller than all valid ids.
+ pub fn null() -> Self {
+ ID(0)
+ }
+
+ /// Returns the largest id.
+ pub fn max() -> Self {
+ ID(::std::i64::MAX)
+ }
+}
+
+impl From<i64> for ID {
+ fn from(id: i64) -> Self {
+ ID(id)
+ }
+}
+
+impl ToSql for ID {
+ fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> {
+ Ok(ToSqlOutput::from(self.0))
+ }
+}
+
+impl FromSql for ID {
+ fn column_result(value: ValueRef) -> FromSqlResult<Self> {
+ value.as_i64().map(|id| id.into())
+ }
+}
+
+
+/// A serializable system time.
+#[derive(Clone, Copy, PartialEq, PartialOrd)]
+pub struct Timestamp(Timespec);
+
+impl Timestamp {
+ pub fn now() -> Self {
+ Timestamp(now_utc().to_timespec())
+ }
+
+ /// Converts to unix time.
+ pub fn unix(&self) -> i64 {
+ self.0.sec
+ }
+}
+
+impl ToSql for Timestamp {
+ fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> {
+ Ok(ToSqlOutput::from(self.0.sec))
+ }
+}
+
+impl FromSql for Timestamp {
+ fn column_result(value: ValueRef) -> FromSqlResult<Self> {
+ value.as_i64().map(|t| Timestamp(Timespec::new(t, 0)))
+ }
+}
+
+impl Add<Duration> for Timestamp {
+ type Output = Timestamp;
+
+ fn add(self, other: Duration) -> Timestamp {
+ Timestamp(self.0 + other)
+ }
+}
+
+impl Sub<Timestamp> for Timestamp {
+ type Output = Duration;
+
+ fn sub(self, other: Self) -> Self::Output {
+ self.0 - other.0
+ }
+}
diff --git a/store/src/lib.rs b/store/src/lib.rs
index 5cb8befc..f201dff8 100644
--- a/store/src/lib.rs
+++ b/store/src/lib.rs
@@ -88,8 +88,7 @@ use store_protocol_capnp::node;
/// Macros managing requests and responses.
#[macro_use] mod macros;
-/// Storage backend.
-mod backend;
+pub mod backend;
/// Returns the service descriptor.
#[doc(hidden)]