Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CXX = g++
CXXFLAGS = -std=c++17 -I./src -I./test
SRC = $(wildcard src/app/*.cpp src/file/*.cpp src/tx/*.cpp src/tx/LogRecord/*.cpp src/logging/*.cpp src/buffer/*.cpp src/record/*.cpp src/scan/*.cpp src/meta/*.cpp src/indexing/*.cpp src/plan/*.cpp src/parse/*.cpp)
SRC = $(wildcard src/app/*.cpp src/file/*.cpp src/tx/*.cpp src/tx/LogRecord/*.cpp src/logging/*.cpp src/buffer/*.cpp src/record/*.cpp src/scan/*.cpp src/meta/*.cpp src/indexing/*.cpp src/plan/*.cpp src/parse/*.cpp src/interface/*.cpp)
TESTS = $(wildcard test/*.cpp)
OUT = build/test_runner

Expand Down
6 changes: 5 additions & 1 deletion src/app/SampleDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace app {

_mdm = std::make_unique<meta::MetaDataMgr>(isNew, *txPtr);
auto qp = std::make_unique<plan::BasicQueryPlanner>(_mdm.get());
auto up = std::make_unique<plan::BasicUpdatePlanner>(_mdm.get());
auto up = std::make_unique<indexing::IndexUpdatePlanner>(_mdm.get());
_planner = std::make_unique<plan::Planner>(std::move(qp), std::move(up));
txPtr->commit();
}
Expand Down Expand Up @@ -50,6 +50,10 @@ namespace app {
return *_bm;
}

meta::MetaDataMgr& SampleDB::getMetaDataManager() {
return *_mdm;
}

plan::Planner& SampleDB::getPlanner() {
return *_planner;
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/SampleDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include <filesystem>
#include <iostream>
#include "file/FileMgr.h"
#include "indexing/IndexUpdatePlanner.h"
#include "logging/LogMgr.h"
#include "buffer/BufferMgr.h"
#include "tx/Transaction.h"
#include "meta/MetaDataMgr.h"
#include "plan/Planner.h"
#include "plan/BasicQueryPlanner.h"
#include "plan/UpdatePlanner.h"
#include "plan/BasicUpdatePlanner.h"

namespace app {
class SampleDB {
Expand All @@ -24,6 +24,7 @@ namespace app {
file::FileMgr& fileMgr();
logging::LogMgr& logMgr();
buffer::BufferMgr& bufferMgr();
meta::MetaDataMgr& getMetaDataManager();
plan::Planner& getPlanner();
std::unique_ptr<tx::Transaction> newTransaction();
private:
Expand Down
2 changes: 1 addition & 1 deletion src/indexing/BTreePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace indexing {

// Method called only from BTreeLeaf
record::RID BTreePage::getDataRid(int slot) const {
return record::RID(getInt(slot, "block"), getInt(slot, "slot"));
return record::RID(getInt(slot, "block"), getInt(slot, "id"));
}

void BTreePage::insertLeaf(int slot, const scan::Constant& dataval, const record::RID& datarid) {
Expand Down
45 changes: 45 additions & 0 deletions src/interface/Connection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "interface/Connection.h"

namespace interface {
Connection::Connection(std::unique_ptr<app::SampleDB> db)
: _db(std::move(db)), _transaction(_db->newTransaction()), _planner(_db->getPlanner()) {}

std::unique_ptr<Statement> Connection::createStatement() {
try {
auto ptr = std::make_unique<Statement>(this, _planner);
return ptr;
} catch (const std::exception& e) {
throw std::runtime_error("Failed to create statement: " + std::string(e.what()));
}
}

void Connection::close() {
try {
commit();
} catch (const std::exception& e) {
throw std::runtime_error("Failed to close connection: " + std::string(e.what()));
}
}

void Connection::commit() {
try {
_transaction->commit();
_transaction = _db->newTransaction();
} catch (const std::exception& e) {
throw std::runtime_error("Failed to commit transaction: " + std::string(e.what()));
}
}

void Connection::rollback() {
try {
_transaction->rollback();
_transaction = _db->newTransaction();
} catch (const std::exception& e) {
throw std::runtime_error("Failed to rollback transaction: " + std::string(e.what()));
}
}

tx::Transaction& Connection::getTransaction() {
return *_transaction;
}
}
27 changes: 27 additions & 0 deletions src/interface/Connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <memory>
#include <string>

#include "app/SampleDB.h"
#include "plan/Planner.h"
#include "interface/Statement.h"
#include "tx/Transaction.h"

namespace interface {
class Statement;

class Connection {
public:
Connection(std::unique_ptr<app::SampleDB> db);
std::unique_ptr<Statement> createStatement();
void close();
void commit();
void rollback();
tx::Transaction& getTransaction();
private:
std::unique_ptr<app::SampleDB> _db;
std::unique_ptr<tx::Transaction> _transaction;
plan::Planner& _planner;
};
}
10 changes: 10 additions & 0 deletions src/interface/Driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "interface/Driver.h"

namespace interface {
std::unique_ptr<Connection> Driver::connect(const std::string& dbName) {
auto db = std::make_unique<app::SampleDB>(dbName);
auto connection = std::make_unique<Connection>(std::move(db));

return connection;
}
}
14 changes: 14 additions & 0 deletions src/interface/Driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <memory>
#include <string>

#include "app/SampleDB.h"
#include "interface/Connection.h"

namespace interface {
class Driver {
public:
std::unique_ptr<Connection> connect(const std::string& dbName);
};
}
41 changes: 41 additions & 0 deletions src/interface/MetaData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "MetaData.h"

namespace interface {
MetaData::MetaData(const record::Schema& schema) : _schema(schema) {}

int MetaData::getColumnCount() const {
try {
return _schema.fieldNames().size();
} catch (const std::exception& e) {
throw std::runtime_error("Failed to get column count: " + std::string(e.what()));
}
}

std::string MetaData::getColumnName(int column) const {
try {
return _schema.fieldNames().at(column - 1);
} catch (const std::exception& e) {
throw std::runtime_error("Failed to get column name: " + std::string(e.what()));
}
}

int MetaData::getColumnType(int column) const {
try {
std::string fieldName = getColumnName(column);
return _schema.fieldType(fieldName);
} catch (const std::exception& e) {
throw std::runtime_error("Failed to get column type: " + std::string(e.what()));
}
}

int MetaData::getColumnDisplaySize(int column) const {
try {
std::string fieldName = getColumnName(column);
int fieldType = _schema.fieldType(fieldName);
int fieldLength = (fieldType == record::Schema::INTEGER) ? 6 : _schema.fieldLength(fieldName);
return fieldName.length() > fieldLength ? fieldName.length() + 1: fieldLength + 1;
} catch (const std::exception& e) {
throw std::runtime_error("Failed to get column display size: " + std::string(e.what()));
}
}
}
19 changes: 19 additions & 0 deletions src/interface/MetaData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <string>
#include <string>

#include "record/Schema.h"

namespace interface {
class MetaData {
public:
MetaData(const record::Schema& schema);
int getColumnCount() const;
std::string getColumnName(int column) const;
int getColumnType(int column) const;
int getColumnDisplaySize(int column) const;
private:
record::Schema _schema;
};
}
52 changes: 52 additions & 0 deletions src/interface/ResultSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "interface/ResultSet.h"

namespace interface {
ResultSet::ResultSet(plan::Plan* plan, Connection* connection) : _scan(plan->open()), _schema(plan->schema()), _connection(connection) {}

bool ResultSet::next() {
try {
return _scan->next();
} catch (const std::exception& e) {
_connection->rollback();
std::runtime_error("Failed to move to next record: " + std::string(e.what()));
return false;
}
}

int ResultSet::getInt(std::string& fieldName) const {
try {
boost::algorithm::to_lower(fieldName);
return _scan->getInt(fieldName);
} catch (const std::exception& e) {
_connection->rollback();
std::runtime_error("Failed to get integer value for field '" + fieldName + "': " + std::string(e.what()));
return -1;
}
}

std::string ResultSet::getString(std::string& fieldName) const {
try {
boost::algorithm::to_lower(fieldName);
return _scan->getString(fieldName);
} catch (const std::exception& e) {
_connection->rollback();
std::runtime_error("Failed to get string value for field '" + fieldName + "': " + std::string(e.what()));
return "";
}
}

MetaData ResultSet::getMetaData() const {
MetaData metaData(_schema);
return metaData;
}

void ResultSet::close() {
try {
_scan->close();
_connection->commit();
} catch (const std::exception& e) {
_scan->close();
_connection->commit();
}
}
}
30 changes: 30 additions & 0 deletions src/interface/ResultSet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <memory>
#include <string>

#include "Connection.h"
#include "MetaData.h"
#include "plan/Plan.h"
#include "scan/Scan.h"
#include "record/Schema.h"

namespace interface {
class Connection;

class ResultSet {
public:
ResultSet(plan::Plan* plan, Connection* connection);
bool next();
int getInt(std::string& fieldName) const;
std::string getString(std::string& fieldName) const;
MetaData getMetaData() const;
void close();
private:
std::shared_ptr<scan::Scan> _scan;
record::Schema _schema;
Connection* _connection;
};
}
34 changes: 34 additions & 0 deletions src/interface/Statement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "Statement.h"

namespace interface {
Statement::Statement(Connection* connection, plan::Planner& planner) : _connection(connection), _planner(planner) {}

ResultSet Statement::executeQuery(const std::string& query) {
try {
tx::Transaction& tx = _connection->getTransaction();
auto plan = _planner.createQueryPlan(query, &tx);
ResultSet rs(plan.get(), _connection);
return rs;
} catch (const std::exception& e) {
_connection->rollback();
throw std::runtime_error("Failed to execute query: " + std::string(e.what()));
}
}

int Statement::executeUpdate(const std::string& cmd) {
try {
tx::Transaction& tx = _connection->getTransaction();
int result = _planner.executeUpdate(cmd, &tx);
_connection->commit();
return result;
} catch (const std::exception& e) {
_connection->rollback();
std::runtime_error("Failed to execute update: " + std::string(e.what()));
return 0;
}
}

void Statement::close() {
return;
}
}
24 changes: 24 additions & 0 deletions src/interface/Statement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <string>

#include "Connection.h"
#include "interface/ResultSet.h"
#include "plan/Planner.h"

namespace interface {
class Connection;

class ResultSet;

class Statement {
public:
Statement(Connection* connection, plan::Planner& planner);
ResultSet executeQuery(const std::string& query);
int executeUpdate(const std::string& cmd);
void close();
private:
Connection* _connection;
plan::Planner& _planner;
};
}
Loading