Skip to content

Commit

Permalink
feat: return data version on each query
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKellerer committed Aug 3, 2023
1 parent 4209258 commit be5c886
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 8 deletions.
17 changes: 17 additions & 0 deletions include/silo/common/data_version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef SILO_INCLUDE_SILO_COMMON_DATAVERSION_H_
#define SILO_INCLUDE_SILO_COMMON_DATAVERSION_H_

#include <string>

class DataVersion {
public:
explicit DataVersion(const std::string& data_version = "");
[[nodiscard]] std::string toString() const;

static std::string mineDataVersion();

private:
std::string data_version;
};

#endif // SILO_INCLUDE_SILO_COMMON_DATAVERSION_H_
10 changes: 7 additions & 3 deletions include/silo/database.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SILO_DATABASE_H
#define SILO_DATABASE_H

#include <silo/common/data_version.h>
#include <cstddef>
#include <cstdint>
#include <filesystem>
Expand Down Expand Up @@ -36,9 +37,6 @@ struct PreprocessingConfig;
namespace silo {

class Database {
private:
PangoLineageAliasLookup alias_key;

public:
silo::config::DatabaseConfig database_config;
std::vector<DatabasePartition> partitions;
Expand All @@ -62,7 +60,13 @@ class Database {

[[nodiscard]] const PangoLineageAliasLookup& getAliasKey() const;

void setDataVersion(const DataVersion& data_version);
DataVersion getDataVersion() const;

private:
PangoLineageAliasLookup alias_key;
DataVersion data_version_;

void build(
const preprocessing::PreprocessingConfig& preprocessing_config,
const preprocessing::Partitions& partition_descriptor,
Expand Down
6 changes: 5 additions & 1 deletion include/silo_api/query_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ namespace silo_api {
class QueryHandler : public RestResource {
private:
const silo::query_engine::QueryEngine& query_engine;
const std::string data_version;

public:
explicit QueryHandler(const silo::query_engine::QueryEngine& query_engine);
explicit QueryHandler(
const silo::query_engine::QueryEngine& query_engine,
std::string data_version
);

void post(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
override;
Expand Down
16 changes: 16 additions & 0 deletions src/silo/common/data_version.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "silo/common/data_version.h"

#include <chrono>

std::string DataVersion::mineDataVersion() {
const auto now = std::chrono::system_clock::now();
const auto now_as_time_t = std::chrono::system_clock::to_time_t(now);
return std::to_string(now_as_time_t);
}

DataVersion::DataVersion(const std::string& data_version)
: data_version(data_version) {}

std::string DataVersion::toString() const {
return data_version;
}
19 changes: 19 additions & 0 deletions src/silo/common/data_version.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "silo/common/data_version.h"

#include <gtest/gtest.h>

TEST(DataVersion, shouldMineDataVersionFromUnixTime) {
const auto mined_version = DataVersion::mineDataVersion();
EXPECT_EQ(mined_version.size(), 10);
EXPECT_EQ(mined_version[0], '1');
}

TEST(DataVersion, shouldConstructFromVersionString) {
const auto version = DataVersion("1234567890");
EXPECT_EQ(version.toString(), "1234567890");
}

TEST(DataVersion, shouldConstructWithDefaultVersion) {
const auto version = DataVersion();
EXPECT_EQ(version.toString(), "");
}
31 changes: 31 additions & 0 deletions src/silo/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <array>
#include <atomic>
#include <chrono>
#include <deque>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -143,6 +144,8 @@ void Database::build(

SPDLOG_INFO("Build took {} ms", micros);
SPDLOG_INFO("database info: {}", getDatabaseInfo());

setDataVersion(DataVersion(DataVersion::mineDataVersion()));
}

using RoaringStatistics = roaring::api::roaring_statistics_t;
Expand Down Expand Up @@ -367,6 +370,12 @@ std::map<std::string, std::vector<AA_SYMBOL>> Database::getAASequences() const {
return aa_sequences_map;
}

void saveDataVersion(const Database& database, const std::string& save_directory) {
std::ofstream data_version_file(save_directory + "data_version.silo");
const auto data_version = database.getDataVersion().toString();
data_version_file << data_version;
}

void Database::saveDatabaseState(const std::string& save_directory) {
const std::string database_config_filename = save_directory + "database_config.yaml";
database_config.writeConfig(database_config_filename);
Expand Down Expand Up @@ -414,6 +423,15 @@ void Database::saveDatabaseState(const std::string& save_directory) {
}
});
SPDLOG_INFO("Finished saving partitions", partitions.size());

saveDataVersion(*this, save_directory);
}

DataVersion loadDataVersion(const std::string& save_directory) {
std::ifstream data_version_file(save_directory + "data_version.silo");
std::string data_version;
data_version_file >> data_version;
return DataVersion(data_version);
}

Database Database::loadDatabaseState(const std::string& save_directory) {
Expand Down Expand Up @@ -482,6 +500,10 @@ Database Database::loadDatabaseState(const std::string& save_directory) {
}
}
);

SPDLOG_INFO("Loading data_version from {}", save_directory + "data_version.silo");
database.setDataVersion(loadDataVersion(save_directory));

return database;
}

Expand Down Expand Up @@ -641,4 +663,13 @@ void Database::finalizeInsertionIndexes() {

Database::Database() = default;

void Database::setDataVersion(const DataVersion& data_version) {
SPDLOG_DEBUG("Set data version to {}", data_version.toString());
data_version_ = data_version;
}

DataVersion Database::getDataVersion() const {
return data_version_;
}

} // namespace silo
11 changes: 8 additions & 3 deletions src/silo_api/query_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

namespace silo_api {

QueryHandler::QueryHandler(const silo::query_engine::QueryEngine& query_engine)
: query_engine(query_engine) {}
QueryHandler::QueryHandler(
const silo::query_engine::QueryEngine& query_engine,
std::string data_version
)
: query_engine(query_engine),
data_version(std::move(data_version)) {}

void QueryHandler::post(
Poco::Net::HTTPServerRequest& request,
Expand All @@ -29,10 +33,11 @@ void QueryHandler::post(
SPDLOG_INFO("received query: {}", query);

response.setContentType("application/json");

try {
const auto query_result = query_engine.executeQuery(query);

response.set("Data-Version", data_version);

std::ostream& out_stream = response.send();
out_stream << nlohmann::json(query_result);
} catch (const silo::QueryParseException& ex) {
Expand Down
2 changes: 1 addition & 1 deletion src/silo_api/request_handler_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Poco::Net::HTTPRequestHandler* SiloRequestHandlerFactory::routeRequest(
return new silo_api::InfoHandler(database);
}
if (path == "/query") {
return new silo_api::QueryHandler(query_engine);
return new silo_api::QueryHandler(query_engine, database.getDataVersion().toString());
}
return new silo_api::NotFoundHandler;
}
Expand Down

0 comments on commit be5c886

Please sign in to comment.