public
Last active

ShitDB - YAML-backed document-oriented database in pure ruby

  • Download Gist
.gitignore
1
my_db
shitdb.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
require 'rubygems'
require 'minitest/spec'
require 'minitest/autorun'
 
require 'yaml'
 
class DB
def initialize(name)
@name = File.join(File.dirname(__FILE__), name)
end
 
def collection(name)
Collection.new(self, name)
end
 
def file
@file ||= File.exist?(@name) ? YAML.load(File.open(@name)) : {}
end
 
def persist
File.open(@name, 'w') do |f|
f.write YAML.dump(@file)
end
end
end
 
class Collection
attr_reader :db
def initialize(db, name)
@db = db
@name = name
@db.file[@name] ||= []
@db.file["_#{@name}_last_id"] ||= 0
end
 
def persist
@db.persist
end
 
def get(id)
@db.file[@name].detect do |record|
record[:id] == id
end
end
 
def put(doc)
new_id = @db.file["_#{@name}_last_id"] + 1
@db.file[@name] << doc.update(:id => new_id)
@db.file["_#{@name}_last_id"] = new_id
end
 
def all
@db.file[@name]
end
 
def where(attrs)
@db.file[@name].select do |record|
attrs.map do |attr|
record[attr.first] == attr.last
end.all?{|n| n == true}
end
end
end
 
 
require 'fileutils'
 
describe 'acceptance tests' do
before do
FileUtils.rm('my_db') if File.exist?('my_db')
@users = DB.new('my_db').collection('users')
end
 
describe 'storage' do
it 'saves records in memory' do
@users.put(:name => 'James')
@users.put(:name => 'John', :age => 30)
 
users = @users.all.map { |record| record[:name] }
assert_equal 2, users.length
assert_includes users, 'James'
assert_includes users, 'John'
end
 
it 'does not save them to disk' do
@users.put(:name => 'James')
@users.put(:name => 'John', :age => 30)
 
persisted = DB.new('my_db').collection('users').all
assert_equal 0, persisted.length
end
 
it 'persists them when told so' do
@users.put(:name => 'James')
@users.put(:name => 'John', :age => 30)
 
@users.persist
 
users = DB.new('my_db').collection('users').all.map { |record| record[:name] }
assert_equal 2, users.length
assert_includes users, 'James'
assert_includes users, 'John'
end
end
 
describe 'querying' do
before do
@users.put(:name => 'James')
@users.put(:name => 'John', :age => 30)
@users.put(:name => 'Charles', :age => 30)
end
 
it 'performs simple queries with one condition inefficiently' do
result = @users.where(:age => 30).map { |record| record[:name] }
assert_equal 2, result.length
assert_includes result, 'John'
assert_includes result, 'Charles'
end
 
it 'performs simple queries with multiple conditions inefficiently' do
result = @users.where(:name => 'Charles', :age => 30).map { |record| record[:name]}
assert_equal 1, result.length
assert_equal 'Charles', result.first
end
 
it 'retrieves records by id' do
james = @users.get(1)
john = @users.get(2)
charles = @users.get(3)
 
assert_equal 'James', james[:name]
assert_equal 'John', john[:name]
assert_equal 'Charles', charles[:name]
end
end
 
describe 'consistency' do
it 'assigns autoincremental ids' do
@users.put(:name => 'James')
@users.put(:name => 'John')
 
ids = @users.all.map { |record| record[:id] }.compact
 
assert_equal 2, ids.length
refute_equal ids.first, ids.last
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.