30 #include "../flint_lock.h" 65 const char *p = getenv(
"XAPIAN_MAX_CHANGESETS");
72 const string & target)
const 74 LOGCALL(DB,
bool,
"ChertDatabaseReplicator::check_revision_at_least", rev | target);
79 const char * ptr = rev.data();
80 const char * end = ptr + rev.size();
82 throw NetworkError(
"Invalid revision string supplied to check_revision_at_least");
86 end = ptr + target.size();
88 throw NetworkError(
"Invalid revision string supplied to check_revision_at_least");
91 RETURN(rev_val >= target_val);
101 const char *ptr = buf.data();
102 const char *end = ptr + buf.size();
105 char letter = ptr[0];
106 if (letter !=
'A' && letter !=
'B')
107 throw NetworkError(
"Invalid base file letter in changeset");
114 string::size_type base_size;
116 throw NetworkError(
"Invalid base file size in changeset");
128 string tmp_path =
db_dir +
"/" + tablename +
"tmp";
129 string base_path =
db_dir +
"/" + tablename +
".base" + letter;
132 string msg =
"Failed to open ";
139 io_write(fd, buf.data(), base_size);
147 string msg(
"Couldn't update base file ");
160 int changes_fd)
const 162 const char *ptr = buf.data();
163 const char *end = ptr + buf.size();
165 unsigned int changeset_blocksize;
170 string db_path =
db_dir +
"/" + tablename +
".DB";
173 string msg =
"Failed to open ";
185 end = ptr + buf.size();
189 throw NetworkError(
"Invalid block number in changeset");
191 if (block_number == 0)
202 io_write_block(fd, buf.data(), changeset_blocksize, block_number);
215 LOGCALL(DB,
string,
"ChertDatabaseReplicator::apply_changeset_from_conn", conn | end_time | valid);
240 const char *ptr = buf.data();
241 const char *end = ptr + buf.size();
244 unsigned int changes_version;
246 throw NetworkError(
"Couldn't read a valid version number from changeset");
254 throw NetworkError(
"Couldn't read a valid start revision from changeset");
256 throw NetworkError(
"Couldn't read a valid end revision from changeset");
258 if (endrev <= startrev)
259 throw NetworkError(
"End revision in changeset is not later than start revision");
279 throw NetworkError(
"Changeset supplied is for wrong revision number");
282 unsigned char changes_type = ptr[0];
283 if (changes_type != 0) {
284 throw NetworkError(
"Unsupported changeset type: " +
str(changes_type));
297 end = ptr + buf.size();
302 unsigned char chunk_type = ptr[0];
311 if (tablename.empty())
313 if (tablename.find_first_not_of(
"abcdefghijklmnopqrstuvwxyz") !=
315 throw NetworkError(
"Invalid character in tablename in changeset");
322 switch (chunk_type) {
332 throw NetworkError(
"Unrecognised item type in changeset");
337 throw NetworkError(
"Couldn't read a valid required revision from changeset");
339 throw NetworkError(
"Required revision in changeset is earlier than end revision");
351 LOGCALL(DB,
string,
"ChertDatabaseReplicator::get_uuid", NO_ARGS);
The Xapian namespace contains public interfaces for the Xapian library.
void throw_databaselockerror(FlintLock::reason why, const std::string &db_dir, const std::string &explanation) const
Throw Xapian::DatabaseLockError.
Support for chert database replication.
unsigned int max_changesets
The maximum number of changesets which should be kept in the database.
Define the XAPIAN_NORETURN macro.
A RemoteConnection object provides a bidirectional connection to another RemoteConnection object on a...
RemoteConnection class used by the remote backend.
void io_write(int fd, const char *p, size_t n)
Write n bytes from block pointed to by p to file descriptor fd.
Records in chert databases.
static void throw_connection_closed_unexpectedly()
void read_and_check()
Read the version file and check it's a version we understand.
XAPIAN_REVISION_TYPE rev
Revision number of a database.
bool io_sync(int fd)
Ensure all data previously written to file descriptor fd has been written to disk.
Provides wrappers with POSIXy semantics.
Internal definitions for chert database replication.
A record in a chert database.
double end_time(double timeout)
Return the end time for a timeout in timeout seconds.
Convert types to std::string.
void open()
Open the btree at the latest revision.
bool check_revision_at_least(const std::string &rev, const std::string &target) const
Virtual methods of DatabaseReplicator.
Utility functions for testing files.
std::string db_dir
Path of database.
Types used by chert backend and the Btree manager.
unsigned int chert_revision_number_t
A type used to store a revision number for a table.
std::string get_uuid_string() const
Return UUID in the standard 36 character string format.
#define REASONABLE_CHANGESET_SIZE
#define CHANGES_MAGIC_STRING
Hierarchy of classes which Xapian can throw as exceptions.
The ChertVersion class manages the "iamchert" file.
int create_changeset_file(const string &changeset_dir, const string &filename, string &changes_name)
Create a new changeset file, and return an open fd for writing to it.
chert_revision_number_t get_open_revision_number() const
Get the revision number at which this table is currently open.
void process_changeset_chunk_blocks(const std::string &tablename, std::string &buf, RemoteConnection &conn, double end_time, int changes_fd) const
Process a chunk which holds a list of changed blocks in the database.
int get_message_chunk(std::string &result, size_t at_least, double end_time)
Read a chunk of a message from fdin.
void io_write_block(int fd, const char *p, size_t n, off_t b, off_t o)
Write block b size n bytes from buffer p to file descriptor fd, offset o.
string str(int value)
Convert int to std::string.
Wrapper class around a file descriptor to avoid leaks.
bool io_tmp_rename(const std::string &tmp_file, const std::string &real_file)
Rename a temporary file to its final position.
#define CONST_STRLEN(S)
Returns the length of a string constant.
void process_changeset_chunk_base(const std::string &tablename, std::string &buf, RemoteConnection &conn, double end_time, int changes_fd) const
Process a chunk which holds a base block.
bool startswith(const std::string &s, char pfx)
void pack_uint(std::string &s, U value)
Append an encoded unsigned integer to a string.
int get_message_chunked(double end_time)
Prepare to read one message from fdin in chunks.
Replication protocol version and message numbers.
Utility functions for replication implementations.
Indicates a problem communicating with a remote database.
Pack types into strings and unpack them again.
Wrappers for low-level POSIX I/O routines.
Various handy helpers which std::string really should provide.
bool unpack_uint(const char **p, const char *end, U *result)
Decode an unsigned integer from a string.
std::string get_uuid() const
Virtual methods of DatabaseReplicator.
reason lock(bool exclusive, bool wait, std::string &explanation)
Attempt to obtain the lock.
bool unpack_string(const char **p, const char *end, std::string &result)
Decode a std::string from a string.
DatabaseError indicates some sort of database related error.
ChertDatabaseReplicator(const std::string &db_dir_)
#define LOGCALL(CATEGORY, TYPE, FUNC, PARAMS)
std::string apply_changeset_from_conn(RemoteConnection &conn, double end_time, bool valid) const
Virtual methods of DatabaseReplicator.
void write_and_clear_changes(int changes_fd, string &buf, size_t bytes)
Write some changes from a buffer, and then drop them from the buffer.