diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-02-16 22:19:44 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-03-01 10:21:44 -0800 |
commit | 6434ad262887a473abd410fd8011bba1291663d6 (patch) | |
tree | 6d0fab7c898c7daf0ab2fe53b2977d94cef60403 /core/fulltext.h | |
parent | 41aae1aebe1aea4db58d61a171f7ecd76b6282d7 (diff) | |
download | subsurface-6434ad262887a473abd410fd8011bba1291663d6.tar.gz |
filter: add fulltext filtering code
Add code that indexes all words of a dive and provides searching
for words.
A query is represented by the FullTextQuery class, which can be
initialized by assigning a string to it. It is basically a list
of words.
The result of a search is stored in the FullTextResult class,
which is a list of dives.
The actual indexing and searching is implemented in the FullText
class. However, this class is not exported because the interface
is partially accessible to C. Notably, the reloading of the
fulltext index is done from the C core.
Currently, the indexing and searching is totally unoptimized.
In a ~4000 dives test-log searches typically took single-digit
ms times. There is ample room for optimization (e.g. when
searching for multiple words, chose the words with few dives
first and when down to a few dives, check them individually).
The words of each dive are tokenized and uppercased and
cached with the dive. A pointer to these words is stashed
in the dive structure.
For now, compile only on desktop.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/fulltext.h')
-rw-r--r-- | core/fulltext.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/core/fulltext.h b/core/fulltext.h new file mode 100644 index 000000000..c22a118b8 --- /dev/null +++ b/core/fulltext.h @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 + +// A class for performing full text searches on dives. +// Search is case-insensitive. Even though QString has many design +// issues such as COW semantics and UTF-16 encoding, it provides +// platform independence and reasonable performance. Therefore, +// this is based in QString instead of std::string. +// +// To make this accessible from C, this does manual memory management: +// Every dive is associated with a cache of words. Thus, when deleting +// a dive, a function freeing that data has to be called. + +#ifndef FULLTEXT_H +#define FULLTEXT_H + +// For now only compile on desktop +#ifndef SUBSURFACE_MOBILE + +// 1) The C-accessible interface + +#ifdef __cplusplus +extern "C" { +#endif + +struct full_text_cache; +struct dive; +void fulltext_register(struct dive *d); // Note: can be called repeatedly +void fulltext_unregister(struct dive *d); // Note: can be called repeatedly +void fulltext_unregister_all(); // Unregisters all dives in the dive table +void fulltext_reload(); // Registers all dives in the dive table + +#ifdef __cplusplus +} +#endif + +// 2) The C++-only interface +#ifdef __cplusplus + +#include <QString> +#include <vector> + +enum class StringFilterMode { + SUBSTRING = 0, + STARTSWITH = 1, + EXACT = 2 +}; + +// A fulltext query. Basically a list of normalized words we search for +struct FullTextQuery { + std::vector<QString> words; + FullTextQuery &operator=(const QString &); // Initialize by assigning a user-provided search string + bool doit() const; // true if we should to a fulltext search +}; + +// Describes the result of a fulltext search +struct FullTextResult { + std::vector<dive *> dives; + bool dive_matches(const struct dive *d) const; +}; + +// Two search modes: +// 1) Find all dives matching the query. +// 2) Test if a given dive matches the query. +FullTextResult fulltext_find_dives(const FullTextQuery &q, StringFilterMode); +bool fulltext_dive_matches(const struct dive *d, const FullTextQuery &q, StringFilterMode); + +#endif +#endif // SUBSURFACE_MOBILE +#endif |