Skip to content

Instantly share code, notes, and snippets.

@rcoder
Forked from lecram/escher.py
Created December 14, 2012 00:03
Show Gist options
  • Save rcoder/4281278 to your computer and use it in GitHub Desktop.
Save rcoder/4281278 to your computer and use it in GitHub Desktop.
=begin
Single-file K/V database and query script.
This one also does compression (because disk I/O and efficiency and
"best practices" and shut up, that's why).
Inspired by (and hopefully more ridiculous than) https://gist.github.com/4280027
=end
USAGE = <<END
{escher} -- one-file key-value storage.
Usage:
Listing current keys:
$ {escher}
Getting the value of the foo key:
$ {escher} foo
Setting the value of foo to bar:
$ {escher} foo bar
Removing the key foo:
$ {escher} foo ""
Removing everything:
$ {escher} --clean
Printing this help message:
$ {escher} --help
END
require 'base64'
require 'zlib'
$data = Marshal.load(Zlib::Inflate.inflate(Base64.decode64(DATA.read)))
$changed = false
END {
if $changed
me = __FILE__
curr = File.read(me)
code, data = curr.split(/^__END__$/)
b64_data = Base64.encode64(Zlib::Deflate.deflate(Marshal.dump($data)))
open(me, 'w') {|fh| fh.write("#{code}__END__\n#{b64_data}") }
end
}
case ARGV.size
when 0
puts $data.keys.join("\n")
when 1
arg1 = ARGV.shift
case arg1
when '--clear'
$data = {}
$changed = true
when '--help'
STDERR.puts(USAGE.gsub(/{escher}/, File.basename(__FILE__)))
exit
else
puts $data[arg1]
end
when 2
key, val = ARGV
if val == ""
$data.delete(key)
else
$data[key] = val
end
puts val
$changed = true
end
__END__
eJxj4ahmAAABIgCI
@lecram
Copy link

lecram commented Dec 18, 2012

This is better than the original in multiple ways. I've just commited a revision to my implementation that borrows some of your ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment