LCOV - code coverage report
Current view: top level - backends/remote - remote-database.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core c2b6f1024d3a Lines: 11 11 100.0 %
Date: 2019-05-16 09:13:18 Functions: 5 5 100.0 %
Branches: 2 4 50.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file remote-database.h
       2                 :            :  *  @brief RemoteDatabase is the baseclass for remote database implementations.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2006,2007,2009,2010,2011,2014,2015,2017 Olly Betts
       5                 :            :  * Copyright (C) 2007,2009,2010 Lemur Consulting Ltd
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU General Public License as
       9                 :            :  * published by the Free Software Foundation; either version 2 of the
      10                 :            :  * License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef XAPIAN_INCLUDED_REMOTE_DATABASE_H
      23                 :            : #define XAPIAN_INCLUDED_REMOTE_DATABASE_H
      24                 :            : 
      25                 :            : #include "backends/backends.h"
      26                 :            : #include "backends/databaseinternal.h"
      27                 :            : #include "api/enquireinternal.h"
      28                 :            : #include "api/queryinternal.h"
      29                 :            : #include "net/remoteconnection.h"
      30                 :            : #include "backends/valuestats.h"
      31                 :            : #include "xapian/weight.h"
      32                 :            : 
      33                 :            : namespace Xapian {
      34                 :            :     class RSet;
      35                 :            : }
      36                 :            : 
      37                 :            : class NetworkPostList;
      38                 :            : 
      39                 :            : /** RemoteDatabase is the baseclass for remote database implementations.
      40                 :            :  *
      41                 :            :  *  A subclass of this class is required which opens a TCP connection or
      42                 :            :  *  pipe to the remote database server.  This subclass works in combination
      43                 :            :  *  with the RemoteSubMatch class during the match process.
      44                 :            :  */
      45         [ -  + ]:       1436 : class RemoteDatabase : public Xapian::Database::Internal {
      46                 :            :     /// Don't allow assignment.
      47                 :            :     void operator=(const RemoteDatabase &);
      48                 :            : 
      49                 :            :     /// Don't allow copying.
      50                 :            :     RemoteDatabase(const RemoteDatabase &);
      51                 :            : 
      52                 :            :     /// The object which does the I/O.
      53                 :            :     mutable OwnedRemoteConnection link;
      54                 :            : 
      55                 :            :     /// The remote document count, given at open.
      56                 :            :     mutable Xapian::doccount doccount;
      57                 :            : 
      58                 :            :     /// The remote last docid, given at open.
      59                 :            :     mutable Xapian::docid lastdocid;
      60                 :            : 
      61                 :            :     /// A lower bound on the smallest document length in this database.
      62                 :            :     mutable Xapian::termcount doclen_lbound;
      63                 :            : 
      64                 :            :     /// An upper bound on the greatest document length in this database.
      65                 :            :     mutable Xapian::termcount doclen_ubound;
      66                 :            : 
      67                 :            :     /// The total length of all documents in this database.
      68                 :            :     mutable Xapian::totallength total_length;
      69                 :            : 
      70                 :            :     /// Has positional information?
      71                 :            :     mutable bool has_positional_info;
      72                 :            : 
      73                 :            :     /// The UUID of the remote database.
      74                 :            :     mutable std::string uuid;
      75                 :            : 
      76                 :            :     /// The context to return with any error messages
      77                 :            :     std::string context;
      78                 :            : 
      79                 :            :     mutable bool cached_stats_valid;
      80                 :            : 
      81                 :            :     /** The most recently used value statistics. */
      82                 :            :     mutable ValueStats mru_valstats;
      83                 :            : 
      84                 :            :     /** The value slot for the most recently used value statistics.
      85                 :            :      *
      86                 :            :      *  Set to BAD_VALUENO if no value statistics have yet been looked up.
      87                 :            :      */
      88                 :            :     mutable Xapian::valueno mru_slot;
      89                 :            : 
      90                 :            :     bool update_stats(message_type msg_code = MSG_UPDATE,
      91                 :            :                       const std::string & body = std::string()) const;
      92                 :            : 
      93                 :            :   protected:
      94                 :            :     /** Constructor.  The constructor is protected so that raw instances
      95                 :            :      *  can't be created - a derived class must be instantiated which
      96                 :            :      *  has code in the constructor to open the socket.
      97                 :            :      *
      98                 :            :      *  @param fd       The file descriptor for the connection to the server.
      99                 :            :      *  @param timeout_ The timeout used with the network operations.
     100                 :            :      *                  Generally a Xapian::NetworkTimeoutError exception will
     101                 :            :      *                  be thrown if the remote end doesn't respond for this
     102                 :            :      *                  length of time (in seconds).  A timeout of 0 means that
     103                 :            :      *                  operations will never timeout.
     104                 :            :      *  @param context_ The context to return with any error messages.
     105                 :            :      *  @param writable Is this a WritableDatabase?
     106                 :            :      *  @param flags    Xapian::DB_RETRY_LOCK or 0.
     107                 :            :      */
     108                 :            :     RemoteDatabase(int fd, double timeout_, const std::string& context_,
     109                 :            :                    bool writable, int flags);
     110                 :            : 
     111                 :            :     /// Receive a message from the server.
     112                 :            :     reply_type get_message(std::string& message,
     113                 :            :                            reply_type required_type,
     114                 :            :                            reply_type required_type2) const;
     115                 :            : 
     116                 :     335282 :     void get_message(std::string& message,
     117                 :            :                      reply_type required_type) const {
     118                 :     335282 :         (void)get_message(message, required_type, required_type);
     119                 :     311086 :     }
     120                 :            : 
     121                 :    1953086 :     bool get_message_or_done(std::string& message,
     122                 :            :                              reply_type required_type) const {
     123                 :    1953086 :         return get_message(message, required_type, REPLY_DONE) != REPLY_DONE;
     124                 :            :     }
     125                 :            : 
     126                 :            :     /// Send a message to the server.
     127                 :            :     void send_message(message_type type, const std::string& data) const;
     128                 :            : 
     129                 :            :     /// Close the socket
     130                 :            :     void do_close();
     131                 :            : 
     132                 :            :     bool get_posting(Xapian::docid& did, double& w, std::string& value);
     133                 :            : 
     134                 :            :     /// The timeout value used in network communications, in seconds.
     135                 :            :     double timeout;
     136                 :            : 
     137                 :            :   public:
     138                 :            :     /** Get the length of the position list.
     139                 :            :      *
     140                 :            :      *  Extra method for RemoteDatabase.
     141                 :            :      */
     142                 :            :     Xapian::termcount positionlist_count(Xapian::docid did,
     143                 :            :                                          const std::string& term) const;
     144                 :            : 
     145                 :            :     /// Send a keep-alive message.
     146                 :            :     void keep_alive();
     147                 :            : 
     148                 :            :     typedef Xapian::Internal::opt_intrusive_ptr<Xapian::MatchSpy> opt_ptr_spy;
     149                 :            : 
     150                 :            :     /** Set the query
     151                 :            :      *
     152                 :            :      * @param query                     The query.
     153                 :            :      * @param qlen                      The query length.
     154                 :            :      * @param collapse_key              The value number to collapse matches on.
     155                 :            :      * @param collapse_max              Max number of items with the same key
     156                 :            :      *                                  to leave after collapsing (0 for don't
     157                 :            :      *                                  collapse).
     158                 :            :      * @param order                     Sort order for docids.
     159                 :            :      * @param sort_key                  The value number to sort on.
     160                 :            :      * @param sort_by                   Which order to apply sorts in.
     161                 :            :      * @param sort_value_forward        Sort order for values.
     162                 :            :      * @param time_limit_               Seconds to reduce check_at_least after
     163                 :            :      *                                  (or <= 0 for no limit).
     164                 :            :      * @param percent_threshold         Lower bound on percentage score.
     165                 :            :      * @param weight_threshold          Lower bound on weight.
     166                 :            :      * @param wtscheme                  Weighting scheme.
     167                 :            :      * @param omrset                    The rset.
     168                 :            :      * @param matchspies                The matchspies to use.
     169                 :            :      */
     170                 :            :     void set_query(const Xapian::Query& query,
     171                 :            :                    Xapian::termcount qlen,
     172                 :            :                    Xapian::valueno collapse_key,
     173                 :            :                    Xapian::doccount collapse_max,
     174                 :            :                    Xapian::Enquire::docid_order order,
     175                 :            :                    Xapian::valueno sort_key,
     176                 :            :                    Xapian::Enquire::Internal::sort_setting sort_by,
     177                 :            :                    bool sort_value_forward,
     178                 :            :                    double time_limit,
     179                 :            :                    int percent_threshold, double weight_threshold,
     180                 :            :                    const Xapian::Weight& wtscheme,
     181                 :            :                    const Xapian::RSet &omrset,
     182                 :            :                    const std::vector<opt_ptr_spy>& matchspies) const;
     183                 :            : 
     184                 :            :     /** Get the underlying fd this remote connection reads from.
     185                 :            :      *
     186                 :            :      *  This allows the matcher to efficiently wait for remote databases to be
     187                 :            :      *  ready in parallel using poll() or select().
     188                 :            :      */
     189                 :         84 :     int get_read_fd() const {
     190                 :         84 :         return link.get_read_fd();
     191                 :            :     }
     192                 :            : 
     193                 :            :     /// Get the stats from the remote server.
     194                 :            :     void get_remote_stats(Xapian::Weight::Internal& out) const;
     195                 :            : 
     196                 :            :     /// Send the global stats to the remote server.
     197                 :            :     void send_global_stats(Xapian::doccount first,
     198                 :            :                            Xapian::doccount maxitems,
     199                 :            :                            Xapian::doccount check_at_least,
     200                 :            :                            const Xapian::Weight::Internal &stats) const;
     201                 :            : 
     202                 :            :     /// Get the MSet from the remote server.
     203                 :            :     Xapian::MSet get_mset(const std::vector<opt_ptr_spy>& matchspies) const;
     204                 :            : 
     205                 :            :     /// Get remote metadata key list.
     206                 :            :     TermList * open_metadata_keylist(const std::string & prefix) const;
     207                 :            : 
     208                 :            :     /// Get remote termlist.
     209                 :            :     TermList * open_term_list(Xapian::docid did) const;
     210                 :            : 
     211                 :            :     TermList * open_term_list_direct(Xapian::docid did) const;
     212                 :            : 
     213                 :            :     /// Iterate all terms.
     214                 :            :     TermList * open_allterms(const std::string& prefix) const;
     215                 :            : 
     216                 :            :     bool has_positions() const;
     217                 :            : 
     218                 :            :     bool reopen();
     219                 :            : 
     220                 :            :     void close();
     221                 :            : 
     222                 :            :     PostList* open_post_list(const std::string& term) const;
     223                 :            : 
     224                 :            :     LeafPostList* open_leaf_post_list(const std::string& term, bool) const;
     225                 :            : 
     226                 :            :     Xapian::doccount read_post_list(const std::string& term, NetworkPostList & pl) const;
     227                 :            : 
     228                 :            :     PositionList * open_position_list(Xapian::docid did,
     229                 :            :                                       const std::string& tname) const;
     230                 :            : 
     231                 :            :     /// Get a remote document.
     232                 :            :     Xapian::Document::Internal * open_document(Xapian::docid did, bool lazy) const;
     233                 :            : 
     234                 :            :     /// Get the document count.
     235                 :            :     Xapian::doccount get_doccount() const;
     236                 :            : 
     237                 :            :     /// Get the last used docid.
     238                 :            :     Xapian::docid get_lastdocid() const;
     239                 :            : 
     240                 :            :     Xapian::totallength get_total_length() const;
     241                 :            : 
     242                 :            :     Xapian::termcount get_doclength(Xapian::docid did) const;
     243                 :            :     Xapian::termcount get_unique_terms(Xapian::docid did) const;
     244                 :            : 
     245                 :            :     /// Check if term exists.
     246                 :            :     bool term_exists(const std::string& tname) const;
     247                 :            : 
     248                 :            :     void get_freqs(const std::string& term,
     249                 :            :                    Xapian::doccount * termfreq_ptr,
     250                 :            :                    Xapian::termcount * collfreq_ptr) const;
     251                 :            : 
     252                 :            :     /// Read the value statistics for a value from a remote database.
     253                 :            :     void read_value_stats(Xapian::valueno slot) const;
     254                 :            :     Xapian::doccount get_value_freq(Xapian::valueno slot) const;
     255                 :            :     std::string get_value_lower_bound(Xapian::valueno slot) const;
     256                 :            :     std::string get_value_upper_bound(Xapian::valueno slot) const;
     257                 :            : 
     258                 :            :     Xapian::termcount get_doclength_lower_bound() const;
     259                 :            :     Xapian::termcount get_doclength_upper_bound() const;
     260                 :            :     Xapian::termcount get_wdf_upper_bound(const std::string& term) const;
     261                 :            : 
     262                 :            :     void commit();
     263                 :            : 
     264                 :            :     void cancel();
     265                 :            : 
     266                 :            :     Xapian::docid add_document(const Xapian::Document & doc);
     267                 :            : 
     268                 :            :     void delete_document(Xapian::docid did);
     269                 :            :     void delete_document(const std::string & unique_term);
     270                 :            : 
     271                 :            :     void replace_document(Xapian::docid did, const Xapian::Document & doc);
     272                 :            :     Xapian::docid replace_document(const std::string & unique_term,
     273                 :            :                                    const Xapian::Document & document);
     274                 :            : 
     275                 :            :     std::string get_uuid() const;
     276                 :            : 
     277                 :            :     std::string get_metadata(const std::string& key) const;
     278                 :            : 
     279                 :            :     void set_metadata(const std::string& key, const std::string& value);
     280                 :            : 
     281                 :            :     void add_spelling(const std::string& word, Xapian::termcount freqinc) const;
     282                 :            : 
     283                 :            :     Xapian::termcount remove_spelling(const std::string& word,
     284                 :            :                                       Xapian::termcount freqdec) const;
     285                 :            : 
     286                 :      10424 :     int get_backend_info(std::string* path) const {
     287         [ -  + ]:      10424 :         if (path) *path = context;
     288                 :      10424 :         return BACKEND_REMOTE;
     289                 :            :     }
     290                 :            : 
     291                 :            :     bool locked() const;
     292                 :            : 
     293                 :            :     std::string get_description() const;
     294                 :            : };
     295                 :            : 
     296                 :            : #endif // XAPIAN_INCLUDED_REMOTE_DATABASE_H

Generated by: LCOV version 1.11