-
-
Save rcoder/4281278 to your computer and use it in GitHub Desktop.
This file contains 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
=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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is better than the original in multiple ways. I've just commited a revision to my implementation that borrows some of your ideas.