Skip to content

Commit 5238e10

Browse files
rewrite noshell
1 parent 2b83bda commit 5238e10

File tree

4 files changed

+1268
-638
lines changed

4 files changed

+1268
-638
lines changed

code/logic/fossil/crabdb/noshell.h

Lines changed: 120 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,111 @@ extern "C" {
4646
// Enumerations for Data Types and Attributes
4747
// *****************************************************************************
4848

49-
// ===========================================================
50-
// NoShell Error Codes
51-
// ===========================================================
49+
/**
50+
* ===========================================================
51+
* NoShell Error Codes
52+
* ===========================================================
53+
* Enumerates all possible error codes returned by NoShell API functions.
54+
* These codes represent various failure and success states encountered
55+
* during database operations such as file handling, queries, concurrency,
56+
* permissions, backup/restore, and parsing.
57+
*/
5258
typedef enum {
53-
FOSSIL_NOSHELL_ERROR_SUCCESS = 0,
54-
FOSSIL_NOSHELL_ERROR_INVALID_FILE,
55-
FOSSIL_NOSHELL_ERROR_FILE_NOT_FOUND,
56-
FOSSIL_NOSHELL_ERROR_IO,
57-
FOSSIL_NOSHELL_ERROR_INVALID_QUERY,
58-
FOSSIL_NOSHELL_ERROR_CONCURRENCY,
59-
FOSSIL_NOSHELL_ERROR_NOT_FOUND,
60-
FOSSIL_NOSHELL_ERROR_PERMISSION_DENIED,
61-
FOSSIL_NOSHELL_ERROR_CORRUPTED,
62-
FOSSIL_NOSHELL_ERROR_OUT_OF_MEMORY,
63-
FOSSIL_NOSHELL_ERROR_UNSUPPORTED,
64-
FOSSIL_NOSHELL_ERROR_LOCKED,
65-
FOSSIL_NOSHELL_ERROR_TIMEOUT,
66-
FOSSIL_NOSHELL_ERROR_ALREADY_EXISTS,
67-
FOSSIL_NOSHELL_ERROR_BACKUP_FAILED,
68-
FOSSIL_NOSHELL_ERROR_PARSE_FAILED,
69-
FOSSIL_NOSHELL_ERROR_RESTORE_FAILED,
70-
FOSSIL_NOSHELL_ERROR_UNKNOWN
59+
FOSSIL_NOSHELL_ERROR_SUCCESS = 0, /**< Operation completed successfully. */
60+
FOSSIL_NOSHELL_ERROR_INVALID_FILE, /**< The specified file is invalid or corrupt. */
61+
FOSSIL_NOSHELL_ERROR_FILE_NOT_FOUND, /**< The requested file was not found. */
62+
FOSSIL_NOSHELL_ERROR_IO, /**< Input/output error occurred during operation. */
63+
FOSSIL_NOSHELL_ERROR_INVALID_QUERY, /**< The query provided is invalid or malformed. */
64+
FOSSIL_NOSHELL_ERROR_CONCURRENCY, /**< Concurrency conflict detected (e.g., locked resource). */
65+
FOSSIL_NOSHELL_ERROR_NOT_FOUND, /**< Requested key or record not found in database. */
66+
FOSSIL_NOSHELL_ERROR_PERMISSION_DENIED, /**< Operation denied due to insufficient permissions. */
67+
FOSSIL_NOSHELL_ERROR_CORRUPTED, /**< Database or file is corrupted. */
68+
FOSSIL_NOSHELL_ERROR_OUT_OF_MEMORY, /**< Memory allocation failed. */
69+
FOSSIL_NOSHELL_ERROR_UNSUPPORTED, /**< Operation or feature is not supported. */
70+
FOSSIL_NOSHELL_ERROR_LOCKED, /**< Resource is locked and cannot be accessed. */
71+
FOSSIL_NOSHELL_ERROR_TIMEOUT, /**< Operation timed out. */
72+
FOSSIL_NOSHELL_ERROR_ALREADY_EXISTS, /**< Resource already exists (e.g., duplicate key). */
73+
FOSSIL_NOSHELL_ERROR_BACKUP_FAILED, /**< Backup operation failed. */
74+
FOSSIL_NOSHELL_ERROR_PARSE_FAILED, /**< Parsing of input or file failed. */
75+
FOSSIL_NOSHELL_ERROR_RESTORE_FAILED, /**< Restore operation failed. */
76+
FOSSIL_NOSHELL_ERROR_LOCK_FAILED, /**< Failed to acquire or release lock. */
77+
FOSSIL_NOSHELL_ERROR_SCHEMA_MISMATCH, /**< Schema or format mismatch between versions. */
78+
FOSSIL_NOSHELL_ERROR_VERSION_UNSUPPORTED, /**< Database created with unsupported version. */
79+
FOSSIL_NOSHELL_ERROR_INDEX_CORRUPTED, /**< Index structure corrupted or unreadable. */
80+
FOSSIL_NOSHELL_ERROR_INTEGRITY, /**< Data integrity check failed (hash mismatch). */
81+
FOSSIL_NOSHELL_ERROR_TRANSACTION_FAILED, /**< Transaction aborted or rolled back. */
82+
FOSSIL_NOSHELL_ERROR_CAPACITY_EXCEEDED, /**< Reached maximum size or record capacity. */
83+
FOSSIL_NOSHELL_ERROR_BUFFER_TOO_SMALL, /**< Provided buffer is too small for operation. */
84+
FOSSIL_NOSHELL_ERROR_INVALID_TYPE, /**< Invalid or unrecognized data type specified. */
85+
FOSSIL_NOSHELL_ERROR_CONFIG_INVALID, /**< Invalid configuration or options. */
86+
FOSSIL_NOSHELL_ERROR_UNKNOWN /**< Unknown or unspecified error occurred. */
7187
} fossil_bluecrab_noshell_error_t;
7288

89+
// ============================================================================
90+
// FSON v2 compatible value representation (local to NoShell)
91+
// ============================================================================
92+
typedef enum {
93+
NOSHELL_FSON_TYPE_NULL = 0,
94+
NOSHELL_FSON_TYPE_BOOL,
95+
96+
// Scalars
97+
NOSHELL_FSON_TYPE_I8,
98+
NOSHELL_FSON_TYPE_I16,
99+
NOSHELL_FSON_TYPE_I32,
100+
NOSHELL_FSON_TYPE_I64,
101+
NOSHELL_FSON_TYPE_U8,
102+
NOSHELL_FSON_TYPE_U16,
103+
NOSHELL_FSON_TYPE_U32,
104+
NOSHELL_FSON_TYPE_U64,
105+
NOSHELL_FSON_TYPE_F32,
106+
NOSHELL_FSON_TYPE_F64,
107+
108+
// Literals
109+
NOSHELL_FSON_TYPE_OCT,
110+
NOSHELL_FSON_TYPE_HEX,
111+
NOSHELL_FSON_TYPE_BIN,
112+
113+
// Strings
114+
NOSHELL_FSON_TYPE_CHAR,
115+
NOSHELL_FSON_TYPE_CSTR,
116+
117+
// Composite
118+
NOSHELL_FSON_TYPE_ARRAY,
119+
NOSHELL_FSON_TYPE_OBJECT,
120+
121+
// v2 Additions
122+
NOSHELL_FSON_TYPE_ENUM,
123+
NOSHELL_FSON_TYPE_DATETIME,
124+
NOSHELL_FSON_TYPE_DURATION
125+
} fossil_bluecrab_noshell_fson_type_t;
126+
127+
typedef struct {
128+
fossil_bluecrab_noshell_fson_type_t type;
129+
union {
130+
bool b;
131+
int8_t i8;
132+
int16_t i16;
133+
int32_t i32;
134+
int64_t i64;
135+
uint8_t u8;
136+
uint16_t u16;
137+
uint32_t u32;
138+
uint64_t u64;
139+
float f32;
140+
double f64;
141+
char *oct;
142+
char *hex;
143+
char *bin;
144+
char c;
145+
char *cstr;
146+
char *array;
147+
char *object;
148+
char *enum_symbol;
149+
char *datetime;
150+
char *duration;
151+
} as;
152+
} fossil_bluecrab_noshell_fson_value_t;
153+
73154
// ===========================================================
74155
// Document CRUD Operations
75156
// ===========================================================
@@ -79,20 +160,22 @@ typedef enum {
79160
*
80161
* @param file_name The database file name (.crabdb enforced).
81162
* @param document The document string to insert.
163+
* @param param_list Optional FSON parameter list for structured data (can be NULL).
82164
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
83165
*/
84-
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert(const char *file_name, const char *document);
166+
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert(const char *file_name, const char *document, const char *param_list);
85167

86168
/**
87169
* @brief Inserts a document and returns a unique internal ID.
88170
*
89171
* @param file_name The database file name (.crabdb enforced).
90172
* @param document The document string to insert.
173+
* @param param_list Optional FSON parameter list for structured data (can be NULL).
91174
* @param out_id Buffer to store generated document ID.
92175
* @param id_size Size of the buffer.
93176
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
94177
*/
95-
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert_with_id(const char *file_name, const char *document, char *out_id, size_t id_size);
178+
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert_with_id(const char *file_name, const char *document, const char *param_list, char *out_id, size_t id_size);
96179

97180
/**
98181
* @brief Finds a document based on a query string.
@@ -121,9 +204,10 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_find_cb(const char *file
121204
* @param file_name The database file name.
122205
* @param query Query string to locate document(s).
123206
* @param new_document New document content to replace matching documents.
207+
* @param param_list Optional FSON parameter list for structured data (can be NULL).
124208
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
125209
*/
126-
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_update(const char *file_name, const char *query, const char *new_document);
210+
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_update(const char *file_name, const char *query, const char *new_document, const char *param_list);
127211

128212
/**
129213
* @brief Removes a document from the database based on a query string.
@@ -303,22 +387,26 @@ namespace fossil {
303387
* @brief Inserts a new document into the database.
304388
* @param file_name The database file name (.crabdb enforced).
305389
* @param document The document string to insert.
390+
* @param param_list Optional FSON parameter list for structured data (can be empty).
306391
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
307392
*/
308-
static fossil_bluecrab_noshell_error_t insert(const std::string& file_name, const std::string& document) {
309-
return fossil_bluecrab_noshell_insert(file_name.c_str(), document.c_str());
393+
static fossil_bluecrab_noshell_error_t insert(const std::string& file_name, const std::string& document, const std::string& param_list = "") {
394+
const char* param = param_list.empty() ? nullptr : param_list.c_str();
395+
return fossil_bluecrab_noshell_insert(file_name.c_str(), document.c_str(), param);
310396
}
311397

312398
/**
313399
* @brief Inserts a document and returns a unique internal ID.
314400
* @param file_name The database file name (.crabdb enforced).
315401
* @param document The document string to insert.
402+
* @param param_list Optional FSON parameter list for structured data (can be empty).
316403
* @param out_id Reference to a string to store the generated document ID.
317404
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
318405
*/
319-
static fossil_bluecrab_noshell_error_t insert_with_id(const std::string& file_name, const std::string& document, std::string& out_id) {
406+
static fossil_bluecrab_noshell_error_t insert_with_id(const std::string& file_name, const std::string& document, const std::string& param_list, std::string& out_id) {
320407
char buffer[128] = {0};
321-
fossil_bluecrab_noshell_error_t err = fossil_bluecrab_noshell_insert_with_id(file_name.c_str(), document.c_str(), buffer, sizeof(buffer));
408+
const char* param = param_list.empty() ? nullptr : param_list.c_str();
409+
fossil_bluecrab_noshell_error_t err = fossil_bluecrab_noshell_insert_with_id(file_name.c_str(), document.c_str(), param, buffer, sizeof(buffer));
322410
if (err == FOSSIL_NOSHELL_ERROR_SUCCESS) {
323411
out_id = buffer;
324412
}
@@ -346,10 +434,12 @@ namespace fossil {
346434
* @param file_name The database file name.
347435
* @param query Query string to locate document(s).
348436
* @param new_document New document content to replace matching documents.
437+
* @param param_list Optional FSON parameter list for structured data (can be empty).
349438
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
350439
*/
351-
static fossil_bluecrab_noshell_error_t update(const std::string& file_name, const std::string& query, const std::string& new_document) {
352-
return fossil_bluecrab_noshell_update(file_name.c_str(), query.c_str(), new_document.c_str());
440+
static fossil_bluecrab_noshell_error_t update(const std::string& file_name, const std::string& query, const std::string& new_document, const std::string& param_list = "") {
441+
const char* param = param_list.empty() ? nullptr : param_list.c_str();
442+
return fossil_bluecrab_noshell_update(file_name.c_str(), query.c_str(), new_document.c_str(), param);
353443
}
354444

355445
/**

0 commit comments

Comments
 (0)