From 95a1bfeea586be75ae8bda50f593b36a640bfd86 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 21 May 2017 11:59:38 +1200 Subject: [PATCH] retrieve database properties --- main.go | 32 +++++++++++++++++++++++++------- qbolt.h | 9 +++++++++ qbolt/boltdb.cpp | 22 ++++++++++++++++++++++ qbolt/boltdb.h | 2 ++ qbolt/mainwindow.cpp | 20 ++++++++++++++++++++ qbolt/mainwindow.ui | 2 +- 6 files changed, 79 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index debb0a9..4d6222f 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,20 @@ package main import "C" import ( + "encoding/json" "errors" "os" "github.com/boltdb/bolt" ) +const ( + ERROR_AND_STOP_CALLING int64 = 100 + ERROR_AND_KEEP_CALLING = 101 + FINISHED_OK = 102 + REAL_MESSAGE = 103 +) + const Magic int64 = 0x10203040 //export GetMagic @@ -73,6 +81,23 @@ type CallResponse struct { e error } +//export Bolt_DBStats +func Bolt_DBStats(b ObjectReference) (int64, *C.char, int) { + var stats bolt.Stats + + err := withBoltDBReference(b, func(db *bolt.DB) error { + stats = db.Stats() + return nil + }) + + jBytes, err := json.Marshal(stats) + if err != nil { + return ERROR_AND_STOP_CALLING, C.CString(err.Error()), len(err.Error()) + } + + return REAL_MESSAGE, C.CString(string(jBytes)), len(jBytes) +} + type NextCall struct { content chan CallResponse } @@ -129,13 +154,6 @@ func Bolt_ListBuckets(b ObjectReference, browse []string) ObjectReference { return pNC_Ref } -const ( - ERROR_AND_STOP_CALLING int64 = 100 - ERROR_AND_KEEP_CALLING = 101 - FINISHED_OK = 102 - REAL_MESSAGE = 103 -) - //export GetNext func GetNext(oRef ObjectReference) (int64, *C.char, int) { pNC_Iface, ok := gms.Get(oRef) diff --git a/qbolt.h b/qbolt.h index 83962d6..2ef1c00 100644 --- a/qbolt.h +++ b/qbolt.h @@ -67,6 +67,15 @@ struct Bolt_Open_return { extern struct Bolt_Open_return Bolt_Open(GoString p0, GoUint32 p1, GoInt64 p2); +/* Return type for Bolt_DBStats */ +struct Bolt_DBStats_return { + GoInt64 r0; + char* r1; + GoInt r2; +}; + +extern struct Bolt_DBStats_return Bolt_DBStats(GoInt64 p0); + extern GoInt64 Bolt_ListBuckets(GoInt64 p0, GoSlice p1); /* Return type for GetNext */ diff --git a/qbolt/boltdb.cpp b/qbolt/boltdb.cpp index 5f9dbaf..86386d6 100644 --- a/qbolt/boltdb.cpp +++ b/qbolt/boltdb.cpp @@ -62,6 +62,28 @@ bool BoltDB::listBucketsAtRoot(QString& errorOut, NameReciever cb) return (errorOut.length() == 0); } +bool BoltDB::getStatsJSON(std::function onSuccess, std::function onError) +{ + auto statresp = Bolt_DBStats(this->gmsDbRef); + + if (statresp.r0 == ERROR_AND_STOP_CALLING) { + QString err = QString::fromUtf8(statresp.r1, statresp.r2); + free(statresp.r1); + onError(err); + return false; + + } else if (statresp.r0 == REAL_MESSAGE) { + QString json = QString::fromUtf8(statresp.r1, statresp.r2); + free(statresp.r1); + onSuccess(json); + return true; + + } else { + // ?? shouldn't be reachable + return false; + } +} + BoltDB::~BoltDB() { auto err = ::Bolt_Close(this->gmsDbRef); diff --git a/qbolt/boltdb.h b/qbolt/boltdb.h index 6ab2e20..9c8de3d 100644 --- a/qbolt/boltdb.h +++ b/qbolt/boltdb.h @@ -18,6 +18,8 @@ public: bool listBucketsAtRoot(QString& errorOut, NameReciever cb); + bool getStatsJSON(std::function onSuccess, std::function onError); + ~BoltDB(); }; diff --git a/qbolt/mainwindow.cpp b/qbolt/mainwindow.cpp index 38420de..20b011e 100644 --- a/qbolt/mainwindow.cpp +++ b/qbolt/mainwindow.cpp @@ -157,6 +157,26 @@ void MainWindow::on_bucketTree_currentItemChanged(QTreeWidgetItem *current, QTre } ui->tabWidget->setVisible(true); + + if (current->parent() == nullptr) { + // Selected a database + auto *bdb = GET_BDB(current); + ui->propertiesArea->clear(); + bdb->getStatsJSON([=](QString j) { + ui->propertiesArea->setPlainText(j); + }, [=](QString error) { + QMessageBox qmb; + qmb.setText(tr("Error retrieving database statistics: %1").arg(error)); + qmb.exec(); + }); + + } else { + // Selected a bucket + // Load the data tab + // TODO + + ui->propertiesArea->clear(); + } // refresh properties for the currently selected tree item... } diff --git a/qbolt/mainwindow.ui b/qbolt/mainwindow.ui index 360aca1..54c52d4 100644 --- a/qbolt/mainwindow.ui +++ b/qbolt/mainwindow.ui @@ -74,7 +74,7 @@ 3 - + QFrame::NoFrame