Skip to content

Commit 302e37a

Browse files
add a few new functions
1 parent ee899c9 commit 302e37a

File tree

2 files changed

+425
-140
lines changed

2 files changed

+425
-140
lines changed

code/logic/fossil/crabdb/myshell.h

Lines changed: 115 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,59 @@ fossil_bluecrab_myshell_error_t fossil_myshell_branch(fossil_bluecrab_myshell_t
207207
*/
208208
fossil_bluecrab_myshell_error_t fossil_myshell_checkout(fossil_bluecrab_myshell_t *db, const char *branch_or_commit);
209209

210+
/**
211+
* o-Commit/branch
212+
* Merges a source branch into the current branch with a commit message.
213+
* Time Complexity: O(n) (n = number of commits in source branch).
214+
* @param db Database handle.
215+
* @param source_branch Name of the source branch to merge.
216+
* @param message Merge commit message.
217+
* @return Error code.
218+
*/
219+
fossil_bluecrab_myshell_error_t fossil_myshell_merge(fossil_bluecrab_myshell_t *db, const char *source_branch, const char *message);
220+
221+
/**
222+
* o-Commit/branch
223+
* Reverts to a specific commit in the current branch.
224+
* Time Complexity: O(n) (n = number of commits).
225+
* @param db Database handle.
226+
* @param commit_hash Commit hash to revert to.
227+
* @return Error code.
228+
*/
229+
fossil_bluecrab_myshell_error_t fossil_myshell_revert(fossil_bluecrab_myshell_t *db, const char *commit_hash);
230+
231+
/**
232+
* o-Staging area
233+
* Stages a key/value pair for the next commit.
234+
* Time Complexity: O(1) for append, O(n) for update (n = number of staged records).
235+
* @param db Database handle.
236+
* @param key Key string.
237+
* @param value Value string.
238+
* @return Error code.
239+
*/
240+
fossil_bluecrab_myshell_error_t fossil_myshell_stage(fossil_bluecrab_myshell_t *db, const char *key, const char *value);
241+
242+
/**
243+
* o-Staging area
244+
* Unstages a key/value pair from the staging area.
245+
* Time Complexity: O(n) (n = number of staged records).
246+
* @param db Database handle.
247+
* @param key Key string.
248+
* @return Error code.
249+
*/
250+
fossil_bluecrab_myshell_error_t fossil_myshell_unstage(fossil_bluecrab_myshell_t *db, const char *key);
251+
252+
/**
253+
* o-Tagging
254+
* Tags a specific commit with a name.
255+
* Time Complexity: O(n) (n = number of commits).
256+
* @param db Database handle.
257+
* @param commit_hash Commit hash to tag.
258+
* @param tag_name Name of the tag.
259+
* @return Error code.
260+
*/
261+
fossil_bluecrab_myshell_error_t fossil_myshell_tag(fossil_bluecrab_myshell_t *db, const char *commit_hash, const char *tag_name);
262+
210263
/**
211264
* o-History iteration
212265
* Callback type for commit log iteration.
@@ -268,6 +321,8 @@ fossil_bluecrab_myshell_error_t fossil_myshell_check_integrity(fossil_bluecrab_m
268321

269322
#ifdef __cplusplus
270323
}
324+
#include <utility>
325+
#include <stdexcept>
271326
#include <string>
272327

273328
namespace fossil {
@@ -295,211 +350,131 @@ namespace fossil {
295350
*/
296351
class MyShell {
297352
public:
298-
/**
299-
* o-Non-copyable, movable
300-
* - Prevents copying, allows move semantics for single ownership.
301-
*/
353+
// Non-copyable, movable
302354
MyShell(const MyShell&) = delete;
303355
MyShell& operator=(const MyShell&) = delete;
304-
MyShell(MyShell&& other) noexcept : db_(other.db_) { other.db_ = nullptr; }
356+
MyShell(MyShell&& other) noexcept : db_(std::exchange(other.db_, nullptr)) {}
305357
MyShell& operator=(MyShell&& other) noexcept {
306358
if (this != &other) {
307359
close();
308-
db_ = other.db_;
309-
other.db_ = nullptr;
360+
db_ = std::exchange(other.db_, nullptr);
310361
}
311362
return *this;
312363
}
313364

314-
/**
315-
* o-Open
316-
* - Opens an existing database file.
317-
* - Time Complexity: O(1) for handle allocation, O(n) for file scan (n = file size).
318-
* @param path Path to the database file.
319-
* @param[out] err Output error code.
320-
*/
365+
// Open
321366
explicit MyShell(const std::string& path, fossil_bluecrab_myshell_error_t& err) {
322367
db_ = fossil_myshell_open(path.c_str(), &err);
323368
}
324369

325-
/**
326-
* o-Create
327-
* - Creates a new database file.
328-
* - Time Complexity: O(1) for file creation.
329-
* @param path Path to the new database file.
330-
* @param[out] err Output error code.
331-
*/
370+
// Create
332371
static MyShell create(const std::string& path, fossil_bluecrab_myshell_error_t& err) {
333372
MyShell shell;
334373
shell.db_ = fossil_myshell_create(path.c_str(), &err);
335374
return shell;
336375
}
337376

338-
/**
339-
* o-Close
340-
* - Closes the database handle and releases resources.
341-
* - Time Complexity: O(1).
342-
*/
377+
// Close
343378
~MyShell() { close(); }
344-
345-
/**
346-
* o-Close
347-
* - Explicitly closes the database handle.
348-
*/
349379
void close() {
350380
if (db_) {
351381
fossil_myshell_close(db_);
352382
db_ = nullptr;
353383
}
354384
}
355385

356-
/**
357-
* o-Record CRUD (put)
358-
* - Inserts or updates a key/value record in the database.
359-
* - Time Complexity: O(1) for append, O(n) for update (n = number of records).
360-
* @param key Key string.
361-
* @param value Value string.
362-
* @return Error code.
363-
*/
386+
// Record CRUD (put)
364387
fossil_bluecrab_myshell_error_t put(const std::string& key, const std::string& value) {
365-
return fossil_myshell_put(db_, key.c_str(), value.c_str());
388+
return fossil_myshell_put(db_, key.c_str(), value.c_str());
366389
}
367390

368-
/**
369-
* o-Record CRUD (get)
370-
* - Retrieves the value for a given key from the database.
371-
* - Time Complexity: O(n) (n = number of records).
372-
* @param key Key string.
373-
* @param out_value Output string for value.
374-
* @return Error code.
375-
*/
391+
// Record CRUD (get)
376392
fossil_bluecrab_myshell_error_t get(const std::string& key, std::string& out_value) {
377-
char buffer[4096] = {0};
378-
fossil_bluecrab_myshell_error_t err = fossil_myshell_get(db_, key.c_str(), buffer, sizeof(buffer));
379-
if (err == FOSSIL_MYSHELL_ERROR_SUCCESS) {
380-
out_value = buffer;
381-
}
382-
return err;
393+
char buffer[4096] = {0};
394+
fossil_bluecrab_myshell_error_t err = fossil_myshell_get(db_, key.c_str(), buffer, sizeof(buffer));
395+
if (err == FOSSIL_MYSHELL_ERROR_SUCCESS) {
396+
out_value = buffer;
397+
}
398+
return err;
383399
}
384400

385-
/**
386-
* o-Record CRUD (del)
387-
* - Deletes a key/value record from the database.
388-
* - Time Complexity: O(n) (n = number of records).
389-
* @param key Key string.
390-
* @return Error code.
391-
*/
401+
// Record CRUD (del)
392402
fossil_bluecrab_myshell_error_t del(const std::string& key) {
393-
return fossil_myshell_del(db_, key.c_str());
403+
return fossil_myshell_del(db_, key.c_str());
394404
}
395405

396-
/**
397-
* o-Commit
398-
* - Commits the current changes to the database with a message.
399-
* - Time Complexity: O(1) for metadata update.
400-
* @param message Commit message.
401-
* @return Error code.
402-
*/
406+
// Commit
403407
fossil_bluecrab_myshell_error_t commit(const std::string& message) {
404-
return fossil_myshell_commit(db_, message.c_str());
408+
return fossil_myshell_commit(db_, message.c_str());
405409
}
406410

407-
/**
408-
* o-Branch
409-
* - Creates a new branch in the database.
410-
* - Time Complexity: O(1).
411-
* @param branch_name Name of the new branch.
412-
* @return Error code.
413-
*/
411+
// Branch
414412
fossil_bluecrab_myshell_error_t branch(const std::string& branch_name) {
415-
return fossil_myshell_branch(db_, branch_name.c_str());
413+
return fossil_myshell_branch(db_, branch_name.c_str());
416414
}
417415

418-
/**
419-
* o-Checkout
420-
* - Checks out a branch or commit in the database.
421-
* - Time Complexity: O(1) for branch, O(n) for commit scan (n = number of commits).
422-
* @param branch_or_commit Branch name or commit hash.
423-
* @return Error code.
424-
*/
416+
// Checkout
425417
fossil_bluecrab_myshell_error_t checkout(const std::string& branch_or_commit) {
426-
return fossil_myshell_checkout(db_, branch_or_commit.c_str());
418+
return fossil_myshell_checkout(db_, branch_or_commit.c_str());
419+
}
420+
421+
// Merge
422+
fossil_bluecrab_myshell_error_t merge(const std::string& source_branch, const std::string& message) {
423+
return fossil_myshell_merge(db_, source_branch.c_str(), message.c_str());
424+
}
425+
426+
// Revert
427+
fossil_bluecrab_myshell_error_t revert(const std::string& commit_hash) {
428+
return fossil_myshell_revert(db_, commit_hash.c_str());
429+
}
430+
431+
// Staging (stage)
432+
fossil_bluecrab_myshell_error_t stage(const std::string& key, const std::string& value) {
433+
return fossil_myshell_stage(db_, key.c_str(), value.c_str());
434+
}
435+
436+
// Staging (unstage)
437+
fossil_bluecrab_myshell_error_t unstage(const std::string& key) {
438+
return fossil_myshell_unstage(db_, key.c_str());
439+
}
440+
441+
// Tag
442+
fossil_bluecrab_myshell_error_t tag(const std::string& commit_hash, const std::string& tag_name) {
443+
return fossil_myshell_tag(db_, commit_hash.c_str(), tag_name.c_str());
427444
}
428445

429-
/**
430-
* o-History iteration (log)
431-
* - Iterates over the commit log, invoking the callback for each commit.
432-
* - Time Complexity: O(n) (n = number of commits).
433-
* @param cb Callback function.
434-
* @param user User data pointer.
435-
* @return Error code.
436-
*/
446+
// History iteration (log)
437447
fossil_bluecrab_myshell_error_t log(fossil_myshell_commit_cb cb, void* user) {
438-
return fossil_myshell_log(db_, cb, user);
448+
return fossil_myshell_log(db_, cb, user);
439449
}
440450

441-
/**
442-
* o-Backup
443-
* - Creates a backup of the database file.
444-
* - Time Complexity: O(n) (n = file size).
445-
* @param backup_path Path to backup file.
446-
* @return Error code.
447-
*/
451+
// Backup
448452
fossil_bluecrab_myshell_error_t backup(const std::string& backup_path) {
449-
return fossil_myshell_backup(db_, backup_path.c_str());
453+
return fossil_myshell_backup(db_, backup_path.c_str());
450454
}
451455

452-
/**
453-
* o-Restore
454-
* - Restores a database file from a backup.
455-
* - Time Complexity: O(n) (n = file size).
456-
* - Static utility, does not require open handle.
457-
* @param backup_path Path to backup file.
458-
* @param target_path Path to restore target file.
459-
* @return Error code.
460-
*/
456+
// Restore
461457
static fossil_bluecrab_myshell_error_t restore(const std::string& backup_path, const std::string& target_path) {
462-
return fossil_myshell_restore(backup_path.c_str(), target_path.c_str());
458+
return fossil_myshell_restore(backup_path.c_str(), target_path.c_str());
463459
}
464460

465-
/**
466-
* o-Utility (errstr)
467-
* - Converts an error code to a human-readable string.
468-
* - Time Complexity: O(1).
469-
* @param err Error code.
470-
* @return Error string.
471-
*/
461+
// Utility (errstr)
472462
static const char* errstr(fossil_bluecrab_myshell_error_t err) {
473-
return fossil_myshell_errstr(err);
463+
return fossil_myshell_errstr(err);
474464
}
475465

476-
/**
477-
* o-Utility (check_integrity)
478-
* - Validates database integrity (hash chain, file size, corruption).
479-
* - Time Complexity: O(n) (n = number of records/commits).
480-
* @return Error code.
481-
*/
466+
// Utility (check_integrity)
482467
fossil_bluecrab_myshell_error_t check_integrity() {
483-
return fossil_myshell_check_integrity(db_);
468+
return fossil_myshell_check_integrity(db_);
484469
}
485470

486-
/**
487-
* o-Utility (is_open)
488-
* - Returns true if the database is open.
489-
*/
471+
// Utility (is_open)
490472
bool is_open() const { return db_ != nullptr; }
491473

492-
/**
493-
* o-Utility (handle)
494-
* - Returns the underlying C handle for advanced operations.
495-
*/
474+
// Utility (handle)
496475
fossil_bluecrab_myshell_t* handle() const { return db_; }
497476

498477
private:
499-
/**
500-
* o-Private default constructor
501-
* - Used internally for static create().
502-
*/
503478
MyShell() : db_(nullptr) {}
504479
fossil_bluecrab_myshell_t* db_;
505480
};

0 commit comments

Comments
 (0)