Pigeon-Ruby/dist/pigeon/storage.rb

139 lines
2.9 KiB
Ruby
Raw Normal View History

2019-11-27 01:46:50 +00:00
require "pstore"
2019-09-22 02:30:03 +00:00
module Pigeon
class Storage
2019-12-28 04:44:10 +00:00
def self.reset
@current.reset_defaults if @current
2019-12-28 04:44:10 +00:00
@current = nil
end
2019-09-22 02:30:03 +00:00
def self.current
@current ||= self.new
end
2020-03-08 16:35:09 +00:00
def get_message_by_depth(author, depth)
store.transaction do
2020-03-08 16:35:09 +00:00
# Map<[author(str), depth(int)], Signature>
store[DEPTH_INDEX_NS][[author.public_key, depth]]
end
end
def message_count
store.transaction do
store[MESG_NS].count
end
end
2019-12-11 04:05:54 +00:00
def save_message(msg)
store.transaction do
insert_and_update_index(msg)
end
end
2019-12-05 04:11:04 +00:00
def set_config(key, value)
2019-11-27 01:53:54 +00:00
store.transaction do
store[CONF_NS][key] = value
end
2019-09-22 02:30:03 +00:00
end
2019-10-26 23:13:06 +00:00
def delete_config(key)
2019-11-27 01:53:54 +00:00
store.transaction do
store[CONF_NS].delete(key)
2019-11-27 01:53:54 +00:00
end
2019-10-26 23:13:06 +00:00
end
def get_config(key)
2019-11-27 01:53:54 +00:00
store.transaction(true) do
store[CONF_NS][key]
2019-11-27 01:53:54 +00:00
end
2019-09-22 02:30:03 +00:00
end
def set_blob(data)
2019-09-22 11:00:19 +00:00
hex_digest = Digest::SHA256.hexdigest(data)
2019-11-27 01:46:50 +00:00
store.transaction do
store[BLOB_NS][hex_digest] = data
end
puts "TODO: Maybe this needs to be URLsafe base 64: "
2019-12-05 00:17:36 +00:00
[BLOB_SIGIL, hex_digest, BLOB_FOOTER].join("")
2019-09-22 11:00:19 +00:00
end
def get_blob(hex_digest)
2019-12-05 00:17:36 +00:00
hd = hex_digest.gsub(BLOB_SIGIL, "").gsub(BLOB_FOOTER, "")
2019-11-27 01:46:50 +00:00
store.transaction(true) do
store[BLOB_NS][hd]
2019-11-27 01:46:50 +00:00
end
2019-09-22 02:30:03 +00:00
end
def add_peer(identity)
path = KeyPair.strip_headers(identity)
2019-11-27 01:46:50 +00:00
store.transaction do
store[PEER_NS].add(identity)
end
identity
end
def remove_peer(identity)
2019-09-25 01:31:32 +00:00
path = KeyPair.strip_headers(identity)
2019-11-27 01:46:50 +00:00
store.transaction do
2019-11-27 01:53:54 +00:00
store[PEER_NS].delete(identity)
2019-11-27 01:46:50 +00:00
end
identity
end
def block_peer(identity)
2019-11-27 01:55:52 +00:00
remove_peer(identity)
store.transaction do
store[BLCK_NS].add(identity)
end
identity
end
def all_peers
2019-11-27 01:53:54 +00:00
store.transaction(true) do
store[PEER_NS].to_a
2019-11-27 01:53:54 +00:00
end
end
def all_blocks
2019-11-27 01:55:52 +00:00
store.transaction(true) do
store[BLCK_NS].to_a
2019-11-27 01:55:52 +00:00
end
end
def reset_defaults
2019-12-28 04:44:10 +00:00
@store.transaction do
@store[DEPTH_INDEX_NS] = {}
@store[BLOB_NS] = {}
@store[CONF_NS] = {}
@store[MESG_NS] = {}
@store[BLCK_NS] = Set.new
@store[PEER_NS] = Set.new
2019-12-28 04:44:10 +00:00
end
@store
2019-12-28 04:44:10 +00:00
end
2019-11-27 01:46:50 +00:00
private
2019-11-27 01:46:50 +00:00
def store
unless @store
@store = PStore.new(PIGEON_DB_PATH)
reset_defaults
end
return @store
end
def insert_and_update_index(message)
# STEP 1: Update MESG_NS, the main storage spot.
store[MESG_NS][message.multihash] = message
# STEP 2: Update the "message by author and depth" index
# this index is used to find a person's nth
# message
2020-03-08 16:35:09 +00:00
# SECURITY AUDIT: How can we be certain the message is
# not lying about its depth?
key = [message.author.public_key, message.depth]
store[DEPTH_INDEX_NS][key] = message.multihash
2019-09-25 00:48:02 +00:00
end
2019-09-22 02:30:03 +00:00
end
end