clj-foundationdb.core

*subspace*

dynamic

clear-all

(clear-all tr)

Clear all keys from the database

clear-key

(clear-key tr key)

Clear a key from the database

(let [fd  (select-api-version 520)
      key "foo"]
(with-open [db (open fd)]
   (tr! db (clear-key tr key))))

clear-range

(clear-range tr begin)(clear-range tr begin end)

Clear a range of keys from the database. When only begin is given then the keys with starting with the tuple are cleared. When begin and end are specified then end is exclusive of the range to be cleared.

(let [fd    (select-api-version 520)
      begin "foo"]
(with-open [db (open fd)]
   (tr! db (clear-range tr begin))))

(let [fd    (select-api-version 520)
      begin "foo"
      end   "foo"]
(with-open [db (open fd)]
   (tr! db (clear-range tr begin end))))

first-greater-or-equal

(first-greater-or-equal tr key)(first-greater-or-equal tr key limit)

Returns key and value pairs with keys greater than or equal to the given key for the given limit

(let [fd  (select-api-version 520)
      key "foo"]
(with-open [db (open fd)]
   (tr! db (first-greater-or-equal tr key))))

first-greater-than

(first-greater-than tr key)(first-greater-than tr key limit)

Returns key and value pairs with keys greater than the given key for the given limit

(let [fd  (select-api-version 520)
      key "foo"]
(with-open [db (open fd)]
   (tr! db (first-greater-than tr key))))

get-all

(get-all tr)

Get all key values as a vector

get-range

(get-range tr begin)(get-range tr begin end)

Get a range of key values as a vector

(let [fd    (select-api-version 520)
      begin "foo"
      end   "foo"]
(with-open [db (open fd)]
   (tr! db (get-range tr begin end))))

get-range-startswith

(get-range-startswith tr prefix)

Get a range of key values as a vector that starts with prefix

(let [fd     (select-api-version 520)
      prefix "f"]
(with-open [db (open fd)]
   (tr! db (get-range-startswith tr key prefix))))

get-val

(get-val tr key & {:keys [subspace coll], :or {subspace *subspace*, coll false}})

Get the value for the given key. Accepts the below :

:subspace - Subspace to be prefixed :coll - Boolean to indicate if the value needs to be deserialized as collection

(let [fd  (select-api-version 520)
      key "foo"]
(with-open [db (open fd)]
   (tr! db
        (get-val tr key))))

(let [fd    (select-api-version 520)
     key   "foo"
     value [1 2 3]]
(with-open [db (open fd)]
  (tr! db
       (set-val tr key value)
       (get-val tr key) ;; 1
       (get-val tr key :coll true)))) ;; [1 2 3]

last-less-or-equal

(last-less-or-equal tr key)(last-less-or-equal tr key limit)

Returns key and value pairs with keys less than or equal the given key for the given limit

(let [fd  (select-api-version 520)
      key "foo"]
(with-open [db (open fd)]
   (tr! db (last-less-or-equal tr key))))

last-less-than

(last-less-than tr key)(last-less-than tr key limit)

Returns key and value pairs with keys less than the given key for the given limit

(let [fd  (select-api-version 520)
      key "foo"]
(with-open [db (open fd)]
   (tr! db (last-less-than tr key))))

make-subspace

(make-subspace prefix key)

Returns a key with name as prefix

(make-subspace ["class" "intro"] ["algebra"]) returns ("class" "intro" "algebra")

serializable?

set-keys

(set-keys tr keys value)

Set given keys with the value

(let [fd    (select-api-version 520)
      keys  ["foo" "baz"]
      value "bar"]
(with-open [db (open fd)]
   (tr! db (set-keys tr keys value))))

set-val

(set-val tr key value & {:keys [subspace], :or {subspace *subspace*}})

Set a value for the key. Accepts the below :

:subspace - Subspace to be prefixed

(let [fd    (select-api-version 520)
      key   "foo"
      value "bar"]
(with-open [db (open fd)]
   (tr! db
        (set-val tr key value))))

tr!

macro

(tr! db & actions)

Transaction macro to perform actions. Always use tr for actions inside each action since the transaction variable is bound to tr in the functions.

(let [fd    (select-api-version 520)
      key   "foo"
      value "bar"]
(with-open [db (open fd)]
   (tr! db
        (set-val tr key value)
        (get-val tr key))))

tr?

watch

(watch tr key callback)

A key to watch and a callback function to be executed on change. It returns a future object that is realized when there is a change to key. Change in key value or clearing the key is noted as a change. A set statement with the old value is not a change.

(let [fd    (select-api-version 520)
     key    "foo"
     value  "bar"]
(with-open [db (open fd)]
  (tr! db
       (clear-key tr key)
       (watch tr key #(println "key is set"))
       (set-val tr key value)
       (watch tr key #(println "key is changed to 1"))
       (set-val tr key value) ;; Doesn't trigger watch
       (set-val tr key "1")
       (watch tr key #(println "cleared key"))
       (clear-key tr key))))

key is set key is changed to 1 cleared key

with-subspace

macro

(with-subspace prefix & actions)

Sets and gets the keys with the given subspace key prefixed. This essentially executes with code binding the given prefix to subspace.

(let [fd    (select-api-version 520)
      key   "foo"
      value "bar"]
  (with-open [db (open fd)]
    (tr! db
         (clear-all tr)
         (with-subspace "class"
           (set-val tr key value)
           (get-val tr key))
          (nil? (get-val tr key)))))