dodb.cr/orm.cr

201 lines
3.9 KiB
Crystal
Raw Normal View History

2018-11-19 21:06:36 +01:00
require "json"
class FS::Hash(K, V)
2018-11-19 23:35:49 +01:00
class PartitionData(V)
property name : String
property key_proc : Proc(V, String)
def initialize(@name, @key_proc)
end
end
@partitions = [] of PartitionData(V)
2018-11-19 21:06:36 +01:00
def initialize(@directory_name : String)
2018-11-19 23:35:49 +01:00
Dir.mkdir_p @directory_name
end
##
# name is the name that will be used on the file system.
def new_partition(name : String, &block : Proc(V, String))
@partitions.push PartitionData(V).new name, block
Dir.mkdir_p "#{@directory_name}/.by_#{name}"
end
def get_partition(name : String, key : K)
r_value = Array(V).new
partition_directory = "#{@directory_name}/.by_#{name}/#{key}"
Dir.each_child partition_directory do |child|
pp child
r_value << V.from_json File.read "#{partition_directory}/#{child}"
end
r_value
2018-11-19 21:06:36 +01:00
end
def []?(key)
begin
read file_path key
rescue
# FIXME: Only rescue JSON and “no such file” errors.
return nil
end
end
def [](key)
read file_path key
end
def []=(key, value)
2018-11-19 23:35:49 +01:00
# FIXME: Update partitions pointing to previous value (in any)
2018-11-19 21:06:36 +01:00
File.write file_path(key), value.to_json
2018-11-19 23:35:49 +01:00
@partitions.each do |index|
index_key = index.key_proc.call value
symlink = file_path(key, index.name, index_key)
Dir.mkdir_p File.dirname symlink
File.delete symlink if File.exists? symlink
File.symlink symlink_path(key), symlink
end
2018-11-19 21:06:36 +01:00
end
def delete(key)
value = self[key]?
begin
File.delete file_path key
rescue
# FIXME: Only intercept “no such file" errors
end
2018-11-19 23:35:49 +01:00
unless value.nil?
@partitions.each do |index|
index_key = index.key_proc.call value
symlink = file_path(key, index.name, index_key)
puts "old index #{key.to_s} => #{index_key}"
puts "symlink is #{symlink}"
File.delete symlink
end
end
2018-11-19 21:06:36 +01:00
value
end
2018-11-19 23:35:49 +01:00
##
# CAUTION: Very slow. Try not to use.
# Can be useful for making dumps or to restore a database, however.
2018-11-19 21:06:36 +01:00
def each
Dir.each_child @directory_name do |child|
2018-11-19 23:35:49 +01:00
next if child.match /^\./
2018-11-19 21:06:36 +01:00
full_path = "#{@directory_name}/#{child}"
begin
# FIXME: Only intercept JSON parsing errors.
field = read full_path
rescue
next
end
# FIXME: Will only work for String. :(
key = child.gsub /\.json$/, ""
yield key, field
end
end
private def file_path(key : K)
"#{@directory_name}/#{key.to_s}.json"
end
2018-11-19 23:35:49 +01:00
private def file_path(key : String, index_name : String, index_key : String)
"#{@directory_name}/.by_#{index_name}/#{index_key}/#{key}.json"
end
private def symlink_path(key : K)
"../../#{key.to_s}.json"
end
2018-11-19 21:06:36 +01:00
private def read(file_path : String)
V.from_json File.read file_path
end
end
2018-11-19 23:35:49 +01:00
# Basic mapping testing.
2018-11-19 21:06:36 +01:00
a = FS::Hash(String, JSON::Any).new "test-storage"
2018-11-19 23:35:49 +01:00
a["a"] = JSON::Any.new "now exists"
2018-11-19 21:06:36 +01:00
pp! a["a"]
pp! a["no file found"]?
pp! a["invalid json"]?
2018-11-19 23:35:49 +01:00
pp! a["new entry"] = JSON::Any.new "blip blop"
2018-11-19 21:06:36 +01:00
pp! a.delete "new entry"
pp! a.delete "non-existant entry"
a.each do |k, v|
pp! k, v
end
2018-11-19 23:35:49 +01:00
# Indexation testing.
require "uuid"
class Article
JSON.mapping({
id: String,
title: String,
author: String
})
def initialize(@id, @title, @author)
end
getter author
getter id
end
articles = FS::Hash(String, Article).new "articles"
by_author = articles.new_partition "author", &.author
article = Article.new UUID.random.to_s, "Bleh foo bar", "Satsuki"
articles[article.id] = article
article = Article.new UUID.random.to_s, "Bleh foo bar", "Natsuki"
articles[article.id] = article
article = Article.new UUID.random.to_s, "Bleh foo bar", "Mutsuki"
articles[article.id] = article
articles.delete articles.get_partition("author", "Natsuki")[0].id
article = Article.new UUID.random.to_s, "Bleh foo bar", "Satsuki"
articles[article.id] = article
articles.delete articles.get_partition("author", "Satsuki")[1].id
article = Article.new UUID.random.to_s, "Bleh foo bar", "Satsuki"
articles[article.id] = article
article = Article.new UUID.random.to_s, "Bleh foo bar", "Nagatsuki"
articles[article.id] = article
articles.each do |a, b|
p a, b
end