69 lines
1.7 KiB
C++
69 lines
1.7 KiB
C++
#include "boltdb.h"
|
|
|
|
BoltDB::BoltDB()
|
|
{
|
|
|
|
}
|
|
|
|
BoltDB* BoltDB::createFrom(QString filePath, QString &errorOut)
|
|
{
|
|
auto opts = ::Bolt_Options_New_Readonly();
|
|
|
|
QByteArray filePathBytes(filePath.toUtf8());
|
|
GoString filePathGS = Interop::toGoString_WeakRef(&filePathBytes);
|
|
|
|
auto open_ret = ::Bolt_Open(filePathGS, 0444, opts);
|
|
if (open_ret.r1.n != 0) {
|
|
errorOut = QString(open_ret.r1.p);
|
|
return nullptr;
|
|
}
|
|
|
|
BoltDB *ret = new BoltDB();
|
|
ret->gmsDbRef = open_ret.r0;
|
|
return ret;
|
|
}
|
|
|
|
static const int ERROR_AND_STOP_CALLING = 100;
|
|
static const int ERROR_AND_KEEP_CALLING = 101;
|
|
static const int FINISHED_OK = 102;
|
|
static const int REAL_MESSAGE = 103;
|
|
|
|
bool BoltDB::listBucketsAtRoot(QString& errorOut, NameReciever cb)
|
|
{
|
|
auto listJob = ::Bolt_ListBucketsAtRoot(this->gmsDbRef);
|
|
|
|
errorOut.clear();
|
|
|
|
for(;;) {
|
|
auto gnr = ::GetNext(listJob);
|
|
|
|
if (gnr.r0 == ERROR_AND_STOP_CALLING) {
|
|
errorOut.append(QString(gnr.r1.p)); // log error
|
|
break; // done
|
|
|
|
} else if (gnr.r0 == ERROR_AND_KEEP_CALLING) {
|
|
errorOut.append(QString(gnr.r1.p)); // log error
|
|
continue;
|
|
|
|
} else if (gnr.r0 == FINISHED_OK) {
|
|
// Once we hit this, the go-side will clean up the channel / associated goroutines
|
|
break;
|
|
|
|
} else if (gnr.r0 == REAL_MESSAGE) {
|
|
cb(QByteArray(gnr.r1.p, gnr.r1.n));
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return (errorOut.length() == 0);
|
|
}
|
|
|
|
BoltDB::~BoltDB()
|
|
{
|
|
GoString err = ::Bolt_Close(this->gmsDbRef);
|
|
if (err.n != 0) {
|
|
// Error closing database!
|
|
// Need to display an alert... somewhere
|
|
}
|
|
}
|