Program Listing for File search.h

Return to documentation for file (include/zim/search.h)

/*
 * Copyright (C) 2017-2021 Matthieu Gautier <mgautier@kymeria.fr>
 * Copyright (C) 2021 Maneesh P M <manu.pm55@gmail.com>
 * 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_SEARCH_H
#define ZIM_SEARCH_H

#include "search_iterator.h"
#include "archive.h"
#include <vector>
#include <string>

namespace Xapian {
  class Enquire;
  class MSet;
};

namespace zim
{

class Archive;
class InternalDataBase;
class Query;
class Search;
class SearchResultSet;

class LIBZIM_API Searcher
{
  public:
    explicit Searcher(const std::vector<Archive>& archives);

    explicit Searcher(const Archive& archive);
    Searcher(const Searcher& other);
    Searcher& operator=(const Searcher& other);
    Searcher(Searcher&& other);
    Searcher& operator=(Searcher&& other);
    ~Searcher();

    Searcher& addArchive(const Archive& archive);

    Search search(const Query& query);

    void setVerbose(bool verbose);

  private: // methods
    void initDatabase();

  private: // data
    std::shared_ptr<InternalDataBase> mp_internalDb;
    std::vector<Archive> m_archives;
    bool m_verbose;
};

class LIBZIM_API Query
{
  public:
    Query(const std::string& query = "");

    Query& setQuery(const std::string& query);

    Query& setGeorange(float latitude, float longitude, float distance);

    std::string m_query { "" };

    bool m_geoquery { false };
    float m_latitude { 0 };
    float m_longitude { 0 };
    float m_distance { 0 } ;
};


class LIBZIM_API Search
{
    public:
        Search(Search&& s);
        Search& operator=(Search&& s);
        ~Search();

        const SearchResultSet getResults(int start, int maxResults) const;

        int getEstimatedMatches() const;

    private: // methods
        Search(std::shared_ptr<InternalDataBase> p_internalDb, const Query& query);
        Xapian::Enquire& getEnquire() const;

    private: // data
         std::shared_ptr<InternalDataBase> mp_internalDb;
         mutable std::unique_ptr<Xapian::Enquire> mp_enquire;
         Query m_query;

  friend class Searcher;
};

class LIBZIM_API SearchResultSet
{
  public:
    typedef SearchIterator iterator;

    iterator begin() const;

    iterator end() const;

    int size() const;

  private:
    SearchResultSet(std::shared_ptr<InternalDataBase> p_internalDb, Xapian::MSet&& mset);
    SearchResultSet(std::shared_ptr<InternalDataBase> p_internalDb);

  private: // data
    std::shared_ptr<InternalDataBase> mp_internalDb;
    std::shared_ptr<Xapian::MSet> mp_mset;
  friend class Search;
};

} //namespace zim

#endif // ZIM_SEARCH_H