2017-04-19 21:44:31 +03:00
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef LOG_DB_H_
# define LOG_DB_H_
# include <string>
# include <sstream>
2017-04-20 17:13:41 +03:00
# include <map>
2017-04-19 21:44:31 +03:00
# include "SqlDB.h"
# include "LogDBRequest.h"
2017-04-20 17:13:41 +03:00
class LogDB : public SqlDB , Callbackable
2017-04-19 21:44:31 +03:00
{
public :
2017-04-21 17:52:54 +03:00
LogDB ( SqlDB * _db ) : db ( _db ) , next_index ( 0 )
2017-04-19 21:44:31 +03:00
{
2017-04-21 17:52:54 +03:00
pthread_mutex_init ( & mutex , 0 ) ;
} ;
2017-04-19 21:44:31 +03:00
2017-04-21 17:52:54 +03:00
virtual ~ LogDB ( )
2017-04-19 21:44:31 +03:00
{
2017-04-21 17:52:54 +03:00
std : : map < unsigned int , LogDBRequest * > : : iterator it ;
for ( it = requests . begin ( ) ; it ! = requests . end ( ) ; + + it )
{
delete it - > second ;
}
delete db ;
} ;
2017-04-19 21:44:31 +03:00
2017-04-20 17:13:41 +03:00
/**
* Return the request associated to the given logdb record . If there is
* no client waiting for its replication it is loaded from the DB .
* @ param index of the associated logDB entry
* @ return the LogDB replication request
*
*/
LogDBRequest * get_request ( unsigned int index ) ;
2017-04-19 21:44:31 +03:00
2017-04-20 17:13:41 +03:00
// -------------------------------------------------------------------------
// SQL interface
// -------------------------------------------------------------------------
/**
* This function replicates the DB changes on followers before updating
* the DB state
*/
int exec_wr ( ostringstream & cmd ) ;
2017-04-19 21:44:31 +03:00
2017-04-21 17:52:54 +03:00
int exec_bootstrap ( ostringstream & cmd )
2017-04-20 17:13:41 +03:00
{
return db - > exec_bootstrap ( cmd ) ;
}
2017-04-19 21:44:31 +03:00
2017-04-21 17:52:54 +03:00
int exec_rd ( ostringstream & cmd , Callbackable * obj )
2017-04-20 17:13:41 +03:00
{
return db - > exec_rd ( cmd , obj ) ;
2017-04-19 21:44:31 +03:00
}
char * escape_str ( const string & str )
{
return db - > escape_str ( str ) ;
}
void free_str ( char * str )
{
db - > free_str ( str ) ;
}
bool multiple_values_support ( )
{
return db - > multiple_values_support ( ) ;
}
2017-04-21 17:52:54 +03:00
// -------------------------------------------------------------------------
// Database methods
// -------------------------------------------------------------------------
int bootstrap ( )
{
ostringstream oss ( db_bootstrap ) ;
return db - > exec_bootstrap ( oss ) ;
}
2017-04-19 21:44:31 +03:00
protected :
int exec ( ostringstream & cmd , Callbackable * obj , bool quiet )
{
2017-04-20 17:13:41 +03:00
return - 1 ;
2017-04-19 21:44:31 +03:00
}
private :
2017-04-21 17:52:54 +03:00
pthread_mutex_t mutex ;
2017-04-19 21:44:31 +03:00
/**
* Pointer to the underlying DB store
*/
SqlDB * db ;
/**
* Index to be used by the next logDB record
*/
unsigned int next_index ;
2017-04-20 17:13:41 +03:00
/**
* List of pending requests ( a client is waiting for the log entry to be
* replicated in a majority of followers )
*/
std : : map < unsigned int , LogDBRequest * > requests ;
// -------------------------------------------------------------------------
// DataBase implementation
// -------------------------------------------------------------------------
static const char * table ;
static const char * db_names ;
static const char * db_bootstrap ;
/**
* This function loads a log record from the database and returns the an
* associated replication request
* @ param index of the record
*
* @ return the request 0 if failure
*/
int select_cb ( void * req , int num , char * * values , char * * names ) ;
LogDBRequest * select ( int index ) ;
/**
* Inserts or update a log record in the database
* @ param request associated to the logDB entry to be inserted / updated
* @ param replace true to replace an existing entry
*
* @ return 0 on success
*/
int insert_replace ( LogDBRequest * request , bool replace ) ;
2017-04-19 21:44:31 +03:00
} ;
# endif /*LOG_DB_H_*/