Skip to content

Commit

Permalink
cats: use separate files for batch insert functions
Browse files Browse the repository at this point in the history
  • Loading branch information
franku committed Feb 27, 2020
1 parent 690e968 commit 954f8ef
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 317 deletions.
4 changes: 2 additions & 2 deletions core/src/cats/CMakeLists.txt
Expand Up @@ -38,9 +38,9 @@ set(LIBBAREOSCATS_SRCS cats_backends.cc)

set(SQLITE_SRCS sqlite.cc ${LIBBAREOSCATS_SRCS})

set(MYSQL_SRCS mysql.cc ${LIBBAREOSCATS_SRCS})
set(MYSQL_SRCS mysql.cc mysql_batch.cc ${LIBBAREOSCATS_SRCS})

set(POSTGRESQL_SRCS postgresql.cc ${LIBBAREOSCATS_SRCS})
set(POSTGRESQL_SRCS postgresql.cc postgresql_batch.cc ${LIBBAREOSCATS_SRCS})

add_library(bareossql SHARED ${LIBBAREOSSQL_SRCS})

Expand Down
3 changes: 2 additions & 1 deletion core/src/cats/bdb_postgresql.h
Expand Up @@ -76,7 +76,8 @@ class BareosDbPostgresql : public BareosDbPrivateInterface {
bool SqlFieldIsNumeric(int field_type) override;
bool SqlBatchStartFileTable(JobControlRecord* jcr) override;
bool SqlBatchEndFileTable(JobControlRecord* jcr, const char* error) override;
bool SqlBatchInsertFileTable(JobControlRecord* jcr, AttributesDbRecord* ar) override;
bool SqlBatchInsertFileTable(JobControlRecord* jcr,
AttributesDbRecord* ar) override;

bool CheckDatabaseEncoding(JobControlRecord* jcr);

Expand Down
5 changes: 3 additions & 2 deletions core/src/cats/cats.h
Expand Up @@ -1082,9 +1082,10 @@ class BareosDb : public BareosDbQueryEnum {
virtual bool SqlFieldIsNotNull(int field_type) = 0;
virtual bool SqlFieldIsNumeric(int field_type) = 0;
virtual bool SqlBatchStartFileTable(JobControlRecord* jcr) = 0;
virtual bool SqlBatchEndFileTable(JobControlRecord* jcr, const char* error) = 0;
virtual bool SqlBatchEndFileTable(JobControlRecord* jcr,
const char* error) = 0;
virtual bool SqlBatchInsertFileTable(JobControlRecord* jcr,
AttributesDbRecord* ar) = 0;
AttributesDbRecord* ar) = 0;
#endif
};

Expand Down
107 changes: 0 additions & 107 deletions core/src/cats/mysql.cc
Expand Up @@ -687,113 +687,6 @@ bool BareosDbMysql::SqlFieldIsNumeric(int field_type)
return IS_NUM(field_type);
}

/**
* Returns true if OK
* false if failed
*/
bool BareosDbMysql::SqlBatchStartFileTable(JobControlRecord* jcr)
{
bool retval;

DbLock(this);
retval = SqlQuery(
"CREATE TEMPORARY TABLE batch ("
"FileIndex integer,"
"JobId integer,"
"Path blob,"
"Name blob,"
"LStat tinyblob,"
"MD5 tinyblob,"
"DeltaSeq integer,"
"Fhinfo NUMERIC(20),"
"Fhnode NUMERIC(20) )");
DbUnlock(this);

/*
* Keep track of the number of changes in batch mode.
*/
changes = 0;

return retval;
}

/* set error to something to abort operation */
/**
* Returns true if OK
* false if failed
*/
bool BareosDbMysql::SqlBatchEndFileTable(JobControlRecord* jcr, const char* error)
{
status_ = 0;

/*
* Flush any pending inserts.
*/
if (changes) { return SqlQuery(cmd); }

return true;
}

/**
* Returns true if OK
* false if failed
*/
bool BareosDbMysql::SqlBatchInsertFileTable(JobControlRecord* jcr,
AttributesDbRecord* ar)
{
const char* digest;
char ed1[50], ed2[50], ed3[50];

esc_name = CheckPoolMemorySize(esc_name, fnl * 2 + 1);
EscapeString(jcr, esc_name, fname, fnl);

esc_path = CheckPoolMemorySize(esc_path, pnl * 2 + 1);
EscapeString(jcr, esc_path, path, pnl);

if (ar->Digest == NULL || ar->Digest[0] == 0) {
digest = "0";
} else {
digest = ar->Digest;
}

/*
* Try to batch up multiple inserts using multi-row inserts.
*/
if (changes == 0) {
Mmsg(cmd,
"INSERT INTO batch VALUES "
"(%u,%s,'%s','%s','%s','%s',%u,'%s','%s')",
ar->FileIndex, edit_int64(ar->JobId, ed1), esc_path, esc_name,
ar->attr, digest, ar->DeltaSeq, edit_uint64(ar->Fhinfo, ed2),
edit_uint64(ar->Fhnode, ed3));
changes++;
} else {
/*
* We use the esc_obj for temporary storage otherwise
* we keep on copying data.
*/
Mmsg(esc_obj, ",(%u,%s,'%s','%s','%s','%s',%u,%u,%u)", ar->FileIndex,
edit_int64(ar->JobId, ed1), esc_path, esc_name, ar->attr, digest,
ar->DeltaSeq, ar->Fhinfo, ar->Fhnode);
PmStrcat(cmd, esc_obj);
changes++;
}

/*
* See if we need to flush the query buffer filled
* with multi-row inserts.
*/
if ((changes % MYSQL_CHANGES_PER_BATCH_INSERT) == 0) {
if (!SqlQuery(cmd)) {
changes = 0;
return false;
} else {
changes = 0;
}
}
return true;
}

/**
* Initialize database data structure. In principal this should
* never have errors, or it is really fatal.
Expand Down
142 changes: 142 additions & 0 deletions core/src/cats/mysql_batch.cc
@@ -0,0 +1,142 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2003-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/

#include "include/bareos.h"

#ifdef HAVE_MYSQL

#include "cats.h"
#include <mysql.h>
#include <errmsg.h>
#include "bdb_mysql.h"
#include "lib/edit.h"

/**
* Returns true if OK
* false if failed
*/
bool BareosDbMysql::SqlBatchStartFileTable(JobControlRecord* jcr)
{
bool retval;

DbLock(this);
retval = SqlQuery(
"CREATE TEMPORARY TABLE batch ("
"FileIndex integer,"
"JobId integer,"
"Path blob,"
"Name blob,"
"LStat tinyblob,"
"MD5 tinyblob,"
"DeltaSeq integer,"
"Fhinfo NUMERIC(20),"
"Fhnode NUMERIC(20) )");
DbUnlock(this);

/*
* Keep track of the number of changes in batch mode.
*/
changes = 0;

return retval;
}

/* set error to something to abort operation */
/**
* Returns true if OK
* false if failed
*/
bool BareosDbMysql::SqlBatchEndFileTable(JobControlRecord* jcr,
const char* error)
{
status_ = 0;

/*
* Flush any pending inserts.
*/
if (changes) { return SqlQuery(cmd); }

return true;
}

/**
* Returns true if OK
* false if failed
*/
bool BareosDbMysql::SqlBatchInsertFileTable(JobControlRecord* jcr,
AttributesDbRecord* ar)
{
const char* digest;
char ed1[50], ed2[50], ed3[50];

esc_name = CheckPoolMemorySize(esc_name, fnl * 2 + 1);
EscapeString(jcr, esc_name, fname, fnl);

esc_path = CheckPoolMemorySize(esc_path, pnl * 2 + 1);
EscapeString(jcr, esc_path, path, pnl);

if (ar->Digest == NULL || ar->Digest[0] == 0) {
digest = "0";
} else {
digest = ar->Digest;
}

/*
* Try to batch up multiple inserts using multi-row inserts.
*/
if (changes == 0) {
Mmsg(cmd,
"INSERT INTO batch VALUES "
"(%u,%s,'%s','%s','%s','%s',%u,'%s','%s')",
ar->FileIndex, edit_int64(ar->JobId, ed1), esc_path, esc_name,
ar->attr, digest, ar->DeltaSeq, edit_uint64(ar->Fhinfo, ed2),
edit_uint64(ar->Fhnode, ed3));
changes++;
} else {
/*
* We use the esc_obj for temporary storage otherwise
* we keep on copying data.
*/
Mmsg(esc_obj, ",(%u,%s,'%s','%s','%s','%s',%u,%u,%u)", ar->FileIndex,
edit_int64(ar->JobId, ed1), esc_path, esc_name, ar->attr, digest,
ar->DeltaSeq, ar->Fhinfo, ar->Fhnode);
PmStrcat(cmd, esc_obj);
changes++;
}

/*
* See if we need to flush the query buffer filled
* with multi-row inserts.
*/
if ((changes % MYSQL_CHANGES_PER_BATCH_INSERT) == 0) {
if (!SqlQuery(cmd)) {
changes = 0;
return false;
} else {
changes = 0;
}
}
return true;
}

#endif // HAVE_MYSQL

0 comments on commit 954f8ef

Please sign in to comment.