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

Add ability to clear namespaces #202

Merged
merged 1 commit into from
Dec 13, 2022
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## x.y.z

- Add ability to clear namespaces (#202). This operation can run for a very long time if the namespace contains
lots of keys! It should be used in tests, or when the namespace is small enough and you are sure you know what you are doing.

## 1.9.0

- Accept Proc as a namespace (#203)
Expand Down
31 changes: 31 additions & 0 deletions lib/redis/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,32 @@ def eval(*args)
end
ruby2_keywords(:eval) if respond_to?(:ruby2_keywords, true)

# This operation can run for a very long time if the namespace contains lots of keys!
# It should be used in tests, or when the namespace is small enough
# and you are sure you know what you are doing.
def clear
if warning?
warn("This operation can run for a very long time if the namespace contains lots of keys! " +
"It should be used in tests, or when the namespace is small enough " +
"and you are sure you know what you are doing.")
end

batch_size = 1000

if supports_scan?
cursor = "0"
begin
cursor, keys = scan(cursor, count: batch_size)
del(*keys) unless keys.empty?
end until cursor == "0"
else
all_keys = keys("*")
all_keys.each_slice(batch_size) do |keys|
del(*keys)
end
end
end

ADMINISTRATIVE_COMMANDS.keys.each do |command|
define_method(command) do |*args, &block|
raise NoMethodError if deprecations?
Expand Down Expand Up @@ -591,5 +617,10 @@ def create_enumerator(&block)
Enumerator.new(&block)
end
end

def supports_scan?
redis_version = @redis.info["redis_version"]
Gem::Version.new(redis_version) >= Gem::Version.new("2.8.0")
end
end
end
26 changes: 26 additions & 0 deletions spec/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1048,4 +1048,30 @@
expect(sub_sub_namespaced.full_namespace).to eql("ns:sub1:sub2")
end
end

describe :clear do
it "warns with helpful output" do
expect { @namespaced.clear }.to output(/can run for a very long time/).to_stderr
end

it "should delete all the keys" do
@redis.set("foo", "bar")
@namespaced.mset("foo1", "bar", "foo2", "bar")
capture_stderr { @namespaced.clear }

expect(@redis.keys).to eq ["foo"]
expect(@namespaced.keys).to be_empty
end

it "should delete all the keys in older redis" do
allow(@redis).to receive(:info).and_return({ "redis_version" => "2.7.0" })

@redis.set("foo", "bar")
@namespaced.mset("foo1", "bar", "foo2", "bar")
capture_stderr { @namespaced.clear }

expect(@redis.keys).to eq ["foo"]
expect(@namespaced.keys).to be_empty
end
end
end