c++ wrappers for bucket enumeration function

This commit is contained in:
mappu 2017-05-20 15:47:18 +12:00
parent e82d0a5dd2
commit 8a93f163c4
5 changed files with 45 additions and 10 deletions

10
main.go
View File

@ -69,9 +69,9 @@ func withBoltDBReference(b ObjectReference, fn func(db *bolt.DB) error) error {
}
//export Bolt_ListBuckets
func Bolt_ListBuckets(b ObjectReference, browse []string, withEach func(string)) string {
func Bolt_ListBuckets(b ObjectReference, browse []string, ctx uintptr, withEach func(uintptr, string)) string {
if len(browse) == 0 {
return Bolt_ListBucketsAtRoot(b, withEach)
return Bolt_ListBucketsAtRoot(b, ctx, withEach)
}
err := withBoltDBReference(b, func(db *bolt.DB) error {
@ -91,7 +91,7 @@ func Bolt_ListBuckets(b ObjectReference, browse []string, withEach func(string))
return bucket.ForEach(func(k, v []byte) error {
withEach(string(k))
withEach(ctx, string(k))
return nil
})
@ -106,13 +106,13 @@ func Bolt_ListBuckets(b ObjectReference, browse []string, withEach func(string))
}
//export Bolt_ListBucketsAtRoot
func Bolt_ListBucketsAtRoot(b ObjectReference, withEach func(string)) string {
func Bolt_ListBucketsAtRoot(b ObjectReference, ctx uintptr, withEach func(uintptr, string)) string {
err := withBoltDBReference(b, func(db *bolt.DB) error {
return db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(n []byte, bucket *bolt.Bucket) error {
withEach(string(n))
withEach(ctx, string(n))
return nil
})

View File

@ -68,9 +68,9 @@ struct Bolt_Open_return {
extern struct Bolt_Open_return Bolt_Open(GoString p0, GoUint32 p1, GoInt64 p2);
extern GoString Bolt_ListBuckets(GoInt64 p0, GoSlice p1, void* p2);
extern GoString Bolt_ListBuckets(GoInt64 p0, GoSlice p1, GoUintptr p2, void* p3);
extern GoString Bolt_ListBucketsAtRoot(GoInt64 p0, void* p1);
extern GoString Bolt_ListBucketsAtRoot(GoInt64 p0, GoUintptr p1, void* p2);
extern GoString Bolt_Close(GoInt64 p0);

View File

@ -7,12 +7,12 @@ BoltDB::BoltDB()
BoltDB* BoltDB::createFrom(QString filePath, QString &errorOut)
{
auto opts = Bolt_Options_New_Readonly();
auto opts = ::Bolt_Options_New_Readonly();
QByteArray filePathBytes(filePath.toUtf8());
GoString filePathGS = Interop::toGoString_WeakRef(&filePathBytes);
auto open_ret = Bolt_Open(filePathGS, 0444, opts);
auto open_ret = ::Bolt_Open(filePathGS, 0444, opts);
if (open_ret.r1.n != 0) {
errorOut = QString(open_ret.r1.p);
return nullptr;
@ -23,9 +23,37 @@ BoltDB* BoltDB::createFrom(QString filePath, QString &errorOut)
return ret;
}
template<typename T>
struct Wrapper {
T t;
};
static void byteArrayCallback(void* ctx, GoString text) {
auto fn = static_cast<Wrapper<NameReciever>*>(ctx);
fn->t(QByteArray::fromRawData(text.p, text.n));
}
bool BoltDB::listBucketsAtRoot(QString& errorOut, NameReciever cb)
{
auto containerVar = Wrapper<NameReciever>{cb};
GoString err = ::Bolt_ListBucketsAtRoot(
this->gmsDbRef,
(GoUintptr)(&containerVar),
byteArrayCallback // -fpermissive
);
if (err.n > 0) {
errorOut = QString(err.p);
return false;
}
return true;
}
BoltDB::~BoltDB()
{
GoString err = Bolt_Close(this->gmsDbRef);
GoString err = ::Bolt_Close(this->gmsDbRef);
if (err.n != 0) {
// Error closing database!
// Need to display an alert... somewhere

View File

@ -2,6 +2,9 @@
#define BOLTDB_H
#include "interop.h"
#include <functional>
typedef std::function<void(QByteArray)> NameReciever;
class BoltDB
{
@ -13,6 +16,8 @@ protected:
public:
static BoltDB* createFrom(QString filePath, QString &errorOut);
bool listBucketsAtRoot(QString& errorOut, NameReciever cb);
~BoltDB();
};

View File

@ -20,6 +20,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
#QMAKE_LFLAGS += ../qbolt.so
QMAKE_LIBS += ../qbolt.a
QMAKE_CXXFLAGS += -fpermissive # needed for some CGO function pointer interop where the header only specifices void*
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.