Part 4: use DateTime for tx_instants.

This commit is contained in:
Richard Newman 2017-04-28 11:40:35 -07:00
parent bc5ffdbc34
commit b03e89fb31
3 changed files with 34 additions and 21 deletions

View file

@ -11,12 +11,12 @@
#[macro_use]
extern crate error_chain;
extern crate itertools;
#[macro_use]
extern crate lazy_static;
extern crate rusqlite;
extern crate time;
extern crate tabwriter;
extern crate time;
#[macro_use]
extern crate edn;
@ -24,8 +24,15 @@ extern crate mentat_core;
extern crate mentat_tx;
extern crate mentat_tx_parser;
use itertools::Itertools;
use std::iter::repeat;
use itertools::Itertools;
use mentat_core::{
DateTime,
UTC,
};
pub use errors::{Error, ErrorKind, ResultExt, Result};
pub mod db;
@ -89,10 +96,7 @@ pub fn repeat_values(values_per_tuple: usize, tuples: usize) -> String {
values
}
/// Return the current time in milliseconds after the Unix epoch according to the local clock.
///
/// Compare `Date.now()` in JavaScript, `System.currentTimeMillis` in Java.
pub fn now() -> i64 {
let now = time::get_time();
(now.sec as i64 * 1_000) + (now.nsec as i64 / (1_000_000))
/// Return the current time as a UTC `DateTime` instance.
pub fn now() -> DateTime<UTC> {
UTC::now()
}

View file

@ -70,10 +70,13 @@ use internal_types::{
TermWithTempIds,
TermWithoutTempIds,
replace_lookup_ref};
use mentat_core::{
DateTime,
Schema,
UTC,
attribute,
intern_set,
Schema,
};
use mentat_tx::entities as entmod;
use mentat_tx::entities::{
@ -127,10 +130,7 @@ pub struct Tx<'conn, 'a> {
tx_id: Entid,
/// The timestamp when the transaction began to be committed.
///
/// This is milliseconds after the Unix epoch according to the transactor's local clock.
// TODO: :db.type/instant.
tx_instant: i64,
tx_instant: DateTime<UTC>,
}
impl<'conn, 'a> Tx<'conn, 'a> {
@ -140,7 +140,7 @@ impl<'conn, 'a> Tx<'conn, 'a> {
schema_for_mutation: &'a Schema,
schema: &'a Schema,
tx_id: Entid,
tx_instant: i64) -> Tx<'conn, 'a> {
tx_instant: DateTime<UTC>) -> Tx<'conn, 'a> {
Tx {
store: store,
partition_map: partition_map,
@ -532,7 +532,7 @@ impl<'conn, 'a> Tx<'conn, 'a> {
non_fts_one.push((self.tx_id,
entids::DB_TX_INSTANT,
self.schema.require_attribute_for_entid(entids::DB_TX_INSTANT).unwrap(),
TypedValue::Long(self.tx_instant),
TypedValue::Instant(self.tx_instant),
true));
if !non_fts_one.is_empty() {

View file

@ -16,12 +16,14 @@ use std::collections::BTreeMap;
extern crate mentat_core;
pub use self::mentat_core::{
DateTime,
Entid,
ValueType,
TypedValue,
Attribute,
AttributeBitFlags,
Schema,
UTC,
};
/// Represents one partition of the entid space.
@ -78,16 +80,13 @@ pub type AVMap<'a> = HashMap<&'a AVPair, Entid>;
/// A transaction report summarizes an applied transaction.
// TODO: include map of resolved tempids.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialOrd, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
pub struct TxReport {
/// The transaction ID of the transaction.
pub tx_id: Entid,
/// The timestamp when the transaction began to be committed.
///
/// This is milliseconds after the Unix epoch according to the transactor's local clock.
// TODO: :db.type/instant.
pub tx_instant: i64,
pub tx_instant: DateTime<UTC>,
/// A map from string literal tempid to resolved or allocated entid.
///
@ -96,3 +95,13 @@ pub struct TxReport {
/// literal tempids to all unify to a single freshly allocated entid.)
pub tempids: BTreeMap<String, Entid>,
}
impl Default for TxReport {
fn default() -> Self {
TxReport {
tx_id: Entid::default(),
tx_instant: UTC::now(),
tempids: BTreeMap::default(),
}
}
}