Skip to content

Commit 1ea7d48

Browse files
author
Paula Gearon
committed
Updated to accept java.time.Instant in as-of and since calls
1 parent 968e080 commit 1ea7d48

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

src/asami/durable/graph.cljc

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
:author "Paula Gearon"}
33
asami.durable.graph
44
(:require [asami.graph :as graph]
5-
[asami.internal :refer [now instant? long-time]]
65
[asami.common-index :as common-index :refer [?]]
76
[asami.durable.common :as common :refer [TxData Transaction Closeable
87
find-tuples tuples-at write-new-tx-tuple!

src/asami/internal.cljc

+24-12
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,44 @@
33
asami.internal
44
(:require [asami.graph :as graph]
55
[asami.cache :refer [lookup hit miss lru-cache-factory]])
6-
#?(:clj (:import [java.util Date])))
6+
#?(:clj (:import [java.util Date]
7+
[java.time Instant])))
78

89
#?(:clj (set! *warn-on-reflection* true))
910

11+
(defprotocol TimeType
12+
(instant? [this] "Indicates if this object is a time type that supports an instant")
13+
(long-time [this] "Returns a long value as the number of milliseconds since the epoch")
14+
(to-timestamp [this] "Converts to a common time type. Useful for comparison"))
15+
16+
(extend-protocol TimeType
17+
#?(:clj Date :cljs js/Date)
18+
(instant? [_] true)
19+
(long-time [this] (.getTime this))
20+
(to-timestamp [this] this)
21+
22+
#?@(:clj
23+
[Instant
24+
(instant? [_] true)
25+
(long-time [this] (.toEpochMilli this))
26+
(to-timestamp [this] (Date. (.toEpochMilli this)))])
27+
28+
#?(:clj Object :cljs default)
29+
(instant? [_] false)
30+
(long-time [this] (throw (ex-info (str "Unable to convert " (type this) " to a time") {:object this})))
31+
(to-timestamp [this] (throw (ex-info (str "Unable to convert " (type this) " to a time") {:object this}))))
32+
1033
(defn now
1134
"Creates an object to represent the current time"
1235
[]
1336
#?(:clj (Date.)
1437
:cljs (js/Date.)))
1538

16-
(defn instant?
17-
"Tests if a value is a timestamp"
18-
[t]
19-
(= #?(:clj Date :cljs js/Date) (type t)))
20-
2139
(defn instant
2240
"Creates an instant from a long millisecond value"
2341
[^long ms]
2442
#?(:clj (Date. ms) :cljs (js/Date. ms)))
2543

26-
(defn long-time
27-
"Converts a timestamp to a long value as the number of milliseconds"
28-
#?(:clj [^java.util.Date t]
29-
:cljs [t])
30-
(.getTime t)) ;; this is an identical operation in both Java and Javascript
31-
3244
(def project-args {:new-node graph/new-node
3345
:node-label graph/node-label})
3446

src/asami/memory.cljc

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
:author "Paula Gearon"}
33
asami.memory
44
(:require [asami.storage :as storage :refer [ConnectionType DatabaseType UpdateData]]
5-
[asami.internal :refer [now instant?]]
5+
[asami.internal :refer [now instant? to-timestamp]]
66
[asami.index :as mem]
77
[asami.multi-graph :as multi]
88
[asami.graph :as gr :refer [GraphType]]
@@ -119,10 +119,11 @@
119119
[{:keys [graph history timestamp] :as db} :- DatabaseType
120120
t :- (s/cond-pre s/Int (s/pred instant?))]
121121
(cond
122-
(instant? t) (if (>= (compare t timestamp) 0)
123-
db
124-
(nth history
125-
(find-index history t #(compare (:timestamp %1) %2))))
122+
(instant? t) (let [ts (to-timestamp t)]
123+
(if (>= (compare ts timestamp) 0)
124+
db
125+
(nth history
126+
(find-index history ts #(compare (:timestamp %1) %2)))))
126127
(int? t) (if (>= t (count history))
127128
db
128129
(nth history (min (max t 0) (dec (count history)))))))
@@ -143,13 +144,14 @@
143144
[{:keys [graph history timestamp] :as db} :- DatabaseType
144145
t :- (s/cond-pre s/Int (s/pred instant?))]
145146
(cond
146-
(instant? t) (cond
147-
(> (compare t timestamp) 0) nil
148-
(< (compare t (:timestamp (first history))) 0) (first history)
149-
:default (let [tx (inc (find-index history t #(compare (:timestamp %1) %2)))]
150-
(if (= tx (count history))
151-
db
152-
(nth history tx))))
147+
(instant? t) (let [ts (to-timestamp t)]
148+
(cond
149+
(> (compare ts timestamp) 0) nil
150+
(< (compare ts (:timestamp (first history))) 0) (first history)
151+
:default (let [tx (inc (find-index history ts #(compare (:timestamp %1) %2)))]
152+
(if (= tx (count history))
153+
db
154+
(nth history tx)))))
153155
(int? t) (cond (>= t (count history)) nil
154156
(= (count history) (inc t)) db
155157
:default (nth history (min (max (inc t) 0) (dec (count history)))))))

test/asami/api_test.cljc

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
:cljs [clojure.test :refer-macros [is run-tests use-fixtures testing]])
1313
#?(:clj [schema.test :as st :refer [deftest]]
1414
:cljs [schema.test :as st :refer-macros [deftest]]))
15-
#?(:clj (:import [clojure.lang ExceptionInfo])))
15+
#?(:clj (:import [clojure.lang ExceptionInfo]
16+
[java.time Instant])))
17+
18+
(defn other-now
19+
"Create a different kind of instant object on the JVM"
20+
[]
21+
#?(:clj (Instant/now) :cljs (now)))
1622

1723
(deftest test-create
1824
(let [c1 (create-database "asami:mem://babaco")
@@ -304,7 +310,7 @@
304310
_ (sleep 100)
305311
c (connect "asami:mem://test5")
306312
_ (sleep 100)
307-
t1 (now)
313+
t1 (other-now)
308314
maksim {:db/id -1
309315
:db/ident :maksim
310316
:name "Maksim"
@@ -323,7 +329,7 @@
323329
_ (sleep 100)
324330
_ @(transact c [anna])
325331
_ (sleep 100)
326-
t3 (now)
332+
t3 (other-now)
327333
latest-db (db c)
328334
db0 (as-of latest-db t0) ;; before
329335
db1 (as-of latest-db t1) ;; empty

0 commit comments

Comments
 (0)