LCOV - code coverage report
Current view: top level - api - replication.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 10 10 100.0 %
Date: 2019-06-30 05:20:33 Functions: 4 4 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /** @file replication.h
       2                 :            :  * @brief Replication support for Xapian databases.
       3                 :            :  */
       4                 :            : /* Copyright 2008 Lemur Consulting Ltd
       5                 :            :  * Copyright 2008,2011,2015,2016 Olly Betts
       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
      20                 :            :  * USA
      21                 :            :  */
      22                 :            : 
      23                 :            : #ifndef XAPIAN_INCLUDED_REPLICATION_H
      24                 :            : #define XAPIAN_INCLUDED_REPLICATION_H
      25                 :            : 
      26                 :            : #include "xapian/visibility.h"
      27                 :            : 
      28                 :            : #include <string>
      29                 :            : 
      30                 :            : namespace Xapian {
      31                 :            : 
      32                 :            : /** Information about the steps involved in performing a replication. */
      33                 :            : struct ReplicationInfo {
      34                 :            :     /// Number of changesets applied.
      35                 :            :     int changeset_count;
      36                 :            : 
      37                 :            :     /// Number of times a full database copy was performed.
      38                 :            :     int fullcopy_count;
      39                 :            : 
      40                 :            :     /** True if and only if the replication corresponds to a change in the
      41                 :            :      *  live version of the database.  Note that this may be false even if
      42                 :            :      *  changeset_count and fullcopy_count are non-zero, since the changes
      43                 :            :      *  may have been made to a non-live copy of the database.
      44                 :            :      */
      45                 :            :     bool changed;
      46                 :            : 
      47                 :        161 :     ReplicationInfo()
      48                 :            :         : changeset_count(0),
      49                 :            :           fullcopy_count(0),
      50                 :        161 :           changed(false)
      51                 :        161 :     {}
      52                 :            : 
      53                 :        105 :     void clear() {
      54                 :        105 :         changeset_count = 0;
      55                 :        105 :         fullcopy_count = 0;
      56                 :        105 :         changed = false;
      57                 :        105 :     }
      58                 :            : };
      59                 :            : 
      60                 :            : /** Access to a master database for replication. */
      61                 :         14 : class XAPIAN_VISIBILITY_DEFAULT DatabaseMaster {
      62                 :            :     /// The path to the master database.
      63                 :            :     std::string path;
      64                 :            : 
      65                 :            :   public:
      66                 :            :     /** Create a new DatabaseMaster for the database at the specified path.
      67                 :            :      *
      68                 :            :      *  The database isn't actually opened until a set of changesets is
      69                 :            :      *  requested.
      70                 :            :      */
      71                 :         14 :     explicit DatabaseMaster(const std::string & path_) : path(path_) {}
      72                 :            : 
      73                 :            :     /** Write a set of changesets for upgrading the database to a file.
      74                 :            :      *
      75                 :            :      *  The changesets will be such that, if they are applied in order to a
      76                 :            :      *  copy of the database at the start revision, a copy of the database
      77                 :            :      *  at the current revision (i.e. the revision which the database
      78                 :            :      *  object is currently open at) will be produced.
      79                 :            :      *
      80                 :            :      *  If suitable changesets have been stored in the database, this will
      81                 :            :      *  write the appropriate changesets, in order.  If suitable changesets
      82                 :            :      *  are not available, this will write a copy of sufficient blocks of
      83                 :            :      *  the database to reconstruct the current revision.
      84                 :            :      *
      85                 :            :      *  This will therefore potentially write a very large amount of data
      86                 :            :      *  to the file descriptor.
      87                 :            :      *
      88                 :            :      *  @param fd       An open file descriptor to write the changes to.
      89                 :            :      *
      90                 :            :      *  @param start_revision The starting revision of the database that the
      91                 :            :      *                  changesets are to be applied to.  Specify an empty
      92                 :            :      *                  string to get a "creation" changeset, which
      93                 :            :      *                  includes the creation of the database.  The
      94                 :            :      *                  revision will include the unique identifier for the
      95                 :            :      *                  database, if one is available.
      96                 :            :      *
      97                 :            :      *  @param info     If non-NULL, the supplied structure will be updated
      98                 :            :      *                  to reflect the changes written to the file
      99                 :            :      *                  descriptor.
     100                 :            :      */
     101                 :            :     void write_changesets_to_fd(int fd,
     102                 :            :                                 const std::string & start_revision,
     103                 :            :                                 ReplicationInfo * info) const;
     104                 :            : 
     105                 :            :     /// Return a string describing this object.
     106                 :            :     std::string get_description() const;
     107                 :            : };
     108                 :            : 
     109                 :            : /** Access to a database replica, for applying replication to it.
     110                 :            :  *
     111                 :            :  *  Warning: the replication interface is currently experimental, and is liable
     112                 :            :  *  to change between releases without warning.
     113                 :            :  */
     114                 :            : class XAPIAN_VISIBILITY_DEFAULT DatabaseReplica {
     115                 :            :     /// Class holding details of the replica.
     116                 :            :     class Internal;
     117                 :            :     /// Internals.
     118                 :            :     Internal * internal;
     119                 :            : 
     120                 :            :     /// No copying.
     121                 :            :     DatabaseReplica(const DatabaseReplica & other);
     122                 :            : 
     123                 :            :     /// No assignment.
     124                 :            :     void operator=(const DatabaseReplica & other);
     125                 :            : 
     126                 :            :   public:
     127                 :            :     /// Destructor.
     128                 :            :     ~DatabaseReplica();
     129                 :            : 
     130                 :            :     /** Open a DatabaseReplica for the database at the specified path.
     131                 :            :      *
     132                 :            :      *  The path should either point to a database previously created by a
     133                 :            :      *  DatabaseReplica, or to a path which doesn't yet exist.
     134                 :            :      *
     135                 :            :      *  The path should always be in a directory which exists.
     136                 :            :      *
     137                 :            :      *  If the specified path does not contain a database, a database will be
     138                 :            :      *  created when an appropriate changeset is supplied to the replica.
     139                 :            :      *
     140                 :            :      *  @param path       The path to make the replica at.
     141                 :            :      */
     142                 :            :     explicit DatabaseReplica(const std::string & path);
     143                 :            : 
     144                 :            :     /** Get a string describing the current revision of the replica.
     145                 :            :      *
     146                 :            :      *  The revision information includes a unique identifier for the master
     147                 :            :      *  database that the replica is of, as well as information about the exact
     148                 :            :      *  revision of the master database that the replica represents.  This
     149                 :            :      *  information allows the master database to send the appropriate
     150                 :            :      *  changeset to mirror whatever changes have been made on the master.
     151                 :            :      */
     152                 :            :     std::string get_revision_info() const;
     153                 :            : 
     154                 :            :     /** Set the file descriptor to read changesets from.
     155                 :            :      *
     156                 :            :      *  This will be remembered in the DatabaseReplica, but the caller is still
     157                 :            :      *  responsible for closing it after it is finished with.
     158                 :            :      *
     159                 :            :      *  @param fd The file descriptor to read the changeset from.
     160                 :            :      */
     161                 :            :     void set_read_fd(int fd);
     162                 :            : 
     163                 :            :     /** Read and apply the next changeset.
     164                 :            :      *
     165                 :            :      *  If no changesets are found on the file descriptor, returns false
     166                 :            :      *  immediately.
     167                 :            :      *
     168                 :            :      *  If any changesets are found on the file descriptor, exactly one of them
     169                 :            :      *  is applied.
     170                 :            :      *
     171                 :            :      *  A common way to use this method is to call it repeatedly until it
     172                 :            :      *  returns false, with an appropriate gap between each call.
     173                 :            :      *
     174                 :            :      *  Information beyond the end of the next changeset may be read from the
     175                 :            :      *  file descriptor and cached in the DatabaseReplica object.  Therefore,
     176                 :            :      *  the file descriptor shouldn't be accessed by any other external code,
     177                 :            :      *  since it will be in an indeterminate state.
     178                 :            :      *
     179                 :            :      *  Note that if this raises an exception (other than DatabaseCorruptError)
     180                 :            :      *  the database will be left in a valid and consistent state.  It
     181                 :            :      *  may or may not be changed from its initial state, and may or may not be
     182                 :            :      *  fully synchronised with the master database.
     183                 :            :      *
     184                 :            :      *  @param info     If non-NULL, the supplied structure will be updated
     185                 :            :      *                  to reflect the changes read from the file descriptor.
     186                 :            :      *  @param reader_close_time        Wait at least this many seconds between
     187                 :            :      *                                  applying changesets to allow active
     188                 :            :      *                                  readers to finish.
     189                 :            :      *
     190                 :            :      *  @return true if there are more changesets to apply on the file
     191                 :            :      *  descriptor, false otherwise.
     192                 :            :      */
     193                 :            :     bool apply_next_changeset(ReplicationInfo * info,
     194                 :            :                               double reader_close_time);
     195                 :            : 
     196                 :            :     /// Return a string describing this object.
     197                 :            :     std::string get_description() const;
     198                 :            : };
     199                 :            : 
     200                 :            : }
     201                 :            : 
     202                 :            : #endif /* XAPIAN_INCLUDED_REPLICATION_H */

Generated by: LCOV version 1.11