daybreak 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/daybreak.gemspec +1 -0
- data/lib/daybreak/db.rb +7 -2
- data/lib/daybreak/reader.rb +3 -1
- data/lib/daybreak/record.rb +3 -2
- data/lib/daybreak/version.rb +1 -1
- data/lib/daybreak/writer.rb +13 -5
- data/test/prof.rb +1 -1
- data/test/test.rb +8 -2
- metadata +5 -4
data/daybreak.gemspec
CHANGED
@@ -12,6 +12,7 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.test_files = gem.files.grep(%r{^(test)/})
|
13
13
|
gem.name = "daybreak"
|
14
14
|
gem.require_paths = ["lib"]
|
15
|
+
gem.licenses = ["MIT"]
|
15
16
|
gem.version = Daybreak::VERSION
|
16
17
|
gem.add_development_dependency 'simplecov'
|
17
18
|
gem.add_development_dependency 'ruby-prof'
|
data/lib/daybreak/db.rb
CHANGED
@@ -11,7 +11,8 @@ module Daybreak
|
|
11
11
|
# @param [String] file the path to the db file
|
12
12
|
# @param default the default value to store and return when a key is
|
13
13
|
# not yet in the database.
|
14
|
-
# @yield [key]
|
14
|
+
# @yield [key] a block that will return the default value to store.
|
15
|
+
# @yieldparam [String] key the key to be stored.
|
15
16
|
def initialize(file, default=nil, &blk)
|
16
17
|
@file_name = file
|
17
18
|
reset!
|
@@ -60,6 +61,9 @@ module Daybreak
|
|
60
61
|
alias_method :get, :"[]"
|
61
62
|
|
62
63
|
# Iterate over the key, value pairs in the database.
|
64
|
+
# @yield [key, value] blk the iterator for each key value pair.
|
65
|
+
# @yieldparam [String] key the key.
|
66
|
+
# @yieldparam value the value from the database.
|
63
67
|
def each(&blk)
|
64
68
|
keys.each { |k| blk.call(k, get(k)) }
|
65
69
|
end
|
@@ -70,6 +74,7 @@ module Daybreak
|
|
70
74
|
end
|
71
75
|
|
72
76
|
# Does this db have a value for this key?
|
77
|
+
# @param [key#to_s] key the key to check if the DB has a key.
|
73
78
|
def has_key?(key)
|
74
79
|
@table.has_key? key.to_s
|
75
80
|
end
|
@@ -104,8 +109,8 @@ module Daybreak
|
|
104
109
|
|
105
110
|
# Reset and empty the database file.
|
106
111
|
def empty!
|
107
|
-
reset!
|
108
112
|
@writer.truncate!
|
113
|
+
reset!
|
109
114
|
end
|
110
115
|
|
111
116
|
# Force all queued commits to be written to disk.
|
data/lib/daybreak/reader.rb
CHANGED
@@ -8,11 +8,13 @@ module Daybreak
|
|
8
8
|
@file_name = file
|
9
9
|
end
|
10
10
|
|
11
|
-
# Close
|
11
|
+
# Close the Reader's file descriptor.
|
12
12
|
def close!
|
13
13
|
@fd.close unless @fd.nil?
|
14
14
|
end
|
15
15
|
|
16
|
+
# Read all values from the aof file.
|
17
|
+
#
|
16
18
|
# Right now this is really expensive, every call to read will
|
17
19
|
# close and reread the whole db file, but since cross process
|
18
20
|
# consistency is handled by the user, this should be fair warning.
|
data/lib/daybreak/record.rb
CHANGED
@@ -16,7 +16,8 @@ module Daybreak
|
|
16
16
|
@data = data
|
17
17
|
end
|
18
18
|
|
19
|
-
# Read a record from an open io source, check the CRC, and set
|
19
|
+
# Read a record from an open io source, check the CRC, and set <tt>@key</tt>
|
20
|
+
# and <tt>@data</tt>.
|
20
21
|
# @param [#read] io an IO instance to read from
|
21
22
|
def read(io)
|
22
23
|
lock io do
|
@@ -28,7 +29,7 @@ module Daybreak
|
|
28
29
|
self
|
29
30
|
end
|
30
31
|
|
31
|
-
# The serialized representation of the key value pair plus the CRC
|
32
|
+
# The serialized representation of the key value pair plus the CRC.
|
32
33
|
# @return [String]
|
33
34
|
def representation
|
34
35
|
raise UnnacceptableDataError, "key and data must be defined" if @key.nil? || @data.nil?
|
data/lib/daybreak/version.rb
CHANGED
data/lib/daybreak/writer.rb
CHANGED
@@ -4,11 +4,9 @@ module Daybreak
|
|
4
4
|
class Writer
|
5
5
|
# Open up the file, ready it for binary and nonblocking writing.
|
6
6
|
def initialize(file)
|
7
|
-
@
|
8
|
-
@fd.binmode
|
7
|
+
@file = file
|
9
8
|
|
10
|
-
|
11
|
-
@fd.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK | f)
|
9
|
+
open!
|
12
10
|
|
13
11
|
@worker = Worker.new(@fd)
|
14
12
|
end
|
@@ -28,7 +26,7 @@ module Daybreak
|
|
28
26
|
@worker.flush!
|
29
27
|
end
|
30
28
|
|
31
|
-
# Finish writing and close the file descriptor
|
29
|
+
# Finish writing and close the file descriptor.
|
32
30
|
def close!
|
33
31
|
finish!
|
34
32
|
@fd.close
|
@@ -36,11 +34,21 @@ module Daybreak
|
|
36
34
|
|
37
35
|
# Truncate the file.
|
38
36
|
def truncate!
|
37
|
+
flush!
|
39
38
|
@fd.truncate(0)
|
39
|
+
open!
|
40
40
|
end
|
41
41
|
|
42
42
|
private
|
43
43
|
|
44
|
+
def open!
|
45
|
+
@fd = File.open @file, 'a'
|
46
|
+
@fd.binmode
|
47
|
+
|
48
|
+
f = @fd.fcntl(Fcntl::F_GETFL, 0)
|
49
|
+
@fd.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK | f)
|
50
|
+
end
|
51
|
+
|
44
52
|
# Workers handle the actual fiddly bits of asynchronous io and
|
45
53
|
# and handle background writes.
|
46
54
|
class Worker
|
data/test/prof.rb
CHANGED
data/test/test.rb
CHANGED
@@ -59,10 +59,9 @@ describe "database functions" do
|
|
59
59
|
db2.set! '1', 5
|
60
60
|
@db.read!
|
61
61
|
assert_equal @db['1'], 5
|
62
|
-
@db.close!
|
63
62
|
end
|
64
63
|
|
65
|
-
it "
|
64
|
+
it "should be able to handle another process's call to compact" do
|
66
65
|
20.times {|i| @db.set i, i, true }
|
67
66
|
db2 = Daybreak::DB.new DB_PATH
|
68
67
|
20.times {|i| @db.set i, i + 1, true }
|
@@ -71,6 +70,13 @@ describe "database functions" do
|
|
71
70
|
assert_equal 20, db2['19']
|
72
71
|
end
|
73
72
|
|
73
|
+
it "can empty the database" do
|
74
|
+
20.times {|i| @db[i] = i }
|
75
|
+
@db.empty!
|
76
|
+
db2 = Daybreak::DB.new DB_PATH
|
77
|
+
assert_equal nil, db2['19']
|
78
|
+
end
|
79
|
+
|
74
80
|
after do
|
75
81
|
@db.empty!
|
76
82
|
@db.close!
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: daybreak
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: simplecov
|
@@ -71,7 +71,8 @@ files:
|
|
71
71
|
- test/test.rb
|
72
72
|
- test/test_helper.rb
|
73
73
|
homepage: http://propublica.github.com/daybreak/
|
74
|
-
licenses:
|
74
|
+
licenses:
|
75
|
+
- MIT
|
75
76
|
post_install_message:
|
76
77
|
rdoc_options: []
|
77
78
|
require_paths:
|
@@ -90,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
91
|
version: '0'
|
91
92
|
requirements: []
|
92
93
|
rubyforge_project:
|
93
|
-
rubygems_version: 1.8.
|
94
|
+
rubygems_version: 1.8.23
|
94
95
|
signing_key:
|
95
96
|
specification_version: 3
|
96
97
|
summary: Daybreak provides an in memory key-value store that is easily enumerable
|