我一直在尝试使用PStore来保存相对大量的哈希值:
require "pstore"
store = PStore.new("data.pstore")
因为它存储在一个充满现在无用数据的文件中,如何以编程方式清除或删除“存储”?显然我可以删除store.pstore
文件,但我正在寻找的是PStore相当于:
DELETE FROM store WHERE 1=1;
答案 0 :(得分:4)
您正在寻找的是delete
:
store.transaction { store.delete(key) }
store = PStore.new("data.pstore")
#=> #<PStore:0x007fd67f35a040 @abort=false, @filename="data.pstore", @lock=#<Thread::Mutex:0x007fd67f359f28>, @thread_safe=false, @ultra_safe=false>
store.transaction { store[:foo] = :bar }
# => #<PStore:0x007fd67f35a040 @abort=false, @filename="data.pstore", @lock=#<Thread::Mutex:0x007fd67f359f28>, @rdonly=false, @table={:foo=>:bar}, @thread_safe=false, @ultra_safe=false>
store.transaction { store.delete(:foo) }
#=> :bar
store
#=> #<PStore:0x007fd67f35a040 @abort=false, @filename="data.pstore", @lock=#<Thread::Mutex:0x007fd67f359f28>, @thread_safe=false, @ultra_safe=false
如果要截断store
,可以迭代roots
并在事务中删除它们:
store.transaction do
store.roots.each do |root|
store.delete(root)
end
end