xapian-core  2.0.0
replicatetcpclient.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2008,2010,2011,2015 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "replicatetcpclient.h"
24 
25 #include "api/replication.h"
26 
27 #include "socket_utils.h"
28 #include "tcpclient.h"
29 
30 using namespace std;
31 
32 ReplicateTcpClient::ReplicateTcpClient(const string & hostname, int port,
33  double timeout_connect,
34  double socket_timeout)
35  : socket{open_socket(hostname, port, timeout_connect)},
36  remconn(-1, socket)
37 {
38  set_socket_timeouts(socket, socket_timeout);
39 }
40 
41 int
42 ReplicateTcpClient::open_socket(const string & hostname, int port,
43  double timeout_connect)
44 {
45  return TcpClient::open_socket(hostname, port, timeout_connect, false,
46  string());
47 }
48 
49 void
50 ReplicateTcpClient::update_from_master(const std::string & path,
51  const std::string & masterdb,
53  double reader_close_time,
54  bool force_copy)
55 {
56  Xapian::DatabaseReplica replica(path);
57  remconn.send_message('R',
58  force_copy ? string() : replica.get_revision_info(),
59  0.0);
60  remconn.send_message('D', masterdb, 0.0);
61  replica.set_read_fd(socket);
62  info.clear();
63  bool more;
64  do {
66  more = replica.apply_next_changeset(&subinfo, reader_close_time);
67  info.changeset_count += subinfo.changeset_count;
68  info.fullcopy_count += subinfo.fullcopy_count;
69  if (subinfo.changed)
70  info.changed = true;
71  } while (more);
72 }
73 
74 ReplicateTcpClient::~ReplicateTcpClient()
75 {
76  remconn.shutdown();
77 }
Access to a database replica, for applying replication to it.
Definition: replication.h:109
int open_socket(std::string_view hostname, int port, double timeout_connect, bool tcp_nodelay, const std::string &context)
Attempt to open a TCP/IP socket connection to a server.
Definition: tcpclient.cc:51
TCP/IP replication client class.
Replication support for Xapian databases.
void set_socket_timeouts(int fd, double timeout)
Attempt to set socket-level timeouts.
Definition: socket_utils.cc:69
Socket handling utilities.
Information about the steps involved in performing a replication.
Definition: replication.h:32
bool changed
True if and only if the replication corresponds to a change in the live version of the database.
Definition: replication.h:44
int fullcopy_count
Number of times a full database copy was performed.
Definition: replication.h:37
int changeset_count
Number of changesets applied.
Definition: replication.h:34
Open a TCP connection to a server.