Last active
March 3, 2026 19:49
-
-
Save maxweber/c5fd8dadf7cb816026af19eba1cc37b5 to your computer and use it in GitHub Desktop.
Slatedb Clojure Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (comment | |
| ;; deps.edn | |
| ;; io.slatedb/slatedb {:mvn/version "0.11.0"} | |
| ;; cheshire/cheshire {:mvn/version "6.1.0"} | |
| (require '[cheshire.core :as json]) | |
| (io.slatedb.SlateDb/initLogging io.slatedb.SlateDbConfig$LogLevel/INFO) | |
| (def db-path (java.nio.file.Path/of "local-cache" | |
| (into-array String []))) | |
| (def object-store-url "gs://your-google-cloud-storage-bucket/") | |
| ;; Build a db with local disk cache enabled | |
| (time | |
| (with-open [builder (io.slatedb.SlateDb/builder | |
| (.toString db-path) | |
| object-store-url | |
| nil)] | |
| (def db (.build | |
| (-> builder | |
| (.withSettingsJson | |
| (-> (io.slatedb.SlateDb/settingsDefault) | |
| (json/parse-string) | |
| (assoc-in ["object_store_cache_options" | |
| "root_folder"] | |
| (str db-path)) | |
| (json/generate-string)))))))) | |
| (def db | |
| (time (io.slatedb.SlateDb/open (.toString db-path) object-store-url nil))) | |
| ;; Put + Get | |
| (def key (.getBytes "hello-key" java.nio.charset.StandardCharsets/UTF_8)) | |
| (def value (.getBytes "hello-value" java.nio.charset.StandardCharsets/UTF_8)) | |
| (.put db key value) | |
| (def loaded (.get db key)) | |
| (String. loaded java.nio.charset.StandardCharsets/UTF_8) | |
| ;; Delete | |
| (.delete db key) | |
| ;; Batch write | |
| (def batch (io.slatedb.SlateDb/newWriteBatch)) | |
| (.put batch (.getBytes "hello-a" java.nio.charset.StandardCharsets/UTF_8) | |
| (.getBytes "value-a" java.nio.charset.StandardCharsets/UTF_8)) | |
| (.put batch (.getBytes "hello-b" java.nio.charset.StandardCharsets/UTF_8) | |
| (.getBytes "value-b" java.nio.charset.StandardCharsets/UTF_8)) | |
| (.write db batch) | |
| (.close batch) | |
| ;; Scan by prefix | |
| (def iter (.scanPrefix db (.getBytes "hello-" java.nio.charset.StandardCharsets/UTF_8))) | |
| (loop [] | |
| (when-let [kv (.next iter)] | |
| (println (str (String. (.key kv) java.nio.charset.StandardCharsets/UTF_8) | |
| "=" | |
| (String. (.value kv) java.nio.charset.StandardCharsets/UTF_8))) | |
| (recur))) | |
| (.close iter) | |
| (.close db) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment