Skip to content

Commit 2b83bda

Browse files
Add error codes and diff utility to MyShell
Added new error codes for buffer size and invalid type in myshell.h. Enhanced MyShell class with diff utility function and updated documentation.
1 parent 28e56f0 commit 2b83bda

File tree

1 file changed

+192
-58
lines changed

1 file changed

+192
-58
lines changed

code/logic/fossil/crabdb/myshell.h

Lines changed: 192 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ typedef enum {
8080
FOSSIL_MYSHELL_ERROR_INTEGRITY, /**< Data integrity check failed (hash mismatch). */
8181
FOSSIL_MYSHELL_ERROR_TRANSACTION_FAILED, /**< Transaction aborted or rolled back. */
8282
FOSSIL_MYSHELL_ERROR_CAPACITY_EXCEEDED, /**< Reached maximum size or record capacity. */
83+
FOSSIL_MYSHELL_ERROR_BUFFER_TOO_SMALL, /**< Provided buffer is too small for operation. */
84+
FOSSIL_MYSHELL_ERROR_INVALID_TYPE, /**< Invalid or unrecognized data type specified. */
8385
FOSSIL_MYSHELL_ERROR_CONFIG_INVALID, /**< Invalid configuration or options. */
8486
FOSSIL_MYSHELL_ERROR_UNKNOWN /**< Unknown or unspecified error occurred. */
8587
} fossil_bluecrab_myshell_error_t;
@@ -369,6 +371,23 @@ fossil_bluecrab_myshell_error_t fossil_myshell_backup(fossil_bluecrab_myshell_t
369371
*/
370372
fossil_bluecrab_myshell_error_t fossil_myshell_restore(const char *backup_path, const char *target_path);
371373

374+
/**
375+
* o-Utility
376+
* Computes the difference between two database files and outputs the result.
377+
* Time Complexity: O(n) (n = number of records/commits).
378+
* @param db1 First database handle.
379+
* @param db2 Second database handle.
380+
* @param out_diff Output buffer for diff result.
381+
* @param out_size Size of output buffer.
382+
* @return Error code.
383+
*/
384+
fossil_bluecrab_myshell_error_t fossil_myshell_diff(
385+
const fossil_bluecrab_myshell_t *db1,
386+
const fossil_bluecrab_myshell_t *db2,
387+
char *out_diff,
388+
size_t out_size
389+
);
390+
372391
/**
373392
* o-Utility
374393
* Converts an error code to a human-readable string.
@@ -398,150 +417,265 @@ namespace fossil {
398417

399418
/**
400419
* o-MyShell C++ RAII Wrapper for Database Operations
401-
*
402420
* o-Overview:
403421
* - Provides a modern C++ interface for MyShell database management.
404422
* - Encapsulates resource management, error handling, and exposes high-level methods for CRUD,
405423
* commit/branch, backup/restore, and utility functions, mapping directly to the underlying C API.
406-
*
407424
* o-Resource Management:
408425
* - Non-copyable, movable. Ensures single ownership of database handle.
409426
* - Destructor and close() guarantee resource release.
410-
*
411427
* o-Error Handling:
412428
* - All operations return fossil_bluecrab_myshell_error_t error codes.
413429
* - Use errstr() to obtain error descriptions.
414-
*
415430
* o-Thread Safety:
416431
* - Not thread-safe. External synchronization required for concurrent access.
417432
*/
418433
class MyShell {
419434
public:
420-
// Non-copyable, movable
435+
/**
436+
* o-Non-copyable, movable
437+
* Ensures single ownership of database handle.
438+
* Move constructor: O(1)
439+
* Move assignment: O(1)
440+
*/
421441
MyShell(const MyShell&) = delete;
422442
MyShell& operator=(const MyShell&) = delete;
423443
MyShell(MyShell&& other) noexcept : db_(std::exchange(other.db_, nullptr)) {}
424444
MyShell& operator=(MyShell&& other) noexcept {
425-
if (this != &other) {
426-
close();
427-
db_ = std::exchange(other.db_, nullptr);
428-
}
429-
return *this;
445+
if (this != &other) {
446+
close();
447+
db_ = std::exchange(other.db_, nullptr);
448+
}
449+
return *this;
430450
}
431451

432-
// Open
452+
/**
453+
* o-Open
454+
* Opens an existing database file.
455+
* Time Complexity: O(1) for handle allocation, O(n) for file scan.
456+
*/
433457
explicit MyShell(const std::string& path, fossil_bluecrab_myshell_error_t& err) {
434-
db_ = fossil_myshell_open(path.c_str(), &err);
458+
db_ = fossil_myshell_open(path.c_str(), &err);
435459
}
436460

437-
// Create
461+
/**
462+
* o-Create
463+
* Creates a new database file.
464+
* Time Complexity: O(1) for file creation.
465+
*/
438466
static MyShell create(const std::string& path, fossil_bluecrab_myshell_error_t& err) {
439-
MyShell shell;
440-
shell.db_ = fossil_myshell_create(path.c_str(), &err);
441-
return shell;
467+
MyShell shell;
468+
shell.db_ = fossil_myshell_create(path.c_str(), &err);
469+
return shell;
442470
}
443471

444-
// Close
472+
/**
473+
* o-Close
474+
* Closes the database handle and releases resources.
475+
* Time Complexity: O(1)
476+
*/
445477
~MyShell() { close(); }
478+
479+
/**
480+
* o-Close
481+
* Closes the database handle and releases resources.
482+
* Time Complexity: O(1)
483+
*/
446484
void close() {
447-
if (db_) {
448-
fossil_myshell_close(db_);
449-
db_ = nullptr;
450-
}
485+
if (db_) {
486+
fossil_myshell_close(db_);
487+
db_ = nullptr;
488+
}
451489
}
452490

453-
// Record CRUD (put)
491+
/**
492+
* o-Record CRUD (put)
493+
* Inserts or updates a key/value record in the database.
494+
* Time Complexity: O(1) for append, O(n) for update.
495+
*/
454496
fossil_bluecrab_myshell_error_t put(const std::string& key, const std::string& type, const std::string& value) {
455497
return fossil_myshell_put(db_, key.c_str(), type.c_str(), value.c_str());
456498
}
457499

458-
// Record CRUD (get)
500+
/**
501+
* o-Record CRUD (get)
502+
* Retrieves the value for a given key from the database.
503+
* Time Complexity: O(n)
504+
*/
459505
fossil_bluecrab_myshell_error_t get(const std::string& key, std::string& out_value) {
460-
char buffer[4096] = {0};
461-
fossil_bluecrab_myshell_error_t err = fossil_myshell_get(db_, key.c_str(), buffer, sizeof(buffer));
462-
if (err == FOSSIL_MYSHELL_ERROR_SUCCESS) {
463-
out_value = buffer;
464-
}
465-
return err;
506+
char buffer[4096] = {0};
507+
fossil_bluecrab_myshell_error_t err = fossil_myshell_get(db_, key.c_str(), buffer, sizeof(buffer));
508+
if (err == FOSSIL_MYSHELL_ERROR_SUCCESS) {
509+
out_value = buffer;
510+
}
511+
return err;
466512
}
467513

468-
// Record CRUD (del)
514+
/**
515+
* o-Record CRUD (del)
516+
* Deletes a key/value record from the database.
517+
* Time Complexity: O(n)
518+
*/
469519
fossil_bluecrab_myshell_error_t del(const std::string& key) {
470-
return fossil_myshell_del(db_, key.c_str());
520+
return fossil_myshell_del(db_, key.c_str());
471521
}
472522

473-
// Commit
523+
/**
524+
* o-Commit
525+
* Commits the current changes to the database with a message.
526+
* Time Complexity: O(1) for metadata update.
527+
*/
474528
fossil_bluecrab_myshell_error_t commit(const std::string& message) {
475-
return fossil_myshell_commit(db_, message.c_str());
529+
return fossil_myshell_commit(db_, message.c_str());
476530
}
477531

478-
// Branch
532+
/**
533+
* o-Branch
534+
* Creates a new branch in the database.
535+
* Time Complexity: O(1)
536+
*/
479537
fossil_bluecrab_myshell_error_t branch(const std::string& branch_name) {
480-
return fossil_myshell_branch(db_, branch_name.c_str());
538+
return fossil_myshell_branch(db_, branch_name.c_str());
481539
}
482540

483-
// Checkout
541+
/**
542+
* o-Checkout
543+
* Checks out a branch or commit in the database.
544+
* Time Complexity: O(1) for branch, O(n) for commit scan.
545+
*/
484546
fossil_bluecrab_myshell_error_t checkout(const std::string& branch_or_commit) {
485-
return fossil_myshell_checkout(db_, branch_or_commit.c_str());
547+
return fossil_myshell_checkout(db_, branch_or_commit.c_str());
486548
}
487549

488-
// Merge
550+
/**
551+
* o-Merge
552+
* Merges a source branch into the current branch with a commit message.
553+
* Time Complexity: O(n) (n = number of commits in source branch)
554+
*/
489555
fossil_bluecrab_myshell_error_t merge(const std::string& source_branch, const std::string& message) {
490-
return fossil_myshell_merge(db_, source_branch.c_str(), message.c_str());
556+
return fossil_myshell_merge(db_, source_branch.c_str(), message.c_str());
491557
}
492558

493-
// Revert
559+
/**
560+
* o-Revert
561+
* Reverts to a specific commit in the current branch.
562+
* Time Complexity: O(n)
563+
*/
494564
fossil_bluecrab_myshell_error_t revert(const std::string& commit_hash) {
495-
return fossil_myshell_revert(db_, commit_hash.c_str());
565+
return fossil_myshell_revert(db_, commit_hash.c_str());
496566
}
497567

498-
// Staging (stage)
568+
/**
569+
* o-Staging (stage)
570+
* Stages a key/value pair for the next commit.
571+
* Time Complexity: O(1) for append, O(n) for update.
572+
*/
499573
fossil_bluecrab_myshell_error_t stage(const std::string& key, const std::string& type, const std::string& value) {
500574
return fossil_myshell_stage(db_, key.c_str(), type.c_str(), value.c_str());
501575
}
502576

503-
// Staging (unstage)
577+
/**
578+
* o-Staging (unstage)
579+
* Unstages a key/value pair from the staging area.
580+
* Time Complexity: O(n)
581+
*/
504582
fossil_bluecrab_myshell_error_t unstage(const std::string& key) {
505-
return fossil_myshell_unstage(db_, key.c_str());
583+
return fossil_myshell_unstage(db_, key.c_str());
506584
}
507585

508-
// Tag
586+
/**
587+
* o-Tag
588+
* Tags a specific commit with a name.
589+
* Time Complexity: O(n)
590+
*/
509591
fossil_bluecrab_myshell_error_t tag(const std::string& commit_hash, const std::string& tag_name) {
510-
return fossil_myshell_tag(db_, commit_hash.c_str(), tag_name.c_str());
592+
return fossil_myshell_tag(db_, commit_hash.c_str(), tag_name.c_str());
511593
}
512594

513-
// History iteration (log)
595+
/**
596+
* o-History iteration (log)
597+
* Iterates over the commit log, invoking the callback for each commit.
598+
* Time Complexity: O(n)
599+
*/
514600
fossil_bluecrab_myshell_error_t log(fossil_myshell_commit_cb cb, void* user) {
515-
return fossil_myshell_log(db_, cb, user);
601+
return fossil_myshell_log(db_, cb, user);
516602
}
517603

518-
// Backup
604+
/**
605+
* o-Backup
606+
* Creates a backup of the database file.
607+
* Time Complexity: O(n)
608+
*/
519609
fossil_bluecrab_myshell_error_t backup(const std::string& backup_path) {
520-
return fossil_myshell_backup(db_, backup_path.c_str());
610+
return fossil_myshell_backup(db_, backup_path.c_str());
521611
}
522612

523-
// Restore
613+
/**
614+
* o-Restore
615+
* Restores a database file from a backup.
616+
* Time Complexity: O(n)
617+
*/
524618
static fossil_bluecrab_myshell_error_t restore(const std::string& backup_path, const std::string& target_path) {
525-
return fossil_myshell_restore(backup_path.c_str(), target_path.c_str());
619+
return fossil_myshell_restore(backup_path.c_str(), target_path.c_str());
620+
}
621+
622+
/**
623+
* o-Utility (diff)
624+
* Computes the difference between two database files and outputs the result.
625+
* Time Complexity: O(n)
626+
*/
627+
fossil_bluecrab_myshell_error_t diff(const MyShell& other, std::string& out_diff) {
628+
char buffer[4096] = {0};
629+
fossil_bluecrab_myshell_error_t err = fossil_myshell_diff(
630+
db_,
631+
other.db_,
632+
buffer,
633+
sizeof(buffer)
634+
);
635+
if (err == FOSSIL_MYSHELL_ERROR_SUCCESS) {
636+
out_diff = buffer;
637+
}
638+
return err;
526639
}
527640

528-
// Utility (errstr)
641+
/**
642+
* o-Utility (errstr)
643+
* Converts an error code to a human-readable string.
644+
* Time Complexity: O(1)
645+
*/
529646
static const char* errstr(fossil_bluecrab_myshell_error_t err) {
530-
return fossil_myshell_errstr(err);
647+
return fossil_myshell_errstr(err);
531648
}
532649

533-
// Utility (check_integrity)
650+
/**
651+
* o-Utility (check_integrity)
652+
* Validates database integrity (hash chain, file size, corruption).
653+
* Time Complexity: O(n)
654+
*/
534655
fossil_bluecrab_myshell_error_t check_integrity() {
535-
return fossil_myshell_check_integrity(db_);
656+
return fossil_myshell_check_integrity(db_);
536657
}
537658

538-
// Utility (is_open)
659+
/**
660+
* o-Utility (is_open)
661+
* Checks if the database handle is open.
662+
* Time Complexity: O(1)
663+
*/
539664
bool is_open() const { return db_ != nullptr; }
540665

541-
// Utility (handle)
666+
/**
667+
* o-Utility (handle)
668+
* Returns the underlying C handle.
669+
* Time Complexity: O(1)
670+
*/
542671
fossil_bluecrab_myshell_t* handle() const { return db_; }
543672

544673
private:
674+
/**
675+
* o-Private default constructor
676+
* Initializes db_ to nullptr.
677+
* Time Complexity: O(1)
678+
*/
545679
MyShell() : db_(nullptr) {}
546680
fossil_bluecrab_myshell_t* db_;
547681
};

0 commit comments

Comments
 (0)