Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support both pre- and post-#83 SDBM key formats #97

Merged
merged 1 commit into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions lib/gel/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,12 @@ def [](key)

if value =~ /\A~(\d+)\z/
value = $1.to_i.times.map do |idx|
@sdbm["#{key}~#{idx}"]
@sdbm["#{key}~#{idx}"] || @sdbm["#{key}---#{idx}"] ||
raise("missing key: " + "#{key}~#{idx}".inspect)
end.join
end

return Marshal.load(value)
Marshal.load(value)
end


Expand All @@ -211,21 +212,34 @@ def [](key)
# amount of extra values stored to hold the split string. This amount is
# determined by string size split by the arbitrary limit imposed by SDBM
def []=(key, value)
return unless value && key
raise if key.nil?

if value.nil?
delete key
return
end

key = key.to_s

dump = Marshal.dump(value)
slicesize = SDBM_PAIRMAX - key.length - 3 # "#{key}~#{i}" where i <= 99
slices = dump.length / slicesize + 1

if (existing_value = @sdbm[key]) && existing_value =~ /\A~(\d+)\z/ && $1.to_i > slices
$1.to_i.times do |idx|
@sdbm.delete("#{key}~#{idx}") || @sdbm.delete("#{key}---#{idx}")
end
end

if slices > 1
slices.times.map do |idx|
slicekey = "#{key.to_s}~#{idx}"
slices.times do |idx|
slicekey = "#{key}~#{idx}"
slicedump = dump.slice!(0, slicesize)
@sdbm[slicekey] = slicedump
end
@sdbm["#{key.to_s}"] = "~#{slices}"
@sdbm[key] = "~#{slices}"
else
@sdbm[key.to_s] = dump
@sdbm[key] = dump
end
end

Expand All @@ -235,11 +249,11 @@ def delete(key)

if value =~ /\A~(\d+)\z/
$1.to_i.times.map do |idx|
@sdbm.delete("#{key}~#{idx}")
@sdbm.delete("#{key}~#{idx}") || @sdbm.delete("#{key}---#{idx}")
end.join
end

return Marshal.load(value)
Marshal.load(value)
end
end

Expand Down
33 changes: 33 additions & 0 deletions test/db_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,37 @@ def test_sdbm_store_value_retrieval
assert_equal db['test'], hash_string
end
end

def test_previous_multikey_format
skip unless defined? ::SDBM
Dir.mktmpdir do |dir|
db = Gel::DB::SDBM.new(dir, "test-store")

sdbm = db.instance_variable_get(:@sdbm)
sdbm["test"] = "~4"
sdbm["test---0"] = "\x04\x08\"\x09a"
sdbm["test---1"] = "b"
sdbm["test---2"] = "c"
sdbm["test---3"] = "d"

assert_equal "abcd", db["test"]

assert_equal %w(test test---0 test---1 test---2 test---3), sdbm.keys.sort

db["test"] = "short"

assert_equal %w(test), sdbm.keys.sort

sdbm["test"] = "~4"
sdbm["test---0"] = "\x04\x08\"\x09a"
sdbm["test---1"] = "b"
sdbm["test---2"] = "c"
sdbm["test---3"] = "d"

long_string = "*" * (Gel::DB::SDBM::SDBM_PAIRMAX + 200)
db["test"] = long_string

assert_equal %w(test test~0 test~1), sdbm.keys.sort
end
end
end