Compare commits

..

111 Commits

Author SHA1 Message Date
2e73bddbab Return to orignal label.
Some checks failed
MoreThanText build / Build-MoreThanText (push) Failing after 1m52s
2024-02-09 23:46:50 -05:00
800cad7ca3 Updated pipenv.
Some checks failed
MoreThanText build / Build-MoreThanText (push) Has been cancelled
2024-02-07 10:36:37 -05:00
95b2763442 Changed to fedora image.
Some checks failed
MoreThanText build / Build-MoreThanText (push) Has been cancelled
2024-02-01 20:59:52 -05:00
460ca4b5a7 Switching from jenkins.
Some checks failed
MoreThanText build / Build-MoreThanText (push) Failing after 1m50s
2024-02-01 20:49:39 -05:00
45522660bb Where I left off. 2024-02-01 20:40:29 -05:00
829b7242bd Added a table. 2023-07-21 18:10:38 -04:00
49b0eaf2ec Made sure that cache did not over write itself. 2023-07-18 11:44:49 -04:00
e49d6f5e46 Completed duplicate database in multiple sessions. 2023-07-13 09:39:25 -04:00
ea7dec2f4e Cache only stores references. 2023-07-06 17:28:10 -04:00
4dad6f7a05 Database is now stored as an entry. 2023-07-01 15:56:12 -04:00
de93ee1f2c Added random id generator. 2023-07-01 13:26:36 -04:00
c70c006abd Now returning duplicate database errors. 2023-06-30 22:38:18 -04:00
e73fdbff75 Commit adds db from each session. 2023-06-30 12:23:28 -04:00
05d445c58b Added a basic commit function. 2023-06-29 00:17:49 -04:00
933d48a47c Make store list work. 2023-06-25 10:39:14 -04:00
c26089caed Protect against duplicate databases. 2023-06-24 10:48:35 -04:00
d90dc3b9fc Can now add database to store. 2023-06-23 08:30:49 -04:00
a23b5d467e Setup store to hold databases. 2023-06-22 11:08:40 -04:00
c8b93d9922 Moved old database code. 2023-06-22 10:35:05 -04:00
ecfc8fdf90 Got the client and engine connecting. 2023-06-20 11:51:32 -04:00
dd1c45ddbe Continuing channel setup. 2023-06-17 12:01:58 -04:00
30ea8d978c Setup for data. 2023-06-03 15:27:26 -04:00
914e7a8146 Setup store. 2023-05-30 00:18:13 -04:00
646e0597ca Move store. 2023-05-29 23:34:52 -04:00
50a4431316 Setting up for data storage. 2023-05-29 15:42:32 -04:00
9f368b0b95 Added a requests trait. 2023-05-25 11:00:20 -04:00
69acfefc0c Added commit data. 2023-05-11 08:46:44 -04:00
0f8670f686 Adding putting a database into a store. 2023-04-23 13:13:38 -04:00
90ab5809b1 Improved query error handling. 2023-04-18 09:02:18 -04:00
41afc07bfa Final bit of interface cleanup. 2023-04-15 10:18:56 -04:00
9faa96d307 Cleaned up the cache interface. 2023-04-15 09:47:48 -04:00
30c9562542 Separated message functions. 2023-04-12 18:33:39 -04:00
448de012d3 Added empty commit. 2023-04-11 08:12:41 -04:00
bb23397eb0 Added the database datatype. 2023-04-10 07:52:53 -04:00
da26693242 Refactored test. 2023-04-09 09:45:39 -04:00
572abbeda8 Got the cache channels setup. 2023-04-08 15:04:04 -04:00
e2d32f4a8c Added new function to DataType 2023-04-04 12:36:10 -04:00
659a2758bb Pulled back to use async channels. 2023-04-04 09:59:29 -04:00
685ddfe32d Moved existing mod out of the way. 2023-04-03 14:59:40 -04:00
6d61af5136 Added id generator. 2023-03-28 07:38:12 -04:00
4b0af019c8 Moved session inside database. 2023-03-26 10:30:22 -04:00
ee45375085 Removed session as separate code. 2023-03-26 09:30:42 -04:00
c75bea3913 Setup initial db session. 2023-03-24 08:00:48 -04:00
40aae3ef5a Moved entry into separate file. 2023-03-22 22:41:40 -04:00
1f36848450 Moved in new mod file. 2023-03-21 18:35:42 -04:00
d4d42a599e Moved to new mod file. 2023-03-21 17:48:24 -04:00
3de5c2857b Removing old mod file. 2023-03-21 17:47:20 -04:00
abfa7114df got datatype read/write working with database. 2023-03-19 13:02:20 -04:00
418ba26da9 Stubbed out database. 2023-03-16 22:32:10 -04:00
e125d79a6c Added error codes to store. 2023-03-15 07:38:39 -04:00
a3d8feb37a Added cache initialization. 2023-03-14 11:32:37 -04:00
c477a92945 Added initialization failure. 2023-03-05 11:15:32 -05:00
8fada737ac Replaced filename string with PathBuf. 2023-03-05 00:19:11 -05:00
691ab6a27e Added entry remove function. 2023-02-19 11:27:28 -05:00
0ffb30d322 Added retrival function. 2023-02-17 09:05:06 -05:00
c0e4a5fad7 added Entry update function. 2023-02-15 12:13:19 -05:00
ef48fde890 Finished out Entry's new and get functions. 2023-02-15 09:47:15 -05:00
9b0154064f Handled unicode error. 2023-02-15 07:10:28 -05:00
894fcbcd02 In process of separating cache from database. 2023-02-13 10:19:20 -05:00
716b559062 Began reworking cache. 2023-02-12 10:39:39 -05:00
af1a60665a Removed the commented code. 2023-02-05 11:32:30 -05:00
5b291e3553 Completed making the session trait for databases. 2023-02-05 11:29:00 -05:00
51fc2c8921 Continued work to get items into a session. 2023-02-04 23:06:29 -05:00
60dae69788 Began abstracting functions for database sessions. 2023-02-04 14:29:07 -05:00
77c0f9f189 Removed session so it is part of the database. 2023-02-02 21:55:18 -05:00
ca418136a7 Added db session. 2023-02-01 07:59:15 -05:00
3ca71733c2 Switched add database from option to result. 2023-01-27 12:44:26 -05:00
766d2e753a Removed databases with empty names. 2023-01-23 11:05:55 -05:00
6e6de135ef Added FileData trait to Databases. 2023-01-22 14:15:34 -05:00
878e5277bf Added databases structure. 2023-01-21 21:46:39 -05:00
cef9843720 Consolidated code. 2023-01-20 09:34:35 -05:00
0a889b6c3c Removed false starts. 2023-01-19 21:52:04 -05:00
aef8c68853 Finished out the database stub. 2023-01-17 13:18:35 -05:00
13aa4328cc Added a database cache type. 2023-01-17 13:02:43 -05:00
809bcb641b Added an entry point to the database. 2023-01-16 12:07:33 -05:00
fa9cbe6a25 Added initial from bytes to databases. 2023-01-16 09:31:13 -05:00
43036bec84 Added read errors. 2023-01-13 07:54:27 -05:00
b5b0016aba Began work to add entry point. 2023-01-11 09:10:15 -05:00
d09474bb14 Removed Record items for now. 2023-01-10 10:12:44 -05:00
85d299b852 Added pull record information. 2023-01-05 11:01:05 -05:00
6983ee366d Added record entry. 2023-01-04 09:56:58 -05:00
58aba1a565 Added Record to cachetype. 2022-12-31 19:35:02 -05:00
4914754f43 Moved cachetype into seperate module. 2022-12-30 22:39:19 -05:00
bab5c27cee Removed the unused interprater code. 2022-12-30 22:09:36 -05:00
085972060b Added delete error. 2022-12-29 08:08:21 -05:00
a22207f3b3 Added delete entry. 2022-12-28 18:21:32 -05:00
6229671212 Refactor 2022-12-27 15:41:49 -05:00
5e0faf4da8 Removes unused entries from memory. 2022-12-26 17:48:50 -05:00
d3f53422df Refactored common filename function. 2022-12-24 11:30:45 -05:00
fe29841ffe Get cache updates time. 2022-12-24 10:28:49 -05:00
56005a5e47 Added time updates to CacheEntry. 2022-12-24 02:03:20 -05:00
f0f8e7455e Added last access time to cache. 2022-12-21 10:27:16 -05:00
3164da0f4c Reads cache entry from the drive. 2022-12-19 15:21:07 -05:00
1403aaa935 Added file errors to update entry. 2022-12-19 10:33:04 -05:00
96ed474568 update_entry updates file. 2022-12-19 08:12:32 -05:00
3e203b40c0 Added io errors to add_entry. 2022-12-18 11:12:58 -05:00
83e3b2ef9e Removed duplicate ids. 2022-12-16 08:22:36 -05:00
ab30604033 Writes out contents of the cache. 2022-12-15 12:20:40 -05:00
947adf6c1d Writes out contents of the cache. 2022-12-13 07:19:05 -05:00
8db7a6d9bf Turn cache entry into writable bytes. 2022-12-11 09:34:07 -05:00
eb97e3399a Added cache entry identifier. 2022-12-10 09:26:06 -05:00
470c6612de Formatting. 2022-12-08 11:08:05 -05:00
fcec57414a Added cache update function. 2022-12-08 10:58:29 -05:00
d73a18be2d Added error for missing cache entry. 2022-12-06 11:42:11 -05:00
bdcee41dcf Return a cache entry. 2022-12-05 23:23:39 -05:00
4f73a31cf0 Added random cache ids. 2022-12-05 23:21:13 -05:00
47b9a071a4 Added random cache ids. 2022-12-04 11:09:56 -05:00
14c6c9dffe Refactored test database setup. 2022-12-03 10:22:54 -05:00
8f704844c3 added add_cache function. 2022-12-03 09:15:58 -05:00
4e4344624e Improved error message. 2022-12-03 08:26:21 -05:00
8eb916771e Sloppy update. 2022-12-02 10:34:45 -05:00
20 changed files with 3843 additions and 279 deletions

View File

@ -0,0 +1,11 @@
---
name: MoreThanText build
run-name: ${{ gitea.actor }} runner
on: push
jobs:
Build-MoreThanText:
runs-on: ubuntu-latest
steps:
- run: cargo test
- run: cargo build --release

25
Cargo.lock generated
View File

@ -1088,8 +1088,10 @@ version = "0.1.0"
dependencies = [ dependencies = [
"async-std", "async-std",
"config", "config",
"rand 0.7.3",
"serde", "serde",
"serial_test", "serial_test",
"tempfile",
"tide", "tide",
"tide-testing", "tide-testing",
] ]
@ -1441,6 +1443,15 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "ron" name = "ron"
version = "0.7.0" version = "0.7.0"
@ -1775,6 +1786,20 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "tempfile"
version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4"
dependencies = [
"cfg-if 1.0.0",
"fastrand",
"libc",
"redox_syscall",
"remove_dir_all",
"winapi",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.30" version = "1.0.30"

View File

@ -9,9 +9,11 @@ edition = "2021"
#async-graphql = "*" #async-graphql = "*"
async-std = { version = "*", features = ["attributes"] } async-std = { version = "*", features = ["attributes"] }
config = "*" config = "*"
rand = "*"
serde = "*" serde = "*"
tide = "*" tide = "*"
[dev-dependencies] [dev-dependencies]
serial_test = "*" serial_test = "*"
tide-testing = "*" tide-testing = "*"
tempfile="*"

View File

@ -11,4 +11,4 @@ black = "*"
pylint = "*" pylint = "*"
[requires] [requires]
python_version = "3.9" python_version = "3"

197
Pipfile.lock generated
View File

@ -24,11 +24,13 @@
"markers": "python_version >= '3.5'", "markers": "python_version >= '3.5'",
"version": "==22.1.0" "version": "==22.1.0"
}, },
"glob2": { "exceptiongroup": {
"hashes": [ "hashes": [
"sha256:85c3dbd07c8aa26d63d7aacee34fa86e9a91a3873bc30bf62ec46e531f92ab8c" "sha256:542adf9dea4055530d6e1279602fa5cb11dab2395fa650b8674eaec35fc4a828",
"sha256:bd14967b79cd9bdb54d97323216f8fdf533e278df937aa2a90089e7d6e06e5ec"
], ],
"version": "==0.7" "markers": "python_version < '3.11'",
"version": "==1.0.4"
}, },
"iniconfig": { "iniconfig": {
"hashes": [ "hashes": [
@ -39,11 +41,11 @@
}, },
"mako": { "mako": {
"hashes": [ "hashes": [
"sha256:df3921c3081b013c8a2d5ff03c18375651684921ae83fd12e64800b7da923257", "sha256:c97c79c018b9165ac9922ae4f32da095ffd3c4e6872b45eded42926deea46818",
"sha256:f054a5ff4743492f1aa9ecc47172cb33b42b9d993cffcc146c9de17e717b0307" "sha256:d60a3903dc3bb01a18ad6a89cdbe2e4eadc69c0bc8ef1e3773ba53d44c3f7a34"
], ],
"markers": "python_version >= '3.7'", "markers": "python_version >= '3.7'",
"version": "==1.2.1" "version": "==1.2.4"
}, },
"markupsafe": { "markupsafe": {
"hashes": [ "hashes": [
@ -122,14 +124,6 @@
"markers": "python_version >= '3.6'", "markers": "python_version >= '3.6'",
"version": "==1.0.0" "version": "==1.0.0"
}, },
"py": {
"hashes": [
"sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719",
"sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",
"version": "==1.11.0"
},
"pyparsing": { "pyparsing": {
"hashes": [ "hashes": [
"sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb", "sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb",
@ -140,19 +134,19 @@
}, },
"pytest": { "pytest": {
"hashes": [ "hashes": [
"sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c", "sha256:892f933d339f068883b6fd5a459f03d85bfcb355e4981e146d2c7616c21fef71",
"sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45" "sha256:c4014eb40e10f11f355ad4e3c2fb2c6c6d1919c73f3b5a433de4708202cade59"
], ],
"markers": "python_version >= '3.7'", "markers": "python_version >= '3.7'",
"version": "==7.1.2" "version": "==7.2.0"
}, },
"pytest-bdd": { "pytest-bdd": {
"hashes": [ "hashes": [
"sha256:1dcea36d5fb59291a5d3c186f4cbbd04cdb62a304341f9a360a07682ba1aba18", "sha256:138af3592bcce5d4684b0d690777cf199b39ce45d423ca28086047ffe6111010",
"sha256:f894e589c9ee9e6a66c54e4917fafbbd11801fce9ce01e9b56dc15ccc57e4620" "sha256:57eba5878d77036f356a85fb1d108cb061d8af4fb4d032b1a424fa9abe9e498b"
], ],
"index": "pypi", "index": "pypi",
"version": "==6.0.1" "version": "==6.1.1"
}, },
"six": { "six": {
"hashes": [ "hashes": [
@ -167,47 +161,53 @@
"sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc", "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc",
"sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f" "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"
], ],
"markers": "python_version >= '3.7'", "markers": "python_version < '3.11'",
"version": "==2.0.1" "version": "==2.0.1"
},
"typing-extensions": {
"hashes": [
"sha256:1511434bb92bf8dd198c12b1cc812e800d4181cfcb867674e0f8279cc93087aa",
"sha256:16fa4864408f655d35ec496218b85f79b3437c829e93320c7c9215ccfd92489e"
],
"markers": "python_version >= '3.7'",
"version": "==4.4.0"
} }
}, },
"develop": { "develop": {
"astroid": { "astroid": {
"hashes": [ "hashes": [
"sha256:86b0a340a512c65abf4368b80252754cda17c02cdbbd3f587dddf98112233e7b", "sha256:10e0ad5f7b79c435179d0d0f0df69998c4eef4597534aae44910db060baeb907",
"sha256:bb24615c77f4837c707669d16907331374ae8a964650a66999da3f5ca68dc946" "sha256:1493fe8bd3dfd73dc35bd53c9d5b6e49ead98497c47b2307662556a5692d29d7"
], ],
"markers": "python_full_version >= '3.6.2'", "markers": "python_full_version >= '3.7.2'",
"version": "==2.11.7" "version": "==2.12.13"
}, },
"black": { "black": {
"hashes": [ "hashes": [
"sha256:074458dc2f6e0d3dab7928d4417bb6957bb834434516f21514138437accdbe90", "sha256:14ff67aec0a47c424bc99b71005202045dc09270da44a27848d534600ac64fc7",
"sha256:187d96c5e713f441a5829e77120c269b6514418f4513a390b0499b0987f2ff1c", "sha256:197df8509263b0b8614e1df1756b1dd41be6738eed2ba9e9769f3880c2b9d7b6",
"sha256:2ea29072e954a4d55a2ff58971b83365eba5d3d357352a07a7a4df0d95f51c78", "sha256:1e464456d24e23d11fced2bc8c47ef66d471f845c7b7a42f3bd77bf3d1789650",
"sha256:4af5bc0e1f96be5ae9bd7aaec219c901a94d6caa2484c21983d043371c733fc4", "sha256:2039230db3c6c639bd84efe3292ec7b06e9214a2992cd9beb293d639c6402edb",
"sha256:560558527e52ce8afba936fcce93a7411ab40c7d5fe8c2463e279e843c0328ee", "sha256:21199526696b8f09c3997e2b4db8d0b108d801a348414264d2eb8eb2532e540d",
"sha256:568ac3c465b1c8b34b61cd7a4e349e93f91abf0f9371eda1cf87194663ab684e", "sha256:2644b5d63633702bc2c5f3754b1b475378fbbfb481f62319388235d0cd104c2d",
"sha256:6797f58943fceb1c461fb572edbe828d811e719c24e03375fd25170ada53825e", "sha256:432247333090c8c5366e69627ccb363bc58514ae3e63f7fc75c54b1ea80fa7de",
"sha256:6c1734ab264b8f7929cef8ae5f900b85d579e6cbfde09d7387da8f04771b51c6", "sha256:444ebfb4e441254e87bad00c661fe32df9969b2bf224373a448d8aca2132b395",
"sha256:6c6d39e28aed379aec40da1c65434c77d75e65bb59a1e1c283de545fb4e7c6c9", "sha256:5b9b29da4f564ba8787c119f37d174f2b69cdfdf9015b7d8c5c16121ddc054ae",
"sha256:7ba9be198ecca5031cd78745780d65a3f75a34b2ff9be5837045dce55db83d1c", "sha256:5cc42ca67989e9c3cf859e84c2bf014f6633db63d1cbdf8fdb666dcd9e77e3fa",
"sha256:94783f636bca89f11eb5d50437e8e17fbc6a929a628d82304c80fa9cd945f256", "sha256:5d8f74030e67087b219b032aa33a919fae8806d49c867846bfacde57f43972ef",
"sha256:a218d7e5856f91d20f04e931b6f16d15356db1c846ee55f01bac297a705ca24f", "sha256:72ef3925f30e12a184889aac03d77d031056860ccae8a1e519f6cbb742736383",
"sha256:a3db5b6409b96d9bd543323b23ef32a1a2b06416d525d27e0f67e74f1446c8f2", "sha256:819dc789f4498ecc91438a7de64427c73b45035e2e3680c92e18795a839ebb66",
"sha256:ac609cf8ef5e7115ddd07d85d988d074ed00e10fbc3445aee393e70164a2219c", "sha256:915ace4ff03fdfff953962fa672d44be269deb2eaf88499a0f8805221bc68c87",
"sha256:b154e6bbde1e79ea3260c4b40c0b7b3109ffcdf7bc4ebf8859169a6af72cd70b", "sha256:9311e99228ae10023300ecac05be5a296f60d2fd10fff31cf5c1fa4ca4b1988d",
"sha256:b270a168d69edb8b7ed32c193ef10fd27844e5c60852039599f9184460ce0807", "sha256:974308c58d057a651d182208a484ce80a26dac0caef2895836a92dd6ebd725e0",
"sha256:b9fd45787ba8aa3f5e0a0a98920c1012c884622c6c920dbe98dbd05bc7c70fbf", "sha256:b8b49776299fece66bffaafe357d929ca9451450f5466e997a7285ab0fe28e3b",
"sha256:c85928b9d5f83b23cee7d0efcb310172412fbf7cb9d9ce963bd67fd141781def", "sha256:c957b2b4ea88587b46cf49d1dc17681c1e672864fd7af32fc1e9664d572b3458",
"sha256:c9a3ac16efe9ec7d7381ddebcc022119794872abce99475345c5a61aa18c45ad", "sha256:e41a86c6c650bcecc6633ee3180d80a025db041a8e2398dcc059b3afa8382cd4",
"sha256:cfaf3895a9634e882bf9d2363fed5af8888802d670f58b279b0bece00e9a872d", "sha256:f513588da599943e0cde4e32cc9879e825d58720d6557062d1098c5ad80080e1",
"sha256:e439798f819d49ba1c0bd9664427a05aab79bfba777a6db94fd4e56fae0cb849", "sha256:fba8a281e570adafb79f7755ac8721b6cf1bbf691186a287e990c7929c7692ff"
"sha256:f586c26118bc6e714ec58c09df0157fe2d9ee195c764f630eb0d8e7ccce72e69",
"sha256:f6fe02afde060bbeef044af7996f335fbe90b039ccf3f5eb8f16df8b20f77666"
], ],
"index": "pypi", "index": "pypi",
"version": "==22.6.0" "version": "==22.10.0"
}, },
"click": { "click": {
"hashes": [ "hashes": [
@ -219,11 +219,11 @@
}, },
"dill": { "dill": {
"hashes": [ "hashes": [
"sha256:33501d03270bbe410c72639b350e941882a8b0fd55357580fbc873fba0c59302", "sha256:a07ffd2351b8c678dfc4a856a3005f8067aea51d6ba6c700796a4d9e280f39f0",
"sha256:d75e41f3eff1eee599d738e76ba8f4ad98ea229db8b085318aa2b3333a208c86" "sha256:e5db55f3687856d8fbdab002ed78544e1c4559a130302693d839dfe8f93f2373"
], ],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6'", "markers": "python_version >= '3.7'",
"version": "==0.3.5.1" "version": "==0.3.6"
}, },
"isort": { "isort": {
"hashes": [ "hashes": [
@ -235,46 +235,28 @@
}, },
"lazy-object-proxy": { "lazy-object-proxy": {
"hashes": [ "hashes": [
"sha256:043651b6cb706eee4f91854da4a089816a6606c1428fd391573ef8cb642ae4f7", "sha256:0c1c7c0433154bb7c54185714c6929acc0ba04ee1b167314a779b9025517eada",
"sha256:07fa44286cda977bd4803b656ffc1c9b7e3bc7dff7d34263446aec8f8c96f88a", "sha256:14010b49a2f56ec4943b6cf925f597b534ee2fe1f0738c84b3bce0c1a11ff10d",
"sha256:12f3bb77efe1367b2515f8cb4790a11cffae889148ad33adad07b9b55e0ab22c", "sha256:4e2d9f764f1befd8bdc97673261b8bb888764dfdbd7a4d8f55e4fbcabb8c3fb7",
"sha256:2052837718516a94940867e16b1bb10edb069ab475c3ad84fd1e1a6dd2c0fcfc", "sha256:4fd031589121ad46e293629b39604031d354043bb5cdf83da4e93c2d7f3389fe",
"sha256:2130db8ed69a48a3440103d4a520b89d8a9405f1b06e2cc81640509e8bf6548f", "sha256:5b51d6f3bfeb289dfd4e95de2ecd464cd51982fe6f00e2be1d0bf94864d58acd",
"sha256:39b0e26725c5023757fc1ab2a89ef9d7ab23b84f9251e28f9cc114d5b59c1b09", "sha256:6850e4aeca6d0df35bb06e05c8b934ff7c533734eb51d0ceb2d63696f1e6030c",
"sha256:46ff647e76f106bb444b4533bb4153c7370cdf52efc62ccfc1a28bdb3cc95442", "sha256:6f593f26c470a379cf7f5bc6db6b5f1722353e7bf937b8d0d0b3fba911998858",
"sha256:4dca6244e4121c74cc20542c2ca39e5c4a5027c81d112bfb893cf0790f96f57e", "sha256:71d9ae8a82203511a6f60ca5a1b9f8ad201cac0fc75038b2dc5fa519589c9288",
"sha256:553b0f0d8dbf21890dd66edd771f9b1b5f51bd912fa5f26de4449bfc5af5e029", "sha256:7e1561626c49cb394268edd00501b289053a652ed762c58e1081224c8d881cec",
"sha256:677ea950bef409b47e51e733283544ac3d660b709cfce7b187f5ace137960d61", "sha256:8f6ce2118a90efa7f62dd38c7dbfffd42f468b180287b748626293bf12ed468f",
"sha256:6a24357267aa976abab660b1d47a34aaf07259a0c3859a34e536f1ee6e76b5bb", "sha256:ae032743794fba4d171b5b67310d69176287b5bf82a21f588282406a79498891",
"sha256:6a6e94c7b02641d1311228a102607ecd576f70734dc3d5e22610111aeacba8a0", "sha256:afcaa24e48bb23b3be31e329deb3f1858f1f1df86aea3d70cb5c8578bfe5261c",
"sha256:6aff3fe5de0831867092e017cf67e2750c6a1c7d88d84d2481bd84a2e019ec35", "sha256:b70d6e7a332eb0217e7872a73926ad4fdc14f846e85ad6749ad111084e76df25",
"sha256:6ecbb350991d6434e1388bee761ece3260e5228952b1f0c46ffc800eb313ff42", "sha256:c219a00245af0f6fa4e95901ed28044544f50152840c5b6a3e7b2568db34d156",
"sha256:7096a5e0c1115ec82641afbdd70451a144558ea5cf564a896294e346eb611be1", "sha256:ce58b2b3734c73e68f0e30e4e725264d4d6be95818ec0a0be4bb6bf9a7e79aa8",
"sha256:70ed0c2b380eb6248abdef3cd425fc52f0abd92d2b07ce26359fcbc399f636ad", "sha256:d176f392dbbdaacccf15919c77f526edf11a34aece58b55ab58539807b85436f",
"sha256:8561da8b3dd22d696244d6d0d5330618c993a215070f473b699e00cf1f3f6443", "sha256:e20bfa6db17a39c706d24f82df8352488d2943a3b7ce7d4c22579cb89ca8896e",
"sha256:85b232e791f2229a4f55840ed54706110c80c0a210d076eee093f2b2e33e1bfd", "sha256:eac3a9a5ef13b332c059772fd40b4b1c3d45a3a2b05e33a361dee48e54a4dad0",
"sha256:898322f8d078f2654d275124a8dd19b079080ae977033b713f677afcfc88e2b9", "sha256:eb329f8d8145379bf5dbe722182410fe8863d186e51bf034d2075eb8d85ee25b"
"sha256:8f3953eb575b45480db6568306893f0bd9d8dfeeebd46812aa09ca9579595148",
"sha256:91ba172fc5b03978764d1df5144b4ba4ab13290d7bab7a50f12d8117f8630c38",
"sha256:9d166602b525bf54ac994cf833c385bfcc341b364e3ee71e3bf5a1336e677b55",
"sha256:a57d51ed2997e97f3b8e3500c984db50a554bb5db56c50b5dab1b41339b37e36",
"sha256:b9e89b87c707dd769c4ea91f7a31538888aad05c116a59820f28d59b3ebfe25a",
"sha256:bb8c5fd1684d60a9902c60ebe276da1f2281a318ca16c1d0a96db28f62e9166b",
"sha256:c19814163728941bb871240d45c4c30d33b8a2e85972c44d4e63dd7107faba44",
"sha256:c4ce15276a1a14549d7e81c243b887293904ad2d94ad767f42df91e75fd7b5b6",
"sha256:c7a683c37a8a24f6428c28c561c80d5f4fd316ddcf0c7cab999b15ab3f5c5c69",
"sha256:d609c75b986def706743cdebe5e47553f4a5a1da9c5ff66d76013ef396b5a8a4",
"sha256:d66906d5785da8e0be7360912e99c9188b70f52c422f9fc18223347235691a84",
"sha256:dd7ed7429dbb6c494aa9bc4e09d94b778a3579be699f9d67da7e6804c422d3de",
"sha256:df2631f9d67259dc9620d831384ed7732a198eb434eadf69aea95ad18c587a28",
"sha256:e368b7f7eac182a59ff1f81d5f3802161932a41dc1b1cc45c1f757dc876b5d2c",
"sha256:e40f2013d96d30217a51eeb1db28c9ac41e9d0ee915ef9d00da639c5b63f01a1",
"sha256:f769457a639403073968d118bc70110e7dce294688009f5c24ab78800ae56dc8",
"sha256:fccdf7c2c5821a8cbd0a9440a456f5050492f2270bd54e94360cac663398739b",
"sha256:fd45683c3caddf83abbb1249b653a266e7069a09f486daa8863fb0e7496a9fdb"
], ],
"markers": "python_version >= '3.6'", "markers": "python_version >= '3.7'",
"version": "==1.7.1" "version": "==1.8.0"
}, },
"mccabe": { "mccabe": {
"hashes": [ "hashes": [
@ -293,42 +275,43 @@
}, },
"pathspec": { "pathspec": {
"hashes": [ "hashes": [
"sha256:7d15c4ddb0b5c802d161efc417ec1a2558ea2653c2e8ad9c19098201dc1c993a", "sha256:88c2606f2c1e818b978540f73ecc908e13999c6c3a383daf3705652ae79807a5",
"sha256:e564499435a2673d586f6b2130bb5b95f04a3ba06f81b8f895b651a3c76aabb1" "sha256:8f6bf73e5758fd365ef5d58ce09ac7c27d2833a8d7da51712eac6e27e35141b0"
], ],
"version": "==0.9.0" "markers": "python_version >= '3.7'",
"version": "==0.10.2"
}, },
"platformdirs": { "platformdirs": {
"hashes": [ "hashes": [
"sha256:027d8e83a2d7de06bbac4e5ef7e023c02b863d7ea5d079477e722bb41ab25788", "sha256:1006647646d80f16130f052404c6b901e80ee4ed6bef6792e1f238a8969106f7",
"sha256:58c8abb07dcb441e6ee4b11d8df0ac856038f944ab98b7be6b27b2a3c7feef19" "sha256:af0276409f9a02373d540bf8480021a048711d572745aef4b7842dad245eba10"
], ],
"markers": "python_version >= '3.7'", "markers": "python_version >= '3.7'",
"version": "==2.5.2" "version": "==2.5.4"
}, },
"pylint": { "pylint": {
"hashes": [ "hashes": [
"sha256:487ce2192eee48211269a0e976421f334cf94de1806ca9d0a99449adcdf0285e", "sha256:15060cc22ed6830a4049cf40bc24977744df2e554d38da1b2657591de5bcd052",
"sha256:fabe30000de7d07636d2e82c9a518ad5ad7908590fe135ace169b44839c15f90" "sha256:25b13ddcf5af7d112cf96935e21806c1da60e676f952efb650130f2a4483421c"
], ],
"index": "pypi", "index": "pypi",
"version": "==2.14.5" "version": "==2.15.6"
}, },
"tomli": { "tomli": {
"hashes": [ "hashes": [
"sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc", "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc",
"sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f" "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"
], ],
"markers": "python_version >= '3.7'", "markers": "python_version < '3.11'",
"version": "==2.0.1" "version": "==2.0.1"
}, },
"tomlkit": { "tomlkit": {
"hashes": [ "hashes": [
"sha256:1c5bebdf19d5051e2e1de6cf70adfc5948d47221f097fcff7a3ffc91e953eaf5", "sha256:07de26b0d8cfc18f871aec595fda24d95b08fef89d147caa861939f37230bf4b",
"sha256:61901f81ff4017951119cd0d1ed9b7af31c821d6845c8c477587bbdcd5e5854e" "sha256:71b952e5721688937fb02cf9d354dbcf0785066149d2855e44531ebdd2b65d73"
], ],
"markers": "python_version >= '3.6' and python_version < '4.0'", "markers": "python_version >= '3.6'",
"version": "==0.11.1" "version": "==0.11.6"
}, },
"wrapt": { "wrapt": {
"hashes": [ "hashes": [
@ -397,7 +380,7 @@
"sha256:ee6acae74a2b91865910eef5e7de37dc6895ad96fa23603d1d27ea69df545015", "sha256:ee6acae74a2b91865910eef5e7de37dc6895ad96fa23603d1d27ea69df545015",
"sha256:ef3f72c9666bba2bab70d2a8b79f2c6d2c1a42a7f7e2b0ec83bb2f9e383950af" "sha256:ef3f72c9666bba2bab70d2a8b79f2c6d2c1a42a7f7e2b0ec83bb2f9e383950af"
], ],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", "markers": "python_version < '3.11'",
"version": "==1.14.1" "version": "==1.14.1"
} }
} }

View File

@ -7,20 +7,20 @@ use tide::{
mod morethantext; mod morethantext;
mod settings; mod settings;
use morethantext::MoreThanText; use morethantext::{start_db, MoreThanText};
use settings::Settings; use settings::Settings;
#[async_std::main] #[async_std::main]
async fn main() -> tide::Result<()> { async fn main() -> tide::Result<()> {
let sett = Settings::new().unwrap(); let sett = Settings::new().unwrap();
let app = app_setup().await; let app = app_setup(sett.data_dir.as_str()).await;
app.listen(format!("{}:{}", sett.address, sett.port)) app.listen(format!("{}:{}", sett.address, sett.port))
.await?; .await?;
Ok(()) Ok(())
} }
async fn app_setup() -> tide::Server<MoreThanText> { async fn app_setup(data_dir: &str) -> tide::Server<MoreThanText> {
let db = MoreThanText::new().await; let db = start_db(data_dir).await.unwrap();
let mut app = tide::with_state(db); let mut app = tide::with_state(db);
app.at("/").get(home); app.at("/").get(home);
app.with( app.with(
@ -48,11 +48,13 @@ async fn home(_req: Request<MoreThanText>) -> tide::Result {
#[cfg(test)] #[cfg(test)]
mod server_app { mod server_app {
use super::*; use super::*;
use tempfile::tempdir;
use tide_testing::TideTestingExt; use tide_testing::TideTestingExt;
#[async_std::test] #[async_std::test]
async fn home_page_available() { async fn home_page_available() {
let app = app_setup().await; let dir = tempdir().unwrap();
let app = app_setup(dir.path().to_str().unwrap()).await;
let response = app.get("/").await.unwrap(); let response = app.get("/").await.unwrap();
assert_eq!(response.status(), StatusCode::Ok); assert_eq!(response.status(), StatusCode::Ok);
} }

336
src/morethantext/cache.rs Normal file
View File

@ -0,0 +1,336 @@
use super::{Database, ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY};
use async_std::{channel::Receiver, path::PathBuf};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{
collections::{HashMap, VecDeque},
iter::Iterator,
};
struct IDGenerator {
ids: Option<VecDeque<String>>,
}
impl IDGenerator {
fn new() -> Self {
Self { ids: None }
}
fn with_ids<T, D>(ids: T) -> Self
where
T: Into<Vec<D>>,
D: Into<String>,
{
let id_list = ids.into();
let mut data = VecDeque::new();
for id in id_list {
data.push_back(id.into());
}
Self { ids: Some(data) }
}
}
impl Iterator for IDGenerator {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
match &self.ids {
Some(id_list) => {
let mut ids = id_list.clone();
let output = ids.pop_front();
self.ids = Some(ids);
output
}
None => Some(thread_rng().sample_iter(&Alphanumeric).take(64).collect()),
}
}
}
#[cfg(test)]
mod genid {
use super::*;
#[test]
fn unique_ids() {
let mut gen = IDGenerator::new();
let mut output: Vec<String> = Vec::new();
for _ in 0..10 {
let id = gen.next().unwrap();
assert!(!output.contains(&id), "{} found in {:?}", id, output);
output.push(id);
}
}
#[test]
fn controlled_ids() {
let ids = ["one", "two", "three"];
let mut gen = IDGenerator::with_ids(ids.clone());
for id in ids {
assert_eq!(id, gen.next().unwrap());
}
}
}
pub struct Cache {
data: HashMap<String, FromCache>,
ids: IDGenerator,
}
impl Cache {
pub async fn new<P>(_dir: P) -> Self
where
P: Into<PathBuf>,
{
let mut data = HashMap::new();
data.insert(ENTRY.to_string(), FromCache::Str(Store::new()));
Self {
data: data,
ids: IDGenerator::new(),
}
}
async fn with_ids<P, T, D>(dir: P, ids: T) -> Self
where
P: Into<PathBuf>,
T: Into<Vec<D>>,
D: Into<String>,
{
let mut output = Self::new(dir).await;
output.ids = IDGenerator::with_ids(ids);
output
}
fn next_id(&mut self) -> String {
let mut id: String;
loop {
id = self.ids.next().unwrap();
match self.get(&id) {
FromCache::Error(_) => break,
_ => (),
}
}
id
}
pub async fn listen(&mut self, listener: Receiver<ToCache>) {
loop {
match listener.recv().await.unwrap() {
ToCache::Get(data) => {
data.result.send(self.get(data.data)).await.unwrap();
}
ToCache::Commit(data) => {
data.result.send(self.commit(data.data)).await.unwrap();
}
}
}
}
pub fn get<S>(&self, id: S) -> FromCache
where
S: Into<String>,
{
let idd = id.into();
match self.data.get(&idd) {
Some(data) => data.clone(),
None => FromCache::Error(MTTError::from_code(ErrorCode::IDNotFound(idd))),
}
}
pub fn commit(&mut self, data: Store) -> FromCache {
let entry_data = self.data.get(ENTRY).unwrap();
let mut store = match entry_data {
FromCache::Str(ep) => ep.clone(),
_ => {
unreachable!()
}
};
for name in data.list() {
let id = self.next_id();
match store.add_by_id(name, &id) {
Ok(_) => {
self.data.insert(id, FromCache::DB(Database::new()));
}
Err(err) => return FromCache::Error(err),
}
}
self.data
.insert(ENTRY.to_string(), FromCache::Str(store))
.unwrap();
FromCache::Ok
}
}
#[cfg(test)]
mod engine {
use super::*;
use tempfile::tempdir;
#[async_std::test]
async fn get_entry() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let expected: Vec<String> = Vec::new();
let result = cache.get(ENTRY);
match result {
FromCache::Str(store) => assert_eq!(store.list(), expected),
_ => assert!(false, "{:?} should be FromCache::Str", result),
}
}
#[async_std::test]
async fn get_bad_entry() -> Result<(), MTTError> {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let ids = ["bad1", "bad2"];
for id in ids {
let output = cache.get(id);
match output {
FromCache::Error(err) => match err.code {
ErrorCode::IDNotFound(_) => {
assert!(
err.to_string().contains(id),
"Had error: {}, Did not contain: {}",
err.to_string(),
id
);
}
_ => return Err(MTTError::new(format!("{:?} is not IDNotFound", err.code))),
},
_ => {
return Err(MTTError::new(format!(
"{:?} is not FromCache::Error",
output
)))
}
}
}
Ok(())
}
#[async_std::test]
async fn commit_database() {
// remove this one for the one below, maybe.
let dir = tempdir().unwrap();
let mut cache = Cache::new(dir.path()).await;
let mut store = Store::new();
let db = "garfield";
store.add(db).unwrap();
cache.commit(store.clone());
let output = cache.get(ENTRY);
match output {
FromCache::Str(result) => assert_eq!(result.list(), store.list()),
_ => assert!(false, "{:?} is not FromCache::Str", output),
}
}
#[async_std::test]
async fn add_database_entry() {
let id = "an_id";
let name = "garfield";
let dir = tempdir().unwrap();
let mut cache = Cache::with_ids(dir.path(), [id]).await;
let mut store = Store::new();
store.add(name).unwrap();
cache.commit(store.clone());
let db_out = cache.get(id);
match db_out {
FromCache::DB(_) => (),
_ => assert!(
false,
"{:?} is not FromCache::DB -- cache is {:?}",
db_out, cache.data
),
}
let store_out = cache.get(ENTRY);
match store_out {
FromCache::Str(updated_store) => match updated_store.get(name) {
Some(output) => {
assert_eq!(output.id, Some(id.to_string()));
assert!(output.data.is_none(), "Should have removed the database.");
}
None => assert!(true, "Store should have stored the database."),
},
_ => assert!(
false,
"{:?} is not FromCache::Str -- cache is {:?}",
db_out, cache.data
),
}
}
#[async_std::test]
async fn ids_are_not_overwritten() {
let ids = ["first", "first", "second"];
let names = ["barney", "fred"];
let dir = tempdir().unwrap();
let mut cache = Cache::with_ids(dir.path(), ids).await;
let mut store1 = Store::new();
store1.add(names[0]).unwrap();
let mut store2 = Store::new();
store2.add(names[1]).unwrap();
cache.commit(store1);
cache.commit(store2);
assert_eq!(
cache.data.len(),
3,
"cache.data had the following entries {:?}",
cache.data.keys()
);
}
#[async_std::test]
async fn no_duplicate_ids() {
let ids = ["one", "two"];
let dir = tempdir().unwrap();
let mut cache = Cache::with_ids(dir.path(), ids).await;
cache
.data
.insert(ids[0].to_string(), FromCache::DB(Database::new()));
assert_eq!(cache.next_id(), ids[1]);
}
}
#[cfg(test)]
mod messages {
use super::{
super::{start_db, ToCacheMsg},
*,
};
use async_std::channel::unbounded;
use tempfile::tempdir;
#[async_std::test]
async fn get_the_store() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
let in_s = mtt.to_cache.clone();
let (out_s, out_r) = unbounded();
let msg = ToCacheMsg {
data: ENTRY.to_string(),
result: out_s,
};
in_s.send(ToCache::Get(msg)).await.unwrap();
let result = out_r.recv().await.unwrap();
match result {
FromCache::Str(_) => (),
_ => assert!(false, "{:?} is not FromCache::Str", result),
}
}
#[async_std::test]
async fn get_bad_id() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
let in_s = mtt.to_cache.clone();
let (out_s, out_r) = unbounded();
let msg = ToCacheMsg {
data: "bad_id!".to_string(),
result: out_s,
};
in_s.send(ToCache::Get(msg)).await.unwrap();
let output = out_r.recv().await.unwrap();
match output {
FromCache::Error(_) => (),
_ => assert!(false, "{:?} is not FromCache::Error", output),
}
}
}

View File

@ -0,0 +1,36 @@
use super::{DBError, FileData, SessionData};
use std::slice;
#[derive(Clone)]
pub struct Database;
impl Database {
pub fn new() -> Self {
Self
}
}
impl FileData<Self> for Database {
fn to_bytes(&self) -> Vec<u8> {
let output = Vec::new();
output
}
fn from_bytes(_data: &mut slice::Iter<u8>) -> Result<Self, DBError> {
Ok(Self {})
}
}
impl SessionData for Database {
fn add(&mut self, _key: &str, _value: &str, _data: &str) -> Result<Vec<String>, DBError> {
Ok(Vec::new())
}
fn eq(&self, _key: &str, _value: &str) -> Result<Vec<String>, DBError> {
Ok(Vec::new())
}
fn list(&self, _keys: Vec<&str>) -> Result<Vec<String>, DBError> {
Ok(Vec::new())
}
}

View File

@ -0,0 +1,171 @@
use super::{Data, ErrorCode, MTTError, Table};
use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct Database {
data: HashMap<String, Data<Table>>,
}
impl Database {
pub fn new() -> Self {
Self {
data: HashMap::new(),
}
}
pub fn add<S>(&mut self, name: S) -> Result<(), MTTError>
where
S: Into<String>,
{
let db_name = name.into();
match self.get(&db_name) {
Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateTable(db_name))),
None => {
self.data.insert(db_name, Data::from_data(Table::new()));
Ok(())
}
}
}
pub fn add_by_id<S, D>(&mut self, name: S, id: D) -> Result<(), MTTError>
where
S: Into<String>,
D: Into<String>,
{
let db_name = name.into();
match self.get(&db_name) {
Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateTable(db_name))),
None => {
self.data.insert(db_name, Data::from_id(id.into()));
Ok(())
}
}
}
pub fn get(&self, name: &str) -> Option<&Data<Table>> {
self.data.get(name)
}
pub fn list(&self) -> Vec<String> {
let mut names = Vec::new();
for name in self.data.keys() {
names.push(name.to_string());
}
names.sort();
names
}
}
#[cfg(test)]
mod databases {
use super::*;
#[test]
fn create_new() {
let db = Database::new();
let expected: Vec<String> = Vec::new();
assert_eq!(db.list(), expected);
}
#[test]
fn add_db_by_str() {
let mut db = Database::new();
let name = "Melvin";
db.add(name).unwrap();
let output = db.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn add_db_by_string() {
let mut db = Database::new();
let name = "Marvin";
db.add(name.to_string()).unwrap();
let output = db.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn fail_on_duplicates() -> Result<(), MTTError> {
let mut db = Database::new();
let name = "Mickie";
db.add(name).unwrap();
match db.add(name) {
Ok(_) => Err(MTTError::new("duplicates should error")),
Err(err) => match err.code {
ErrorCode::DuplicateTable(db_name) => {
assert_eq!(db_name, name);
Ok(())
}
_ => Err(MTTError::new(format!("{:?} is not DuplicateTable", err))),
},
}
}
#[test]
fn add_using_cache_id() {
let mut db = Database::new();
let name = "fred";
let id = "12345";
db.add_by_id(name, id).unwrap();
let output = db.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn add_by_cache_id_name_string() {
let mut db = Database::new();
let name = "barney";
let id = "67890";
db.add_by_id(name.to_string(), id).unwrap();
let output = db.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn no_duplicate_databases_for_add_by_id() {
let mut db = Database::new();
let name = "betty";
db.add_by_id(name, "fghij").unwrap();
match db.add_by_id(name, "klmno") {
Ok(_) => assert!(false, "Duplicates should error."),
Err(err) => match err.code {
ErrorCode::DuplicateTable(db_name) => assert_eq!(db_name, name),
_ => assert!(false, "{:?} is not DuplicateTable", err),
},
}
}
#[test]
fn add_by_cache_id_string() {
let mut db = Database::new();
let name = "wilma";
let id = "abcdef";
db.add_by_id(name, id.to_string()).unwrap();
let output = db.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn get_bad_database() -> Result<(), MTTError> {
let db = Database::new();
match db.get("missing") {
Some(_) => Err(MTTError::new("Should have returned None.")),
None => Ok(()),
}
}
#[test]
fn get_list() {
let mut db = Database::new();
let mut ids = ["one", "two", "three", "four", "five"];
for name in ids {
db.add(name.to_string()).unwrap();
}
ids.sort();
assert_eq!(db.list(), ids);
}
}

381
src/morethantext/entry.rs Normal file
View File

@ -0,0 +1,381 @@
use super::{DBError, DataType, ErrorCode, FileData, SessionData};
use async_std::{
fs::{read, remove_file, write},
path::PathBuf,
};
use std::{
cell::Cell,
time::{Duration, Instant},
};
pub struct Entry {
data: DataType,
filename: PathBuf,
last_used: Cell<Instant>,
}
impl Entry {
pub async fn new<P>(filename: P, data: DataType) -> Result<Self, DBError>
where
P: Into<PathBuf>,
{
let pathbuf = filename.into();
if pathbuf.as_path().exists().await {
return Err(DBError::from_code(ErrorCode::EntryExists(pathbuf)));
} else {
match write(&pathbuf, data.to_bytes()).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::from_code(ErrorCode::EntryWriteFailure(pathbuf));
error.add_source(err);
return Err(error);
}
}
}
Ok(Self {
data: data,
filename: pathbuf,
last_used: Cell::new(Instant::now()),
})
}
pub async fn get<P>(filename: P) -> Result<Self, DBError>
where
P: Into<PathBuf>,
{
let pathbuf = filename.into();
let content = match read(&pathbuf).await {
Ok(text) => text,
Err(err) => {
let mut error = DBError::from_code(ErrorCode::EntryReadFailure(pathbuf));
error.add_source(err);
return Err(error);
}
};
let data = match DataType::from_bytes(&mut content.iter()) {
Ok(raw) => raw,
Err(err) => {
let mut error = DBError::from_code(ErrorCode::EntryReadFailure(pathbuf));
error.add_source(err);
return Err(error);
}
};
Ok(Self {
data: data,
filename: pathbuf,
last_used: Cell::new(Instant::now()),
})
}
pub fn elapsed(&self) -> Duration {
self.last_used.get().elapsed()
}
pub fn data(&self) -> DataType {
self.last_used.set(Instant::now());
self.data.clone()
}
async fn update(&mut self, data: DataType) -> Result<(), DBError> {
self.last_used.set(Instant::now());
match write(&self.filename, data.to_bytes()).await {
Ok(_) => (),
Err(err) => {
let mut error =
DBError::from_code(ErrorCode::EntryWriteFailure(self.filename.clone()));
error.add_source(err);
return Err(error);
}
};
self.data = data;
Ok(())
}
async fn remove(&self) -> Result<(), DBError> {
match remove_file(&self.filename).await {
Ok(_) => Ok(()),
Err(err) => {
let mut error =
DBError::from_code(ErrorCode::EntryDeleteFailure(self.filename.clone()));
error.add_source(err);
Err(error)
}
}
}
}
#[cfg(test)]
mod entry {
use super::*;
use std::error::Error;
use tempfile::tempdir;
#[async_std::test]
async fn get_elapsed_time() {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
let filepath = dir.path().join("count");
let filename = filepath.to_str().unwrap();
let item = Entry::new(filename.to_string(), data).await.unwrap();
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been now."
);
item.last_used
.set(Instant::now() - Duration::from_secs(500));
assert!(
Duration::from_secs(499) < item.elapsed(),
"The duration should have increased."
);
}
#[async_std::test]
async fn create() {
let dir = tempdir().unwrap();
let mut data = DataType::new("store").unwrap();
data.add("database", "roger", "moore").unwrap();
let filepath = dir.path().join("wiliam");
let filename = filepath.to_str().unwrap();
let item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been now."
);
let output = item.data();
assert_eq!(
data.list(["database"].to_vec()).unwrap(),
output.list(["database"].to_vec()).unwrap()
);
assert!(filepath.is_file(), "Should have created the entry file.");
let content = read(&filepath).await.unwrap();
assert_eq!(content, data.to_bytes());
}
#[async_std::test]
async fn create_errors_on_bad_files() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
let filepath = dir.path().join("bad").join("path");
let filename = filepath.to_str().unwrap();
match Entry::new(filename.to_string(), data).await {
Ok(_) => Err(DBError::new("bad file names should raise an error")),
Err(err) => match err.code {
ErrorCode::EntryWriteFailure(_) => {
assert!(err.source().is_some(), "Must include the source error.");
assert!(err
.source()
.unwrap()
.to_string()
.contains("could not write to file"));
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn create_does_not_over_writes() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let id = "wicked";
let file = dir.path().join(id);
let filename = file.to_str().unwrap();
write(&file, b"previous").await.unwrap();
let data = DataType::new("store").unwrap();
match Entry::new(filename.to_string(), data).await {
Ok(_) => {
return Err(DBError::new(
"Should produce an error for an existing Entry",
))
}
Err(err) => match err.code {
ErrorCode::EntryExists(_) => Ok(()),
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn get_updates_last_used() {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
let filepath = dir.path().join("holder");
let filename = filepath.to_str().unwrap();
let item = Entry::new(filename.to_string(), data).await.unwrap();
item.last_used
.set(Instant::now() - Duration::from_secs(300));
item.data();
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been reset."
);
}
#[async_std::test]
async fn update_entry() {
let dir = tempdir().unwrap();
let mut data = DataType::new("store").unwrap();
let filepath = dir.path().join("changing");
let filename = filepath.to_str().unwrap();
let mut item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
item.last_used
.set(Instant::now() - Duration::from_secs(500));
data.add("database", "new", "stuff").unwrap();
item.update(data.clone()).await.unwrap();
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been reset."
);
let output = item.data();
assert_eq!(
data.list(["database"].to_vec()).unwrap(),
output.list(["database"].to_vec()).unwrap()
);
let content = read(&filepath).await.unwrap();
assert_eq!(content, data.to_bytes());
}
#[async_std::test]
async fn update_write_errors() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
let filepath = dir.path().join("changing");
let filename = filepath.to_str().unwrap();
let mut item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
drop(dir);
match item.update(data).await {
Ok(_) => Err(DBError::new("file writes should return an error")),
Err(err) => match err.code {
ErrorCode::EntryWriteFailure(_) => {
assert!(err.source().is_some(), "Must include the source error.");
assert!(err
.source()
.unwrap()
.to_string()
.contains("could not write to file"));
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn retrieve() {
let dir = tempdir().unwrap();
let mut data = DataType::new("store").unwrap();
data.add("database", "something_old", "3.14159").unwrap();
let filepath = dir.path().join("existing");
let filename = filepath.to_str().unwrap();
let item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
let output = Entry::get(filename).await.unwrap();
assert_eq!(
output.data().list(["database"].to_vec()).unwrap(),
data.list(["database"].to_vec()).unwrap()
);
assert_eq!(output.filename.to_str().unwrap(), filename);
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been reset."
);
}
#[async_std::test]
async fn retrieve_file_missing() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let filepath = dir.path().join("justnotthere");
let filename = filepath.to_str().unwrap();
match Entry::get(filename).await {
Ok(_) => Err(DBError::new("should have returned an error")),
Err(err) => match err.code {
ErrorCode::EntryReadFailure(_) => {
assert!(err.source().is_some(), "Error should have a source.");
assert!(
err.source()
.unwrap()
.to_string()
.contains("could not read file"),
"Source Error Message: {}",
err.source().unwrap().to_string()
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn retrieve_corrupt_file() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let filepath = dir.path().join("garbage");
let filename = filepath.to_str().unwrap();
write(&filepath, b"jhsdfghlsdf").await.unwrap();
match Entry::get(filename).await {
Ok(_) => Err(DBError::new("should have returned an error")),
Err(err) => match err.code {
ErrorCode::EntryReadFailure(_) => {
assert!(err.source().is_some(), "Error should have a source.");
assert!(
err.source().unwrap().to_string().contains("corrupt file"),
"Source Error Message: {}",
err.source().unwrap().to_string()
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn delete() {
let dir = tempdir().unwrap();
let filepath = dir.path().join("byebye");
let filename = filepath.to_str().unwrap();
let data = DataType::new("store").unwrap();
let item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
item.remove().await.unwrap();
assert!(!filepath.exists(), "Entry file should be removed.");
}
#[async_std::test]
async fn delete_bad_file() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let filepath = dir.path().join("itsnotthere");
let filename = filepath.to_str().unwrap();
let data = DataType::new("store").unwrap();
let item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
remove_file(filename).await.unwrap();
match item.remove().await {
Ok(_) => Err(DBError::new("should have produced an error")),
Err(err) => match err.code {
ErrorCode::EntryDeleteFailure(_) => {
assert!(err.source().is_some(), "Error should have a source.");
assert!(
err.source()
.unwrap()
.to_string()
.contains("could not remove file"),
"Source Error Message: {}",
err.source().unwrap().to_string()
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
}

View File

@ -1,90 +1,135 @@
use std::{error::Error, fmt}; use std::{error::Error, fmt};
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct DBError { pub enum ErrorCode {
msg: String, // General
src: Option<Box<dyn Error + 'static>>, Undefined(String),
// Cache
IDNotFound(String),
// Store
DuplicateDatabase(String),
// Database
DuplicateTable(String),
} }
impl DBError { impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
ErrorCode::IDNotFound(id) => write!(f, "ID '{}' not found", id),
ErrorCode::DuplicateDatabase(name) => write!(f, "database '{}' already exists", name),
ErrorCode::DuplicateTable(name) => write!(f, "table '{}' already exists", name),
}
}
}
mod errorcodes {
use super::*;
const ITEMS: [&str; 2] = ["one", "two"];
#[test]
fn undefined_display() {
for item in ITEMS {
let err = ErrorCode::Undefined(item.to_string());
assert_eq!(err.to_string(), item);
}
}
#[test]
fn cache_missing() {
for item in ITEMS {
let err = ErrorCode::IDNotFound(item.to_string());
assert_eq!(err.to_string(), format!("ID '{}' not found", item));
}
}
#[test]
fn duplicate_database() {
for item in ITEMS {
let err = ErrorCode::DuplicateDatabase(item.to_string());
assert_eq!(
err.to_string(),
format!("database '{}' already exists", item)
);
}
}
#[test]
fn duplicate_table() {
for item in ITEMS {
let err = ErrorCode::DuplicateTable(item.to_string());
assert_eq!(err.to_string(), format!("table '{}' already exists", item));
}
}
}
#[derive(Clone, Debug)]
pub struct MTTError {
pub code: ErrorCode,
}
impl MTTError {
pub fn new<S>(msg: S) -> Self pub fn new<S>(msg: S) -> Self
where where
S: Into<String>, S: Into<String>,
{ {
let text = msg.into();
Self { Self {
msg: msg.into(), code: ErrorCode::Undefined(text),
src: None,
} }
} }
pub fn add_source<E>(&mut self, src: E) pub fn from_code(code: ErrorCode) -> Self {
where Self { code: code }
E: Error + 'static,
{
self.src = Some(Box::new(src));
} }
} }
impl Error for DBError { impl Error for MTTError {}
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self.src {
Some(err) => Some(err.as_ref()),
None => None,
}
}
}
impl fmt::Display for DBError { impl fmt::Display for MTTError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg) write!(f, "{}", self.code)
} }
} }
#[cfg(test)] #[cfg(test)]
mod create { mod errors {
use super::*; use super::*;
#[test] #[test]
fn with_str() { fn create_with_str() {
let msg = "something happened"; let msgs = ["one", "two"];
let err = DBError::new(msg); for msg in msgs {
assert!( let err = MTTError::new(msg);
err.to_string() == msg, assert_eq!(err.to_string(), msg);
"Got: {} -- Want: {}", }
err.to_string(),
msg
);
assert!(
err.source().is_none(),
"Error should initialize with no source."
);
} }
#[test] #[test]
fn with_string() { fn create_with_string() {
let msg = "it went boom".to_string(); let msg = "three";
let err = DBError::new(msg.clone()); let err = MTTError::new(msg.to_string());
assert!( assert_eq!(err.to_string(), msg);
err.to_string() == msg,
"Got: {} -- Want: {}",
err.to_string(),
msg
);
assert!(
err.source().is_none(),
"Error should initialize with no source."
);
} }
#[test] #[test]
fn with_source() { fn create_from_code() {
let msg = "but this caused the problem"; let code = ErrorCode::Undefined("oops".to_string());
let mut par = DBError::new("parent error"); let err = MTTError::from_code(code);
let src = DBError::new(msg); match err.code {
par.add_source(src); ErrorCode::Undefined(_) => (),
let output = par.source(); _ => assert!(false, "{:?} is not undefined", err.code),
assert!(output.is_some(), "Should return source."); }
let source = output.unwrap(); }
assert!(source.to_string() == msg);
#[test]
fn create_missing_id_from_code() {
let code = ErrorCode::IDNotFound("123".to_string());
let err = MTTError::from_code(code);
match err.code {
ErrorCode::IDNotFound(_) => (),
_ => assert!(false, "{:?} is not undefined", err.code),
}
} }
} }

402
src/morethantext/mod-2.rs Normal file
View File

@ -0,0 +1,402 @@
mod database;
mod entry;
mod error;
mod store;
use async_std::path::PathBuf;
use database::Database;
use entry::Entry;
use error::{DBError, ErrorCode};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{slice, str};
use store::Store;
const ENTRY: &str = "EntryPoint";
trait ID {
fn next(&self) -> String;
}
trait FileData<F> {
fn to_bytes(&self) -> Vec<u8>;
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<F, DBError>;
}
trait SessionData {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError>;
fn eq(&self, key: &str, value: &str) -> Result<Vec<String>, DBError>;
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError>;
}
#[derive(Clone)]
pub enum DataType {
DBMap(Store),
TableMap(Database),
}
impl DataType {
fn new(data_type: &str) -> Result<Self, DBError> {
match data_type {
"store" => Ok(DataType::DBMap(Store::new())),
"database" => Ok(DataType::TableMap(Database::new())),
_ => Err(DBError::from_code(ErrorCode::DataTypeIncorrect(
data_type.to_string(),
))),
}
}
}
impl SessionData for DataType {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError> {
match self {
DataType::DBMap(dbs) => dbs.add(key, value, data),
DataType::TableMap(_) => todo!(),
}
}
fn eq(&self, key: &str, value: &str) -> Result<Vec<String>, DBError> {
match self {
DataType::DBMap(dbs) => dbs.eq(key, value),
DataType::TableMap(_) => todo!(),
}
}
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError> {
match self {
DataType::DBMap(dbs) => dbs.list(keys),
DataType::TableMap(_) => todo!(),
}
}
}
impl FileData<Self> for DataType {
fn to_bytes(&self) -> Vec<u8> {
let mut output = Vec::new();
match self {
DataType::DBMap(_) => output.append(&mut "DBMap".as_bytes().to_vec()),
DataType::TableMap(_) => output.append(&mut "TableMap".as_bytes().to_vec()),
}
output.push(0);
match self {
DataType::DBMap(store) => output.append(&mut store.to_bytes()),
DataType::TableMap(_) => (),
}
output
}
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<Self, DBError> {
let mut header: Vec<u8> = Vec::new();
loop {
let letter = match data.next() {
Some(a) => a.clone(),
None => 0,
};
if letter == 0 {
break;
} else {
header.push(letter);
}
}
let header = match str::from_utf8(&header) {
Ok(item) => item,
Err(_) => return Err(DBError::from_code(ErrorCode::CorruptFile)),
};
match header {
"DBMap" => match Store::from_bytes(data) {
Ok(store) => Ok(DataType::DBMap(store)),
Err(err) => Err(err),
},
"TableMap" => Ok(DataType::new("database").unwrap()),
_ => Err(DBError::from_code(ErrorCode::CorruptFile)),
}
}
}
#[derive(Clone)]
pub struct MoreThanText {
next_id: &dyn Fn() -> String,
session: Vec<String>,
}
impl MoreThanText {
pub async fn new<P>(dir: P) -> Result<Self, DBError>
where
P: Into<PathBuf>,
{
let pathbuf = dir.into();
let entry = pathbuf.as_path().join(ENTRY);
match Entry::get(entry.clone()).await {
Ok(_) => (),
Err(_) => {
let store = DataType::new("store").unwrap();
match Entry::new(entry, store).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::from_code(ErrorCode::CacheReadWrite);
error.add_source(err);
return Err(error);
}
}
}
}
Ok(Self {
next_id: fn id() -> String { thread_rng().sample_iter(&Alphanumeric).take(64).collect() },
session: [ENTRY.to_string()].to_vec(),
})
}
fn set_session(&mut self, sess: Vec<String>) {
self.session = sess;
}
async fn new_entry(&self, _name: &str) -> Self {
self.clone()
}
}
#[cfg(test)]
mod datatype {
use super::*;
#[test]
fn bad_data_type() -> Result<(), DBError> {
let dt = "bufcuss";
match DataType::new(dt) {
Ok(_) => Err(DBError::new("should have produced an error")),
Err(err) => match err.code {
ErrorCode::DataTypeIncorrect(value) => {
assert_eq!(value, dt, "Incorrect input value");
Ok(())
}
_ => {
let mut error = DBError::new("incorrect error");
error.add_source(err);
Err(error)
}
},
}
}
#[test]
fn create_store() -> Result<(), DBError> {
match DataType::new("store") {
Ok(dt) => match dt {
DataType::DBMap(_) => Ok(()),
_ => Err(DBError::new("incorrect data type")),
},
Err(err) => Err(err),
}
}
#[test]
fn create_database() -> Result<(), DBError> {
match DataType::new("database") {
Ok(dt) => match dt {
DataType::TableMap(_) => Ok(()),
_ => Err(DBError::new("incorrect data type")),
},
Err(err) => Err(err),
}
}
}
#[cfg(test)]
mod datatype_sesssion {
use super::*;
#[test]
fn update_storage() {
let mut dbs = DataType::new("store").unwrap();
let name = "new_database";
let id = "someid";
dbs.add("database", name, id).unwrap();
assert_eq!(dbs.eq("database", name).unwrap(), [id].to_vec());
assert_eq!(dbs.list(["database"].to_vec()).unwrap(), [name].to_vec());
}
}
#[cfg(test)]
mod datatype_file {
use super::*;
#[test]
fn new_store_bytes() {
let dbs = DataType::new("store").unwrap();
let mut expected = "DBMap".as_bytes().to_vec();
expected.push(0);
assert_eq!(dbs.to_bytes(), expected);
}
#[test]
fn store_bytes_with_info() {
let name = "title";
let id = "king";
let mut store = Store::new();
let mut dt_store = DataType::new("store").unwrap();
let mut expected = dt_store.to_bytes();
store.add("database", name, id).unwrap();
expected.append(&mut store.to_bytes());
dt_store.add("database", name, id).unwrap();
assert_eq!(dt_store.to_bytes(), expected);
}
#[test]
fn read_empty_store() {
let dt_store = DataType::new("store").unwrap();
let data = dt_store.to_bytes();
let mut feed = data.iter();
let output = DataType::from_bytes(&mut feed).unwrap();
assert_eq!(
dt_store.list(["database"].to_vec()).unwrap(),
output.list(["database"].to_vec()).unwrap()
);
}
#[test]
fn read_store_info() {
let mut dt_store = DataType::new("store").unwrap();
dt_store.add("database", "raven", "beastboy").unwrap();
let data = dt_store.to_bytes();
let mut feed = data.iter();
let output = DataType::from_bytes(&mut feed).unwrap();
assert_eq!(
dt_store.list(["database"].to_vec()).unwrap(),
output.list(["database"].to_vec()).unwrap()
);
}
#[test]
fn new_database_bytes() {
let db = DataType::new("database").unwrap();
let mut expected = "TableMap".as_bytes().to_vec();
expected.push(0);
assert_eq!(db.to_bytes(), expected);
}
#[test]
fn read_empty_database() {
let dt = DataType::new("database").unwrap();
let data = dt.to_bytes();
let mut feed = data.iter();
match DataType::from_bytes(&mut feed).unwrap() {
DataType::TableMap(_) => (),
_ => assert!(false, "Incorrect data type"),
}
}
#[test]
fn read_bad_header() -> Result<(), DBError> {
let data = "sdghsdl".as_bytes().to_vec();
let mut feed = data.iter();
match DataType::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have raised an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error")),
},
}
}
#[test]
fn read_bad_store() -> Result<(), DBError> {
let mut data = "DBMap".as_bytes().to_vec();
data.push(0);
data.append(&mut "sdfgs".as_bytes().to_vec());
let mut feed = data.iter();
match DataType::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have raised an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
},
}
}
}
#[cfg(test)]
mod db {
use super::*;
use async_std::fs::write;
use std::error::Error;
use tempfile::tempdir;
#[async_std::test]
async fn create() {
let dir = tempdir().unwrap();
let mtt = MoreThanText::new(dir.path()).await.unwrap();
let epoint = dir.path().join(ENTRY);
assert!(
epoint.is_file(),
"{} did not get created.",
epoint.display()
);
let entry = Entry::get(epoint.to_str().unwrap()).await.unwrap();
assert_eq!(
entry.data().list(["database"].to_vec()).unwrap(),
Vec::<String>::new()
);
let sess = [ENTRY];
assert_eq!(mtt.session, sess);
}
#[async_std::test]
async fn entry_failure() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let path = dir.path().join("bad").join("path");
match MoreThanText::new(path).await {
Ok(_) => Err(DBError::new("Should have produced an error.")),
Err(err) => match err.code {
ErrorCode::CacheReadWrite => {
assert!(err.source().is_some(), "Error should have a source.");
assert!(
err.source().unwrap().to_string().contains("write failure"),
"Source Error Message: {}",
err.source().unwrap().to_string()
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn existing_entry_point() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
Entry::new(dir.path().join(ENTRY), data.clone())
.await
.unwrap();
match MoreThanText::new(dir.path()).await {
Ok(_) => Ok(()),
Err(err) => Err(err),
}
}
#[async_std::test]
async fn corrupt_enty_point() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let file = dir.path().join(ENTRY);
write(file, b"Really bad data.").await.unwrap();
match MoreThanText::new(dir.path()).await {
Ok(_) => Err(DBError::new("should have errored")),
Err(_) => Ok(()),
}
}
#[async_std::test]
async fn set_session() {
let dir = tempdir().unwrap();
let mut mtt = MoreThanText::new(dir.path()).await.unwrap();
let sess = ["different".to_string()];
mtt.set_session(sess.to_vec());
assert_eq!(mtt.session, sess);
}
#[async_std::test]
async fn add_a_database() {
let dir = tempdir().unwrap();
let mtt = MoreThanText::new(dir.path()).await.unwrap();
mtt.new_entry("wilbur").await;
}
}

640
src/morethantext/mod-3.rs Normal file
View File

@ -0,0 +1,640 @@
use async_std::{
channel::{unbounded, Receiver, Sender},
path::PathBuf,
task::spawn,
};
use std::{collections::HashMap, error::Error, fmt};
const ENTRY: &str = "EntryPoint";
#[derive(Debug)]
enum ErrorCode {
// General
Undefined(String),
// Cache
EntryNotFound(String),
InvalidCommitData,
// Store
DatabaseAlreadyExists(String),
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
ErrorCode::EntryNotFound(id) => write!(f, "entry '{}' was not found", id),
ErrorCode::InvalidCommitData => write!(f, "commit data was not a database store"),
ErrorCode::DatabaseAlreadyExists(name) => {
write!(f, "database '{}' already exists", name)
}
}
}
}
mod errorcodes {
use super::*;
const ITEMS: [&str; 2] = ["one", "two"];
#[test]
fn undefined_display() {
for item in ITEMS {
let err = ErrorCode::Undefined(item.to_string());
assert_eq!(err.to_string(), item);
}
}
#[test]
fn bad_entry() {
for item in ITEMS {
let err = ErrorCode::EntryNotFound(item.to_string());
assert_eq!(err.to_string(), format!("entry '{}' was not found", item));
}
}
#[test]
fn invalid_commit_data() {
let err = ErrorCode::InvalidCommitData;
assert_eq!(err.to_string(), "commit data was not a database store");
}
#[test]
fn database_already_exists() {
for item in ITEMS {
let err = ErrorCode::DatabaseAlreadyExists(item.to_string());
assert_eq!(
err.to_string(),
format!("database '{}' already exists", item)
);
}
}
}
#[derive(Debug)]
pub struct MTTError {
code: ErrorCode,
}
impl MTTError {
fn new<S>(msg: S) -> Self
where
S: Into<String>,
{
let text = msg.into();
Self {
code: ErrorCode::Undefined(text),
}
}
fn from_code(code: ErrorCode) -> Self {
Self { code: code }
}
}
impl Error for MTTError {}
impl fmt::Display for MTTError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.code)
}
}
#[cfg(test)]
mod errors {
use super::*;
#[test]
fn create_with_str() {
let msgs = ["one", "two"];
for msg in msgs {
let err = MTTError::new(msg);
assert_eq!(err.to_string(), msg);
}
}
#[test]
fn create_with_string() {
let msg = "three";
let err = MTTError::new(msg.to_string());
assert_eq!(err.to_string(), msg);
}
#[test]
fn create_from_code() {
let code = ErrorCode::Undefined("oops".to_string());
let err = MTTError::from_code(code);
match err.code {
ErrorCode::Undefined(_) => (),
_ => assert!(false, "{:?} is not undefined", err.code),
}
}
#[test]
fn create_missing_entry() {
let code = ErrorCode::EntryNotFound("an_id".to_string());
let err = MTTError::from_code(code);
match err.code {
ErrorCode::EntryNotFound(_) => (),
_ => assert!(false, "{:?} is not undefined", err.code),
}
}
}
#[derive(Clone, Debug)]
struct Storage<D> {
id: Option<String>,
data: Option<D>,
// delete: bool,
}
impl Storage<D> {
fn from_id<S>(id: S) -> Self
where
S: Into<String>,
{
Self {
id: Some(id.into()),
data: None,
}
}
fn from_datatype(dt: DataType) -> Self {
Self {
id: None,
data: Some(dt),
}
}
}
#[cfg(test)]
mod storage {
use super::*;
#[test]
fn from_id_with_str() {
let ids = ["first", "second"];
for id in ids {
let output = Storage::from_id(id);
assert_eq!(output.id, Some(id.to_string()));
assert!(
output.data.is_none(),
"The storage data should have been Non."
);
}
}
#[test]
fn from_id_with_string() {
let id = "my_id".to_string();
let output = Storage::from_id(id.clone());
assert_eq!(output.id, Some(id));
}
#[test]
fn from_store() {
let output = Storage::from_datatype(DataType::new("store"));
assert!(output.id.is_none(), "id should be None.");
assert!(output.data.is_some(), "There should be data");
let result = output.data.unwrap();
match result {
DataType::DBMap(_) => (),
_ => assert!(false, "{:?} should have been DataType::DBMap.", result),
}
}
#[test]
fn from_database() {
let output = Storage::from_datatype(DataType::new("database"));
let result = output.data.unwrap();
match result {
DataType::TableMap(_) => (),
_ => assert!(false, "{:?} should have been DataType::TableMap.", result),
}
}
}
#[derive(Clone, Debug)]
struct Store {
data: HashMap<String, Storage<Database>>,
}
impl Store {
fn new() -> Self {
Self {
data: HashMap::new(),
}
}
fn add_new<S>(&mut self, name: S) -> Result<(), MTTError>
where
S: Into<String>,
{
let dbname = name.into();
match self.get(&dbname) {
Some(_) => Err(MTTError::from_code(ErrorCode::DatabaseAlreadyExists(
dbname,
))),
None => {
self.data
.insert(dbname, Storage::from_datatype(DataType::new("database")));
Ok(())
}
}
}
fn get(&self, name: &str) -> Option<&Storage<Database>> {
self.data.get(name)
}
}
#[cfg(test)]
mod stores {
use super::*;
#[test]
fn get_no_database() -> Result<(), MTTError> {
let store = Store::new();
match store.get("missing_name") {
Some(_) => Err(MTTError::new("should have returned None")),
None => Ok(()),
}
}
#[test]
fn add_database_str() {
let mut store = Store::new();
let names = ["first", "second"];
for name in names {
store.add_new(name).unwrap();
let output = store.get(name).unwrap();
assert!(output.data.is_some(), "There should be a data type.");
match output.data.clone().unwrap() {
DataType::TableMap(_) => (),
_ => assert!(
false,
"{:?} should have been DataType::TableMap.",
output.data
),
}
assert!(output.id.is_none(), "Should not have an id.");
}
}
#[test]
fn add_database_string() {
let mut store = Store::new();
let name = "third".to_string();
store.add_new(name.clone()).unwrap();
let output = store.get(&name).unwrap();
match output.data.clone().unwrap() {
DataType::TableMap(_) => (),
_ => assert!(
false,
"{:?} should have been DataType::TableMap.",
output.data
),
}
}
#[test]
fn no_duplicate_database_names() -> Result<(), MTTError> {
let mut store = Store::new();
let name = "duplicate";
store.add_new(name).unwrap();
match store.add_new(name) {
Ok(_) => Err(MTTError::new("should have been an error")),
Err(err) => match err.code {
ErrorCode::DatabaseAlreadyExists(dbname) => {
assert_eq!(dbname, name);
Ok(())
}
_ => Err(MTTError::new(format!(
"{:?} should have been DatabaseAlreadyExists.",
err.code
))),
},
}
}
}
#[derive(Clone, Debug)]
struct Database;
#[cfg(test)]
mod databases {
use super::*;
#[test]
fn create() {
Database::new();
}
}
impl Database {
fn new() -> Self {
Self {}
}
}
#[derive(Clone, Debug)]
enum DataType {
DBMap(Store),
TableMap(Database),
}
impl DataType {
fn new(dtype: &str) -> DataType {
match dtype {
"store" => Self::DBMap(Store::new()),
"database" => Self::TableMap(Database::new()),
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod datatypes {
use super::*;
#[test]
fn create_store() {
let dtype = DataType::new("store");
match dtype {
DataType::DBMap(_) => (),
_ => assert!(false, "{:?} is not incorrect data type", dtype),
}
}
#[test]
fn create_database() {
let dtype = DataType::new("database");
match dtype {
DataType::TableMap(_) => (),
_ => assert!(false, "{:?} is not incorrect data type", dtype),
}
}
}
#[derive(Debug)]
enum FromCache {
Ok,
Data(HashMap<String, DataType>),
Error(MTTError),
}
struct CacheQuery {
ids: Vec<String>,
reply: Sender<FromCache>,
}
struct CacheCommit {
reply: Sender<FromCache>,
data: DataType,
}
impl CacheCommit {
fn new(data: DataType, channel: Sender<FromCache>) -> Result<Self, MTTError> {
match data {
DataType::DBMap(_) => (),
_ => return Err(MTTError::from_code(ErrorCode::InvalidCommitData)),
}
Ok(Self {
data: data,
reply: channel,
})
}
}
mod commits {
use super::*;
#[test]
fn create() -> Result<(), MTTError> {
let (s, _) = unbounded();
match CacheCommit::new(DataType::new("store"), s) {
Ok(output) => match output.data {
DataType::DBMap(_) => Ok(()),
_ => Err(MTTError::new(format!(
"{:?} should have been DBMap",
output.data
))),
},
Err(err) => Err(err),
}
}
#[test]
fn bad_data_type() -> Result<(), MTTError> {
let (s, _) = unbounded();
match CacheCommit::new(DataType::new("database"), s) {
Ok(_) => Err(MTTError::new("CacheCommit::new did not return error")),
Err(err) => match err.code {
ErrorCode::InvalidCommitData => Ok(()),
_ => Err(MTTError::new(format!(
"{:?} is not the correct error",
err.code
))),
},
}
}
}
enum ToCache {
Query(CacheQuery),
Commit(CacheCommit),
}
#[derive(Clone)]
pub struct MoreThanText {
session: Vec<String>,
cache: Sender<Vec<String>>,
}
impl MoreThanText {
async fn new(cache: Sender<Vec<String>>) -> Result<Self, MTTError> {
Ok(Self {
session: [ENTRY.to_string()].to_vec(),
cache: cache,
})
}
}
#[cfg(test)]
mod mtt {
use super::*;
#[async_std::test]
async fn create() {
let (s, _) = unbounded();
let mtt = MoreThanText::new(s).await.unwrap();
assert_eq!(mtt.session, [ENTRY]);
}
}
struct Cache;
impl Cache {
async fn new<P>(_dir: P) -> Result<Self, MTTError>
where
P: Into<PathBuf>,
{
Ok(Self {})
}
async fn query(&self, qry: &Vec<String>) -> Result<HashMap<String, DataType>, MTTError> {
let mut output = HashMap::new();
for id in qry {
if id == ENTRY {
output.insert(ENTRY.to_string(), DataType::new("store"));
} else {
return Err(MTTError::from_code(ErrorCode::EntryNotFound(
id.to_string(),
)));
}
}
Ok(output)
}
async fn commit(&self) -> Result<(), MTTError> {
Ok(())
}
async fn start(&self, listener: Receiver<ToCache>) {
loop {
match listener.recv().await.unwrap() {
ToCache::Query(qry) => match self.query(&qry.ids).await {
Ok(data) => qry.reply.send(FromCache::Data(data)).await.unwrap(),
Err(error) => qry.reply.send(FromCache::Error(error)).await.unwrap(),
},
ToCache::Commit(commit) => match self.commit().await {
Ok(_) => commit.reply.send(FromCache::Ok).await.unwrap(),
Err(error) => commit.reply.send(FromCache::Error(error)).await.unwrap(),
},
}
}
}
}
#[cfg(test)]
mod caches {
use super::*;
use tempfile::tempdir;
async fn start_cache<P>(dir: P) -> Sender<ToCache>
where
P: Into<PathBuf>,
{
let (s, r) = unbounded();
let datadir = dir.into();
spawn(async move {
let cache = Cache::new(datadir).await.unwrap();
cache.start(r).await;
});
s
}
async fn send_request(data: Vec<&str>, channel: Sender<ToCache>) -> FromCache {
let mut ids = Vec::new();
for id in data.iter() {
ids.push(id.to_string());
}
let (s, r) = unbounded();
let msg = ToCache::Query(CacheQuery { ids: ids, reply: s });
channel.send(msg).await.unwrap();
r.recv().await.unwrap()
}
#[async_std::test]
async fn create() {
let dir = tempdir().unwrap();
let s_cache = start_cache(dir.path()).await;
let result = send_request(vec![ENTRY], s_cache).await;
match result {
FromCache::Data(data) => match data.get(ENTRY) {
Some(output) => match output {
DataType::DBMap(_) => (),
_ => assert!(false, "{:?} is not a database store.", output),
},
None => assert!(false, "Should contain entry point."),
},
_ => assert!(false, "{:?} should have been a store.", result),
}
}
#[async_std::test]
async fn bad_entry() {
let dir = tempdir().unwrap();
let s_cache = start_cache(dir.path()).await;
let result = send_request(vec!["bad_id"], s_cache).await;
match result {
FromCache::Error(_) => (),
_ => assert!(false, "{:?} should have been an error.", result),
}
}
#[async_std::test]
async fn empty_commit() {
let dir = tempdir().unwrap();
let s_cache = start_cache(dir.path()).await;
let (s, r) = unbounded();
let msg = ToCache::Commit(CacheCommit::new(DataType::new("store"), s).unwrap());
s_cache.send(msg).await.unwrap();
let result = r.recv().await.unwrap();
match result {
FromCache::Ok => (),
_ => assert!(false, "{:?} should have been an Ok.", result),
}
}
#[async_std::test]
async fn get_store() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await.unwrap();
let output = cache.query(&[ENTRY.to_string()].to_vec()).await.unwrap();
let result = output.get(ENTRY).unwrap();
match result {
DataType::DBMap(_) => (),
_ => assert!(false, "{:?} should have been an Ok.", result),
}
}
#[async_std::test]
async fn bad_get() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await.unwrap();
let bad_id = "really_bad_id";
match cache.query(&[bad_id.to_string()].to_vec()).await {
Ok(_) => assert!(false, "Should have produced an error."),
Err(err) => match err.code {
ErrorCode::EntryNotFound(id) => assert_eq!(id, bad_id),
_ => assert!(false, "{:?} should have been EntryNotFound.", err.code),
},
}
}
}
pub async fn start_db<P>(_dir: P) -> Result<MoreThanText, MTTError>
where
P: Into<PathBuf>,
{
let (s, r) = unbounded();
spawn(async move {
loop {
r.recv().await.unwrap();
}
});
Ok(MoreThanText::new(s).await.unwrap())
}
#[cfg(test)]
mod db_start_up {
use super::*;
use tempfile::tempdir;
#[async_std::test]
async fn initial_session() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
assert_eq!(mtt.session, [ENTRY]);
}
}

823
src/morethantext/mod-old.rs Normal file
View File

@ -0,0 +1,823 @@
mod cache;
mod database;
pub mod error;
mod store;
use async_std::{
fs::{create_dir, read, remove_file, write},
path::Path,
sync::{Arc, Mutex},
task::{sleep, spawn},
};
use database::Database;
use error::{DBError, ErrorCode};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{
collections::HashMap,
fmt, slice, str,
time::{Duration, Instant},
};
use store::Store;
const DATA: &str = "data";
const ENTRY: &str = "databases";
trait FileData<F> {
fn to_bytes(&self) -> Vec<u8>;
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<F, DBError>;
}
trait SessionData {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError>;
fn eq(&self, key: &str, value: &str) -> Result<Vec<String>, DBError>;
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError>;
}
#[derive(Clone)]
pub enum CacheType {
Raw(String),
DBMap(Store),
TableMap,
}
impl CacheType {
pub fn entry_type(&self) -> String {
match self {
CacheType::Raw(_) => "Raw".to_string(),
CacheType::DBMap(_) => "DBMap".to_string(),
CacheType::TableMap => "TableMap".to_string(),
}
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut output = self.entry_type().into_bytes();
output.push(0);
match self {
CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()),
CacheType::DBMap(_) => (),
CacheType::TableMap => (),
}
return output;
}
pub fn from_bytes(data: Vec<u8>) -> Result<CacheType, DBError> {
let mut data_iter = data.iter();
let mut letter: u8;
match data_iter.next() {
Some(item) => letter = *item,
None => return Err(DBError::new("empty file")),
}
let mut header: Vec<u8> = Vec::new();
while letter != 0 {
header.push(letter.clone());
match data_iter.next() {
Some(item) => letter = *item,
None => return Err(DBError::new("incomplete file")),
}
}
let header = str::from_utf8(&header).unwrap().to_string();
match header.as_str() {
"Raw" => {
let mut output: Vec<u8> = Vec::new();
for letter in data_iter {
output.push(letter.clone());
}
Ok(CacheType::Raw(str::from_utf8(&output).unwrap().to_string()))
}
"DBMap" => Ok(CacheType::DBMap(Store::new())),
"TableMap" => Ok(CacheType::TableMap),
_ => Err(DBError::new("data corruption")),
}
}
}
impl fmt::Display for CacheType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CacheType::Raw(s) => write!(f, "{}", s),
CacheType::DBMap(_) => todo!(),
CacheType::TableMap => todo!(),
}
}
}
#[derive(Clone)]
struct CacheEntry {
data: CacheType,
last_used: Instant,
}
impl CacheEntry {
fn new(data: CacheType) -> Self {
Self {
data: data,
last_used: Instant::now(),
}
}
fn elapsed(&self) -> Duration {
self.last_used.elapsed()
}
fn touch(&mut self) {
self.last_used = Instant::now();
}
fn update(&mut self, data: CacheType) {
self.data = data;
self.touch();
}
}
impl fmt::Display for CacheEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.data)
}
}
#[derive(Clone)]
pub struct MoreThanText {
cache: Arc<Mutex<HashMap<String, CacheEntry>>>,
dir: String,
session: Vec<String>,
}
impl MoreThanText {
pub async fn new(dir: &str) -> Result<Self, DBError> {
let data_dir = Path::new(dir).join(DATA);
if !data_dir.is_dir().await {
match create_dir(&data_dir).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::new("failed to create data directory");
error.add_source(err);
return Err(error);
}
}
}
let mut output = Self {
cache: Arc::new(Mutex::new(HashMap::new())),
dir: data_dir.to_str().unwrap().to_string(),
session: Vec::new(),
};
let entry_file = Path::new(dir).join(ENTRY);
let id: String;
if entry_file.is_file().await {
let holder = read(entry_file).await.unwrap();
id = str::from_utf8(&holder).unwrap().to_string();
} else {
id = output
.add_entry(CacheType::DBMap(Store::new()))
.await
.unwrap();
write(entry_file, id.as_bytes()).await.unwrap();
}
output.session.push(id);
let looper = output.cache.clone();
spawn(async move {
let hold_time = Duration::from_secs(300);
loop {
sleep(Duration::from_secs(1)).await;
let mut ids: Vec<String> = Vec::new();
let mut cache = looper.lock().await;
for (id, entry) in cache.iter() {
if entry.elapsed() > hold_time {
ids.push(id.to_string());
}
}
for id in ids.iter() {
cache.remove(id);
}
}
});
Ok(output)
}
fn filename(&self, id: &str) -> String {
let filename = Path::new(&self.dir).join(&id);
filename.into_os_string().into_string().unwrap()
}
fn new_id(&self) -> String {
thread_rng().sample_iter(&Alphanumeric).take(64).collect()
}
async fn add(&self, feature: &str, key: &str, value: &str) -> Self {
let mut ids: Vec<String> = Vec::new();
for id in self.session.clone().into_iter() {
let holder = self.get_entry(&id).await.unwrap();
//holder.add(feature, key, value);
}
let mut output = self.clone();
output.session.clear();
output.session.push(value.to_string());
output
}
async fn list(&self, feature: Vec<&str>) -> Result<Vec<String>, DBError> {
Ok(Vec::new())
}
async fn add_entry(&self, entry: CacheType) -> Result<String, DBError> {
let mut id: String = "".to_string();
let mut dup = true;
while dup {
id = thread_rng().sample_iter(&Alphanumeric).take(32).collect();
dup = Path::new(&self.dir).join(&id).as_path().exists().await;
}
match write(Path::new(&self.filename(&id)), entry.to_bytes()).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::new("data write");
error.add_source(err);
return Err(error);
}
};
let mut cache = self.cache.lock().await;
let data = CacheEntry::new(entry);
cache.insert(id.clone(), data);
Ok(id)
}
async fn get_entry(&self, id: &str) -> Result<CacheEntry, DBError> {
let mut cache = self.cache.lock().await;
match cache.get_mut(id) {
Some(entry) => {
entry.touch();
Ok(entry.clone())
}
None => match read(Path::new(&self.filename(id))).await {
Ok(content) => {
let data = CacheEntry::new(CacheType::from_bytes(content).unwrap());
cache.insert(id.to_string(), data.clone());
Ok(data)
}
Err(_) => Err(DBError::new("cache entry not found")),
},
}
}
async fn update_entry(&self, id: &str, entry: CacheType) -> Result<(), DBError> {
match self.get_entry(id).await {
Ok(_) => (),
Err(err) => return Err(err),
}
match write(Path::new(&self.filename(id)), entry.to_bytes()).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::new("data write");
error.add_source(err);
return Err(error);
}
}
let mut cache = self.cache.lock().await;
let data = CacheEntry::new(entry);
cache.insert(id.to_string(), data);
Ok(())
}
async fn delete_entry(&self, id: &str) -> Result<(), DBError> {
let mut cache = self.cache.lock().await;
cache.remove(id);
match remove_file(Path::new(&self.filename(id))).await {
Ok(_) => Ok(()),
Err(err) => {
let mut error = DBError::new("data delete");
error.add_source(err);
Err(error)
}
}
}
}
#[cfg(test)]
mod setup {
use super::*;
use async_std::fs::remove_dir_all;
use tempfile::{tempdir, TempDir};
pub struct MTT {
pub db: MoreThanText,
pub dir: TempDir,
}
impl MTT {
pub async fn new() -> Self {
let dir = tempdir().unwrap();
let db = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
Self { db: db, dir: dir }
}
pub async fn create_io_error(&self) {
remove_dir_all(self.dir.path().join(DATA)).await.unwrap();
}
}
}
#[cfg(test)]
mod init {
use super::*;
use std::error::Error;
use tempfile::tempdir;
#[async_std::test]
async fn create_data_dir() {
let dir = tempdir().unwrap();
MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
let data_dir = dir.path().join(DATA);
assert!(data_dir.is_dir(), "Did not create the data directory.");
dir.close().unwrap();
}
#[async_std::test]
async fn existing_data_dir() {
let dir = tempdir().unwrap();
let data_dir = dir.path().join(DATA);
create_dir(data_dir).await.unwrap();
MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
dir.close().unwrap();
}
#[async_std::test]
async fn bad_data_dir() {
let msg = "could not create directory";
match MoreThanText::new("kljsdgfhslkfrh").await {
Ok(_) => assert!(false, "This test should fail to create a data directory"),
Err(err) => {
assert_eq!(err.to_string(), "failed to create data directory");
assert!(err.source().is_some(), "Must include the source error.");
let err_msg = err.source().unwrap().to_string();
assert!(err_msg.contains(msg), "'{}' not in '{}'", msg, err_msg);
}
};
}
#[async_std::test]
async fn creates_entry_point() {
let dir = tempdir().unwrap();
let db = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
let entry = dir.path().join(ENTRY);
assert!(entry.is_file(), "Did not create entry point file.");
let data = read(entry).await.unwrap();
let id = str::from_utf8(&data).unwrap();
let cache = db.get_entry(&id).await.unwrap();
assert_eq!(cache.data.entry_type(), "DBMap");
assert_eq!(db.session, [id]);
}
#[async_std::test]
async fn use_existing_entry_point() {
let dir = tempdir().unwrap();
let db1 = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
let db2 = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
assert_eq!(db1.session, db2.session, "Did not read existing entry.");
}
}
#[cfg(test)]
mod data {
use super::*;
use setup::MTT;
#[async_std::test]
async fn ids_are_random() {
let mtt = MTT::new().await;
let id1 = mtt.db.new_id();
let id2 = mtt.db.new_id();
assert_ne!(id1, id2, "Ids should be random");
}
#[async_std::test]
async fn add_database() {
let mtt = MTT::new().await;
let name = "fred";
let id = "*gsdfg";
let output = mtt.db.add("database", name, id).await;
assert_eq!(output.session, [id], "should update session info.");
/*
assert_eq!(
mtt.db.list(["database"].to_vec()).await.unwrap(),
[name],
"Should list the databases."
);
*/
}
}
#[cfg(test)]
mod cache_test {
use super::*;
use async_std::fs::read;
use setup::MTT;
use std::error::Error;
#[async_std::test]
async fn entry_ids_are_random() {
let mtt = MTT::new().await;
let data1 = CacheType::Raw("one".to_string());
let data2 = CacheType::Raw("two".to_string());
let id1 = mtt.db.add_entry(data1).await.unwrap();
let id2 = mtt.db.add_entry(data2).await.unwrap();
assert_ne!(id1, id2, "Ids should be unique.")
}
#[async_std::test]
async fn store_cache() {
let mtt = MTT::new().await;
let data = "something";
let expected = CacheType::Raw(data.to_string());
let id = mtt.db.add_entry(expected.clone()).await.unwrap();
let output = mtt.db.get_entry(&id).await.unwrap();
assert_eq!(output.to_string(), data);
let dfile = mtt.dir.path().join(DATA).join(&id);
assert!(dfile.is_file(), "Cache file should exist.");
let content = read(dfile).await.unwrap();
assert_eq!(content, expected.to_bytes());
}
#[async_std::test]
async fn get_entry_uodates_time() {
let mtt = MTT::new().await;
let id = "something";
let holder = CacheEntry {
data: CacheType::Raw("old".to_string()),
last_used: Instant::now() - Duration::from_secs(200),
};
let mut cache = mtt.db.cache.lock().await;
cache.insert(id.to_string(), holder);
drop(cache);
mtt.db.get_entry(&id).await.unwrap();
let cache = mtt.db.cache.lock().await;
let entry = cache.get(id).unwrap();
let held = entry.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
}
#[async_std::test]
async fn retrieve_from_disk() {
let mtt = MTT::new().await;
let id = "someid";
let data = CacheType::Raw("stored".to_string());
write(mtt.dir.path().join(DATA).join(id), data.to_bytes())
.await
.unwrap();
let output = mtt.db.get_entry(id).await.unwrap();
assert_eq!(output.to_string(), data.to_string());
let cache = mtt.db.cache.lock().await;
let stored = cache.get(id);
assert!(stored.is_some(), "Did not store entry in the cache.");
}
#[async_std::test]
async fn store_bad_file() {
let mtt = MTT::new().await;
let msg = "could not write to file";
mtt.create_io_error().await;
match mtt.db.add_entry(CacheType::Raw("fail".to_string())).await {
Ok(_) => assert!(false, "This test should fail."),
Err(err) => {
assert_eq!(err.to_string(), "data write");
assert!(err.source().is_some(), "Must include the source error.");
let err_msg = err.source().unwrap().to_string();
assert!(err_msg.contains(msg), "'{}' not in '{}'", msg, err_msg);
}
}
}
#[async_std::test]
async fn retrieve_bad_id() {
let mtt = MTT::new().await;
match mtt.db.get_entry(&"Not Valid").await {
Ok(_) => assert!(false, "Should have raised an error."),
Err(err) => assert_eq!(err.to_string(), "cache entry not found"),
}
}
#[async_std::test]
async fn update_cache_entry() {
let mtt = MTT::new().await;
let id = "updateable";
let holder = CacheEntry {
data: CacheType::Raw("elder".to_string()),
last_used: Instant::now() - Duration::from_secs(500),
};
let mut cache = mtt.db.cache.lock().await;
cache.insert(id.to_string(), holder);
drop(cache);
let expected = "different";
let expect = CacheType::Raw(expected.to_string());
mtt.db.update_entry(id, expect.clone()).await.unwrap();
let output = mtt.db.get_entry(id).await.unwrap();
assert_eq!(output.to_string(), expected);
let cache = mtt.db.cache.lock().await;
let entry = cache.get(id).unwrap();
let held = entry.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
drop(cache);
let content = read(mtt.dir.path().join(DATA).join(id)).await.unwrap();
assert_eq!(content, expect.to_bytes());
}
#[async_std::test]
async fn update_bad_id() {
let mtt = MTT::new().await;
match mtt
.db
.update_entry("wilma", CacheType::Raw("wrong".to_string()))
.await
{
Ok(_) => assert!(false, "Bad id should raise an error."),
Err(err) => assert_eq!(err.to_string(), "cache entry not found"),
}
}
#[async_std::test]
async fn update_bad_file() {
let mtt = MTT::new().await;
let msg = "could not write to file";
let id = mtt
.db
.add_entry(CacheType::Raw("fleeting".to_string()))
.await
.unwrap();
mtt.create_io_error().await;
match mtt
.db
.update_entry(&id, CacheType::Raw("failure".to_string()))
.await
{
Ok(_) => assert!(false, "This should produce a write failure."),
Err(err) => {
assert_eq!(err.to_string(), "data write");
assert!(err.source().is_some(), "Must include the source error.");
let err_msg = err.source().unwrap().to_string();
assert!(err_msg.contains(msg), "'{}' not in '{}'", msg, err_msg);
}
}
}
#[async_std::test]
async fn remove_entry() {
let mtt = MTT::new().await;
let id = mtt
.db
.add_entry(CacheType::Raw("delete".to_string()))
.await
.unwrap();
mtt.db.delete_entry(&id).await.unwrap();
match mtt.db.get_entry(&id).await {
Ok(_) => assert!(false, "Entry should be removed from cache."),
Err(_) => (),
};
}
#[async_std::test]
async fn remove_missing_entry() {
let mtt = MTT::new().await;
let msg = "could not remove file";
match mtt.db.delete_entry("missing").await {
Ok(_) => assert!(false, "This should produce a write failure."),
Err(err) => {
assert_eq!(err.to_string(), "data delete");
assert!(err.source().is_some(), "Must include the source error.");
let err_msg = err.source().unwrap().to_string();
assert!(err_msg.contains(msg), "'{}' not in '{}'", msg, err_msg);
}
}
}
#[async_std::test]
async fn remove_older() {
let mtt = MTT::new().await;
let id = mtt
.db
.add_entry(CacheType::Raw("removed".to_string()))
.await
.unwrap();
let mut cache = mtt.db.cache.lock().await;
let entry = cache.get_mut(&id).unwrap();
entry.last_used = Instant::now() - Duration::from_secs(1000);
drop(cache);
sleep(Duration::from_secs(2)).await;
let cache = mtt.db.cache.lock().await;
let output = cache.get(&id);
assert!(output.is_none(), "The entry shoould not be in memory.");
drop(cache);
let filename = mtt.db.filename(&id);
let fpath = Path::new(&filename);
assert!(
fpath.is_file().await,
"The stored version should still exist."
);
}
#[async_std::test]
async fn keep_newer() {
let mtt = MTT::new().await;
let id = mtt
.db
.add_entry(CacheType::Raw("keep".to_string()))
.await
.unwrap();
sleep(Duration::from_secs(2)).await;
let cache = mtt.db.cache.lock().await;
let output = cache.get(&id);
assert!(output.is_some(), "The entry shoould be in memory.");
}
}
#[cfg(test)]
mod cache_entry {
use super::*;
#[test]
fn init() {
let text = "new entry";
let holder = CacheEntry::new(CacheType::Raw(text.to_string()));
assert_eq!(holder.to_string(), text);
let held = holder.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
}
#[test]
fn older() {
let secs = 800;
let holder = CacheEntry {
data: CacheType::Raw("older".to_string()),
last_used: Instant::now() - Duration::from_secs(secs),
};
let held = holder.elapsed() - Duration::from_secs(secs);
assert!(
Duration::from_secs(1) > held,
"{:?} should be close to {}s",
holder.elapsed(),
secs
);
}
#[test]
fn accessed() {
let mut holder = CacheEntry {
data: CacheType::Raw("older".to_string()),
last_used: Instant::now() - Duration::from_secs(700),
};
holder.touch();
let held = holder.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
}
#[test]
fn updated() {
let text = "new data";
let mut holder = CacheEntry {
data: CacheType::Raw("old data".to_string()),
last_used: Instant::now() - Duration::from_secs(900),
};
holder.update(CacheType::Raw(text.to_string()));
assert_eq!(holder.to_string(), text);
let held = holder.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
}
}
#[cfg(test)]
mod enum_ctype {
use super::*;
#[test]
fn bad_file_header() {
let mut data: Vec<u8> = Vec::new();
let mut ctype = "jlksdfg".as_bytes().to_vec();
let mut cdata = "ghjk".as_bytes().to_vec();
data.append(&mut ctype);
data.push(0);
data.append(&mut cdata);
match CacheType::from_bytes(data) {
Ok(_) => assert!(false, "This should fail."),
Err(err) => assert_eq!(err.to_string(), "data corruption"),
}
}
#[test]
fn incomplete_file() {
let mut data: Vec<u8> = Vec::new();
let mut ctype = "uoisfde".as_bytes().to_vec();
data.append(&mut ctype);
match CacheType::from_bytes(data) {
Ok(_) => assert!(false, "This should fail."),
Err(err) => assert_eq!(err.to_string(), "incomplete file"),
}
}
#[test]
fn empty_file() {
let data: Vec<u8> = Vec::new();
match CacheType::from_bytes(data) {
Ok(_) => assert!(false, "This should fail."),
Err(err) => assert_eq!(err.to_string(), "empty file"),
}
}
#[test]
fn get_raw_type() {
let holder = CacheType::Raw("nothing important".to_string());
assert_eq!(holder.entry_type(), "Raw");
}
#[test]
fn get_raw_bytes() {
let data = "addams";
let holder = CacheType::Raw(data.to_string());
let mut expected = holder.entry_type().into_bytes();
expected.push(0);
expected.append(&mut data.as_bytes().to_vec());
let output = holder.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_raw_bytes() {
let holder = CacheType::Raw("stored item".to_string());
let data = holder.to_bytes();
let output = CacheType::from_bytes(data).unwrap();
assert_eq!(output.to_string(), holder.to_string());
}
#[test]
fn get_dbmap_type() {
let holder = CacheType::DBMap(Store::new());
assert_eq!(holder.entry_type(), "DBMap");
}
#[test]
fn get_new_databases_bytes() {
let holder = CacheType::DBMap(Store::new());
let mut expected = "DBMap".as_bytes().to_vec();
expected.push(0);
let output = holder.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_new_databases_bytes() {
let mut data = "DBMap".as_bytes().to_vec();
data.push(0);
let output = CacheType::from_bytes(data).unwrap();
assert_eq!(output.entry_type(), "DBMap");
}
#[test]
fn get_tablemap_type() {
let holder = CacheType::TableMap;
assert_eq!(holder.entry_type(), "TableMap");
}
#[test]
fn get_new_database_bytes() {
let holder = CacheType::TableMap;
let mut expected = "TableMap".as_bytes().to_vec();
expected.push(0);
let output = holder.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_new_database_bytes() {
let mut data = "TableMap".as_bytes().to_vec();
data.push(0);
let output = CacheType::from_bytes(data).unwrap();
assert_eq!(output.entry_type(), "TableMap");
}
}

View File

@ -1,135 +1,186 @@
pub mod error; mod cache;
mod database;
mod error;
mod store;
mod table;
use async_std::sync::{Arc, RwLock}; use async_std::{
use error::DBError; channel::{unbounded, Sender},
use std::collections::HashMap; path::PathBuf,
task::spawn,
};
use cache::Cache;
use database::Database;
use error::{ErrorCode, MTTError};
use store::Store;
use table::Table;
const ENTRY: &str = "EntryPoint";
#[derive(Debug)]
pub struct ToCacheMsg<D> {
data: D,
result: Sender<FromCache>,
}
#[derive(Debug)]
pub enum ToCache {
Get(ToCacheMsg<String>),
Commit(ToCacheMsg<Store>),
}
#[derive(Clone, Debug)]
pub enum FromCache {
Ok,
Str(Store),
DB(Database),
Error(MTTError),
}
#[derive(Clone, Debug)]
pub struct Data<D> {
id: Option<String>,
data: Option<D>,
}
impl<D> Data<D> {
fn from_id<S>(id: S) -> Self
where
S: Into<String>,
{
Self {
id: Some(id.into()),
data: None,
}
}
fn from_data(data: D) -> Self {
Self {
id: None,
data: Some(data),
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct MoreThanText { pub struct MoreThanText {
databases: Arc<RwLock<HashMap<String, Database>>>, to_cache: Sender<ToCache>,
entry: Data<Store>,
} }
impl MoreThanText { impl MoreThanText {
pub async fn new() -> Self { fn new(to_cache: Sender<ToCache>) -> Self {
Self { Self {
databases: Arc::new(RwLock::new(HashMap::new())), to_cache: to_cache,
entry: Data::from_id(ENTRY),
} }
} }
async fn create_database(&self, name: &str) -> Result<(), DBError> { async fn session(&self) -> Result<Store, MTTError> {
let mut databases = self.databases.write().await; let (s, r) = unbounded();
match databases.get(name) { let msg = ToCacheMsg {
Some(_) => Err(DBError::new("duplicate database name")), data: ENTRY.to_string(),
None => { result: s,
let db = Database::new().await; };
databases.insert(name.to_string(), db); self.to_cache.send(ToCache::Get(msg)).await.unwrap();
Ok(()) match r.recv().await.unwrap() {
} FromCache::Str(store) => Ok(store),
FromCache::Error(err) => Err(err),
_ => unreachable!(),
} }
} }
async fn use_database(&self, name: &str) -> Result<Database, DBError> { async fn commit(&self, store: Store) -> Result<(), MTTError> {
let databases = self.databases.read().await; let (s, r) = unbounded();
match databases.get(name) { let msg = ToCacheMsg {
Some(db) => Ok(db.clone()), data: store,
None => Err(DBError::new("database name not found")), result: s,
};
self.to_cache.send(ToCache::Commit(msg)).await.unwrap();
match r.recv().await.unwrap() {
FromCache::Ok => Ok(()),
FromCache::Error(err) => Err(err),
_ => unreachable!(),
} }
} }
} }
#[derive(Clone)]
struct Database;
impl Database {
async fn new() -> Self {
Self {}
}
async fn add_table(&self, _name: &str) {}
}
#[cfg(test)]
mod engine_functions {
use super::*;
#[async_std::test]
async fn create_database() {
let mtt = MoreThanText::new().await;
mtt.create_database("smith").await.unwrap();
}
#[async_std::test]
async fn database_names_must_be_unique() -> Result<(), DBError> {
let mtt = MoreThanText::new().await;
let msg = "duplicate database name";
mtt.create_database("john").await.unwrap();
match mtt.create_database("john").await {
Ok(_) => Err(DBError::new("Duplicate names should cause error")),
Err(err) => {
if err.to_string() == msg {
Ok(())
} else {
Err(DBError::new(format!(
"incorrect err message: got: '{}' want: '{}'",
err.to_string(),
msg
)))
}
}
}
}
#[async_std::test]
async fn use_database() -> Result<(), DBError> {
let mtt = MoreThanText::new().await;
let dbname = "Johnson";
mtt.create_database(dbname).await.unwrap();
mtt.use_database(dbname).await.unwrap();
Ok(())
}
#[async_std::test]
async fn use_missing_database() -> Result<(), DBError> {
let error = "database name not found";
let mtt = MoreThanText::new().await;
match mtt.use_database("ssmith").await {
Ok(_) => Err(DBError::new("Should raise database missing error")),
Err(err) => {
if err.to_string() == error {
Ok(())
} else {
Err(DBError::new(format!(
"Incorrect error message: Got '{}' Want '{}'",
err.to_string(),
error
)))
}
}
}
}
#[async_std::test]
async fn create_get_table() {
let db = "thedatabase";
let mtt = MoreThanText::new().await;
mtt.create_database(db).await.unwrap();
let dbase = mtt.use_database(db).await.unwrap();
dbase.add_table("melvin").await;
}
}
#[cfg(test)] #[cfg(test)]
mod database_functions { mod mtt {
use super::*; use super::*;
use tempfile::tempdir;
#[async_std::test] #[async_std::test]
async fn new_database() { async fn create_new() {
Database::new().await; let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
assert_eq!(mtt.entry.id, Some(ENTRY.to_string()));
assert!(mtt.entry.data.is_none());
let store = mtt.session().await.unwrap();
let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected);
} }
#[async_std::test] #[async_std::test]
async fn new_table() { async fn commit_db() {
let db = Database::new().await; let dir = tempdir().unwrap();
db.add_table("fred").await; let db = "fred";
let mtt = start_db(dir.path()).await.unwrap();
let mut store = mtt.session().await.unwrap();
store.add(db).unwrap();
mtt.commit(store).await.unwrap();
let store2 = mtt.session().await.unwrap();
assert_eq!(store2.list(), [db]);
}
#[async_std::test]
async fn commit_from_multiple_sources() {
let dir = tempdir().unwrap();
let mtt1 = start_db(dir.path()).await.unwrap();
let mtt2 = mtt1.clone();
let db1 = "first";
let db2 = "second";
let mut store1 = mtt1.session().await.unwrap();
let mut store2 = mtt2.session().await.unwrap();
store1.add(db1).unwrap();
store2.add(db2).unwrap();
mtt1.commit(store1).await.unwrap();
mtt2.commit(store2).await.unwrap();
let output = mtt1.session().await.unwrap();
assert_eq!(output.list(), [db1, db2]);
}
#[async_std::test]
async fn fail_on_duplicates() {
let dir = tempdir().unwrap();
let mtt1 = start_db(dir.path()).await.unwrap();
let mtt2 = mtt1.clone();
let name = "unique_only";
let mut store1 = mtt1.session().await.unwrap();
let mut store2 = mtt2.session().await.unwrap();
store1.add(name).unwrap();
store2.add(name).unwrap();
mtt1.commit(store1).await.unwrap();
let output = mtt2.commit(store2).await;
match output {
Ok(_) => assert!(false, "Should have returned an error"),
Err(err) => match err.code {
ErrorCode::DuplicateDatabase(_) => (),
_ => assert!(false, "{:?} is not ErrorCode::DuplicateDatabase", err.code),
},
}
} }
} }
pub async fn start_db<P>(dir: P) -> Result<MoreThanText, MTTError>
where
P: Into<PathBuf>,
{
let path = dir.into();
let (s, r) = unbounded();
spawn(async move {
let mut cache = Cache::new(path).await;
cache.listen(r).await;
});
Ok(MoreThanText::new(s))
}

View File

@ -0,0 +1,153 @@
pub mod error;
use async_std::sync::{Arc, RwLock};
use error::DBError;
use std::collections::HashMap;
#[derive(Clone)]
pub struct MoreThanText {
databases: Arc<RwLock<HashMap<String, Database>>>,
}
impl MoreThanText {
pub async fn new() -> Self {
Self {
databases: Arc::new(RwLock::new(HashMap::new())),
}
}
async fn create_database(&self, name: &str) -> Result<(), DBError> {
let mut databases = self.databases.write().await;
match databases.get(name) {
Some(_) => Err(DBError::new("duplicate database name")),
None => {
let db = Database::new().await;
databases.insert(name.to_string(), db);
Ok(())
}
}
}
async fn use_database(&self, name: &str) -> Result<Database, DBError> {
let databases = self.databases.read().await;
match databases.get(name) {
Some(db) => Ok(db.clone()),
None => Err(DBError::new("database name not found")),
}
}
}
#[derive(Clone)]
struct Database;
impl Database {
async fn new() -> Self {
Self {}
}
async fn add_table(&self, _name: &str) {}
}
struct Table;
impl Table {
async fn new() -> Self {
Self {}
}
}
#[cfg(test)]
mod engine_functions {
use super::*;
#[async_std::test]
async fn create_database() {
let mtt = MoreThanText::new().await;
mtt.create_database("smith").await.unwrap();
}
#[async_std::test]
async fn database_names_must_be_unique() -> Result<(), DBError> {
let mtt = MoreThanText::new().await;
let msg = "duplicate database name";
mtt.create_database("john").await.unwrap();
match mtt.create_database("john").await {
Ok(_) => Err(DBError::new("Duplicate names should cause error")),
Err(err) => {
if err.to_string() == msg {
Ok(())
} else {
Err(DBError::new(format!(
"incorrect err message: got: '{}' want: '{}'",
err.to_string(),
msg
)))
}
}
}
}
#[async_std::test]
async fn use_database() -> Result<(), DBError> {
let mtt = MoreThanText::new().await;
let dbname = "Johnson";
mtt.create_database(dbname).await.unwrap();
mtt.use_database(dbname).await.unwrap();
Ok(())
}
#[async_std::test]
async fn use_missing_database() -> Result<(), DBError> {
let error = "database name not found";
let mtt = MoreThanText::new().await;
match mtt.use_database("ssmith").await {
Ok(_) => Err(DBError::new("Should raise database missing error")),
Err(err) => {
if err.to_string() == error {
Ok(())
} else {
Err(DBError::new(format!(
"Incorrect error message: Got '{}' Want '{}'",
err.to_string(),
error
)))
}
}
}
}
#[async_std::test]
async fn create_get_table() {
let db = "thedatabase";
let mtt = MoreThanText::new().await;
mtt.create_database(db).await.unwrap();
let dbase = mtt.use_database(db).await.unwrap();
dbase.add_table("melvin").await;
}
}
#[cfg(test)]
mod database_functions {
use super::*;
#[async_std::test]
async fn new_database() {
Database::new().await;
}
#[async_std::test]
async fn new_table() {
let db = Database::new().await;
db.add_table("fred").await;
}
}
#[cfg(test)]
mod table_functions {
use super::*;
#[async_std::test]
async fn new_table() {
Table::new().await;
}
}

View File

@ -0,0 +1,306 @@
use super::{DBError, ErrorCode, FileData, SessionData};
use std::{collections::HashMap, slice, str};
#[derive(Clone)]
pub struct Store {
db_map: HashMap<String, String>,
}
impl Store {
pub fn new() -> Self {
Self {
db_map: HashMap::new(),
}
}
fn test_key(key: &str) -> Result<(), DBError> {
match key {
"database" => (),
_ => return Err(DBError::new(format!("databases do not have a {}", key))),
}
Ok(())
}
}
impl FileData<Self> for Store {
fn to_bytes(&self) -> Vec<u8> {
let mut output = Vec::new();
for (name, id) in self.db_map.iter() {
output.append(&mut name.as_bytes().to_vec());
output.push(0);
output.append(&mut id.as_bytes().to_vec());
output.push(0);
}
output
}
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<Self, DBError> {
let mut output = Store::new();
let mut name: Vec<u8> = Vec::new();
let mut id: Vec<u8> = Vec::new();
let mut get_id = false;
let mut letter: u8;
loop {
match data.next() {
Some(a) => letter = a.clone(),
None => {
if !name.is_empty() {
return Err(DBError::from_code(ErrorCode::CorruptFile));
}
break;
}
}
if letter == 0 {
if get_id {
let name_holder = match str::from_utf8(&name) {
Ok(item) => item,
Err(_) => return Err(DBError::from_code(ErrorCode::CorruptFile)),
};
let id_holder = match str::from_utf8(&id) {
Ok(item) => item,
Err(_) => return Err(DBError::from_code(ErrorCode::CorruptFile)),
};
match output.add("database", name_holder, id_holder) {
Ok(_) => (),
Err(err) => {
let mut error = DBError::from_code(ErrorCode::CorruptFile);
error.add_source(err);
return Err(error);
}
};
name.clear();
id.clear();
}
get_id = !get_id;
} else {
if get_id {
id.push(letter);
} else {
name.push(letter);
}
}
}
Ok(output)
}
}
impl SessionData for Store {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError> {
match Self::test_key(key) {
Ok(_) => (),
Err(err) => return Err(err),
}
match self.db_map.get(value) {
Some(_) => return Err(DBError::new(format!("database {} already exists", value))),
None => (),
}
self.db_map.insert(value.to_string(), data.to_string());
let mut output = Vec::new();
output.push(data.to_string());
Ok(output)
}
fn eq(&self, key: &str, value: &str) -> Result<Vec<String>, DBError> {
match Self::test_key(key) {
Ok(_) => (),
Err(err) => return Err(err),
}
let mut output = Vec::new();
match self.db_map.get(value) {
Some(data) => output.push(data.to_string()),
None => (),
}
Ok(output)
}
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError> {
for key in keys {
match Self::test_key(key) {
Ok(_) => (),
Err(err) => return Err(err),
}
}
let mut names: Vec<String> = self.db_map.clone().into_keys().collect();
names.sort();
Ok(names)
}
}
#[cfg(test)]
mod file_data {
use super::*;
use std::error::Error;
#[test]
fn to_bytes_new() {
let dbs = Store::new();
let expected: Vec<u8> = Vec::new();
let output = dbs.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn to_bytes_with_database() {
let mut dbs = Store::new();
let name = "something";
let id = "id";
dbs.add("database", name, id).unwrap();
let mut expected: Vec<u8> = Vec::new();
expected.append(&mut name.as_bytes().to_vec());
expected.push(0);
expected.append(&mut id.as_bytes().to_vec());
expected.push(0);
let output = dbs.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_bytes() {
let mut dbs = Store::new();
dbs.add("database", "one", "1").unwrap();
dbs.add("database", "two", "2").unwrap();
dbs.add("database", "three", "3").unwrap();
let data = dbs.to_bytes();
let mut feed = data.iter();
let output = Store::from_bytes(&mut feed).unwrap();
assert_eq!(output.db_map, dbs.db_map);
}
#[test]
fn from_bytes_incomplete_name() -> Result<(), DBError> {
let data = "notName".as_bytes();
let mut feed = data.iter();
match Store::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have produced an errpr")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[test]
fn from_bytes_incomplete_id() -> Result<(), DBError> {
let mut data = "proper".as_bytes().to_vec();
data.push(0);
data.append(&mut "nope".as_bytes().to_vec());
let mut feed = data.iter();
match Store::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have produced an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[test]
fn from_bytes_handles_error() -> Result<(), DBError> {
let name = "duplicate";
let mut data = name.as_bytes().to_vec();
data.push(0);
data.append(&mut "first".as_bytes().to_vec());
data.push(0);
data.append(&mut "duplicate".as_bytes().to_vec());
data.push(0);
data.append(&mut "second".as_bytes().to_vec());
data.push(0);
let mut feed = data.iter();
match Store::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have returned an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => {
assert!(
err.source().is_some(),
"Should state file corruption cause."
);
assert_eq!(
err.source().unwrap().to_string(),
format!("database {} already exists", name)
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
}
#[cfg(test)]
mod session_data {
use super::*;
#[test]
fn add_new() {
let mut dbs = Store::new();
let key = "database";
let value = "marvin";
let data = "123456";
assert_eq!(dbs.add(key, value, data).unwrap(), [data]);
let output = dbs.eq(key, value).unwrap();
assert_eq!(output, [data]);
}
#[test]
fn add_bad_key() {
let mut dbs = Store::new();
let key = "sdgfjksg";
match dbs.add(key, "fred", "barney") {
Ok(_) => assert!(false, "Bad keys should produce an error."),
Err(err) => assert_eq!(err.to_string(), format!("databases do not have a {}", key)),
}
}
#[test]
fn get_bad_key() {
let dbs = Store::new();
let key = "bvdfgert";
match dbs.eq(key, "fred") {
Ok(_) => assert!(false, "Bad keys should produce an error."),
Err(_) => (),
}
}
#[test]
fn unique_names() {
let mut dbs = Store::new();
let value = "wilma";
dbs.add("database", value, "something").unwrap();
match dbs.add("database", value, "overwrite") {
Ok(_) => assert!(false, "Duplicate names should produce an error."),
Err(err) => assert_eq!(
err.to_string(),
format!("database {} already exists", value)
),
}
}
#[test]
fn get_missing() {
let dbs = Store::new();
let output = dbs.eq("database", "melvin").unwrap();
assert_eq!(output, Vec::<String>::new());
}
#[test]
fn list_bad_keys() {
let dbs = Store::new();
let key = "sdfgren";
let keys = [key];
match dbs.list(keys.to_vec()) {
Ok(_) => assert!(false, "Bad keys should error."),
Err(err) => assert_eq!(err.to_string(), format!("databases do not have a {}", key)),
}
}
#[test]
fn list_is_sorted() {
let mut dbs = Store::new();
let mut data = ["fred", "barney", "wilma", "betty", "pebbles", "bambam"];
for db in data {
dbs.add("database", db, db).unwrap();
}
data.sort();
assert_eq!(dbs.list(["database"].to_vec()).unwrap(), data);
}
}

171
src/morethantext/store.rs Normal file
View File

@ -0,0 +1,171 @@
use super::{Data, Database, ErrorCode, MTTError};
use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct Store {
data: HashMap<String, Data<Database>>,
}
impl Store {
pub fn new() -> Self {
Self {
data: HashMap::new(),
}
}
pub fn add<S>(&mut self, name: S) -> Result<(), MTTError>
where
S: Into<String>,
{
let db_name = name.into();
match self.get(&db_name) {
Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateDatabase(db_name))),
None => {
self.data.insert(db_name, Data::from_data(Database::new()));
Ok(())
}
}
}
pub fn add_by_id<S, D>(&mut self, name: S, id: D) -> Result<(), MTTError>
where
S: Into<String>,
D: Into<String>,
{
let db_name = name.into();
match self.get(&db_name) {
Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateDatabase(db_name))),
None => {
self.data.insert(db_name, Data::from_id(id.into()));
Ok(())
}
}
}
pub fn get(&self, name: &str) -> Option<&Data<Database>> {
self.data.get(name)
}
pub fn list(&self) -> Vec<String> {
let mut names = Vec::new();
for name in self.data.keys() {
names.push(name.to_string());
}
names.sort();
names
}
}
#[cfg(test)]
mod storage {
use super::*;
#[test]
fn create_new() {
let store = Store::new();
let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected);
}
#[test]
fn add_db_by_str() {
let mut store = Store::new();
let name = "Melvin";
store.add(name).unwrap();
let output = store.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn add_db_by_string() {
let mut store = Store::new();
let name = "Marvin";
store.add(name.to_string()).unwrap();
let output = store.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn fail_on_duplicates() -> Result<(), MTTError> {
let mut store = Store::new();
let name = "Mickie";
store.add(name).unwrap();
match store.add(name) {
Ok(_) => Err(MTTError::new("duplicates should error")),
Err(err) => match err.code {
ErrorCode::DuplicateDatabase(db_name) => {
assert_eq!(db_name, name);
Ok(())
}
_ => Err(MTTError::new(format!("{:?} is not DuplicateDatabase", err))),
},
}
}
#[test]
fn add_using_cache_id() {
let mut store = Store::new();
let name = "fred";
let id = "12345";
store.add_by_id(name, id).unwrap();
let output = store.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn add_by_cache_id_name_string() {
let mut store = Store::new();
let name = "barney";
let id = "67890";
store.add_by_id(name.to_string(), id).unwrap();
let output = store.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn no_duplicate_databases_for_add_by_id() {
let mut store = Store::new();
let name = "betty";
store.add_by_id(name, "fghij").unwrap();
match store.add_by_id(name, "klmno") {
Ok(_) => assert!(false, "Duplicates should error."),
Err(err) => match err.code {
ErrorCode::DuplicateDatabase(db_name) => assert_eq!(db_name, name),
_ => assert!(false, "{:?} is not DuplicateDatabase", err),
},
}
}
#[test]
fn add_by_cache_id_string() {
let mut store = Store::new();
let name = "wilma";
let id = "abcdef";
store.add_by_id(name, id.to_string()).unwrap();
let output = store.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn get_bad_database() -> Result<(), MTTError> {
let store = Store::new();
match store.get("missing") {
Some(_) => Err(MTTError::new("Should have returned None.")),
None => Ok(()),
}
}
#[test]
fn get_list() {
let mut store = Store::new();
let mut ids = ["one", "two", "three", "four", "five"];
for name in ids {
store.add(name.to_string()).unwrap();
}
ids.sort();
assert_eq!(store.list(), ids);
}
}

18
src/morethantext/table.rs Normal file
View File

@ -0,0 +1,18 @@
#[derive(Clone, Debug)]
pub struct Table;
impl Table {
pub fn new() -> Self {
Self {}
}
}
#[cfg(test)]
mod tables {
use super::*;
#[test]
fn create_new() {
Table::new();
}
}

View File

@ -1,18 +1,23 @@
use config::{Config, ConfigError}; use config::{Config, ConfigError};
use serde::Deserialize; use serde::Deserialize;
use std::env::current_dir;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
//#[allow(unused)] //#[allow(unused)]
pub struct Settings { pub struct Settings {
pub address: String, pub address: String,
pub port: u16, pub port: u16,
pub data_dir: String,
} }
impl Settings { impl Settings {
pub fn new() -> Result<Self, ConfigError> { pub fn new() -> Result<Self, ConfigError> {
let working_dir = current_dir().unwrap();
let data_dir = working_dir.to_str().unwrap();
let s = Config::builder() let s = Config::builder()
.set_default("port", 9090)? .set_default("port", 9090)?
.set_default("address", "127.0.0.1")? .set_default("address", "127.0.0.1")?
.set_default("data_dir", data_dir)?
.add_source(config::File::with_name("morethantext").required(false)) .add_source(config::File::with_name("morethantext").required(false))
.add_source( .add_source(
config::Environment::with_prefix("MTT") config::Environment::with_prefix("MTT")
@ -34,9 +39,12 @@ mod settings_tests {
#[serial] #[serial]
fn defaults() { fn defaults() {
let set = Settings::new().unwrap(); let set = Settings::new().unwrap();
let working_dir = current_dir().unwrap();
let data_dir = working_dir.to_str().unwrap();
println!("{:?}", set); println!("{:?}", set);
assert_eq!(set.port, 9090); assert_eq!(set.port, 9090);
assert_eq!(set.address, "127.0.0.1"); assert_eq!(set.address, "127.0.0.1");
assert_eq!(set.data_dir, data_dir);
} }
#[test] #[test]