A minimalist append-only key/value store written in Ruby.
c = Collection.new('example.db')
c.set('hello', 'world')
c.get('hello') # => "world"
c.delete('hello')
c.get('hello') # => nil
c.set('foo', {'baz' => 'qux'})
c.get('foo')['baz'] # => "qux"
c.set('bar', [1,2,3])
c.get('bar').first # => 1
c.keys # => ["foo", "bar"]
Every update to the collection results in a JSON object being appended
to the data file (example.db
in the case above). Yes, even on deletes.
Indices are kept in memory for speedy reads and the file can periodically
be compacted to keep it from getting too unwieldy in size.
c.compact
Included is a simple socket server interface built with EventMachine. Start listening on a given port and writing to the specified data file by running:
$ ruby server.rb 3456 example.db
Example client usage:
$ telnet localhost 3456
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get foo
{"baz":"qux"}
delete foo
OK
set hello world
OK
get hello
"world"
keys
["bar","hello"]
compact
OK
exit