Info storage: WIP.

This commit is contained in:
Philippe PITTOLI 2020-01-11 12:47:11 +01:00
parent 0c14d8a32e
commit f5f75dda7b
2 changed files with 51 additions and 4 deletions

View File

@ -1,4 +1,6 @@
require "json"
# keep track of connected users and their requests
# TODO: requests should be handled concurrently
class User
@ -14,9 +16,36 @@ class User
end
end
class TransferInfo
include JSON::Serializable
property owner : Int32
property file_info : FileInfo
property chunks : Hash(Int32, Bool)
def initialize(@owner, @file_info)
@chunks = Hash(Int32, Bool).new
@file_info.nb_chunks.times do |n|
@chunks[n] = false
end
end
end
class Context
class_property service_name = "filestorage"
class_property storage_directory = "./storage"
class_property file_info_directory = "./file-infos"
class_property db : DODB::DataBase(TransferInfo) = self.init_db
def init_db
@@db = DODB::DataBase(TransferInfo).new @@file_info_directory
# init index, partitions and tags
Context.db.new_index "filedigest", &.file_info.digest
Context.db.new_partition "owner", &.owner
Context.db.new_tags "tags", &.tags
end
# list of connected users (fd => uid)
class_property connected_users = Hash(Int32, Int32).new

View File

@ -13,11 +13,27 @@ def hdl_transfer(message : FileStorage::Transfer,
# pp! message
file_info = nil
begin
file_info = user.uploads.select do |v|
v.file.digest == message.filedigest
end.first.file
pp! file_info
rescue e : IndexError
puts "No recorded upload request for file #{message.filedigest}"
rescue e
puts "Unexpected error: #{e}"
end
# Get the transfer info from the db
# is the file info recorded?
by_digest = Context.db.get_index "filedigest"
transfer_info = by_digest.get? message.filedigest
by_owner = Context.db.get_partition "owner"
pp! by_owner
# TODO: verify the digest
@ -42,6 +58,8 @@ def hdl_transfer(message : FileStorage::Transfer,
# TODO: register the file with dodb, with its tags
Context.db <<
FileStorage::Response.new mid, "Ok"
rescue e