Skip to content

Commit fb8d7ec

Browse files
authored
Revert "Design a basic storage abstraction layer for the server (#657)" (#658)
This reverts commit fa0cf7b.
1 parent 147dca3 commit fb8d7ec

File tree

10 files changed

+131
-282
lines changed

10 files changed

+131
-282
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ endif()
8484
if(ONE_SERVER)
8585
find_package(uSockets REQUIRED)
8686
find_package(uWebSockets REQUIRED)
87-
add_subdirectory(src/storage)
8887
add_subdirectory(src/server)
8988
endif()
9089

src/server/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ target_link_libraries(sourcemeta_one_server PRIVATE uNetworking::uSockets)
2222
target_link_libraries(sourcemeta_one_server PRIVATE uNetworking::uWebSockets)
2323
target_link_libraries(sourcemeta_one_server PRIVATE sourcemeta::one::gzip)
2424
target_link_libraries(sourcemeta_one_server PRIVATE sourcemeta::one::shared)
25-
target_link_libraries(sourcemeta_one_server PRIVATE sourcemeta::one::storage)
2625
target_link_libraries(sourcemeta_one_server PRIVATE sourcemeta::blaze::evaluator)
2726
target_link_libraries(sourcemeta_one_server PRIVATE sourcemeta::blaze::output)
2827

src/server/action_jsonschema_evaluate.h

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
#include <sourcemeta/blaze/output.h>
99

1010
#include <sourcemeta/one/shared.h>
11-
#include <sourcemeta/one/storage.h>
1211

1312
#include "helpers.h"
1413
#include "request.h"
1514
#include "response.h"
1615

1716
#include <cassert> // assert
17+
#include <filesystem> // std::filesystem::path
1818
#include <sstream> // std::ostringstream
19-
#include <string> // std::string
2019
#include <string_view> // std::string_view
2120
#include <type_traits> // std::underlying_type_t
2221
#include <utility> // std::move
@@ -25,14 +24,14 @@ namespace {
2524

2625
auto trace(sourcemeta::blaze::Evaluator &evaluator,
2726
const sourcemeta::blaze::Template &schema_template,
28-
const std::string &instance, const sourcemeta::one::Storage &storage,
29-
const sourcemeta::one::Storage::Key template_key)
27+
const std::string &instance,
28+
const std::filesystem::path &template_path)
3029
-> sourcemeta::core::JSON {
3130
auto steps{sourcemeta::core::JSON::make_array()};
3231

32+
auto locations_path{template_path.parent_path() / "locations.metapack"};
3333
// TODO: Cache this across runs?
34-
const auto locations{
35-
storage.read_sibling_json(template_key, "locations.metapack")};
34+
const auto locations{sourcemeta::one::read_json(locations_path)};
3635
assert(locations.defines("static"));
3736
const auto &static_locations{locations.at("static")};
3837

@@ -121,13 +120,15 @@ namespace sourcemeta::one {
121120

122121
enum class EvaluateType { Standard, Trace };
123122

124-
auto evaluate(const Storage &storage, const Storage::Key template_key,
123+
auto evaluate(const std::filesystem::path &template_path,
125124
const std::string &instance, const EvaluateType type)
126125
-> sourcemeta::core::JSON {
126+
assert(std::filesystem::exists(template_path));
127+
127128
// TODO: Cache this conversion across runs, potentially using the schema file
128129
// "checksum" as the cache key. This is important as the template might be
129130
// compressed
130-
const auto template_json{storage.read_json(template_key)};
131+
const auto template_json{read_json(template_path)};
131132
const auto schema_template{sourcemeta::blaze::from_json(template_json)};
132133
assert(schema_template.has_value());
133134

@@ -140,8 +141,7 @@ auto evaluate(const Storage &storage, const Storage::Key template_key,
140141
sourcemeta::core::parse_json(instance),
141142
sourcemeta::blaze::StandardOutput::Basic);
142143
case EvaluateType::Trace:
143-
return trace(evaluator, schema_template.value(), instance, storage,
144-
template_key);
144+
return trace(evaluator, schema_template.value(), instance, template_path);
145145
default:
146146
// We should never get here
147147
assert(false);
@@ -151,13 +151,12 @@ auto evaluate(const Storage &storage, const Storage::Key template_key,
151151

152152
} // namespace sourcemeta::one
153153

154-
static auto action_jsonschema_evaluate_callback(
155-
const sourcemeta::one::Storage &storage,
156-
const sourcemeta::one::Storage::Key template_key,
157-
const sourcemeta::one::EvaluateType mode,
158-
sourcemeta::one::HTTPRequest &request,
159-
sourcemeta::one::HTTPResponse &response, std::string &&body, bool too_big)
160-
-> void {
154+
static auto
155+
action_jsonschema_evaluate_callback(const std::filesystem::path &template_path,
156+
const sourcemeta::one::EvaluateType mode,
157+
sourcemeta::one::HTTPRequest &request,
158+
sourcemeta::one::HTTPResponse &response,
159+
std::string &&body, bool too_big) -> void {
161160
if (too_big) {
162161
json_error(request, response, sourcemeta::one::STATUS_PAYLOAD_TOO_LARGE,
163162
"payload-too-large", "The request body is too large");
@@ -171,8 +170,7 @@ static auto action_jsonschema_evaluate_callback(
171170
}
172171

173172
try {
174-
const auto result{
175-
sourcemeta::one::evaluate(storage, template_key, body, mode)};
173+
const auto result{sourcemeta::one::evaluate(template_path, body, mode)};
176174
response.write_status(sourcemeta::one::STATUS_OK);
177175
response.write_header("Content-Type", "application/json");
178176
response.write_header("Access-Control-Allow-Origin", "*");
@@ -194,7 +192,7 @@ static auto action_jsonschema_evaluate_callback(
194192
}
195193
}
196194

197-
static auto action_jsonschema_evaluate(const sourcemeta::one::Storage &storage,
195+
static auto action_jsonschema_evaluate(const std::filesystem::path &base,
198196
const std::string_view &path,
199197
sourcemeta::one::HTTPRequest &request,
200198
sourcemeta::one::HTTPResponse &response,
@@ -209,10 +207,13 @@ static auto action_jsonschema_evaluate(const sourcemeta::one::Storage &storage,
209207
response.write_header("Access-Control-Max-Age", "3600");
210208
send_response(sourcemeta::one::STATUS_NO_CONTENT, request, response);
211209
} else if (request.method() == "post") {
212-
const auto template_key{sourcemeta::one::Storage::key(
213-
"schemas", path, SENTINEL, "blaze-exhaustive.metapack")};
214-
if (!storage.exists(template_key)) {
215-
if (storage.sibling_exists(template_key, "schema.metapack")) {
210+
auto template_path{base / "schemas"};
211+
template_path /= path;
212+
template_path /= SENTINEL;
213+
template_path /= "blaze-exhaustive.metapack";
214+
if (!std::filesystem::exists(template_path)) {
215+
const auto schema_path{template_path.parent_path() / "schema.metapack"};
216+
if (std::filesystem::exists(schema_path)) {
216217
json_error(request, response,
217218
sourcemeta::one::STATUS_METHOD_NOT_ALLOWED, "no-template",
218219
"This schema was not precompiled for schema evaluation");
@@ -225,12 +226,12 @@ static auto action_jsonschema_evaluate(const sourcemeta::one::Storage &storage,
225226
}
226227

227228
request.body(
228-
[mode, &storage,
229-
template_key](sourcemeta::one::HTTPRequest &callback_request,
230-
sourcemeta::one::HTTPResponse &callback_response,
231-
std::string &&body, bool too_big) {
229+
[mode, template_path = std::move(template_path)](
230+
sourcemeta::one::HTTPRequest &callback_request,
231+
sourcemeta::one::HTTPResponse &callback_response,
232+
std::string &&body, bool too_big) {
232233
action_jsonschema_evaluate_callback(
233-
storage, template_key, mode, callback_request, callback_response,
234+
template_path, mode, callback_request, callback_response,
234235
std::move(body), too_big);
235236
},
236237
[](sourcemeta::one::HTTPRequest &callback_request,

src/server/action_jsonschema_serve.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define SOURCEMETA_ONE_SERVER_ACTION_JSONSCHEMA_SERVE_H
33

44
#include <sourcemeta/one/shared.h>
5-
#include <sourcemeta/one/storage.h>
65

76
#include "action_serve_metapack_file.h"
87
#include "helpers.h"
@@ -11,10 +10,11 @@
1110

1211
#include <algorithm> // std::transform
1312
#include <cctype> // std::tolower
13+
#include <filesystem> // std::filesystem
1414
#include <string> // std::string
1515
#include <string_view> // std::string_view
1616

17-
static auto action_jsonschema_serve(const sourcemeta::one::Storage &storage,
17+
static auto action_jsonschema_serve(const std::filesystem::path &base,
1818
const std::string_view &path,
1919
sourcemeta::one::HTTPRequest &request,
2020
sourcemeta::one::HTTPResponse &response)
@@ -33,33 +33,30 @@ static auto action_jsonschema_serve(const sourcemeta::one::Storage &storage,
3333
user_agent.starts_with("VSCodium")};
3434
const auto is_deno{user_agent.starts_with("Deno/")};
3535
const auto bundle{!request.query("bundle").empty()};
36-
std::string filename;
36+
auto absolute_path{base / "schemas" / lowercase_path / SENTINEL};
3737
if (is_vscode) {
38-
filename = "editor.metapack";
38+
absolute_path /= "editor.metapack";
3939
} else if (bundle || is_deno) {
40-
filename = "bundle.metapack";
40+
absolute_path /= "bundle.metapack";
4141
} else {
42-
filename = "schema.metapack";
42+
absolute_path /= "schema.metapack";
4343
}
4444

45-
const auto key{sourcemeta::one::Storage::key("schemas", lowercase_path,
46-
SENTINEL, filename)};
47-
4845
if (request.method() != "get" && request.method() != "head" &&
49-
!storage.exists(key)) {
46+
!std::filesystem::exists(absolute_path)) {
5047
json_error(request, response, sourcemeta::one::STATUS_NOT_FOUND,
5148
"not-found", "There is nothing at this URL");
5249
return;
5350
}
5451

5552
if (is_deno) {
56-
action_serve_metapack_file(storage, request, response, key,
53+
action_serve_metapack_file(request, response, absolute_path,
5754
sourcemeta::one::STATUS_OK, true,
5855
// For HTTP imports, as Deno won't like the
5956
// `application/schema+json` one
6057
"application/json");
6158
} else {
62-
action_serve_metapack_file(storage, request, response, key,
59+
action_serve_metapack_file(request, response, absolute_path,
6360
sourcemeta::one::STATUS_OK, true);
6461
}
6562
}

src/server/action_schema_search.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@
44
#include <sourcemeta/core/json.h>
55

66
#include <sourcemeta/one/shared.h>
7-
#include <sourcemeta/one/storage.h>
87

98
#include "helpers.h"
109
#include "request.h"
1110
#include "response.h"
1211

1312
#include <algorithm> // std::search
1413
#include <cassert> // assert
14+
#include <filesystem> // std::filesystem
1515
#include <sstream> // std::ostringstream
1616
#include <string> // std::string, std::getline
1717
#include <string_view> // std::string_view
1818

1919
namespace sourcemeta::one {
2020

21-
static auto search(const Storage &storage, const Storage::Key search_key,
21+
static auto search(const std::filesystem::path &search_index,
2222
const std::string_view query) -> sourcemeta::core::JSON {
23-
auto file{storage.read_raw(search_key)};
23+
assert(std::filesystem::exists(search_index));
24+
assert(search_index.is_absolute());
25+
auto file{read_stream_raw(search_index)};
2426
assert(file.has_value());
2527

2628
auto result{sourcemeta::core::JSON::make_array()};
@@ -55,7 +57,7 @@ static auto search(const Storage &storage, const Storage::Key search_key,
5557

5658
} // namespace sourcemeta::one
5759

58-
static auto action_schema_search(const sourcemeta::one::Storage &storage,
60+
static auto action_schema_search(const std::filesystem::path &base,
5961
sourcemeta::one::HTTPRequest &request,
6062
sourcemeta::one::HTTPResponse &response)
6163
-> void {
@@ -66,9 +68,8 @@ static auto action_schema_search(const sourcemeta::one::Storage &storage,
6668
"missing-query",
6769
"You must provide a query parameter to search for");
6870
} else {
69-
const auto search_key{sourcemeta::one::Storage::key("explorer", SENTINEL,
70-
"search.metapack")};
71-
auto result{sourcemeta::one::search(storage, search_key, query)};
71+
auto result{sourcemeta::one::search(
72+
base / "explorer" / SENTINEL / "search.metapack", query)};
7273
response.write_status(sourcemeta::one::STATUS_OK);
7374
response.write_header("Access-Control-Allow-Origin", "*");
7475
response.write_header("Content-Type", "application/json");

src/server/action_serve_metapack_file.h

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@
44
#include <sourcemeta/core/time.h>
55

66
#include <sourcemeta/one/shared.h>
7-
#include <sourcemeta/one/storage.h>
87

98
#include "helpers.h"
109
#include "request.h"
1110
#include "response.h"
1211

13-
#include <chrono> // std::chrono::seconds
14-
#include <fstream> // std::ifstream
15-
#include <optional> // std::optional
16-
#include <sstream> // std::ostringstream
17-
#include <string> // std::string
12+
#include <chrono> // std::chrono::seconds
13+
#include <filesystem> // std::filesystem
14+
#include <optional> // std::optional
15+
#include <sstream> // std::ostringstream
16+
#include <string> // std::string
1817

1918
static auto action_serve_metapack_file(
2019
const sourcemeta::one::HTTPRequest &request,
2120
sourcemeta::one::HTTPResponse &response,
22-
std::optional<sourcemeta::one::File<std::ifstream>> file,
23-
const char *const code, const bool enable_cors = false,
21+
const std::filesystem::path &absolute_path, const char *const code,
22+
const bool enable_cors = false,
2423
const std::optional<std::string> &mime = std::nullopt,
2524
const std::optional<std::string> &link = std::nullopt) -> void {
2625
if (request.method() != "get" && request.method() != "head") {
@@ -30,6 +29,7 @@ static auto action_serve_metapack_file(
3029
return;
3130
}
3231

32+
auto file{sourcemeta::one::read_stream_raw(absolute_path)};
3333
if (!file.has_value()) {
3434
json_error(request, response, sourcemeta::one::STATUS_NOT_FOUND,
3535
"not-found", "There is nothing at this URL");
@@ -116,28 +116,4 @@ static auto action_serve_metapack_file(
116116
}
117117
}
118118

119-
static auto action_serve_metapack_file_local(
120-
const sourcemeta::one::HTTPRequest &request,
121-
sourcemeta::one::HTTPResponse &response,
122-
const std::filesystem::path &absolute_path, const char *const code,
123-
const bool enable_cors = false,
124-
const std::optional<std::string> &mime = std::nullopt,
125-
const std::optional<std::string> &link = std::nullopt) -> void {
126-
action_serve_metapack_file(request, response,
127-
sourcemeta::one::read_stream_raw(absolute_path),
128-
code, enable_cors, mime, link);
129-
}
130-
131-
static auto action_serve_metapack_file(
132-
const sourcemeta::one::Storage &storage,
133-
const sourcemeta::one::HTTPRequest &request,
134-
sourcemeta::one::HTTPResponse &response,
135-
const sourcemeta::one::Storage::Key key, const char *const code,
136-
const bool enable_cors = false,
137-
const std::optional<std::string> &mime = std::nullopt,
138-
const std::optional<std::string> &link = std::nullopt) -> void {
139-
action_serve_metapack_file(request, response, storage.read_raw(key), code,
140-
enable_cors, mime, link);
141-
}
142-
143119
#endif

0 commit comments

Comments
 (0)