Skip to content

Commit

Permalink
Generate a search index (#70)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jan 3, 2025
1 parent fd9a4fe commit 851b3e7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ ifeq ($(ENTERPRISE), ON)
./test/cli/ee/index/no-options.sh $(PREFIX)/bin/sourcemeta-registry-index
./test/cli/ee/index/no-output.sh $(PREFIX)/bin/sourcemeta-registry-index
./test/cli/ee/index/directory-index.sh $(PREFIX)/bin/sourcemeta-registry-index
./test/cli/ee/index/search-index.sh $(PREFIX)/bin/sourcemeta-registry-index
else
./test/cli/ce/index/no-options.sh $(PREFIX)/bin/sourcemeta-registry-index
./test/cli/ce/index/no-output.sh $(PREFIX)/bin/sourcemeta-registry-index
Expand Down
27 changes: 24 additions & 3 deletions src/enterprise/enterprise_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace sourcemeta::registry::enterprise {

auto generate_toc(const sourcemeta::jsontoolkit::JSON &configuration,
const std::filesystem::path &base,
const std::filesystem::path &directory) -> void {
const std::filesystem::path &directory,
sourcemeta::jsontoolkit::JSON &search_index) -> void {
assert(directory.string().starts_with(base.string()));
auto entries{sourcemeta::jsontoolkit::JSON::make_array()};

Expand Down Expand Up @@ -67,6 +68,18 @@ auto generate_toc(const sourcemeta::jsontoolkit::JSON &configuration,
entry_json.assign("url",
sourcemeta::jsontoolkit::JSON{entry_relative_path});

// Collect schemas high-level metadata for searching purposes
auto search_entry{sourcemeta::jsontoolkit::JSON::make_object()};
search_entry.assign("url", entry_json.at("url"));
search_entry.assign("title", entry_json.defines("title")
? entry_json.at("title")
: sourcemeta::jsontoolkit::JSON{""});
search_entry.assign("description",
entry_json.defines("description")
? entry_json.at("description")
: sourcemeta::jsontoolkit::JSON{""});
search_index.push_back(std::move(search_entry));

entries.push_back(std::move(entry_json));
}
}
Expand Down Expand Up @@ -126,7 +139,8 @@ auto attach(const sourcemeta::jsontoolkit::JSON &configuration,
-> int {
std::cerr << "-- Indexing directory: " << output.string() << "\n";
const auto base{std::filesystem::canonical(output / "schemas")};
generate_toc(configuration, base, base);
auto search_index{sourcemeta::jsontoolkit::JSON::make_array()};
generate_toc(configuration, base, base, search_index);

for (const auto &entry :
std::filesystem::recursive_directory_iterator{output / "schemas"}) {
Expand All @@ -135,9 +149,16 @@ auto attach(const sourcemeta::jsontoolkit::JSON &configuration,
}

std::cerr << "-- Processing: " << entry.path().string() << "\n";
generate_toc(configuration, base, std::filesystem::canonical(entry.path()));
generate_toc(configuration, base, std::filesystem::canonical(entry.path()),
search_index);
}

std::ofstream stream{output / "search.json"};
assert(!stream.fail());
sourcemeta::jsontoolkit::prettify(search_index, stream);
stream << "\n";
stream.close();

return EXIT_SUCCESS;
}

Expand Down
58 changes: 58 additions & 0 deletions test/cli/ee/index/search-index.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << EOF > "$TMP/configuration.json"
{
"url": "https://sourcemeta.com/",
"port": 8000,
"schemas": {
"example/schemas": {
"base": "https://example.com/",
"path": "./schemas"
}
}
}
EOF

mkdir "$TMP/schemas"

cat << 'EOF' > "$TMP/schemas/test.json"
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/test.json",
"title": "My title",
"description": "My description"
}
EOF

cat << 'EOF' > "$TMP/schemas/no-title.json"
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/no-title.json"
}
EOF

"$1" "$TMP/configuration.json" "$TMP/output"

cat << 'EOF' > "$TMP/expected.json"
[
{
"url": "/example/schemas/no-title.json",
"title": "",
"description": ""
},
{
"url": "/example/schemas/test.json",
"title": "My title",
"description": "My description"
}
]
EOF

diff "$TMP/output/search.json" "$TMP/expected.json"
1 change: 1 addition & 0 deletions test/sandbox/manifest-ee.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
./schemas/example/schemas/string.json
./schemas/example/v2.0
./schemas/example/v2.0/schema.json
./search.json

0 comments on commit 851b3e7

Please sign in to comment.