4

I've been experimenting with PStore to hold relatively large numbers of hash values using:

 require "pstore"
 store = PStore.new("data.pstore")

Seeing as this is stored in a file full of now-useless data, how do I clear or delete the "store" programatically? Obviously I could just delete the store.pstore file, but what i'm looking for is the PStore equivalent of:

DELETE FROM store WHERE 1=1;
Nakilon
  • 34,866
  • 14
  • 107
  • 142
user1051849
  • 2,307
  • 5
  • 26
  • 43

1 Answers1

4

What you're looking for is 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

If you want to truncate the store you can iterate over the roots and delete them in a transaction:

store.transaction do
  store.roots.each do |root|
    store.delete(root)
  end
end
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • I don't think ObjectSpace mumbo-jumbo is relevant here. Do you think he has hundreds-thousands of PStore objects randomly lying around? – Sergio Tulentsev Sep 28 '16 at 20:25
  • Also, the question was: how to truncate the store? – Sergio Tulentsev Sep 28 '16 at 20:25
  • Also, no need for shelling out when there is ruby api for deleting files :) – Sergio Tulentsev Sep 28 '16 at 20:28
  • @SergioTulentsev to truncate the store (if I understand it correctly) one just would iterate over table's keys (`store.roots`) and delete them, unless there is more efficient one-go method for it. – Andrey Deineko Sep 28 '16 at 20:32
  • @SergioTulentsev about ObjectSpace - you must be right, just didn't think of a better way to get all `PStore` objects. What do you mean by "shelling out"? If you mean `rm` thing - it's just that I wasn't able to delete the file with `File.delete` - or did I just have something messed up? I always appreciate your input, so thx in advance! – Andrey Deineko Sep 28 '16 at 20:34
  • Yes, "to shell out" means going to the OS/shell to do some stuff. In this case, deleting a file by executing `rm` command in a subprocess. It's interesting, why couldn't you delete it with `File.delete`? Perhaps the file was still open by the store? – Sergio Tulentsev Sep 28 '16 at 20:42
  • "a better way to get all PStore objects" - there is only one object in the question. :) – Sergio Tulentsev Sep 28 '16 at 20:43
  • Yes, I think the way to go is iterate roots and delete them in a transaction. Could you put that into the answer (and remove the objectspace part)? – Sergio Tulentsev Sep 28 '16 at 20:44
  • @SergioTulentsev edited. just tried it now and `File.delete(File.absolute_path(store.path))` and got `Errno::ENOENT: No such file or directory @ unlink_internal - /path/to/data.pstore`.. (`"data.pstore"` is a filename) – Andrey Deineko Sep 28 '16 at 20:47
  • When I do `File.delete(store.path)` it returns `1`, but I still can access `store` object.. – Andrey Deineko Sep 28 '16 at 20:48
  • @SergioTulentsev edited the answer, don't know why yesterday I wrote it the way I did..? – Andrey Deineko Sep 29 '16 at 06:47
  • 1
    Yep, that's what I meant – Sergio Tulentsev Sep 29 '16 at 07:11