Skip to content

Commit 7f99ef2

Browse files
adding type id param
1 parent a9978e2 commit 7f99ef2

File tree

4 files changed

+128
-67
lines changed

4 files changed

+128
-67
lines changed

code/logic/fossil/crabdb/noshell.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,23 @@ typedef struct {
161161
* @param file_name The database file name (.crabdb enforced).
162162
* @param document The document string to insert.
163163
* @param param_list Optional FSON parameter list for structured data (can be NULL).
164+
* @param type Document type as string parameter.
164165
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
165166
*/
166-
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert(const char *file_name, const char *document, const char *param_list);
167+
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert(const char *file_name, const char *document, const char *param_list, const char *type);
167168

168169
/**
169170
* @brief Inserts a document and returns a unique internal ID.
170171
*
171172
* @param file_name The database file name (.crabdb enforced).
172173
* @param document The document string to insert.
173174
* @param param_list Optional FSON parameter list for structured data (can be NULL).
175+
* @param type Document type as string parameter.
174176
* @param out_id Buffer to store generated document ID.
175177
* @param id_size Size of the buffer.
176178
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
177179
*/
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);
180+
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert_with_id(const char *file_name, const char *document, const char *param_list, const char *type, char *out_id, size_t id_size);
179181

180182
/**
181183
* @brief Finds a document based on a query string.
@@ -184,9 +186,10 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert_with_id(const cha
184186
* @param query The query string to search.
185187
* @param result Buffer to store the matching document.
186188
* @param buffer_size Size of the buffer.
189+
* @param type_id Optional type id parameter.
187190
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
188191
*/
189-
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_find(const char *file_name, const char *query, char *result, size_t buffer_size);
192+
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_find(const char *file_name, const char *query, char *result, size_t buffer_size, const char *type_id);
190193

191194
/**
192195
* @brief Finds documents using a callback filter function.
@@ -205,9 +208,10 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_find_cb(const char *file
205208
* @param query Query string to locate document(s).
206209
* @param new_document New document content to replace matching documents.
207210
* @param param_list Optional FSON parameter list for structured data (can be NULL).
211+
* @param type_id Optional type id parameter.
208212
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
209213
*/
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);
214+
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_update(const char *file_name, const char *query, const char *new_document, const char *param_list, const char *type_id);
211215

212216
/**
213217
* @brief Removes a document from the database based on a query string.
@@ -388,25 +392,29 @@ namespace fossil {
388392
* @param file_name The database file name (.crabdb enforced).
389393
* @param document The document string to insert.
390394
* @param param_list Optional FSON parameter list for structured data (can be empty).
395+
* @param type Document type as string parameter (can be empty).
391396
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
392397
*/
393-
static fossil_bluecrab_noshell_error_t insert(const std::string& file_name, const std::string& document, const std::string& param_list = "") {
398+
static fossil_bluecrab_noshell_error_t insert(const std::string& file_name, const std::string& document, const std::string& param_list = "", const std::string& type = "") {
394399
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);
400+
const char* type_str = type.empty() ? nullptr : type.c_str();
401+
return fossil_bluecrab_noshell_insert(file_name.c_str(), document.c_str(), param, type_str);
396402
}
397403

398404
/**
399405
* @brief Inserts a document and returns a unique internal ID.
400406
* @param file_name The database file name (.crabdb enforced).
401407
* @param document The document string to insert.
402408
* @param param_list Optional FSON parameter list for structured data (can be empty).
409+
* @param type Document type as string parameter (can be empty).
403410
* @param out_id Reference to a string to store the generated document ID.
404411
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
405412
*/
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) {
413+
static fossil_bluecrab_noshell_error_t insert_with_id(const std::string& file_name, const std::string& document, const std::string& param_list, const std::string& type, std::string& out_id) {
407414
char buffer[128] = {0};
408415
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));
416+
const char* type_str = type.empty() ? nullptr : type.c_str();
417+
fossil_bluecrab_noshell_error_t err = fossil_bluecrab_noshell_insert_with_id(file_name.c_str(), document.c_str(), param, type_str, buffer, sizeof(buffer));
410418
if (err == FOSSIL_NOSHELL_ERROR_SUCCESS) {
411419
out_id = buffer;
412420
}
@@ -418,11 +426,13 @@ namespace fossil {
418426
* @param file_name The database file name.
419427
* @param query The query string to search.
420428
* @param result Reference to a string to store the matching document.
429+
* @param type_id Optional type id parameter (can be empty).
421430
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
422431
*/
423-
static fossil_bluecrab_noshell_error_t find(const std::string& file_name, const std::string& query, std::string& result) {
432+
static fossil_bluecrab_noshell_error_t find(const std::string& file_name, const std::string& query, std::string& result, const std::string& type_id = "") {
424433
char buffer[4096] = {0};
425-
fossil_bluecrab_noshell_error_t err = fossil_bluecrab_noshell_find(file_name.c_str(), query.c_str(), buffer, sizeof(buffer));
434+
const char* type_str = type_id.empty() ? nullptr : type_id.c_str();
435+
fossil_bluecrab_noshell_error_t err = fossil_bluecrab_noshell_find(file_name.c_str(), query.c_str(), buffer, sizeof(buffer), type_str);
426436
if (err == FOSSIL_NOSHELL_ERROR_SUCCESS) {
427437
result = buffer;
428438
}
@@ -435,11 +445,13 @@ namespace fossil {
435445
* @param query Query string to locate document(s).
436446
* @param new_document New document content to replace matching documents.
437447
* @param param_list Optional FSON parameter list for structured data (can be empty).
448+
* @param type_id Optional type id parameter (can be empty).
438449
* @return FOSSIL_NOSHELL_ERROR_SUCCESS on success, otherwise error code.
439450
*/
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 = "") {
451+
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 = "", const std::string& type_id = "") {
441452
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);
453+
const char* type_str = type_id.empty() ? nullptr : type_id.c_str();
454+
return fossil_bluecrab_noshell_update(file_name.c_str(), query.c_str(), new_document.c_str(), param, type_str);
443455
}
444456

445457
/**

code/logic/noshell.c

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ uint64_t noshell_hash64(const char *str) {
227227
fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert(
228228
const char *file_name,
229229
const char *document,
230-
const char *param_list
230+
const char *param_list,
231+
const char *type // type as string parameter
231232
) {
232233
if (!file_name || !document)
233234
return FOSSIL_NOSHELL_ERROR_INVALID_FILE;
@@ -236,19 +237,20 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert(
236237
return FOSSIL_NOSHELL_ERROR_INVALID_FILE;
237238

238239
// Basic FSON validation: must start with '{' or '['
239-
while (isspace((unsigned char)*document)) document++;
240-
if (*document != '{' && *document != '[')
240+
const char *doc_ptr = document;
241+
while (isspace((unsigned char)*doc_ptr)) doc_ptr++;
242+
if (*doc_ptr != '{' && *doc_ptr != '[')
241243
return FOSSIL_NOSHELL_ERROR_INVALID_TYPE;
242244

243245
FILE *fp = fopen(file_name, "a");
244246
if (!fp)
245247
return FOSSIL_NOSHELL_ERROR_IO;
246248

247-
// Optionally append param_list if provided
249+
// Optionally append param_list if provided, always append #type=TYPE
248250
if (param_list && strlen(param_list) > 0) {
249-
fprintf(fp, "%s %s\n", document, param_list);
251+
fprintf(fp, "%s %s #type=%s\n", document, param_list, type);
250252
} else {
251-
fprintf(fp, "%s\n", document);
253+
fprintf(fp, "%s #type=%s\n", document, type);
252254
}
253255

254256
fclose(fp);
@@ -259,6 +261,7 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert_with_id(
259261
const char *file_name,
260262
const char *document,
261263
const char *param_list,
264+
const char *type, // type as string parameter
262265
char *out_id,
263266
size_t id_size
264267
) {
@@ -282,11 +285,11 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_insert_with_id(
282285
if (!fp)
283286
return FOSSIL_NOSHELL_ERROR_IO;
284287

285-
// Write document in FSON format, append param_list and #id
288+
// Write document in FSON format, append param_list, #type and #id
286289
if (param_list && strlen(param_list) > 0) {
287-
fprintf(fp, "%s %s #id=%s\n", document, param_list, out_id);
290+
fprintf(fp, "%s %s #type=%s #id=%s\n", document, param_list, type, out_id);
288291
} else {
289-
fprintf(fp, "%s #id=%s\n", document, out_id);
292+
fprintf(fp, "%s #type=%s #id=%s\n", document, type, out_id);
290293
}
291294

292295
fclose(fp);
@@ -297,7 +300,8 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_find(
297300
const char *file_name,
298301
const char *query,
299302
char *result,
300-
size_t buffer_size
303+
size_t buffer_size,
304+
const char *type_id // optional type id parameter
301305
) {
302306
if (!file_name || !query || !result || buffer_size == 0)
303307
return FOSSIL_NOSHELL_ERROR_INVALID_FILE;
@@ -317,6 +321,12 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_find(
317321
if (*p != '{' && *p != '[')
318322
continue;
319323
if (strstr(line, query)) {
324+
if (type_id && strlen(type_id) > 0) {
325+
// Check for type match in line
326+
char type_tag[32];
327+
snprintf(type_tag, sizeof(type_tag), "#type=%s", type_id);
328+
if (!strstr(line, type_tag)) continue;
329+
}
320330
strncpy(result, line, buffer_size - 1);
321331
result[buffer_size - 1] = '\0';
322332
fclose(fp);
@@ -365,7 +375,8 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_update(
365375
const char *file_name,
366376
const char *query,
367377
const char *new_document,
368-
const char *param_list
378+
const char *param_list,
379+
const char *type_id // optional type id parameter
369380
) {
370381
if (!file_name || !query || !new_document)
371382
return FOSSIL_NOSHELL_ERROR_INVALID_FILE;
@@ -390,16 +401,40 @@ fossil_bluecrab_noshell_error_t fossil_bluecrab_noshell_update(
390401
bool updated = false;
391402

392403
while (fgets(buf, sizeof(buf), fp)) {
393-
// Only update FSON-formatted lines that match the query
404+
// Only update FSON-formatted lines that match the query and (if provided) type_id
394405
char *p = buf;
395406
while (isspace((unsigned char)*p)) p++;
396407
if ((*p == '{' || *p == '[') && strstr(buf, query)) {
397-
// Replace line with new_document (+ param_list if provided)
408+
if (type_id && strlen(type_id) > 0) {
409+
char type_tag[32];
410+
snprintf(type_tag, sizeof(type_tag), "#type=%s", type_id);
411+
if (!strstr(buf, type_tag)) {
412+
// Not matching type, keep original line
413+
if (count == cap) {
414+
cap = cap ? cap * 2 : 16;
415+
lines = (char **)realloc(lines, cap * sizeof(char *));
416+
if (!lines) {
417+
fclose(fp);
418+
return FOSSIL_NOSHELL_ERROR_OUT_OF_MEMORY;
419+
}
420+
}
421+
lines[count++] = noshell_strdup(buf);
422+
continue;
423+
}
424+
}
425+
// Replace line with new_document (+ param_list if provided) and #type if type_id is given
398426
char new_line[1024];
399-
if (param_list && strlen(param_list) > 0)
400-
snprintf(new_line, sizeof(new_line), "%s %s\n", new_document, param_list);
401-
else
402-
snprintf(new_line, sizeof(new_line), "%s\n", new_document);
427+
if (param_list && strlen(param_list) > 0) {
428+
if (type_id && strlen(type_id) > 0)
429+
snprintf(new_line, sizeof(new_line), "%s %s #type=%s\n", new_document, param_list, type_id);
430+
else
431+
snprintf(new_line, sizeof(new_line), "%s %s\n", new_document, param_list);
432+
} else {
433+
if (type_id && strlen(type_id) > 0)
434+
snprintf(new_line, sizeof(new_line), "%s #type=%s\n", new_document, type_id);
435+
else
436+
snprintf(new_line, sizeof(new_line), "%s\n", new_document);
437+
}
403438

404439
if (count == cap) {
405440
cap = cap ? cap * 2 : 16;

0 commit comments

Comments
 (0)