commit 22e4f7783ff40252cb51bdc112676159381d091a
parent 0d8f2be167e567d3238f48a9407c4344f5de1c70
Author: lash <dev@holbrook.no>
Date: Thu, 28 Mar 2024 20:12:22 +0000
Expose rewind db to start of key prefix
Diffstat:
4 files changed, 51 insertions(+), 3 deletions(-)
diff --git a/src/db.c b/src/db.c
@@ -51,6 +51,7 @@ int db_put(struct db_ctx *ctx, enum DbKey pfx, char *data, size_t data_len) {
r = clock_gettime(CLOCK_REALTIME, &ts);
if (r) {
+ free(buf);
return ERR_FAIL;
}
memcpy(rts, &ts.tv_sec, sizeof(ts.tv_sec));
@@ -60,6 +61,7 @@ int db_put(struct db_ctx *ctx, enum DbKey pfx, char *data, size_t data_len) {
e = gcry_md_open(&h, GCRY_MD_SHA256, 0);
if (e) {
+ free(buf);
return ERR_DIGESTFAIL;
}
gcry_md_write(h, data, data_len);
@@ -71,11 +73,13 @@ int db_put(struct db_ctx *ctx, enum DbKey pfx, char *data, size_t data_len) {
r = mdb_txn_begin(ctx->env, NULL, 0, &tx);
if (r) {
+ free(buf);
return ERR_FAIL;
}
r = mdb_dbi_open(tx, NULL, MDB_CREATE, &dbi);
if (r) {
+ free(buf);
return ERR_FAIL;
}
@@ -86,6 +90,7 @@ int db_put(struct db_ctx *ctx, enum DbKey pfx, char *data, size_t data_len) {
r = mdb_put(tx, dbi, &k, &v, 0);
if (r) {
+ free(buf);
return ERR_FAIL;
}
@@ -100,13 +105,16 @@ int db_put(struct db_ctx *ctx, enum DbKey pfx, char *data, size_t data_len) {
r = mdb_put(tx, dbi, &k, &v, 0);
if (r) {
+ free(buf);
return ERR_FAIL;
}
r = mdb_txn_commit(tx);
if (r) {
+ free(buf);
return ERR_FAIL;
}
+ free(buf);
return ERR_OK;
@@ -188,6 +196,11 @@ int db_next(struct db_ctx *ctx, enum DbKey pfx, char **key, size_t *key_len, cha
}
+void db_rewind(struct db_ctx *ctx) {
+ ctx->browsing = 0;
+}
+
+
void db_reset(struct db_ctx *ctx) {
mdb_cursor_close(ctx->crsr);
mdb_dbi_close(ctx->env, ctx->dbi);
diff --git a/src/db.h b/src/db.h
@@ -53,6 +53,7 @@ struct db_ctx {
int db_connect(struct db_ctx *ctx, char *conn);
int db_put(struct db_ctx *ctx, enum DbKey pfx, char *data, size_t data_len);
int db_next(struct db_ctx *ctx, enum DbKey pfx, char **key, size_t *key_len, char **value, size_t *value_len);
+void db_rewind(struct db_ctx *ctx);
void db_reset(struct db_ctx *ctx);
#endif // _DB_H
diff --git a/src/gtk/kee-entry-store.c b/src/gtk/kee-entry-store.c
@@ -88,8 +88,10 @@ static int kee_entry_store_seek(KeeEntryStore *o, int idx) {
//direction = 0;
key_len = 9;
o->last_key = o->last;
+ memset(o->last_key, 0, key_len);
o->last_value = o->last_digest + 64;
*o->last_key = DbKeyLedgerHead;
+ o->last_value_length = 1024;
i = 0;
o->last_state = 2;
while (i <= idx) {
@@ -103,6 +105,7 @@ static int kee_entry_store_seek(KeeEntryStore *o, int idx) {
o->last_state = 1;
i++;
}
+ db_rewind(o->db);
return i;
}
@@ -111,9 +114,9 @@ KeeEntryStore* kee_entry_store_new(struct db_ctx *db) {
o = g_object_new(KEE_TYPE_ENTRY_STORE, NULL);
o->db = db;
- o->last_value_length = 1024;
o->last = calloc(2048, 1);
o->last_digest = o->last + DB_KEY_SIZE_LIMIT;
+ o->last_value_length = 1024;
o->last_count = kee_entry_store_seek(o, INT_MAX);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "max key index is: %d", o->last_idx - 1);
@@ -123,4 +126,5 @@ KeeEntryStore* kee_entry_store_new(struct db_ctx *db) {
void kee_entry_store_finalize(KeeEntryStore *o) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "freeing entry store");
free(o->last);
+ free(o->last);
}
diff --git a/src/gtk/kee-entry.c b/src/gtk/kee-entry.c
@@ -5,6 +5,7 @@
#include "db.h"
#include "err.h"
#include "export.h"
+#include "hex.h"
typedef struct {
} KeeEntryPrivate;
@@ -19,7 +20,10 @@ struct _KeeEntry {
int state;
long long timestamp;
char mem[4096];
+ char header[1024];
char *unit_of_account;
+ char *alice;
+ char *bob;
char decimals;
};
@@ -57,24 +61,45 @@ int kee_entry_deserialize(KeeEntry *o, const char *key, size_t key_len, const ch
int r;
struct kee_import im;
size_t out_len;
+ size_t remaining;
+ char *p;
o->state = 1;
import_init(&im, data, data_len);
out_len = 4096;
+ remaining = out_len;
r = import_read(&im, o->unit_of_account, out_len);
- *(o->unit_of_account + r) = 0;
+ p = o->unit_of_account + r;
+ *p = 0;
+ remaining -= (r + 1);
+ p += 1;
out_len = 1;
r = import_read(&im, &o->decimals, out_len);
+ out_len = remaining;
+ o->alice = p;
+ r = import_read(&im, o->alice, out_len);
+ remaining -= r;
+ p += r;
+
+ out_len = remaining;
+ o->bob = p;
+ r = import_read(&im, o->bob, out_len);
+
o->state = 0;
+ import_free(&im);
+
return ERR_OK;
}
void kee_entry_apply_list_item_widget(KeeEntry *o) {
GtkWidget *widget;
+ size_t l;
+ char alice_hex[129];
+ char bob_hex[129];
if (o->state) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "entry must be loaded first");
@@ -82,7 +107,12 @@ void kee_entry_apply_list_item_widget(KeeEntry *o) {
}
//widget = gtk_label_new(o->unit_of_account);
- widget = gtk_label_new(o->unit_of_account);
+ l = 129;
+ bin_to_hex(o->alice, 64, alice_hex, &l);
+ l = 129;
+ bin_to_hex(o->bob, 64, bob_hex, &l);
+ sprintf(o->header, "[%s] %s -> %s", o->unit_of_account, alice_hex, bob_hex);
+ widget = gtk_label_new(o->header);
gtk_box_append(GTK_BOX(o), widget);
return;
}