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

List:       kde-commits
Subject:    [baloo] /: Fix several issue of mtime related search.
From:       Weng Xuetian <wengxt () gmail ! com>
Date:       2015-12-15 5:28:28
Message-ID: E1a8iA0-0007mw-CP () scm ! kde ! org
[Download RAW message or body]

Git commit 83fda1c75ff1a6b73b582e2bd05bfb9c70b49afa by Weng Xuetian.
Committed on 15/12/2015 at 05:28.
Pushed by xuetianweng into branch 'master'.

Fix several issue of mtime related search.

1. If it is an and search, the search result from mtime is not sorted
nor deduplicated, so result may be incomplete or contain duplicate
result. mtimedb may contains multiple mtime of same file.
2. MtimeDB::get should uses quint32 as parameter instead of quint64,
otherwise &mtime may be wrong when passed as search parameter as
quint32.
3. MtimeDB::get didn't call with SET_RANGE thus it always return result
for first key.

REVIEW: 126227

M  +35   -0    autotests/unit/engine/mtimedbtest.cpp
M  +9    -2    src/engine/mtimedb.cpp
M  +1    -1    src/engine/mtimedb.h

http://commits.kde.org/baloo/83fda1c75ff1a6b73b582e2bd05bfb9c70b49afa

diff --git a/autotests/unit/engine/mtimedbtest.cpp b/autotests/unit/engine/mtimedbtest.cpp
index e38022d..921e402 100644
--- a/autotests/unit/engine/mtimedbtest.cpp
+++ b/autotests/unit/engine/mtimedbtest.cpp
@@ -97,6 +97,41 @@ private Q_SLOTS:
             QCOMPARE(it->docId(), static_cast<quint64>(val));
         }
     }
+
+    void testSortedAndUnique()
+    {
+        MTimeDB db(MTimeDB::create(m_txn), m_txn);
+
+        db.put(5, 1);
+        db.put(6, 4);
+        db.put(6, 2);
+        db.put(6, 3);
+        db.put(7, 3);
+
+        QCOMPARE(db.get(6), QVector<quint64>() << 2 << 3 << 4);
+
+        PostingIterator* it = db.iterRange(5, 7);
+        QVERIFY(it);
+
+        {
+            QVector<quint64> result = {1, 2, 3, 4};
+            for (quint64 val : result) {
+                QCOMPARE(it->next(), static_cast<quint64>(val));
+                QCOMPARE(it->docId(), static_cast<quint64>(val));
+            }
+        }
+
+        {
+            it = db.iter(6, MTimeDB::GreaterEqual);
+            QVERIFY(it);
+
+            QVector<quint64> result = {2, 3, 4};
+            for (quint64 val : result) {
+                QCOMPARE(it->next(), static_cast<quint64>(val));
+                QCOMPARE(it->docId(), static_cast<quint64>(val));
+            }
+        }
+    }
 };
 
 QTEST_MAIN(MTimeDBTest)
diff --git a/src/engine/mtimedb.cpp b/src/engine/mtimedb.cpp
index f7283b5..a248d5a 100644
--- a/src/engine/mtimedb.cpp
+++ b/src/engine/mtimedb.cpp
@@ -20,6 +20,7 @@
 
 #include "mtimedb.h"
 #include "vectorpostingiterator.h"
+#include <algorithm>
 
 using namespace Baloo;
 
@@ -73,7 +74,7 @@ void MTimeDB::put(quint32 mtime, quint64 docId)
     Q_ASSERT_X(rc == 0, "MTimeDB::put", mdb_strerror(rc));
 }
 
-QVector<quint64> MTimeDB::get(quint64 mtime)
+QVector<quint64> MTimeDB::get(quint32 mtime)
 {
     Q_ASSERT(mtime > 0);
 
@@ -87,7 +88,7 @@ QVector<quint64> MTimeDB::get(quint64 mtime)
     mdb_cursor_open(m_txn, m_dbi, &cursor);
 
     MDB_val val;
-    int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT);
+    int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE);
     if (rc == MDB_NOTFOUND) {
         mdb_cursor_close(cursor);
         return values;
@@ -107,6 +108,8 @@ QVector<quint64> MTimeDB::get(quint64 mtime)
     }
 
     mdb_cursor_close(cursor);
+    std::sort(values.begin(), values.end());
+    values.erase(std::unique(values.begin(), values.end()), values.end());
     return values;
 }
 
@@ -183,6 +186,8 @@ PostingIterator* MTimeDB::iter(quint32 mtime, MTimeDB::Comparator com)
     }
 
     mdb_cursor_close(cursor);
+    std::sort(results.begin(), results.end());
+    results.erase(std::unique(results.begin(), results.end()), results.end());
     return new VectorPostingIterator(results);
 }
 
@@ -224,6 +229,8 @@ PostingIterator* MTimeDB::iterRange(quint32 beginTime, quint32 endTime)
     }
 
     mdb_cursor_close(cursor);
+    std::sort(results.begin(), results.end());
+    results.erase(std::unique(results.begin(), results.end()), results.end());
     return new VectorPostingIterator(results);
 }
 
diff --git a/src/engine/mtimedb.h b/src/engine/mtimedb.h
index 1dc1cb9..327498f 100644
--- a/src/engine/mtimedb.h
+++ b/src/engine/mtimedb.h
@@ -44,7 +44,7 @@ public:
     static MDB_dbi open(MDB_txn* txn);
 
     void put(quint32 mtime, quint64 docId);
-    QVector<quint64> get(quint64 mtime);
+    QVector<quint64> get(quint32 mtime);
 
     void del(quint32 mtime, quint64 docId);
 
[prev in list] [next in list] [prev in thread] [next in thread] 

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