Skip to content

Instantly share code, notes, and snippets.

@jacobobryant
Created August 31, 2024 21:17
Show Gist options
  • Save jacobobryant/2afa53e33c5d658de79d431c30554521 to your computer and use it in GitHub Desktop.
Save jacobobryant/2afa53e33c5d658de79d431c30554521 to your computer and use it in GitHub Desktop.
Clojure / RocksDB Java interop examples
(ns com.biffweb.impl.index
(:require [taoensso.nippy :as nippy])
(:import java.util.Map
java.util.HashMap
java.util.function.Function
(java.io Closeable File)
(java.nio.file Files Path)
java.nio.file.attribute.FileAttribute
(org.rocksdb BlockBasedTableConfig Checkpoint CompressionType FlushOptions LRUCache
DBOptions Options ReadOptions RocksDB RocksIterator
WriteBatchWithIndex WriteBatch WriteOptions Statistics StatsLevel
ColumnFamilyOptions ColumnFamilyDescriptor ColumnFamilyHandle BloomFilter))
)
(comment
(RocksDB/loadLibrary)
;; need to close
(def cf-opts (.optimizeUniversalStyleCompaction (ColumnFamilyOptions.)))
(def cf-descriptors (java.util.ArrayList. [(ColumnFamilyDescriptor. RocksDB/DEFAULT_COLUMN_FAMILY cf-opts)
(ColumnFamilyDescriptor. (.getBytes "cf-1") cf-opts)
(ColumnFamilyDescriptor. (.getBytes "cf-2") cf-opts)]))
;; need to close (before the db)
(def cf-handles (java.util.ArrayList.))
;; need to close
(def db-options (.. (DBOptions.)
(setCreateIfMissing true)
(setCreateMissingColumnFamilies true)))
;; need to close
(def db (RocksDB/open db-options "rocksdb-index-test" cf-descriptors cf-handles))
(def id->handle (into {}
(map (fn [cf-handle]
[(String. (.getName cf-handle)) cf-handle]))
cf-handles))
(.put db (id->handle "cf-1") (.getBytes "foo") (.getBytes "bar"))
(String. (.get db (id->handle "cf-1") (.getBytes "foo")))
(.put db (id->handle "cf-2") (.getBytes "foo") (.getBytes "quux"))
(String. (.get db (id->handle "cf-2") (.getBytes "foo")))
(.dropColumnFamily db (id->handle "cf-1"))
(.close (id->handle "cf-1"))
(def new-handle (.createColumnFamily db (ColumnFamilyDescriptor. (.getBytes "cf-1") cf-opts)))
(def id->handle (assoc id->handle "cf-1" new-handle))
(with-open [batch (WriteBatch.)
write-options (WriteOptions.)]
(.put batch (id->handle "cf-1") (.getBytes "a") (.getBytes "b"))
(.put batch (id->handle "cf-1") (.getBytes "c") (.getBytes "d"))
(.write db write-options batch)
)
(String. (.get db (id->handle "cf-1") (.getBytes "a")))
(String. (.get db (id->handle "cf-1") (.getBytes "c")))
(mapv #(String. %) (.multiGetAsList db
(java.util.ArrayList. [(id->handle "cf-1") (id->handle "cf-1")])
(java.util.ArrayList. [(.getBytes "a") (.getBytes "c")])))
;; need to close
(def snapshot (.getSnapshot db))
;; need to close
(def read-options (doto (ReadOptions.) (.setSnapshot snapshot)))
(String. (.get db (id->handle "cf-1") (.getBytes "foo")))
(String. (.get db (id->handle "cf-1") read-options (.getBytes "foo")))
(.put db (id->handle "cf-1") (.getBytes "foo") (.getBytes "abc"))
(.close read-options)
(.releaseSnapshot db snapshot)
(.close snapshot)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment