Added test to the simple db lib, and depending on config, exclude or not json or memory database.

This commit is contained in:
2017-02-16 14:53:55 +01:00
parent ad3fe2fc7b
commit ae7aeaea30
14 changed files with 543 additions and 241 deletions

View File

@@ -38,6 +38,12 @@ feature -- Access
ensure
has_not_item: not has (a_entry_type, a_id)
end
wipe_out
-- Remove all items, and delete the database
deferred
end
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -0,0 +1,177 @@
note
description: "Summary description for {BASIC_FS_DATABASE}."
date: "$Date$"
revision: "$Revision$"
deferred class
BASIC_FS_DATABASE
inherit
BASIC_DATABASE
feature {NONE} -- Initialization
make (a_location: PATH)
local
d: DIRECTORY
do
location := a_location
ensure_directory_exists (a_location)
end
feature -- Access
location: PATH
feature {NONE} -- Access
default_extension: detachable STRING_32
-- Default file extension, if any.
deferred
end
feature -- Access
count_of (a_entry_type: TYPE [detachable ANY]): INTEGER
local
d: DIRECTORY
ext: like default_extension
do
create d.make_with_path (location.extended (entry_type_name (a_entry_type)))
if d.exists then
ext := default_extension
across
d.entries as ic
loop
if
ext = Void or else
attached ic.item.extension as e and then e.is_case_insensitive_equal (ext) then
Result := Result + 1
end
end
end
end
has (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): BOOLEAN
-- Has entry of type `a_entry_type` associated with id `a_id`?
local
fut: FILE_UTILITIES
do
Result := fut.file_path_exists (entry_path (a_entry_type, a_id))
end
item (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): detachable ANY
do
Result := item_from_location (a_entry_type, entry_path (a_entry_type, a_id))
end
save (a_entry_type: TYPE [detachable ANY]; a_entry: detachable ANY; cl_entry_id: CELL [detachable READABLE_STRING_GENERAL])
local
f: RAW_FILE
l_id: detachable READABLE_STRING_GENERAL
p: PATH
do
l_id := cl_entry_id.item
if l_id = Void then
l_id := next_identifier (a_entry_type)
cl_entry_id.replace (l_id)
end
p := entry_path (a_entry_type, l_id)
ensure_directory_exists (p.parent)
save_item_to_location (a_entry, p)
end
delete (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL)
local
f: RAW_FILE
do
create f.make_with_path (entry_path (a_entry_type, a_id))
if f.exists and then f.is_access_writable then
f.delete
end
end
wipe_out
local
d: DIRECTORY
do
create d.make_with_path (location)
if d.exists then
d.recursive_delete
end
end
feature {NONE} -- Implementation
item_from_location (a_entry_type: TYPE [detachable ANY]; p: PATH): like item
deferred
end
save_item_to_location (a_entry: detachable ANY; p: PATH)
deferred
end
ensure_directory_exists (dn: PATH)
local
d: DIRECTORY
do
create d.make_with_path (dn)
if not d.exists then
d.recursive_create_dir
end
end
entry_path (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended (a_id).appended_with_extension ("json")
end
entry_type_name (a_entry_type: TYPE [detachable ANY]): STRING
do
Result := a_entry_type.name.as_lower
Result.prune_all ('!')
end
last_id_file_path (a_entry_type: TYPE [detachable ANY]): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended ("last-id")
end
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
local
i: NATURAL_64
f: RAW_FILE
s: STRING
do
create f.make_with_path (last_id_file_path (a_entry_type))
ensure_directory_exists (f.path.parent)
if f.exists then
create s.make (f.count)
f.open_read
f.read_line
s := f.last_string
f.close
if s.is_natural_64 then
i := s.to_natural_64
end
end
from
i := i + 1
Result := i.out
until
not has (a_entry_type, Result)
loop
i := i + 1
Result := i.out
end
f.open_write
f.put_string (Result)
f.close
end
invariant
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -11,7 +11,10 @@ class
BASIC_JSON_FS_DATABASE
inherit
BASIC_DATABASE
BASIC_FS_DATABASE
redefine
make
end
create
make
@@ -19,54 +22,28 @@ create
feature {NONE} -- Initialization
make (a_location: PATH)
local
d: DIRECTORY
do
location := a_location
Precursor (a_location)
create serialization
ensure_directory_exists (a_location)
end
feature -- Access serialization
serialization: JSON_SERIALIZATION
feature -- Access
feature {NONE} -- Access
location: PATH
default_extension: STRING_32 = "json"
-- Default file extension, if any.
feature -- Access
feature {NONE} -- Implementation
count_of (a_entry_type: TYPE [detachable ANY]): INTEGER
local
d: DIRECTORY
do
create d.make_with_path (location.extended (entry_type_name (a_entry_type)))
if d.exists then
across
d.entries as ic
loop
if attached ic.item.extension as e and then e.is_case_insensitive_equal ("json") then
Result := Result + 1
end
end
end
end
has (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): BOOLEAN
-- Has entry of type `a_entry_type` associated with id `a_id`?
local
fut: FILE_UTILITIES
do
Result := fut.file_path_exists (entry_path (a_entry_type, a_id))
end
item (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): detachable ANY
item_from_location (a_entry_type: TYPE [detachable ANY]; p: PATH): like item
local
f: RAW_FILE
s: STRING
do
create f.make_with_path (entry_path (a_entry_type, a_id))
create f.make_with_path (p)
if f.exists then
create s.make (f.count)
f.open_read
@@ -82,95 +59,16 @@ feature -- Access
end
end
save (a_entry_type: TYPE [detachable ANY]; a_entry: detachable ANY; cl_entry_id: CELL [detachable READABLE_STRING_GENERAL])
save_item_to_location (a_entry: detachable ANY; p: PATH)
local
f: RAW_FILE
l_id: detachable READABLE_STRING_GENERAL
do
l_id := cl_entry_id.item
if l_id = Void then
l_id := next_identifier (a_entry_type)
cl_entry_id.replace (l_id)
end
create f.make_with_path (entry_path (a_entry_type, l_id))
ensure_directory_exists (f.path.parent)
create f.make_with_path (p)
f.open_write
f.put_string (serialization.to_json (a_entry).representation)
f.close
end
delete (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL)
local
f: RAW_FILE
do
create f.make_with_path (entry_path (a_entry_type, a_id))
if f.exists and then f.is_access_writable then
f.delete
end
end
feature {NONE} -- Implementation
ensure_directory_exists (dn: PATH)
local
d: DIRECTORY
do
create d.make_with_path (dn)
if not d.exists then
d.recursive_create_dir
end
end
entry_path (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended (a_id).appended_with_extension ("json")
end
entry_type_name (a_entry_type: TYPE [detachable ANY]): STRING
do
Result := a_entry_type.name.as_lower
Result.prune_all ('!')
end
last_id_file_path (a_entry_type: TYPE [detachable ANY]): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended ("last-id")
end
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
local
i: NATURAL_64
f: RAW_FILE
s: STRING
do
create f.make_with_path (last_id_file_path (a_entry_type))
ensure_directory_exists (f.path.parent)
if f.exists then
create s.make (f.count)
f.open_read
f.read_line
s := f.last_string
f.close
if s.is_natural_64 then
i := s.to_natural_64
end
end
from
i := i + 1
Result := i.out
until
not has (a_entry_type, Result)
loop
i := i + 1
Result := i.out
end
f.open_write
f.put_string (Result)
f.close
end
invariant
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -2,7 +2,7 @@ note
description: "[
Basic database for simple example based on memory.
WARNING: for now, not concurrent compliant.
WARNING: for now, this is a database per instance, this is not shared memory inside the same process.
]"
date: "$Date$"
revision: "$Revision$"
@@ -19,6 +19,8 @@ create
feature {NONE} -- Initialization
make
local
b: SED_MEMORY_READER_WRITER
do
create collections.make (0)
end
@@ -74,6 +76,11 @@ feature -- Access
end
end
wipe_out
do
collections.wipe_out
end
feature {NONE} -- Implementation
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
@@ -81,20 +88,22 @@ feature {NONE} -- Implementation
i: INTEGER
f: RAW_FILE
s: STRING
tb: detachable STRING_TABLE [detachable ANY]
do
if attached collections.item (a_entry_type) as tb then
tb := collections.item (a_entry_type)
if tb /= Void then
i := tb.count
from
i := i + 1
Result := i.out
until
not tb.has_key (Result)
loop
i := i + 1
Result := i.out
end
else
i := 0
end
from
i := i + 1
Result := i.out
until
not has (a_entry_type, Result)
loop
i := i + 1
Result := i.out
Result := "1"
end
end

View File

@@ -11,7 +11,10 @@ class
BASIC_SED_FS_DATABASE
inherit
BASIC_DATABASE
BASIC_FS_DATABASE
redefine
make
end
create
make
@@ -19,53 +22,29 @@ create
feature {NONE} -- Initialization
make (a_location: PATH)
local
d: DIRECTORY
do
location := a_location
Precursor (a_location)
create serialization
ensure_directory_exists (a_location)
end
feature -- Access
location: PATH
serialization: SED_STORABLE_FACILITIES
feature -- Access
feature {NONE} -- Access
count_of (a_entry_type: TYPE [detachable ANY]): INTEGER
local
d: DIRECTORY
do
create d.make_with_path (location.extended (entry_type_name (a_entry_type)))
if d.exists then
across
d.entries as ic
loop
if attached ic.item.extension as e and then e.is_case_insensitive_equal ("sed") then
Result := Result + 1
end
end
end
end
default_extension: STRING_32 = "sed"
-- Default file extension, if any.
has (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): BOOLEAN
-- Has entry of type `a_entry_type` associated with id `a_id`?
local
fut: FILE_UTILITIES
do
Result := fut.file_path_exists (entry_path (a_entry_type, a_id))
end
feature {NONE} -- Implementation
item (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): detachable ANY
item_from_location (a_entry_type: TYPE [detachable ANY]; p: PATH): like item
local
f: RAW_FILE
s: STRING
l_reader: SED_MEDIUM_READER_WRITER
s: STRING
do
create f.make_with_path (entry_path (a_entry_type, a_id))
create f.make_with_path (p)
if f.exists then
create s.make (f.count)
f.open_read
@@ -75,19 +54,12 @@ feature -- Access
end
end
save (a_entry_type: TYPE [detachable ANY]; a_entry: detachable ANY; cl_entry_id: CELL [detachable READABLE_STRING_GENERAL])
save_item_to_location (a_entry: detachable ANY; p: PATH)
local
f: RAW_FILE
l_id: detachable READABLE_STRING_GENERAL
l_writer: SED_MEDIUM_READER_WRITER
do
l_id := cl_entry_id.item
if l_id = Void then
l_id := next_identifier (a_entry_type)
cl_entry_id.replace (l_id)
end
create f.make_with_path (entry_path (a_entry_type, l_id))
ensure_directory_exists (f.path.parent)
create f.make_with_path (p)
f.open_write
if a_entry /= Void then
create l_writer.make_for_writing (f)
@@ -96,78 +68,6 @@ feature -- Access
f.close
end
delete (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL)
local
f: RAW_FILE
do
create f.make_with_path (entry_path (a_entry_type, a_id))
if f.exists and then f.is_access_writable then
f.delete
end
end
feature {NONE} -- Implementation
ensure_directory_exists (dn: PATH)
local
d: DIRECTORY
do
create d.make_with_path (dn)
if not d.exists then
d.recursive_create_dir
end
end
entry_path (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended (a_id).appended_with_extension ("sed")
end
entry_type_name (a_entry_type: TYPE [detachable ANY]): STRING
do
Result := a_entry_type.name.as_lower
Result.prune_all ('!')
end
last_id_file_path (a_entry_type: TYPE [detachable ANY]): PATH
do
Result := location.extended (entry_type_name (a_entry_type)).extended ("last-id")
end
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
local
i: NATURAL_64
f: RAW_FILE
s: STRING
do
create f.make_with_path (last_id_file_path (a_entry_type))
ensure_directory_exists (f.path.parent)
if f.exists then
create s.make (f.count)
f.open_read
f.read_line
s := f.last_string
f.close
if s.is_natural_64 then
i := s.to_natural_64
end
end
from
i := i + 1
Result := i.out
until
not has (a_entry_type, Result)
loop
i := i + 1
Result := i.out
end
f.open_write
f.put_string (Result)
f.close
end
invariant
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"