DRY up storage initialization / reset code

This commit is contained in:
Netscape Navigator 2020-03-08 11:18:05 -05:00
parent be5dfc982a
commit c621e5c070
4 changed files with 49 additions and 25 deletions

View File

@ -33,10 +33,14 @@ Eg: `pigeon identity show` becomes `./pigeon-cli show`.
- [ ] pigeon draft save
- [ ] pigeon message find
- [ ] pigeon message find-all
- [ ] Map/reduce plugin support for custom indices?
- [ ] 100% documentation
- [ ] add parsers and validators for all CLI inputs
- [ ] Performance benchmarks
- [ ] Performance tuning (DO THIS LAST!)
- [ ] Add Lipmaa links like the Bamboo folks do.
- [ ] Update spec to look [like this](https://gist.github.com/RickCarlino/3ff4178db4a75fd135832c403cd313d4)
- [ ] Publish a RubyGem
# Idea Bin
- [ ] Map/reduce plugin support for custom indices?
- [ ] Add Lipmaa links like the Bamboo folks do.
- [ ] Ability to add a blob in one swoop using File objects and `Message#[]=`, maybe?

View File

@ -3,7 +3,7 @@ require "pstore"
module Pigeon
class Storage
def self.reset
@current.implode if @current
@current.reset_defaults if @current
@current = nil
end
@ -14,48 +14,43 @@ module Pigeon
def get_message_by_depth(depth)
store.transaction do
# Map<Integer, Signature>
index = store[DEPTH_INDEX_NS] ||= {}
index[depth]
store[DEPTH_INDEX_NS][depth]
end
end
def message_count
store.transaction do
index = store[MESG_NS] ||= {}
index.count
store[MESG_NS].count
end
end
def save_message(msg)
store.transaction do
store[MESG_NS] ||= {}
store[MESG_NS][msg.signature] = msg
end
end
def set_config(key, value)
store.transaction do
store[CONF_NS] ||= {}
store[CONF_NS][key] = value
end
end
def delete_config(key)
store.transaction do
(store[CONF_NS] || {}).delete(key)
store[CONF_NS].delete(key)
end
end
def get_config(key)
store.transaction(true) do
(store[CONF_NS] || {})[key]
store[CONF_NS][key]
end
end
def set_blob(data)
hex_digest = Digest::SHA256.hexdigest(data)
store.transaction do
store[BLOB_NS] ||= {}
store[BLOB_NS][hex_digest] = data
end
@ -65,7 +60,6 @@ module Pigeon
def get_blob(hex_digest)
hd = hex_digest.gsub(BLOB_SIGIL, "").gsub(BLOB_FOOTER, "")
store.transaction(true) do
store[BLOB_NS] ||= {}
store[BLOB_NS][hd]
end
end
@ -73,7 +67,6 @@ module Pigeon
def add_peer(identity)
path = KeyPair.strip_headers(identity)
store.transaction do
store[PEER_NS] ||= Set.new
store[PEER_NS].add(identity)
end
identity
@ -82,7 +75,6 @@ module Pigeon
def remove_peer(identity)
path = KeyPair.strip_headers(identity)
store.transaction do
store[PEER_NS] ||= Set.new
store[PEER_NS].delete(identity)
end
identity
@ -91,7 +83,6 @@ module Pigeon
def block_peer(identity)
remove_peer(identity)
store.transaction do
store[BLCK_NS] ||= Set.new
store[BLCK_NS].add(identity)
end
identity
@ -99,28 +90,40 @@ module Pigeon
def all_peers
store.transaction(true) do
(store[PEER_NS] || Set.new).to_a
store[PEER_NS].to_a
end
end
def all_blocks
store.transaction(true) do
(store[BLCK_NS] || Set.new).to_a
store[BLCK_NS].to_a
end
end
def implode
def reset_defaults
@store.transaction do
@store.roots.map do |x|
store.delete(x)
end
@store[DEPTH_INDEX_NS] = {}
@store[BLOB_NS] = {}
@store[CONF_NS] = {}
@store[MESG_NS] = {}
@store[BLCK_NS] = Set.new
@store[PEER_NS] = Set.new
end
@store
end
private
def store
@store ||= PStore.new(PIGEON_DB_PATH)
unless @store
@store = PStore.new(PIGEON_DB_PATH)
reset_defaults
end
return @store
end
def update_indices(message)
puts "TODO: Finish this!!"
end
end
end

View File

@ -46,5 +46,21 @@ RSpec.describe Pigeon::Message do
expect(actual).to eq(expected)
end
it "creates a chain of messages"
it "creates a chain of messages" do
all = []
1.upto(5) do |n|
print "Begin message chain #{n} / 3"
expected_depth = n - 1
draft1 = Pigeon::Draft.create(kind: "unit_test")
draft1["description"] = "Message number #{n}"
message = Pigeon::Message.from_draft(draft1)
all.push(message)
expect(message.depth).to eq(expected_depth)
if n > 1
expect(message.prev).to eq(all[n - 2].signature)
else
expect(message.prev).to be nil
end
end
end
end

View File

@ -5,7 +5,8 @@ RSpec.describe Pigeon::Storage do
IDS = %w(@_TlC2z3FT4fimecC4eytrBhOwhLUZsVBZEZriBO9cWs=.ed25519
@28FyT7evjcYrrwngr8G2V1HZ0ODK0VPsFctDEZwfZJc=.ed25519)
let(:s) do
FileUtils.rm_r(Pigeon::PIGEON_DB_PATH)
# FileUtils.rm_r(Pigeon::PIGEON_DB_PATH)
Pigeon::Storage.reset
Pigeon::Storage.current
end