[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [jungle/mockPoc] /: Add a database which uses EJDB internally
From:       Vishesh Handa <me () vhanda ! in>
Date:       2014-10-01 0:18:33
Message-ID: E1XZ7cn-00045S-SH () scm ! kde ! org
[Download RAW message or body]

Git commit 8df74aeb3d1efb114e1cb5de5924f9545a2a0947 by Vishesh Handa.
Committed on 01/10/2014 at 00:32.
Pushed by vhanda into branch 'mockPoc'.

Add a database which uses EJDB internally

We could really use a nice wrapper over this database, like we have
qsql. For now this just stores QVariantMaps and retrieves them with the
id specified.

M  +2    -0    CMakeLists.txt
M  +8    -0    autotest/CMakeLists.txt
A  +57   -0    autotest/databasetest.cpp     [License: LGPL (v2.1+)]
A  +31   -0    cmake/FindEjdb.cmake
A  +186  -0    src/database.cpp     [License: LGPL (v2.1+)]
A  +47   -0    src/database.h     [License: LGPL (v2.1+)]
A  +32   -0    src/interfaces/databaseinterface.h     [License: LGPL (v2.1+)]

http://commits.kde.org/jungle/8df74aeb3d1efb114e1cb5de5924f9545a2a0947

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 32b453c..7c78e32 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -9,6 +9,7 @@ find_package(KF5 REQUIRED COMPONENTS I18n Service Baloo Config)
 find_package(TmdbQt5 REQUIRED)
 find_package(GTest REQUIRED)
 find_package(GMock REQUIRED)
+find_package(Ejdb REQUIRED)
 
 include(ECMAddTests)
 include(ECMSetupVersion)
@@ -22,6 +23,7 @@ include(KDECompilerSettings)
 
 include_directories(
   ${CMAKE_SOURCE_DIR}/src
+  ${EJDB_INCLUDE_DIRS}
 )
 
 add_subdirectory(src)
diff --git a/autotest/CMakeLists.txt b/autotest/CMakeLists.txt
index 03eac3f..a07662e 100644
--- a/autotest/CMakeLists.txt
+++ b/autotest/CMakeLists.txt
@@ -69,3 +69,11 @@ ecm_add_test(tvdbjobtest.cpp
     LINK_LIBRARIES ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES} Qt5::Test pthread
                    TmdbQt::TmdbQt5
 )
+
+ecm_add_test(databasetest.cpp
+    ../src/database.cpp
+
+    TEST_NAME "databaseTest"
+    LINK_LIBRARIES ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES} Qt5::Test pthread
+                   ${EJDB_LIBRARIES}
+)
diff --git a/autotest/databasetest.cpp b/autotest/databasetest.cpp
new file mode 100644
index 0000000..558d59f
--- /dev/null
+++ b/autotest/databasetest.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2014  Vishesh Handa <vhanda@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include "database.h"
+#include "shared.h"
+
+#include <QTest>
+#include <QVariantMap>
+#include <QDebug>
+#include <QTemporaryDir>
+
+class DatabaseTest : public QObject
+{
+    Q_OBJECT
+private Q_SLOTS:
+    void test();
+};
+
+using namespace Jungle;
+
+void DatabaseTest::test()
+{
+    QVariantMap data;
+    data["type"] = "episode";
+    data["mimetype"] = "video/mp4";
+    data["series"] = "Outlander";
+    data["episodeNumber"] = 5;
+
+    QTemporaryDir dir;
+    Database db(dir.path() + "/db");
+
+    QByteArray id = db.add(data);
+    QVariantMap output = db.fetch(id);
+
+    data["_id"] = QString::fromUtf8(id);
+    QCOMPARE(output, data);
+}
+
+QTEST_GMOCK_MAIN(DatabaseTest);
+
+#include "databasetest.moc"
diff --git a/cmake/FindEjdb.cmake b/cmake/FindEjdb.cmake
new file mode 100644
index 0000000..2d2cd70
--- /dev/null
+++ b/cmake/FindEjdb.cmake
@@ -0,0 +1,31 @@
+# - Try to find EJDB using pkg-config
+# Once done this will define
+#  EJDB_FOUND - System has EJDB
+#  EJDB_INCLUDE_DIRS - The EJDB include directories
+#  EJDB_LIBRARIES - The libraries needed to use EJDB
+#  EJDB_DEFINITIONS - Compiler switches required for using EJDB
+#  EJDB_STATIC_LIB - Determines whether or not to use static EJDB
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(PC_EJDB tcejdb)
+set(EJDB_DEFINITIONS ${PC_EJDB_CFLAGS_OTHER})
+
+IF(EJDB_STATIC_LIB)
+    SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
+ENDIF(EJDB_STATIC_LIB)
+
+find_path(EJDB_INCLUDE_DIR tcejdb/ejdb.h
+          HINTS ${PC_EJDB_INCLUDEDIR} ${PC_EJDB_INCLUDE_DIRS})
+
+find_library(EJDB_LIBRARY NAMES tcejdb
+             HINTS ${PC_EJDB_LIBDIR} ${PC_EJDB_LIBRARY_DIRS} ${EJDB_LIBRARY_DIR})
+
+set(EJDB_LIBRARIES ${EJDB_LIBRARY})
+set(EJDB_INCLUDE_DIRS ${EJDB_INCLUDE_DIR})
+
+include(FindPackageHandleStandardArgs)
+# handle the QUIETLY and REQUIRED arguments and set EJDB_FOUND to TRUE
+# if all listed variables are TRUE
+find_package_handle_standard_args(EJDB DEFAULT_MSG
+                                  EJDB_LIBRARY EJDB_INCLUDE_DIR)
+
+mark_as_advanced(EJDB_INCLUDE_DIR EJDB_LIBRARY)
diff --git a/src/database.cpp b/src/database.cpp
new file mode 100644
index 0000000..33cec53
--- /dev/null
+++ b/src/database.cpp
@@ -0,0 +1,186 @@
+/*
+ * <one line to give the library's name and an idea of what it does.>
+ * Copyright (C) 2014  Vishesh Handa <me@vhanda.in>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include "database.h"
+
+#include <QDateTime>
+#include <QFile>
+#include <QDebug>
+
+#include <QJsonDocument>
+#include <QJsonObject>
+
+using namespace Jungle;
+
+Database::Database(const QString& dbPath)
+{
+    m_jdb = ejdbnew();
+    if (!ejdbopen(m_jdb, QFile::encodeName(dbPath).constData(), JBOWRITER | JBOCREAT)) {
+        qDebug() << "Could not open db" << dbPath;
+    }
+    m_coll = ejdbcreatecoll(m_jdb, "default", 0);
+}
+
+Database::~Database()
+{
+    ejdbclose(m_jdb);
+    ejdbdel(m_jdb);
+}
+
+/*
+static void addVariant(bson &rec, const QVariant& variant)
+{
+    switch (variant.type()) {
+        case QVariant::Double:
+
+            return BSON_DOUBLE;
+        case QVariant::String:
+            return BSON_STRING;
+        case QVariant::Map:
+            return BSON_OBJECT;
+        case QVariant::List:
+            return BSON_ARRAY;
+        case QVariant::Bool:
+            return BSON_BOOL;
+        case QVariant::Date:
+            return BSON_DATE;
+        case QVariant::RegExp:
+            return BSON_REGEX;
+        case QVariant::Int:
+            return BSON_INT;
+        case QVariant::DateTime:
+            return BSON_TIMESTAMP;
+        case QVariant::LongLong:
+            return BSON_LONG;
+    }
+
+    return BSON_EOO;
+}
+*/
+
+QByteArray Database::add(const QVariantMap& map)
+{
+    bson rec;
+    bson_init(&rec);
+
+    QMapIterator<QString, QVariant> it(map);
+    while (it.hasNext()) {
+        it.next();
+
+        const QByteArray key = it.key().toUtf8();
+        const QVariant &var = it.value();
+
+        switch (var.type()) {
+            case QVariant::Double: {
+                bson_append_double(&rec, key.constData(), var.toDouble());
+                break;
+            }
+
+            case QVariant::String: {
+                QByteArray val = var.toString().toUtf8();
+                bson_append_string(&rec, key.constData(), val.constData());
+                break;
+            }
+
+            case QVariant::Map: {
+                Q_ASSERT(0);
+            }
+
+            case QVariant::List: {
+                Q_ASSERT(0);
+            }
+
+            case QVariant::Bool: {
+                bson_append_bool(&rec, key.constData(), var.toBool());
+                break;
+            }
+
+            case QVariant::Date: {
+                bson_date_t date = var.toDateTime().toTime_t();
+                bson_append_date(&rec, key.constData(), date);
+                break;
+            }
+
+            case QVariant::RegExp: {
+                Q_ASSERT(0);
+            }
+
+            case QVariant::Int: {
+                bson_append_int(&rec, key.constData(), var.toInt());
+                break;
+            }
+
+            case QVariant::DateTime: {
+                bson_date_t date = var.toDateTime().toTime_t();
+                bson_append_time_t(&rec, key.constData(), date);
+                break;
+            }
+
+            case QVariant::LongLong: {
+                // FIXME: Data is being lost?
+                bson_append_long(&rec, key.constData(), var.toLongLong());
+                break;
+            }
+
+            default: {
+                Q_ASSERT(0);
+            }
+        }
+    }
+
+    bson_finish(&rec);
+
+    bson_oid_t oid;
+    ejdbsavebson(m_coll, &rec, &oid);
+
+    char str[26];
+    bson_oid_to_string(&oid, str);
+    QByteArray id(str);
+
+    bson_destroy(&rec);
+
+    return id;
+}
+
+QVariantMap Database::fetch(const QByteArray& id)
+{
+    bson_oid_t oid;
+    bson_oid_from_string(&oid, id.constData());
+
+    bson* rec = ejdbloadbson(m_coll, &oid);
+    if (!rec) {
+        qDebug() << "Record not found" << id;
+        return QVariantMap();
+    }
+
+    char* buf;
+    int length;
+
+    bson2json(bson_data(rec), &buf, &length);
+
+    QByteArray arr = QByteArray::fromRawData(buf, length);
+    QJsonDocument doc = QJsonDocument::fromJson(arr);
+    QVariantMap map = doc.object().toVariantMap();
+
+    free(buf);
+    bson_del(rec);
+
+    return map;
+}
diff --git a/src/database.h b/src/database.h
new file mode 100644
index 0000000..f410831
--- /dev/null
+++ b/src/database.h
@@ -0,0 +1,47 @@
+/*
+ * <one line to give the library's name and an idea of what it does.>
+ * Copyright (C) 2014  Vishesh Handa <me@vhanda.in>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef JUNGLE_DATABASE_H
+#define JUNGLE_DATABASE_H
+
+#include "interfaces/databaseinterface.h"
+
+#include <tcejdb/ejdb.h>
+
+namespace Jungle {
+
+class Database : public DatabaseInterface
+{
+public:
+    Database(const QString& dbPath);
+    virtual ~Database();
+
+    QByteArray add(const QVariantMap& map);
+
+    QVariantMap fetch(const QByteArray& id);
+
+private:
+    EJDB* m_jdb;
+    EJCOLL* m_coll;
+
+};
+}
+
+#endif // JUNGLE_DATABASE_H
diff --git a/src/interfaces/databaseinterface.h b/src/interfaces/databaseinterface.h
new file mode 100644
index 0000000..23433e0
--- /dev/null
+++ b/src/interfaces/databaseinterface.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2014  Vishesh Handa <me@vhanda.in>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef DATABASE_INTERFACE_H
+#define DATABASE_INTERFACE_H
+
+#include <QVariantMap>
+
+class DatabaseInterface
+{
+public:
+    virtual ~DatabaseInterface() {}
+    virtual QByteArray add(const QVariantMap& map) = 0;
+};
+
+#endif
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic