| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifndef MAZDB_H |
| 19 |
#define MAZDB_H |
| 20 |
|
| 21 |
#include <QObject> |
| 22 |
#include <QMutex> |
| 23 |
#include <functional> |
| 24 |
#include <QSharedPointer> |
| 25 |
#include <QVariant> |
| 26 |
#include <QString> |
| 27 |
#include <QJSValue> |
| 28 |
#include <QQmlParserStatus> |
| 29 |
#include "MazOptions.h" |
| 30 |
#include "MazUtils.h" |
| 31 |
|
| 32 |
namespace leveldb { |
| 33 |
class DB; |
| 34 |
class Status; |
| 35 |
} |
| 36 |
|
| 37 |
class MazDBBatch; |
| 38 |
|
| 39 |
typedef std::function<bool(QString, QVariant)> MazCallBack; |
| 40 |
typedef std::function<bool(QString)> MazKeyCallBack; |
| 41 |
typedef std::function<bool(QVariant)> MazValueCallBack; |
| 42 |
|
| 43 |
class MazDB : public QObject, public QQmlParserStatus |
| 44 |
{ |
| 45 |
Q_OBJECT |
| 46 |
Q_INTERFACES(QQmlParserStatus) |
| 47 |
Q_PROPERTY(QString source READ filename WRITE setFilename NOTIFY filenameChanged) |
| 48 |
Q_PROPERTY(bool opened READ opened NOTIFY openedChanged) |
| 49 |
Q_PROPERTY(Status status READ status NOTIFY statusChanged) |
| 50 |
Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged) |
| 51 |
Q_PROPERTY(MazOptions* options READ options CONSTANT) |
| 52 |
Q_ENUMS(Status) |
| 53 |
public: |
| 54 |
explicit MazDB(QObject *parent = 0); |
| 55 |
explicit MazDB(const QString &filename, QObject *parent = 0); |
| 56 |
~MazDB(); |
| 57 |
|
| 58 |
enum Status { |
| 59 |
Undefined = -1, |
| 60 |
Ok = 0, |
| 61 |
NotFound = 1, |
| 62 |
Corruption = 2, |
| 63 |
NotSupported = 3, |
| 64 |
InvalidArgument = 4, |
| 65 |
IOError = 5 |
| 66 |
}; |
| 67 |
|
| 68 |
QString filename() const; |
| 69 |
bool opened() const; |
| 70 |
Status open(); |
| 71 |
Q_INVOKABLE void close(); |
| 72 |
Status status() const; |
| 73 |
QString errorString() const; |
| 74 |
MazOptions *options(); |
| 75 |
|
| 76 |
Q_INVOKABLE MazDBBatch *batch(); |
| 77 |
Q_INVOKABLE bool del(const QString &key); |
| 78 |
Q_INVOKABLE QVariant get(const QString &key, QVariant defaultValue = QVariant()); |
| 79 |
Q_INVOKABLE bool put(const QString &key, const QVariant &value); |
| 80 |
Q_INVOKABLE bool putSync(const QString &key, const QVariant &value); |
| 81 |
Q_INVOKABLE bool destroyDB(const QString &filename); |
| 82 |
Q_INVOKABLE bool repairDB(const QString &filename); |
| 83 |
|
| 84 |
|
| 85 |
bool readStream(MazCallBack callback, const QString startKey = QString(), const int length = -1); |
| 86 |
Q_INVOKABLE bool readStream(QJSValue callback, QString startKey = QString(), int length = -1); |
| 87 |
bool keyStream(MazKeyCallBack callback, const QString startKey = QString(), const int length = -1); |
| 88 |
Q_INVOKABLE bool keyStream(QJSValue callback, QString startKey = QString(), int length = -1); |
| 89 |
bool valueStream(MazValueCallBack callback, const QString startKey = QString(), const int length = -1); |
| 90 |
Q_INVOKABLE bool valueStream(QJSValue callback, QString startKey = QString(), int length = -1); |
| 91 |
bool rangeStream(MazCallBack callback, const QString startKey, const QString endKey); |
| 92 |
Q_INVOKABLE bool rangeStream(QJSValue callback, QString startKey, const QString endKey); |
| 93 |
|
| 94 |
signals: |
| 95 |
void filenameChanged(); |
| 96 |
void openedChanged(); |
| 97 |
void statusChanged(Status status); |
| 98 |
void errorStringChanged(QString errorString); |
| 99 |
void keyValueChanged(QString key, QVariant value); |
| 100 |
void keyValueRemoved(QString key); |
| 101 |
|
| 102 |
public slots: |
| 103 |
void setFilename(const QString &name); |
| 104 |
|
| 105 |
protected: |
| 106 |
QSharedPointer<leveldb::DB> m_leveldb; |
| 107 |
virtual void classBegin() override; |
| 108 |
virtual void componentComplete() override; |
| 109 |
|
| 110 |
private slots: |
| 111 |
void onBatchWritten(QSet<QString> keys); |
| 112 |
private: |
| 113 |
void setStatus(const Status status); |
| 114 |
void setErrorString(const QString &errorString); |
| 115 |
Status parseStatusCode(leveldb::Status &status); |
| 116 |
void emitPropertyChanged(const QString &key, const QVariant &value); |
| 117 |
void emitKeyValueRemoved(const QString &key); |
| 118 |
|
| 119 |
QString m_filename; |
| 120 |
bool m_opened; |
| 121 |
Status m_status; |
| 122 |
QString m_errorString; |
| 123 |
MazOptions m_options; |
| 124 |
MazDBBatch *m_batch; |
| 125 |
QMutex m_mutex; |
| 126 |
}; |
| 127 |
|
| 128 |
#endif |