Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloprobst committed Sep 19, 2024
1 parent 03ec7d0 commit 36fcaca
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
19 changes: 5 additions & 14 deletions hyperon_das_atomdb_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ project(hyperon_das_atomdb)
# Set C++ standard and optimization flags
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -flto -w")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast -flto")
set(CMAKE_VERBOSE_MAKEFILE ON)

if (CMAKE_VERSION VERSION_LESS 3.18)
if(CMAKE_VERSION VERSION_LESS 3.18)
set(DEV_MODULE Development)
else()
set(DEV_MODULE Development.Module)
Expand All @@ -18,9 +18,6 @@ find_package(Python 3.10 COMPONENTS Interpreter ${DEV_MODULE} REQUIRED)
# Find OpenSSL package
find_package(OpenSSL REQUIRED)

# Set the module path for custom CMake modules
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

# Include directories
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/src
Expand All @@ -30,7 +27,7 @@ include_directories(
file(GLOB_RECURSE headers
${CMAKE_CURRENT_SOURCE_DIR}/*.h
${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)
file(GLOB_RECURSE sources
file(GLOB_RECURSE sources
${CMAKE_CURRENT_SOURCE_DIR}/src/*.c
${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
Expand All @@ -42,16 +39,10 @@ execute_process(

find_package(nanobind CONFIG REQUIRED)

# Print the source files for debugging purposes
message(STATUS "Source files: ${sources}")

# Add the executable target
# add_executable(${PROJECT_NAME} ${sources} ${headers})

nanobind_add_module(${PROJECT_NAME} MODULE ${cpp} ${sources} ${includes})
nanobind_add_module(${PROJECT_NAME} MODULE ${sources} ${headers})

# Include OpenSSL directories
target_include_directories(${PROJECT_NAME} PRIVATE ${OpenSSL_INCLUDE_DIR})

# Link OpenSSL libraries
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::Crypto)
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::Crypto)
4 changes: 2 additions & 2 deletions hyperon_das_atomdb_cpp/src/document_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Link : public Atom {
}
}

const string to_string() const noexcept {
const string to_string() const {
string result = "Link(" + Atom::to_string();
result += ", composite_type: " + composite_type_list_to_string(composite_type);
result += ", named_type_hash: '" + named_type_hash + "'";
Expand Down Expand Up @@ -204,7 +204,7 @@ class Link : public Atom {
return move(result);
}

const string composite_type_list_to_string(const ListOfAny& composite_type) const noexcept {
const string composite_type_list_to_string(const ListOfAny& composite_type) const {
string result = "[";
for (const auto& element : composite_type) {
if (auto str = any_cast<string>(&element)) {
Expand Down
10 changes: 5 additions & 5 deletions hyperon_das_atomdb_cpp/src/nanobind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ NB_MODULE(hyperon_das_atomdb, m) {
"buffer"_a = nullopt
);
// ---------------------------------------------------------------------------------------------
// adapters submodule --------------------------------------------------------------------------
nb::module_ adapters = m.def_submodule("adapters");
nb::class_<InMemoryDB, AtomDB>(adapters, "InMemoryDB")
.def(nb::init<>());
// ---------------------------------------------------------------------------------------------
// exceptions submodule ------------------------------------------------------------------------
nb::module_ exceptions = m.def_submodule("exceptions");
nb::exception<AtomDoesNotExist>(exceptions, "AtomDoesNotExist");
Expand Down Expand Up @@ -545,10 +550,5 @@ NB_MODULE(hyperon_das_atomdb, m) {
}
);
// ---------------------------------------------------------------------------------------------
// adapters submodule --------------------------------------------------------------------------
nb::module_ adapters = m.def_submodule("adapters");
nb::class_<InMemoryDB, AtomDB>(adapters, "InMemoryDB")
.def(nb::init<>());
// ---------------------------------------------------------------------------------------------
}

1 change: 1 addition & 0 deletions hyperon_das_atomdb_cpp/src/type_aliases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ using Pattern_or_Template_List = vector<Pattern_or_Template>;
using ListOfAny = vector<any>;

/**
* NOTE:
* The following type alias was commented out because std::unordered_map<T, std::any> performs
* poorly, and it was kept here just as a reminder of the performance implications.
*
Expand Down
36 changes: 25 additions & 11 deletions hyperon_das_atomdb_cpp/src/utils/expression_hasher.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <openssl/md5.h>
#include <openssl/evp.h>

#include <string>

Expand All @@ -16,27 +16,41 @@ namespace atomdb {

class ExpressionHasher {
public:
struct EVP_MD_CTX_RAII {
EVP_MD_CTX* ctx;
EVP_MD_CTX_RAII() : ctx(EVP_MD_CTX_new()) {
if (not ctx) throw std::runtime_error("Failed to create EVP_MD_CTX");
}
~EVP_MD_CTX_RAII() {
if (ctx) EVP_MD_CTX_free(ctx);
}
};

/**
* @brief Computes the MD5 hash of the given input string.
*
* @param input The input string to be hashed.
* @return A string representing the MD5 hash of the input.
*/
static const string compute_hash(const string& input) {
MD5_CTX ctx;
unsigned char MD5_BUFFER[MD5_DIGEST_LENGTH];
char HASH[2 * MD5_DIGEST_LENGTH + 1];
EVP_MD_CTX_RAII ctx_raii;

MD5_Init(&ctx);
MD5_Update(&ctx, input.c_str(), input.length());
MD5_Final(MD5_BUFFER, &ctx);
unsigned char md5_buffer[EVP_MAX_MD_SIZE];
unsigned int md5_length = 0;

if (EVP_DigestInit_ex(ctx_raii.ctx, EVP_md5(), nullptr) != 1 ||
EVP_DigestUpdate(ctx_raii.ctx, input.c_str(), input.length()) != 1 ||
EVP_DigestFinal_ex(ctx_raii.ctx, md5_buffer, &md5_length) != 1) {
throw std::runtime_error("Failed to compute MD5 hash");
}

for (unsigned int i = 0; i < MD5_DIGEST_LENGTH; i++) {
sprintf(HASH + 2 * i, "%02x", MD5_BUFFER[i]);
char hash[2 * md5_length + 1];
for (unsigned int i = 0; i < md5_length; i++) {
sprintf(hash + 2 * i, "%02x", md5_buffer[i]);
}
HASH[2 * MD5_DIGEST_LENGTH] = '\0';
hash[2 * md5_length] = '\0';

return move(string(HASH));
return move(std::string(hash));
}

/**
Expand Down

0 comments on commit 36fcaca

Please sign in to comment.