2023-05-27 04:12:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Maneesh P M <manu.pm55@gmail.com>
|
|
|
|
* Copyright (C) 2017-2021 Matthieu Gautier <mgautier@kymeria.fr>
|
|
|
|
* Copyright (C) 2007 Tommi Maekitalo
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
|
|
|
|
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
|
|
|
|
* NON-INFRINGEMENT. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZIM_SUGGESTION_H
|
|
|
|
#define ZIM_SUGGESTION_H
|
|
|
|
|
|
|
|
#include "suggestion_iterator.h"
|
|
|
|
#include "archive.h"
|
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
#if defined(LIBZIM_WITH_XAPIAN)
|
2023-05-27 04:12:16 +00:00
|
|
|
namespace Xapian {
|
2024-04-06 06:20:56 +00:00
|
|
|
class Enquire;
|
|
|
|
class MSet;
|
|
|
|
};
|
2023-05-27 04:12:16 +00:00
|
|
|
#endif
|
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
namespace zim
|
|
|
|
{
|
2023-05-27 04:12:16 +00:00
|
|
|
|
|
|
|
class SuggestionSearcher;
|
|
|
|
class SuggestionSearch;
|
|
|
|
class SuggestionIterator;
|
|
|
|
class SuggestionDataBase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A SuggestionSearcher is a object suggesting over titles of an Archive
|
|
|
|
*
|
|
|
|
* A SuggestionSearcher is mainly used to create new `SuggestionSearch`
|
|
|
|
* Internaly, this is a wrapper around a SuggestionDataBase with may or may not
|
|
|
|
* include a Xapian index.
|
|
|
|
*
|
|
|
|
* You should consider that all search operations are NOT threadsafe.
|
|
|
|
* It is up to you to protect your calls to avoid race competition.
|
|
|
|
* However, SuggestionSearcher (and subsequent classes) do not maintain a global/
|
|
|
|
* share state You can create several Searchers and use them in different threads.
|
|
|
|
*/
|
|
|
|
class LIBZIM_API SuggestionSearcher
|
|
|
|
{
|
2024-04-06 06:20:56 +00:00
|
|
|
public:
|
|
|
|
/** SuggestionSearcher constructor.
|
2023-05-27 04:12:16 +00:00
|
|
|
*
|
|
|
|
* Construct a SuggestionSearcher on top of an archive.
|
|
|
|
*
|
|
|
|
* @param archive An archive to suggest on.
|
|
|
|
*/
|
2024-04-06 06:20:56 +00:00
|
|
|
explicit SuggestionSearcher(const Archive& archive);
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
SuggestionSearcher(const SuggestionSearcher& other);
|
|
|
|
SuggestionSearcher& operator=(const SuggestionSearcher& other);
|
|
|
|
SuggestionSearcher(SuggestionSearcher&& other);
|
|
|
|
SuggestionSearcher& operator=(SuggestionSearcher&& other);
|
|
|
|
~SuggestionSearcher();
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
/** Create a SuggestionSearch for a specific query.
|
2023-05-27 04:12:16 +00:00
|
|
|
*
|
|
|
|
* The search is made on the archive under the SuggestionSearcher.
|
|
|
|
*
|
|
|
|
* @param query The SuggestionQuery to search.
|
|
|
|
*/
|
2024-04-06 06:20:56 +00:00
|
|
|
SuggestionSearch suggest(const std::string& query);
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
/** Set the verbosity of search operations.
|
2023-05-27 04:12:16 +00:00
|
|
|
*
|
|
|
|
* @param verbose The verbose mode to set
|
|
|
|
*/
|
2024-04-06 06:20:56 +00:00
|
|
|
void setVerbose(bool verbose);
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
private: // methods
|
|
|
|
void initDatabase();
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
private: // data
|
|
|
|
std::shared_ptr<SuggestionDataBase> mp_internalDb;
|
|
|
|
Archive m_archive;
|
|
|
|
bool m_verbose;
|
2023-05-27 04:12:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A SuggestionSearch represent a particular suggestion search, based on a `SuggestionSearcher`.
|
|
|
|
*/
|
|
|
|
class LIBZIM_API SuggestionSearch
|
|
|
|
{
|
2024-04-06 06:20:56 +00:00
|
|
|
public:
|
|
|
|
SuggestionSearch(SuggestionSearch&& s);
|
|
|
|
SuggestionSearch& operator=(SuggestionSearch&& s);
|
|
|
|
~SuggestionSearch();
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
/** Get a set of results for this search.
|
2023-05-27 04:12:16 +00:00
|
|
|
*
|
|
|
|
* @param start The begining of the range to get
|
|
|
|
* (offset of the first result).
|
|
|
|
* @param maxResults The maximum number of results to return
|
|
|
|
* (offset of last result from the start of range).
|
|
|
|
*/
|
2024-04-06 06:20:56 +00:00
|
|
|
const SuggestionResultSet getResults(int start, int maxResults) const;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
/** Get the number of estimated results for this suggestion search.
|
2023-05-27 04:12:16 +00:00
|
|
|
*
|
|
|
|
* As the name suggest, it is a estimation of the number of results.
|
|
|
|
*/
|
2024-04-06 06:20:56 +00:00
|
|
|
int getEstimatedMatches() const;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
private: // methods
|
|
|
|
SuggestionSearch(std::shared_ptr<SuggestionDataBase> p_internalDb, const std::string& query);
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
private: // data
|
|
|
|
std::shared_ptr<SuggestionDataBase> mp_internalDb;
|
|
|
|
std::string m_query;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
|
|
|
friend class SuggestionSearcher;
|
|
|
|
|
|
|
|
#ifdef ZIM_PRIVATE
|
2024-04-06 06:20:56 +00:00
|
|
|
public:
|
|
|
|
// Close Xapian db to force range based search
|
|
|
|
const void forceRangeSuggestion();
|
2023-05-27 04:12:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Xapian based methods and data
|
2024-04-06 06:20:56 +00:00
|
|
|
#if defined(LIBZIM_WITH_XAPIAN)
|
|
|
|
private: // Xapian based methods
|
|
|
|
Xapian::Enquire& getEnquire() const;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
private: // Xapian based data
|
|
|
|
mutable std::unique_ptr<Xapian::Enquire> mp_enquire;
|
|
|
|
#endif // LIBZIM_WITH_XAPIAN
|
2023-05-27 04:12:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `SuggestionResultSet` represent a range of results corresponding to a `SuggestionSearch`.
|
|
|
|
*
|
|
|
|
* It mainly allows to get a iterator either based on an MSetIterator or a RangeIterator.
|
|
|
|
*/
|
|
|
|
class LIBZIM_API SuggestionResultSet
|
|
|
|
{
|
2024-04-06 06:20:56 +00:00
|
|
|
public:
|
|
|
|
typedef SuggestionIterator iterator;
|
|
|
|
typedef Archive::EntryRange<EntryOrder::titleOrder> EntryRange;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
/** The begin iterator on the result range. */
|
|
|
|
iterator begin() const;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
/** The end iterator on the result range. */
|
|
|
|
iterator end() const;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
/** The size of the SearchResult (end()-begin()) */
|
|
|
|
int size() const;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
private: // data
|
|
|
|
std::shared_ptr<SuggestionDataBase> mp_internalDb;
|
|
|
|
std::shared_ptr<EntryRange> mp_entryRange;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
private:
|
|
|
|
SuggestionResultSet(EntryRange entryRange);
|
2023-05-27 04:12:16 +00:00
|
|
|
|
|
|
|
friend class SuggestionSearch;
|
|
|
|
|
|
|
|
// Xapian based methods and data
|
2024-04-06 06:20:56 +00:00
|
|
|
#if defined(LIBZIM_WITH_XAPIAN)
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
private: // Xapian based methods
|
|
|
|
SuggestionResultSet(std::shared_ptr<SuggestionDataBase> p_internalDb, Xapian::MSet&& mset);
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
private: // Xapian based data
|
|
|
|
std::shared_ptr<Xapian::MSet> mp_mset;
|
2023-05-27 04:12:16 +00:00
|
|
|
|
2024-04-06 06:20:56 +00:00
|
|
|
#endif // LIBZIM_WITH_XAPIAN
|
2023-05-27 04:12:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace zim
|
|
|
|
|
|
|
|
#endif // ZIM_SUGGESTION_H
|