Remove ".json" suffix in the generated files.

master
Philippe PITTOLI 2024-05-06 11:47:07 +02:00
parent 96f290eb40
commit f5c9d1a7f2
5 changed files with 34 additions and 44 deletions

View File

@ -132,24 +132,24 @@ After adding a few objects in the database, here the index in action on the file
$ tree storage/
storage
├── data
│   ├── 0000000000.json
│   ├── 0000000001.json
│   ├── 0000000002.json
│   ├── 0000000003.json
│   ├── 0000000004.json
│   └── 0000000005.json
│   ├── 0000000000
│   ├── 0000000001
│   ├── 0000000002
│   ├── 0000000003
│   ├── 0000000004
│   └── 0000000005
├── indices
│   └── by_id
│   ├── 6e109b82-25de-4250-9c67-e7e8415ad5a7.json -> ../../data/0000000003.json
│   ├── 2080131b-97d7-4300-afa9-55b93cdfd124.json -> ../../data/0000000000.json
│   ├── 2118bf1c-e413-4658-b8c1-a08925e20945.json -> ../../data/0000000005.json
│   ├── b53fab8e-f394-49ef-b939-8a670abe278b.json -> ../../data/0000000004.json
│   ├── 7e918680-6bc2-4f29-be7e-3d2e9c8e228c.json -> ../../data/0000000002.json
│   └── 8b4e83e3-ef95-40dc-a6e5-e6e697ce6323.json -> ../../data/0000000001.json
│   ├── 6e109b82-25de-4250-9c67-e7e8415ad5a7 -> ../../data/0000000003
│   ├── 2080131b-97d7-4300-afa9-55b93cdfd124 -> ../../data/0000000000
│   ├── 2118bf1c-e413-4658-b8c1-a08925e20945 -> ../../data/0000000005
│   ├── b53fab8e-f394-49ef-b939-8a670abe278b -> ../../data/0000000004
│   ├── 7e918680-6bc2-4f29-be7e-3d2e9c8e228c -> ../../data/0000000002
│   └── 8b4e83e3-ef95-40dc-a6e5-e6e697ce6323 -> ../../data/0000000001
```
We have 5 objects in the DB, each of them has a unique ID attribute, each attribute is related to a single object.
Getting an object by its ID is as simple as `cat storage/indices/by_id/<id>.json`.
Getting an object by its ID is as simple as `cat storage/indices/by_id/<id>`.
Now we want to sort cars based on their `color` attribute.
@ -165,14 +165,14 @@ $ tree storage/
├── partitions
│   └── by_color
│   ├── blue
│   │   ├── 0000000000.json -> ../../../data/0000000000.json
│   │   └── 0000000004.json -> ../../../data/0000000004.json
│   │   ├── 0000000000 -> ../../../data/0000000000
│   │   └── 0000000004 -> ../../../data/0000000004
│   ├── red
│   │   ├── 0000000001.json -> ../../../data/0000000001.json
│   │   ├── 0000000002.json -> ../../../data/0000000002.json
│   │   └── 0000000003.json -> ../../../data/0000000003.json
│   │   ├── 0000000001 -> ../../../data/0000000001
│   │   ├── 0000000002 -> ../../../data/0000000002
│   │   └── 0000000003 -> ../../../data/0000000003
│   └── violet
│   └── 0000000005.json -> ../../../data/0000000005.json
│   └── 0000000005 -> ../../../data/0000000005
```
Now the attribute corresponds to a directory (blue, red, violet, etc.) containing a symlink for each related object.
@ -190,12 +190,12 @@ $ tree storage/
└── tags
└── by_keyword
├── elegant
│   ├── 0000000000.json -> ../../../data/0000000000.json
│   └── 0000000003.json -> ../../../data/0000000003.json
│   ├── 0000000000 -> ../../../data/0000000000
│   └── 0000000003 -> ../../../data/0000000003
├── impressive
│   ├── 0000000000.json -> ../../../data/0000000000.json
│   ├── 0000000001.json -> ../../../data/0000000001.json
│   └── 0000000003.json -> ../../../data/0000000003.json
│   ├── 0000000000 -> ../../../data/0000000000
│   ├── 0000000001 -> ../../../data/0000000001
│   └── 0000000003 -> ../../../data/0000000003
...
```
Tags are very similar to partitions and are used the exact same way for search, update and deletion.

View File

@ -200,7 +200,7 @@ abstract class DODB::Storage(V)
end
private def file_path(key : Int32)
"#{data_path}/%010i.json" % key
"#{data_path}/%010i" % key
end
private def locks_directory : String

View File

@ -1,5 +1,4 @@
require "file_utils"
require "json"
require "./exceptions.cr"
require "./indexer.cr"
@ -91,10 +90,7 @@ class DODB::Index(V) < DODB::Indexer(V)
raise MissingEntry.new(@name, index) unless ::File.exists? file_path
::File.readlink(file_path)
.sub(/\.json$/, "")
.sub(/^.*\//, "")
.to_i
::File.readlink(file_path).sub(/^.*\//, "").to_i
end
def get_with_key(index : String) : Tuple(V, Int32)
@ -138,11 +134,11 @@ class DODB::Index(V) < DODB::Indexer(V)
# FIXME: Now that its being used outside of this class, name it properly.
def file_path_index(index_key : String)
"#{indexing_directory}/#{index_key}.json"
"#{indexing_directory}/#{index_key}"
end
private def get_data_symlink_index(key : String)
"../../data/#{key}.json"
"../../data/#{key}"
end
end

View File

@ -1,5 +1,4 @@
require "file_utils"
require "json"
require "./indexer.cr"
@ -78,9 +77,7 @@ class DODB::Partition(V) < DODB::Indexer(V)
end
private def get_key(path : String) : Int32
path.sub(/\.json$/, "")
.sub(/^.*\//, "")
.to_i
path.sub(/^.*\//, "").to_i
end
private def indexing_directory(partition)
@ -88,11 +85,11 @@ class DODB::Partition(V) < DODB::Indexer(V)
end
private def get_partition_symlink(partition : String, key : String)
"#{indexing_directory partition}/#{key}.json"
"#{indexing_directory partition}/#{key}"
end
private def get_data_symlink(key : String)
"../../../data/#{key}.json"
"../../../data/#{key}"
end
end

View File

@ -1,5 +1,4 @@
require "file_utils"
require "json"
class DODB::Tags(V) < DODB::Indexer(V)
property name : String
@ -89,9 +88,7 @@ class DODB::Tags(V) < DODB::Indexer(V)
end
private def get_key(path : String) : Int32
path.sub(/\.json$/, "")
.sub(/^.*\//, "")
.to_i
path.sub(/^.*\//, "").to_i
end
def indexing_directory : String
@ -103,10 +100,10 @@ class DODB::Tags(V) < DODB::Indexer(V)
end
private def get_tagged_entry_path(tag : String, key : String)
"#{indexing_directory}/#{tag}/#{key}.json"
"#{indexing_directory}/#{tag}/#{key}"
end
private def get_data_symlink(key : String)
"../../../data/#{key}.json"
"../../../data/#{key}"
end
end