← Back to team overview

uonedb-qt team mailing list archive

[Merge] lp:~3v1n0/u1db-qt/objects-subfields-list-all into lp:u1db-qt

 

Marco Trevisan (Treviño) has proposed merging lp:~3v1n0/u1db-qt/objects-subfields-list-all into lp:u1db-qt with lp:~3v1n0/u1db-qt/uri-path-parsing as a prerequisite.

Commit message:
Query: allow adding more than one result for each doc, allowing indexing subfields of objects in a list

This is to be conformant to what the u1db specs (implemented in python) say.

Requested reviews:
  U1DB Qt developers (uonedb-qt)
Related bugs:
  Bug #1322156 in U1DB Qt/ QML: "Using query across different levels doesn't function"
  https://bugs.launchpad.net/u1db-qt/+bug/1322156

For more details, see:
https://code.launchpad.net/~3v1n0/u1db-qt/objects-subfields-list-all/+merge/252059

Indexing subfields of ojects in a list doesn't work as expected in u1db-qt, we only list the first element and not everyone as defined in docs [1].

The reason was that we didn't add a result if we already had one for that doc... And this seems wrong to me.

In a next MP we should probably also fix the "Name a list" case (see [1] again).

[1] https://pythonhosted.org/u1db/high-level-api.html#index-expressions
-- 
Your team U1DB Qt developers is requested to review the proposed merge of lp:~3v1n0/u1db-qt/objects-subfields-list-all into lp:u1db-qt.
=== added directory 'examples/u1db-qt-example-7'
=== added file 'examples/u1db-qt-example-7/u1db-qt-example-7.qml'
--- examples/u1db-qt-example-7/u1db-qt-example-7.qml	1970-01-01 00:00:00 +0000
+++ examples/u1db-qt-example-7/u1db-qt-example-7.qml	2015-03-06 05:17:53 +0000
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2015 Canonical, Ltd.
+ *
+ * Authors:
+ *  Marco Trevisan <marco.trevisan@xxxxxxxxxxxxx>
+ *
+ * This program 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; version 3.
+ *
+ * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import QtQuick 2.0
+import U1db 1.0 as U1db
+import Ubuntu.Components 0.1
+
+MainView {
+    width: units.gu(45)
+    height: units.gu(80)
+
+    U1db.Database {
+        id: aDatabase
+    }
+
+   U1db.Document {
+        database: aDatabase
+        docId: "department"
+        contents: {"department": "department of redundancy department",
+                   "managers": [
+                        {"name": "Mary", "phone_number": "12345"},
+                        {"name": "Katherine"},
+                        {"name": "Rob", "phone_number": "54321"}
+                    ]
+                  }
+  }
+
+   U1db.Index{
+       database: aDatabase
+       id: by_phonenumber
+       expression: ["managers.phone_number"]
+   }
+
+   U1db.Query{
+       id: aQuery
+       index: by_phonenumber
+   }
+
+    Tabs {
+        id: tabs
+
+        Tab {
+            title: i18n.tr("Hello U1Db!")
+
+            page: Page {
+                id: helloPage
+
+                ListView {
+                    anchors.fill: parent
+                    model: aQuery
+                    delegate: Text {
+                        text: "(" + index + ") " + JSON.stringify(contents)
+                    }
+                }
+            }
+        }
+    }
+}
+

=== modified file 'src/query.cpp'
--- src/query.cpp	2014-03-13 19:31:58 +0000
+++ src/query.cpp	2015-03-06 05:17:53 +0000
@@ -172,9 +172,11 @@
 
         if(match == true){
             // Results must be unique and not empty aka deleted
-            if (!m_documents.contains(docId) && result_variant.isValid())
+            if (result_variant.isValid())
             {
-                m_documents.append(docId);
+                if (!m_documents.contains(docId))
+                    m_documents.append(docId);
+
                 m_results.append(result);
             }
         }

=== modified file 'tests/test-database.cpp'
--- tests/test-database.cpp	2015-03-06 05:17:53 +0000
+++ tests/test-database.cpp	2015-03-06 05:17:53 +0000
@@ -21,6 +21,7 @@
 #include <QObject>
 
 #include "database.h"
+#include "document.h"
 #include "index.h"
 #include "query.h"
 
@@ -110,6 +111,46 @@
         query.setQuery(QStringList() << "2014*" << "basketball" << "linux");
     }
 
+    void testSingleDocumentQuery()
+    {
+        const char * json = "{\"department\": \"department of redundancy department\"," \
+                            " \"managers\": [" \
+                            "    {\"name\": \"Mary\", \"phone_number\": \"12345\"}," \
+                            "    {\"name\": \"Katherine\"}," \
+                            "    {\"name\": \"Rob\", \"phone_number\": [\"54321\"]}" \
+                            "  ]" \
+                            "}";
+        Database db;
+        Document doc;
+        doc.setDocId("department");
+        doc.setDatabase(&db);
+        doc.setContents(QJsonDocument::fromJson(QByteArray(json)).toVariant());
+
+        Index index;
+        index.setDatabase(&db);
+        index.setName("by-phone-number");
+        index.setExpression(QStringList() << "managers.phone_number");
+
+        Query query;
+        query.setIndex(&index);
+
+        QCOMPARE(query.getDocuments().size(), 1);
+        QCOMPARE(query.getDocuments().front(), QString("department"));
+
+        QList<QVariant> expected_numbers;
+        Q_FOREACH (QVariant manager, doc.getContents().toMap()["managers"].toList())
+        {
+            QVariantMap man = manager.toMap();
+            if (man.keys().contains("phone_number"))
+            {
+                man.remove("name");
+                expected_numbers.append(man);
+            }
+        }
+
+        QCOMPARE(query.getResults(), expected_numbers);
+    }
+
     void cleanupTestCase()
     {
     }


Follow ups