182 lines
4.2 KiB
Clojure
182 lines
4.2 KiB
Clojure
(ns ets
|
|
"Erlang :ets module — Erlang Term Storage (in-memory tables).
|
|
|
|
In CljElixir: (ets/new :my-table [:set :public]), etc.
|
|
ETS tables are mutable, concurrent, in-memory key-value stores.")
|
|
|
|
(defn new
|
|
"Creates a new ETS table. Returns table ID.
|
|
(ets/new :my-table [:set :public :named-table])
|
|
Types: :set, :ordered-set, :bag, :duplicate-bag
|
|
Access: :public, :protected (default), :private"
|
|
[name opts])
|
|
|
|
(defn insert
|
|
"Inserts one or more tuples. Returns true.
|
|
(ets/insert table {:key \"value\"})
|
|
(ets/insert table [{:a 1} {:b 2}])"
|
|
[table objects])
|
|
|
|
(defn insert-new
|
|
"Inserts only if key doesn't exist. Returns true/false."
|
|
[table objects])
|
|
|
|
(defn lookup
|
|
"Returns all objects matching `key`. Returns list.
|
|
(ets/lookup table :key) ;=> [{:key \"value\"}]"
|
|
[table key])
|
|
|
|
(defn lookup-element
|
|
"Returns specific element at `pos` for `key`.
|
|
(ets/lookup-element table :key 2) ;=> \"value\""
|
|
([table key pos])
|
|
([table key pos default]))
|
|
|
|
(defn member
|
|
"Returns true if `key` exists.
|
|
(ets/member table :key) ;=> true"
|
|
[table key])
|
|
|
|
(defn delete
|
|
"Deletes a table or entries matching `key`.
|
|
(ets/delete table) ;=> deletes entire table
|
|
(ets/delete table :key) ;=> deletes entry"
|
|
([table])
|
|
([table key]))
|
|
|
|
(defn delete-object
|
|
"Deletes a specific object from the table."
|
|
[table object])
|
|
|
|
(defn delete-all-objects
|
|
"Deletes all objects from the table."
|
|
[table])
|
|
|
|
(defn update-counter
|
|
"Atomically updates a counter. Returns new value.
|
|
(ets/update-counter table :hits 1) ;=> increments by 1
|
|
(ets/update-counter table :hits {2 1}) ;=> increments pos 2 by 1"
|
|
([table key update-op])
|
|
([table key update-op default]))
|
|
|
|
(defn update-element
|
|
"Atomically updates specific elements of a tuple."
|
|
([table key element-spec])
|
|
([table key element-spec default]))
|
|
|
|
(defn select
|
|
"Selects objects matching a match specification.
|
|
(ets/select table match-spec)"
|
|
([table match-spec])
|
|
([table match-spec limit]))
|
|
|
|
(defn match
|
|
"Pattern matches objects in the table.
|
|
(ets/match table {:_ :$1}) ;=> extracts matched values"
|
|
([table pattern])
|
|
([table pattern limit]))
|
|
|
|
(defn match-object
|
|
"Returns full objects matching the pattern.
|
|
(ets/match-object table {:_ :_}) ;=> all objects"
|
|
([table pattern])
|
|
([table pattern limit]))
|
|
|
|
(defn match-delete
|
|
"Deletes all objects matching the pattern.
|
|
(ets/match-delete table {:_ :_}) ;=> deletes all"
|
|
[table pattern])
|
|
|
|
(defn select-delete
|
|
"Deletes objects matching a match specification. Returns count."
|
|
[table match-spec])
|
|
|
|
(defn select-count
|
|
"Counts objects matching a match specification."
|
|
[table match-spec])
|
|
|
|
(defn select-replace
|
|
"Replaces objects matching a match specification."
|
|
[table match-spec])
|
|
|
|
(defn first
|
|
"Returns the first key in the table.
|
|
(ets/first table) ;=> :some-key or :'$end_of_table'"
|
|
[table])
|
|
|
|
(defn last
|
|
"Returns the last key (only for ordered-set)."
|
|
[table])
|
|
|
|
(defn next
|
|
"Returns the key after `key`.
|
|
(ets/next table :key) ;=> :next-key"
|
|
[table key])
|
|
|
|
(defn prev
|
|
"Returns the key before `key` (only for ordered-set)."
|
|
[table key])
|
|
|
|
(defn tab2list
|
|
"Converts entire table to a list.
|
|
(ets/tab2list table) ;=> [{:key1 \"val1\"} ...]"
|
|
[table])
|
|
|
|
(defn info
|
|
"Returns information about the table.
|
|
(ets/info table) ;=> keyword list
|
|
(ets/info table :size) ;=> number of objects"
|
|
([table])
|
|
([table item]))
|
|
|
|
(defn rename
|
|
"Renames a named table."
|
|
[table name])
|
|
|
|
(defn give-away
|
|
"Transfers table ownership to another process."
|
|
[table pid gift-data])
|
|
|
|
(defn i
|
|
"Prints brief info about all ETS tables."
|
|
([])
|
|
([table]))
|
|
|
|
(defn foldl
|
|
"Folds left over table entries."
|
|
[fun acc table])
|
|
|
|
(defn foldr
|
|
"Folds right over table entries."
|
|
[fun acc table])
|
|
|
|
(defn tab2file
|
|
"Dumps table to a file."
|
|
([table filename])
|
|
([table filename opts]))
|
|
|
|
(defn file2tab
|
|
"Loads table from a file."
|
|
([filename])
|
|
([filename opts]))
|
|
|
|
(defn whereis
|
|
"Returns the table ID for a named table."
|
|
[name])
|
|
|
|
(defn safe-fixtable
|
|
"Fixes/unfixes a table for safe traversal."
|
|
[table fix])
|
|
|
|
(defn fun2ms
|
|
"Converts a literal fun to a match specification."
|
|
[fun])
|
|
|
|
(defn test-ms
|
|
"Tests a match specification against a tuple."
|
|
[tuple match-spec])
|
|
|
|
(defn all
|
|
"Returns a list of all ETS table IDs."
|
|
[])
|