From 37f9307db1e5b7e8621fdb327a6d4da9e2a81623 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 21 May 2017 17:44:07 +1200 Subject: [PATCH] wip set/delete items --- main.go | 42 +++++++++++++ qbolt/boltdb.cpp | 58 +++++++++++------- qbolt/boltdb.h | 4 ++ qbolt/mainwindow.cpp | 139 +++++++++++++++++++++++-------------------- qbolt/mainwindow.h | 8 +++ qbolt/mainwindow.ui | 34 ++++++++++- 6 files changed, 197 insertions(+), 88 deletions(-) diff --git a/main.go b/main.go index a81c8d3..00df9dc 100644 --- a/main.go +++ b/main.go @@ -148,6 +148,48 @@ func Bolt_DeleteBucket(b_ref ObjectReference, browse []string, delBucket string) return err2triple(err) } +//export Bolt_SetItem +func Bolt_SetItem(b_ref ObjectReference, browse []string, key, val string) (int64, *C.char, int) { + if len(browse) == 0 { + return err2triple(errors.New("Can't create top-level items")) + } + + err := withBoltDBReference(b_ref, func(db *bolt.DB) error { + return db.Update(func(tx *bolt.Tx) error { + + bucket, err := walkBuckets(tx, browse) + if err != nil { + return err + } + + return bucket.Put([]byte(key), []byte(val)) + }) + }) + + return err2triple(err) +} + +//export Bolt_DeleteItem +func Bolt_DeleteItem(b_ref ObjectReference, browse []string, key string) (int64, *C.char, int) { + if len(browse) == 0 { + return err2triple(errors.New("Can't create top-level items")) + } + + err := withBoltDBReference(b_ref, func(db *bolt.DB) error { + return db.Update(func(tx *bolt.Tx) error { + + bucket, err := walkBuckets(tx, browse) + if err != nil { + return err + } + + return bucket.Delete([]byte(key)) + }) + }) + + return err2triple(err) +} + type CallResponse struct { s string e error diff --git a/qbolt/boltdb.cpp b/qbolt/boltdb.cpp index ad472a5..85add74 100644 --- a/qbolt/boltdb.cpp +++ b/qbolt/boltdb.cpp @@ -29,18 +29,13 @@ static const int ERROR_AND_KEEP_CALLING = 101; static const int FINISHED_OK = 102; static const int REAL_MESSAGE = 103; -bool BoltDB::addBucket(QStringList bucketPath, QByteArray bucketName, QString& errorOut) -{ - GoSliceManagedWrapper browse(&bucketPath); - GoString bucketNameGS = Interop::toGoString_WeakRef(&bucketName); - auto resp = ::Bolt_CreateBucket(this->gmsDbRef, browse.slice, bucketNameGS); - - if (resp.r0 == ERROR_AND_STOP_CALLING) { - errorOut = QString::fromUtf8(resp.r1, resp.r2); - free(resp.r1); +static bool handleTriple(int r0, char* r1, int64_t r2, QString& errorOut) { + if (r0 == ERROR_AND_STOP_CALLING) { + errorOut = QString::fromUtf8(r1, r2); + free(r1); return false; - } else if (resp.r0 == FINISHED_OK) { + } else if (r0 == FINISHED_OK) { return true; } else { @@ -50,27 +45,44 @@ bool BoltDB::addBucket(QStringList bucketPath, QByteArray bucketName, QString& e } } +bool BoltDB::addBucket(QStringList bucketPath, QByteArray bucketName, QString& errorOut) +{ + GoSliceManagedWrapper browse(&bucketPath); + GoString bucketNameGS = Interop::toGoString_WeakRef(&bucketName); + auto resp = ::Bolt_CreateBucket(this->gmsDbRef, browse.slice, bucketNameGS); + + return handleTriple(resp.r0, resp.r1, resp.r2, errorOut); +} + bool BoltDB::deleteBucket(QStringList bucketPath, QByteArray bucketName, QString& errorOut) { GoSliceManagedWrapper browse(&bucketPath); GoString bucketNameGS = Interop::toGoString_WeakRef(&bucketName); auto resp = ::Bolt_DeleteBucket(this->gmsDbRef, browse.slice, bucketNameGS); - if (resp.r0 == ERROR_AND_STOP_CALLING) { - errorOut = QString::fromUtf8(resp.r1, resp.r2); - free(resp.r1); - return false; - - } else if (resp.r0 == FINISHED_OK) { - return true; - - } else { - // ?? unreachable - return false; - - } + return handleTriple(resp.r0, resp.r1, resp.r2, errorOut); } +bool BoltDB::setItem(QStringList bucketPath, QByteArray keyName, QByteArray value, QString& errorOut) +{ + GoSliceManagedWrapper browse(&bucketPath); + GoString keyNameGS = Interop::toGoString_WeakRef(&keyName); + GoString valueGS = Interop::toGoString_WeakRef(&value); + auto resp = ::Bolt_SetItem(this->gmsDbRef, browse.slice, keyNameGS, valueGS); + + return handleTriple(resp.r0, resp.r1, resp.r2, errorOut); +} + +bool BoltDB::deleteItem(QStringList bucketPath, QByteArray keyName, QString& errorOut) +{ + GoSliceManagedWrapper browse(&bucketPath); + GoString keyNameGS = Interop::toGoString_WeakRef(&keyName); + auto resp = ::Bolt_DeleteItem(this->gmsDbRef, browse.slice, keyNameGS); + + return handleTriple(resp.r0, resp.r1, resp.r2, errorOut); +} + + bool BoltDB::listBucketsAtRoot(QString& errorOut, NameReciever cb) { auto listJob = ::Bolt_ListBucketsAtRoot(this->gmsDbRef); diff --git a/qbolt/boltdb.h b/qbolt/boltdb.h index 8b80e90..e9e7602 100644 --- a/qbolt/boltdb.h +++ b/qbolt/boltdb.h @@ -24,6 +24,10 @@ public: bool deleteBucket(QStringList bucketPath, QByteArray bucketName, QString& errorOut); + bool setItem(QStringList bucketPath, QByteArray keyName, QByteArray value, QString& errorOut); + + bool deleteItem(QStringList bucketPath, QByteArray keyName, QString& errorOut); + bool listKeys(QStringList bucketPath, QString& errorOut, std::function cb); bool getData(QStringList bucketPath, QByteArray key, std::function onSuccess, std::function onError); diff --git a/qbolt/mainwindow.cpp b/qbolt/mainwindow.cpp index aa2b372..d9e64b7 100644 --- a/qbolt/mainwindow.cpp +++ b/qbolt/mainwindow.cpp @@ -235,52 +235,56 @@ void MainWindow::on_bucketTree_currentItemChanged(QTreeWidgetItem *current, QTre ); // Load the data tab - ui->bucketData->clear(); - QString err; - bool ok = bdb->listKeys(browse, err, [=](QByteArray name, int64_t dataLen) { - auto *itm = new QTreeWidgetItem(); - itm->setText(0, QString::fromUtf8(name)); - itm->setText(1, QString("%1").arg(dataLen)); - ui->bucketData->addTopLevelItem(itm); - }); - - if (! ok) { - QMessageBox qmb; - qmb.setText(tr("Error listing bucket content: %1").arg(err)); - qmb.exec(); - } - - ui->bucketData->resizeColumnToContents(0); + refreshData(bdb, browse); // Clean up foreign areas ui->databasePropertiesArea->clear(); } } +void MainWindow::refreshData(BoltDB *bdb, QStringList browse) +{ + // Load the data tab + ui->bucketData->clear(); + QString err; + bool ok = bdb->listKeys(browse, err, [=](QByteArray name, int64_t dataLen) { + auto *itm = new QTreeWidgetItem(); + itm->setText(0, QString::fromUtf8(name)); + itm->setText(1, QString("%1").arg(dataLen)); + ui->bucketData->addTopLevelItem(itm); + }); + + if (! ok) { + QMessageBox qmb; + qmb.setText(tr("Error listing bucket content: %1").arg(err)); + qmb.exec(); + } + + ui->bucketData->resizeColumnToContents(0); + on_bucketData_itemSelectionChanged(); +} + void MainWindow::on_actionClear_selection_triggered() { ui->bucketTree->setCurrentItem(nullptr); } +#define GET_ITM_TOP_BROWSE_BDB \ + QTreeWidgetItem* itm = lastContextSelection; \ + if (itm == nullptr) { \ + return; \ + } \ + QTreeWidgetItem* top = itm; \ + QStringList browse; \ + while(top->parent() != nullptr) { \ + browse.push_front(top->text(0)); \ + top = top->parent(); \ + } \ + auto *bdb = GET_BDB(top); + void MainWindow::on_bucketData_doubleClicked(const QModelIndex &index) { - auto *itm = ui->bucketTree->currentItem(); - if (itm == nullptr) { - return; - } - - // Get browse path - - QTreeWidgetItem* top = itm; - QStringList browse; - while(top->parent() != nullptr) { - browse.push_front(top->text(0)); - top = top->parent(); - } - - // Get BDB - - auto *bdb = GET_BDB(top); + GET_ITM_TOP_BROWSE_BDB; // Get item key @@ -310,21 +314,7 @@ void MainWindow::on_bucketData_doubleClicked(const QModelIndex &index) void MainWindow::on_actionAdd_bucket_triggered() { - QTreeWidgetItem* itm = lastContextSelection; - if (itm == nullptr) { - return; - } - - QTreeWidgetItem* top = itm; - QStringList browse; - while(top->parent() != nullptr) { - browse.push_front(top->text(0)); - top = top->parent(); - } - - // Get BDB - - auto *bdb = GET_BDB(top); + GET_ITM_TOP_BROWSE_BDB; // Prompt for bucket name @@ -349,21 +339,7 @@ void MainWindow::on_actionAdd_bucket_triggered() void MainWindow::on_actionDelete_bucket_triggered() { - QTreeWidgetItem* itm = lastContextSelection; - if (itm == nullptr) { - return; - } - - QTreeWidgetItem* top = itm; - QStringList browse; - while(top->parent() != nullptr) { - browse.push_front(top->text(0)); - top = top->parent(); - } - - // Get BDB - - auto *bdb = GET_BDB(top); + GET_ITM_TOP_BROWSE_BDB; // Prompt for confirmation QString bucketToDelete = itm->text(0); @@ -388,3 +364,40 @@ void MainWindow::on_actionDelete_bucket_triggered() ui->bucketTree->expandItem(itm->parent()); } + +void MainWindow::on_AddDataButton_clicked() +{ + +} + + +void MainWindow::on_DeleteDataButton_clicked() +{ + GET_ITM_TOP_BROWSE_BDB; + + auto selection = ui->bucketData->selectedItems(); + if (selection.length() == 0) { + return; // nothing to do + } + + // Prompt for confirmation + if (QMessageBox::question(this, tr("Delete items"), tr("Are you sure you want to remove %1 item(s)?").arg(selection.length()), QMessageBox::Yes, QMessageBox::Cancel) != QMessageBox::Yes) { + return; + } + + QString err; + for (int i = selection.length(); i-->0;) { + if (! bdb->deleteItem(browse, selection[i]->text(0).toUtf8(), err)) { + QMessageBox qmb; + qmb.setText(tr("Error removing item: %1").arg(err)); + qmb.exec(); + } + } + + refreshData(bdb, browse); +} + +void MainWindow::on_bucketData_itemSelectionChanged() +{ + ui->DeleteDataButton->setEnabled( (ui->bucketData->selectedItems().size() > 0) ); +} diff --git a/qbolt/mainwindow.h b/qbolt/mainwindow.h index bd26e30..df88fa5 100644 --- a/qbolt/mainwindow.h +++ b/qbolt/mainwindow.h @@ -1,6 +1,7 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H +#include "boltdb.h" #include #include #include @@ -44,9 +45,16 @@ private slots: void on_actionDelete_bucket_triggered(); + void on_AddDataButton_clicked(); + + void on_DeleteDataButton_clicked(); + + void on_bucketData_itemSelectionChanged(); + protected: void openDatabase(QString file); void refreshBucketTree(QTreeWidgetItem* top); + void refreshData(BoltDB *bdb, QStringList browse); private: Ui::MainWindow *ui; diff --git a/qbolt/mainwindow.ui b/qbolt/mainwindow.ui index e55a6e9..a99a427 100644 --- a/qbolt/mainwindow.ui +++ b/qbolt/mainwindow.ui @@ -128,7 +128,7 @@ - 0 + 1 @@ -176,8 +176,11 @@ 3 - + + + QAbstractItemView::ExtendedSelection + 0 @@ -202,6 +205,33 @@ + + + + Add... + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Delete... + + +