From fc21b48870b928b7bdba6e60a9bc643cc2fabe6e Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Wed, 11 Dec 2019 18:05:02 +0100 Subject: [PATCH] Name and namespace change. This is very much a breaking change. --- shard.yml | 4 ++-- src/{fs.cr => fsdb.cr} | 18 +++++++++--------- test-edit.cr | 17 +++++++++++++++-- test-index.cr | 6 +++--- test-tags.cr | 4 ++-- test.cr | 6 +++--- 6 files changed, 34 insertions(+), 21 deletions(-) rename src/{fs.cr => fsdb.cr} (95%) diff --git a/shard.yml b/shard.yml index acbdc4d..8b460c8 100644 --- a/shard.yml +++ b/shard.yml @@ -1,10 +1,10 @@ -name: fs +name: fsdb version: 0.2.0 authors: - Luka Vandervelden description: | - Filesystem abstraction through near-native types. + Simple, embeddable document-oriented database in Crystal. license: MIT diff --git a/src/fs.cr b/src/fsdb.cr similarity index 95% rename from src/fs.cr rename to src/fsdb.cr index d5335c7..dd61ebd 100644 --- a/src/fs.cr +++ b/src/fsdb.cr @@ -2,14 +2,14 @@ require "file_utils" require "json" -abstract class FS::Indexer(V) +abstract class FSDB::Indexer(V) abstract def index (key : String, value : V) abstract def deindex (key : String, value : V) abstract def check! (key : String, value : V, old_value : V?) abstract def name : String end -class FS::Partition(V) < FS::Indexer(V) +class FSDB::Partition(V) < FSDB::Indexer(V) property name : String property key_proc : Proc(V, String) getter storage_root : String @@ -71,7 +71,7 @@ class FS::Partition(V) < FS::Indexer(V) end end -class FS::Index(V) < FS::Indexer(V) +class FSDB::Index(V) < FSDB::Indexer(V) property name : String property key_proc : Proc(V, String) getter storage_root : String @@ -136,7 +136,7 @@ class FS::Index(V) < FS::Indexer(V) end end -class FS::Tags(V) < FS::Indexer(V) +class FSDB::Tags(V) < FSDB::Indexer(V) property name : String property key_proc : Proc(V, Array(String)) getter storage_root : String @@ -200,10 +200,10 @@ class FS::Tags(V) < FS::Indexer(V) end end -class FS::IndexOverload < Exception +class FSDB::IndexOverload < Exception end -class FS::Hash(K, V) +class FSDB::DataBase(K, V) @indexers = [] of Indexer(V) def initialize(@directory_name : String) @@ -235,20 +235,20 @@ class FS::Hash(K, V) def get_index(name : String, key) index = @indexers.find &.name.==(name) - index.not_nil!.as(FS::Index).get key + index.not_nil!.as(FSDB::Index).get key end # FIXME: Is this “key” really a K, not just a String? def get_partition(table_name : String, partition_name : String) partition = @indexers.find &.name.==(table_name) - partition.not_nil!.as(FS::Partition).get partition_name + partition.not_nil!.as(FSDB::Partition).get partition_name end def get_tags(name, key : K) partition = @indexers.find &.name.==(name) - partition.not_nil!.as(FS::Tags).get name, key + partition.not_nil!.as(FSDB::Tags).get name, key end def []?(key : K) : V? diff --git a/test-edit.cr b/test-edit.cr index a19ac67..94d0e7b 100644 --- a/test-edit.cr +++ b/test-edit.cr @@ -1,5 +1,5 @@ require "json" -require "./src/fs.cr" +require "./src/fsdb.cr" require "uuid" # This test basically works if no data is obtained when fetching "broken" @@ -21,7 +21,7 @@ class Ship getter id end -ships = FS::Hash(String, Ship).new "test-edit" +ships = FSDB::DataBase(String, Ship).new "test-edit" by_name = ships.new_index "name", &.name by_class = ships.new_partition "class", &.class by_id = ships.new_index "id", &.id @@ -46,6 +46,8 @@ ships.each do |id, ship| p "#{ship.name} (#{ship.class}) [#{ship.tags.join ", "}]" end +pp! by_class.get("Mutsuki").map(&.name) + no_broken = Array(Array(Ship)).new puts puts "Partitions/indices" @@ -59,3 +61,14 @@ if no_broken.flatten.size > 0 puts "ERROR: the test failed" end +## +# Not implemented, will *not* work (or compile). +## + +#ships.partition("class", "Mutsuki").get(min: 1, max: 3) +#ships.partition("class", "Mutsuki").get[1] + +#ships.partition("class", "Mutsuki").partition("batch", "first").get + +#ships.partition("class", "Mutsuki").sorted_by("name").get(min: 0, max: 2) + diff --git a/test-index.cr b/test-index.cr index 3017f85..b7ba9d9 100644 --- a/test-index.cr +++ b/test-index.cr @@ -1,5 +1,5 @@ require "json" -require "./src/fs.cr" +require "./src/fsdb.cr" require "uuid" # This test basically works if no data is obtained when fetching "broken" @@ -21,7 +21,7 @@ class Ship getter id end -ships = FS::Hash(String, Ship).new "test-index" +ships = FSDB::DataBase(String, Ship).new "test-index" by_name = ships.new_index "name", &.name by_class = ships.new_partition "class", &.class by_id = ships.new_index "id", &.id @@ -33,7 +33,7 @@ ships[ship.id] = ship begin ship = Ship.new "Mutsuki", "broken", tags: ["kuchikukan"] ships[ship.id] = ship -rescue FS::IndexOverload +rescue FSDB::IndexOverload puts "rescue: Adding an entry that would overload an index has been prevented." # Should happen, ignore it. else diff --git a/test-tags.cr b/test-tags.cr index 7b3c461..559043d 100644 --- a/test-tags.cr +++ b/test-tags.cr @@ -1,6 +1,6 @@ require "json" require "uuid/json" -require "./src/fs.cr" +require "./src/fsdb.cr" class Article JSON.mapping({ @@ -13,7 +13,7 @@ class Article end end -s = FS::Hash(String, Article).new "test-tags" +s = FSDB::DataBase(String, Article).new "test-tags" s.new_tags "tags", &.tags.map(&.downcase) diff --git a/test.cr b/test.cr index 934517a..1266795 100644 --- a/test.cr +++ b/test.cr @@ -1,9 +1,9 @@ require "json" -require "./src/fs.cr" +require "./src/fsdb.cr" # Basic mapping testing. -a = FS::Hash(String, JSON::Any).new "test-storage" +a = FSDB::DataBase(String, JSON::Any).new "test-storage" a["a"] = JSON::Any.new "now exists" @@ -37,7 +37,7 @@ class Article getter id end -articles = FS::Hash(String, Article).new "articles" +articles = FSDB::DataBase(String, Article).new "articles" by_author = articles.new_partition "author", &.author by_id = articles.new_index "id", &.id