Improved errors.

remotes/1708105384931250775/master
Luka Vandervelden 2018-11-22 22:28:41 +09:00
parent b3296bd1e5
commit 0ef95ce452
2 changed files with 72 additions and 69 deletions

73
orm.cr
View File

@ -37,7 +37,7 @@ class FS::Hash(K, V)
r_value
end
def []?(key)
def []?(key : K) : V?
begin
read file_path key
rescue
@ -46,11 +46,11 @@ class FS::Hash(K, V)
end
end
def [](key)
def [](key : K) : V
read file_path key
end
def []=(key, value)
def []=(key : K, value : V)
# FIXME: Update partitions pointing to previous value (in any)
File.write file_path(key), value.to_json
@ -68,7 +68,7 @@ class FS::Hash(K, V)
end
end
def delete(key)
def delete(key : K)
value = self[key]?
begin
@ -133,68 +133,3 @@ class FS::Hash(K, V)
end
end
# Basic mapping testing.
a = FS::Hash(String, JSON::Any).new "test-storage"
a["a"] = JSON::Any.new "now exists"
pp! a["a"]
pp! a["no file found"]?
pp! a["invalid json"]?
pp! a["new entry"] = JSON::Any.new "blip blop"
pp! a.delete "new entry"
pp! a.delete "non-existant entry"
a.each do |k, v|
pp! k, v
end
# 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

68
test.cr Normal file
View File

@ -0,0 +1,68 @@
require "json"
require "./orm.cr"
# Basic mapping testing.
a = FS::Hash(String, JSON::Any).new "test-storage"
a["a"] = JSON::Any.new "now exists"
pp! a["a"]
pp! a["no file found"]?
pp! a["invalid json"]?
pp! a["new entry"] = JSON::Any.new "blip blop"
pp! a.delete "new entry"
pp! a.delete "non-existant entry"
a.each do |k, v|
pp! k, v
end
# 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