Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ project(tundradb)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Configure compile-time logging levels
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_definitions(TUNDRA_LOG_LEVEL_DEBUG)
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_definitions(TUNDRA_LOG_LEVEL_INFO)
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
add_compile_definitions(TUNDRA_LOG_LEVEL_INFO)
else()
add_compile_definitions(TUNDRA_LOG_LEVEL_DEBUG)
endif()

# Enable Address Sanitizer
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
Expand Down Expand Up @@ -269,6 +280,20 @@ target_link_libraries(tundra_shell
LLVMCore
)

# Benchmark runner executable for loading CSVs and running queries
add_executable(tundra_bench_runner bench/tundra_runner.cpp)
target_link_libraries(tundra_bench_runner
PRIVATE
core
Arrow::arrow_shared
${ARROW_DATASET_LIB}
Parquet::parquet_shared
${UUID_LIBRARY}
${ANTLR4_RUNTIME}
LLVMSupport
LLVMCore
)

# ANTLR Integration
# Find Java for running ANTLR generator
find_package(Java REQUIRED)
Expand Down
19 changes: 19 additions & 0 deletions include/concurrency.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ class ConcurrentSet {
return snapshot;
}

class LockedView {
public:
using iterator =
typename tbb::concurrent_hash_map<T, std::monostate>::const_iterator;

LockedView(const tbb::concurrent_hash_map<T, std::monostate>& data)
: data_(data) {}

iterator begin() const { return data_.begin(); }
iterator end() const { return data_.end(); }

size_t size() const { return data_.size(); }

private:
const tbb::concurrent_hash_map<T, std::monostate>& data_;
};

LockedView get_all_unsafe() const { return LockedView(data_); }

/**
* @brief Clear all elements from the set
*
Expand Down
47 changes: 25 additions & 22 deletions include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,28 +765,31 @@ class Database {

arrow::Result<std::shared_ptr<arrow::Table>> get_table(
const std::string &schema_name, size_t chunk_size = 10000) const {
ARROW_ASSIGN_OR_RAISE(auto schema,
schema_registry_->get_arrow(schema_name));

ARROW_ASSIGN_OR_RAISE(auto all_nodes,
shard_manager_->get_nodes(schema_name));

if (all_nodes.empty()) {
std::vector<std::shared_ptr<arrow::ChunkedArray>> empty_columns;
empty_columns.reserve(schema->num_fields());
for (int i = 0; i < schema->num_fields(); i++) {
empty_columns.push_back(std::make_shared<arrow::ChunkedArray>(
std::vector<std::shared_ptr<arrow::Array>>{}));
}
return arrow::Table::Make(schema, empty_columns);
}

std::ranges::sort(all_nodes, [](const std::shared_ptr<Node> &a,
const std::shared_ptr<Node> &b) {
return a->id < b->id;
});

return create_table(schema, all_nodes, chunk_size);
auto shard = shard_manager_->get_shard(schema_name, 0).ValueOrDie();
return shard->get_table();

// ARROW_ASSIGN_OR_RAISE(auto schema,
// schema_registry_->get_arrow(schema_name));
//
// ARROW_ASSIGN_OR_RAISE(auto all_nodes,
// shard_manager_->get_nodes(schema_name));
//
// if (all_nodes.empty()) {
// std::vector<std::shared_ptr<arrow::ChunkedArray>> empty_columns;
// empty_columns.reserve(schema->num_fields());
// for (int i = 0; i < schema->num_fields(); i++) {
// empty_columns.push_back(std::make_shared<arrow::ChunkedArray>(
// std::vector<std::shared_ptr<arrow::Array>>{}));
// }
// return arrow::Table::Make(schema, empty_columns);
// }
//
// std::ranges::sort(all_nodes, [](const std::shared_ptr<Node> &a,
// const std::shared_ptr<Node> &b) {
// return a->id < b->id;
// });
//
// return create_table(schema, all_nodes, chunk_size);
}

arrow::Result<size_t> get_shard_count(const std::string &schema_name) const {
Expand Down
102 changes: 102 additions & 0 deletions include/edge_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,98 @@

namespace tundradb {

// Forward declaration for EdgeView
class EdgeStore;

/**
* @brief A view over edges that avoids copying shared_ptr<Edge> objects
*
* This class provides iteration over edges without materializing them into
* a vector, reducing memory allocations and improving performance.
*/
class EdgeView {
public:
class iterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = std::shared_ptr<Edge>;
using difference_type = std::ptrdiff_t;
using pointer = const value_type *;
using reference = const value_type &;

iterator(const EdgeStore *store,
ConcurrentSet<int64_t>::LockedView::iterator edge_ids_it,
ConcurrentSet<int64_t>::LockedView::iterator edge_ids_end,
const std::string &type_filter)
: store_(store),
edge_ids_it_(edge_ids_it),
edge_ids_end_(edge_ids_end),
type_filter_(type_filter) {
advance_to_valid();
}

iterator &operator++() {
++edge_ids_it_;
advance_to_valid();
return *this;
}

iterator operator++(int) {
iterator tmp = *this;
++(*this);
return tmp;
}

reference operator*() const { return current_edge_; }
pointer operator->() const { return &current_edge_; }

bool operator==(const iterator &other) const {
return edge_ids_it_ == other.edge_ids_it_;
}

bool operator!=(const iterator &other) const { return !(*this == other); }

private:
void advance_to_valid();

const EdgeStore *store_;
ConcurrentSet<int64_t>::LockedView::iterator edge_ids_it_;
ConcurrentSet<int64_t>::LockedView::iterator edge_ids_end_;
std::string type_filter_;
std::shared_ptr<Edge> current_edge_;
};

EdgeView(const EdgeStore *store, const ConcurrentSet<int64_t> &edge_ids,
const std::string &type_filter = "")
: store_(store),
edge_ids_view_(edge_ids.get_all_unsafe()),
type_filter_(type_filter) {}

iterator begin() const {
return iterator(store_, edge_ids_view_.begin(), edge_ids_view_.end(),
type_filter_);
}

iterator end() const {
return iterator(store_, edge_ids_view_.end(), edge_ids_view_.end(),
type_filter_);
}

// Convenience method to count matching edges without materializing them
size_t count() const {
size_t result = 0;
for (auto it = begin(); it != end(); ++it) {
++result;
}
return result;
}

private:
const EdgeStore *store_;
ConcurrentSet<int64_t>::LockedView edge_ids_view_;
std::string type_filter_;
};

// todo rename to EdgeManager
class EdgeStore {
struct TableCache;
Expand Down Expand Up @@ -94,6 +186,13 @@ class EdgeStore {
arrow::Result<std::vector<std::shared_ptr<Edge>>> get_incoming_edges(
int64_t id, const std::string &type = "") const;

// New view-based methods that avoid copying
arrow::Result<EdgeView> get_outgoing_edges_view(
int64_t id, const std::string &type = "") const;

arrow::Result<EdgeView> get_incoming_edges_view(
int64_t id, const std::string &type = "") const;

arrow::Result<std::vector<std::shared_ptr<Edge>>> get_by_type(
const std::string &type) const;

Expand All @@ -114,6 +213,9 @@ class EdgeStore {
void set_id_seq(const int64_t v) {
edge_id_counter_.store(v, std::memory_order_relaxed);
}

// Friend class for EdgeView iterator access
friend class EdgeView::iterator;
};
} // namespace tundradb

Expand Down
62 changes: 62 additions & 0 deletions include/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,68 @@ class ContextLogger {
std::string prefix_;
};

// ============================================================================
// COMPILE-TIME LOGGING OPTIMIZATIONS
// ============================================================================

// Compile-time log level configuration
#ifdef TUNDRA_LOG_LEVEL_DEBUG
constexpr LogLevel COMPILE_TIME_LOG_LEVEL = LogLevel::DEBUG;
#elif defined(TUNDRA_LOG_LEVEL_INFO)
constexpr LogLevel COMPILE_TIME_LOG_LEVEL = LogLevel::INFO;
#elif defined(TUNDRA_LOG_LEVEL_WARN)
constexpr LogLevel COMPILE_TIME_LOG_LEVEL = LogLevel::WARN;
#elif defined(TUNDRA_LOG_LEVEL_ERROR)
constexpr LogLevel COMPILE_TIME_LOG_LEVEL = LogLevel::ERROR;
#else
// Default to INFO in release builds, DEBUG in debug builds
#ifdef NDEBUG
constexpr LogLevel COMPILE_TIME_LOG_LEVEL = LogLevel::INFO;
#else
constexpr LogLevel COMPILE_TIME_LOG_LEVEL = LogLevel::DEBUG;
#endif
#endif

// Compile-time log level checks - completely eliminated in release builds
constexpr bool is_debug_enabled() {
return COMPILE_TIME_LOG_LEVEL <= LogLevel::DEBUG;
}

constexpr bool is_info_enabled() {
return COMPILE_TIME_LOG_LEVEL <= LogLevel::INFO;
}

constexpr bool is_warn_enabled() {
return COMPILE_TIME_LOG_LEVEL <= LogLevel::WARN;
}

// Fast logging macros that compile to nothing when disabled
#define LOG_DEBUG_FAST(msg, ...) \
do { \
if constexpr (is_debug_enabled()) { \
log_debug(msg, ##__VA_ARGS__); \
} \
} while (0)

#define LOG_INFO_FAST(msg, ...) \
do { \
if constexpr (is_info_enabled()) { \
log_info(msg, ##__VA_ARGS__); \
} \
} while (0)

#define LOG_WARN_FAST(msg, ...) \
do { \
if constexpr (is_warn_enabled()) { \
log_warn(msg, ##__VA_ARGS__); \
} \
} while (0)

// Conditional code blocks - completely eliminated when disabled
#define IF_DEBUG_ENABLED if constexpr (is_debug_enabled())

#define IF_INFO_ENABLED if constexpr (is_info_enabled())

} // namespace tundradb

#endif // LOGGER_HPP
Loading
Loading