* Copyright (c) 2025 Huawei Device Co., Ltd.All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FUNCTIONFLOWRUNTIME_DATABASE_OPS_H
#define FUNCTIONFLOWRUNTIME_DATABASE_OPS_H
#include <string>
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_TABLE_NAME 50
#define MAX_COLUMN_NAME 30
#define MAX_COLUMNS 20
#define MAX_ROWS 1000
#define MAX_STRING_LEN 256
#define MAX_INDEXES 10
#define MAX_QUERY_LEN 1024
#define MAX_TRANSACTION_DEPTH 5
#define MAX_ERROR_MSG 200
#define PAGE_SIZE 4096
typedef enum { TYPE_INT, TYPE_FLOAT, TYPE_STRING, TYPE_BOOL, TYPE_DATE, TYPE_NULL } DataType;
typedef enum {
ERR_NONE,
ERR_MEMORY,
ERR_SYNTAX,
ERR_TYPE_MISMATCH,
ERR_TABLE_EXISTS,
ERR_TABLE_NOT_FOUND,
ERR_COLUMN_NOT_FOUND,
ERR_DUPLICATE_ENTRY,
ERR_INDEX_EXISTS,
ERR_INDEX_NOT_FOUND,
ERR_TRANSACTION_ACTIVE,
ERR_TRANSACTION_NESTED,
ERR_CONSTRAINT_VIOLATION,
ERR_IO_ERROR,
ERR_INVALID_VALUE,
ERR_MAX_ROWS_EXCEEDED
} ErrorCode;
typedef struct {
char name[MAX_COLUMN_NAME];
DataType type;
int length;
bool notNull;
bool isPrimary;
bool hasIndex;
char defaultValue[MAX_STRING_LEN];
} ColumnDef;
typedef struct {
DataType type;
union {
int intVal;
float floatVal;
char stringVal[MAX_STRING_LEN];
bool boolVal;
time_t dateVal;
} value;
bool isNull;
} DataCell;
#ifdef __cplusplus
}
#endif
class SmartTableRow {
public:
std::unique_ptr<DataCell[]> cells;
size_t cellCount;
int rowId;
bool deleted;
time_t createdAt;
time_t updatedAt;
SmartTableRow()
{
cells = nullptr;
cellCount = 0;
rowId = 0;
deleted = false;
}
explicit SmartTableRow(int columnCnt)
{
cellCount = columnCnt;
cells = std::make_unique<DataCell[]>(columnCnt);
rowId = 0;
deleted = false;
}
SmartTableRow(int id, const std::vector<DataCell> &cellData) : rowId(id), deleted(false), cellCount(cellData.size())
{
time_t now = time(nullptr);
if (now == static_cast<time_t>(-1)) {
throw std::runtime_error("Failed to get current time");
}
createdAt = now;
updatedAt = now;
cells = std::make_unique<DataCell[]>(cellCount);
for (size_t i = 0; i < cellCount; ++i) {
cells[i] = cellData[i];
}
}
SmartTableRow(int id, std::initializer_list<DataCell> initList) : SmartTableRow(id, std::vector<DataCell>(initList))
{
}
SmartTableRow(const SmartTableRow &other)
: cellCount(other.cellCount), rowId(other.rowId), deleted(other.deleted), createdAt(other.createdAt),
updatedAt(other.updatedAt)
{
if (other.cells) {
cells = std::make_unique<DataCell[]>(cellCount);
for (size_t i = 0; i < cellCount; ++i) {
cells[i] = other.cells[i];
}
}
}
SmartTableRow &operator=(const SmartTableRow &other)
{
if (this != &other) {
cells.reset();
cellCount = other.cellCount;
rowId = other.rowId;
deleted = other.deleted;
createdAt = other.createdAt;
updatedAt = other.updatedAt;
if (other.cells) {
cells = std::make_unique<DataCell[]>(cellCount);
for (size_t i = 0; i < cellCount; ++i) {
cells[i] = other.cells[i];
}
}
}
return *this;
}
};
class SmartBPlusTreeNode {
public:
std::unique_ptr<int[]> keys;
std::unique_ptr<std::shared_ptr<void>[]> children;
int numKeys;
bool isLeaf;
std::weak_ptr<SmartBPlusTreeNode> nextPtr;
std::weak_ptr<SmartBPlusTreeNode> parentPtr;
int maxDegree;
SmartBPlusTreeNode(int degree, bool leaf) : maxDegree(degree), isLeaf(leaf), numKeys(0)
{
int maxKeys = maxDegree - 1;
keys = std::make_unique<int[]>(maxKeys);
int maxChildren = isLeaf ? 0 : maxDegree;
children = std::make_unique<std::shared_ptr<void>[]>(maxChildren);
for (size_t i = 0; i < maxChildren; ++i) {
children[i] = nullptr;
}
}
};
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
char name[MAX_COLUMN_NAME];
int columnIndex;
SmartBPlusTreeNode *root;
int height;
int keyCount;
} TableIndex;
typedef struct {
char name[MAX_TABLE_NAME];
ColumnDef columns[MAX_COLUMNS];
SmartTableRow rows[MAX_ROWS];
TableIndex indexes[MAX_INDEXES];
int columnCount;
int rowCount;
int nextRowId;
int indexCount;
time_t createdAt;
bool inTransaction;
char dataFile[256];
} DatabaseTable;
typedef struct {
enum { OP_INSERT, OP_UPDATE, OP_DELETE } operation;
int rowId;
DataCell *oldData;
DataCell *newData;
int columnCount;
time_t timestamp;
} TransactionLog;
typedef struct {
TransactionLog logs[1000];
int logCount;
DatabaseTable *table;
bool active;
int savepointCount;
int savepoints[10];
} Transaction;
typedef struct {
DataCell **rows;
int rowCount;
int columnCount;
char **columnNames;
ErrorCode error;
char errorMsg[MAX_ERROR_MSG];
} QueryResult;
#ifdef __cplusplus
}
#endif
class SmartQueryResult {
public:
std::unique_ptr<std::unique_ptr<DataCell[]>[]> rows;
int rowCount;
int columnCount;
std::unique_ptr<std::unique_ptr<char[]>[]> columnNames;
ErrorCode error;
char errorMsg[MAX_ERROR_MSG];
SmartQueryResult(int cols, const std::vector<std::string> &colNames, const std::vector<std::vector<DataCell>> &data)
: columnCount(cols)
{
columnNames = std::make_unique<std::unique_ptr<char[]>[]>(columnCount);
for (int i = 0; i < columnCount; i++) {
if (i < static_cast<int>(colNames.size()) && !colNames[i].empty()) {
columnNames[i] = std::make_unique<char[]>(colNames[i].size() + 1);
}
}
rowCount = data.size();
rows = std::make_unique<std::unique_ptr<DataCell[]>[]>(rowCount);
for (int i = 0; i < rowCount; ++i) {
rows[i] = std::make_unique<DataCell[]>(columnCount);
for (int j = 0; j < columnCount; ++j) {
rows[i][j] = data[i][j];
}
}
}
const char *GetColumnName(int col) const
{
if (col < 0 || col >= columnCount || !columnNames || !columnNames[col]) {
return "";
}
return columnNames[col].get();
}
};
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
DatabaseTable *tables[50];
int tableCount;
Transaction activeTransactions[MAX_TRANSACTION_DEPTH];
int transactionDepth;
char currentDatabase[256];
bool autoCommit;
FILE *logFile;
} DatabaseSystem;
time_t ParseDate(const char *dateStr);
DataCell CreateDataCell(DataType type, const char *value);
#ifdef __cplusplus
}
#endif
class DataBaseOps {
private:
const char *DataTypeToString(DataType type);
DataType StringToDataType(const char *str);
char *FormatDate(time_t timestamp);
void FreeDataCell(DataCell *cell);
int CompareDataCells(const DataCell *a, const DataCell *b);
SmartBPlusTreeNode *CreateBplusTreeNode(bool isLeaf, int order);
void DeleteFromIndex(TableIndex *index, int key);
SmartTableRow *FindTableRow(int rowId);
private:
std::unique_ptr<DatabaseTable> table;
std::unique_ptr<SmartQueryResult> queryResult;
public:
DataBaseOps(const char *tableName, ColumnDef *columns, int columnCount);
~DataBaseOps();
TableIndex *CreateTableIndex(const char *columnName);
bool DeleteTableRow(int rowId);
SmartQueryResult *ExecuteSelectQuery(const char *whereClause);
bool BeginTransaction();
bool CommitTransaction();
bool RollbackTransaction();
void PrintTableSchema();
void PrintQueryResult(SmartQueryResult *result);
int InsertTableRow(DataCell *cells);
bool UpdateTableRow(int rowId, DataCell *newCells);
};
#ifdef __cplusplus
extern "C" {
#endif
int DatabaseOpsDemo();
#ifdef __cplusplus
}
#endif
#endif