Improved errors.
parent
b3296bd1e5
commit
0ef95ce452
73
orm.cr
73
orm.cr
|
@ -37,7 +37,7 @@ class FS::Hash(K, V)
|
||||||
r_value
|
r_value
|
||||||
end
|
end
|
||||||
|
|
||||||
def []?(key)
|
def []?(key : K) : V?
|
||||||
begin
|
begin
|
||||||
read file_path key
|
read file_path key
|
||||||
rescue
|
rescue
|
||||||
|
@ -46,11 +46,11 @@ class FS::Hash(K, V)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def [](key)
|
def [](key : K) : V
|
||||||
read file_path key
|
read file_path key
|
||||||
end
|
end
|
||||||
|
|
||||||
def []=(key, value)
|
def []=(key : K, value : V)
|
||||||
# FIXME: Update partitions pointing to previous value (in any)
|
# FIXME: Update partitions pointing to previous value (in any)
|
||||||
|
|
||||||
File.write file_path(key), value.to_json
|
File.write file_path(key), value.to_json
|
||||||
|
@ -68,7 +68,7 @@ class FS::Hash(K, V)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(key)
|
def delete(key : K)
|
||||||
value = self[key]?
|
value = self[key]?
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
@ -133,68 +133,3 @@ class FS::Hash(K, V)
|
||||||
end
|
end
|
||||||
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
|
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue