Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F88185880
pusher.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Oct 17, 07:40
Size
6 KB
Mime Type
text/x-c++
Expires
Sat, Oct 19, 07:40 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
21731240
Attached To
R3127 blackdynamite
pusher.hh
View Options
/*
author : Guillaume ANCIAUX (anciaux@labri.fr, g.anciaux@laposte.net)
author : Till JUNGE <till.junge@epfl.ch>
*/
/* -------------------------------------------------------------------------- */
#ifndef __BLACK_DYNAMITE_PUSHER_HH__
#define __BLACK_DYNAMITE_PUSHER_HH__
/* -------------------------------------------------------------------------- */
#include <pqxx/pqxx>
#include <iostream>
#include <cstdlib>
#include <cmath>
/* -------------------------------------------------------------------------- */
typedef
unsigned
int
UInt
;
typedef
double
Real
;
/* -------------------------------------------------------------------------- */
#ifndef FATAL
#define FATAL(x) {std::cerr << x << std::endl ; exit(EXIT_FAILURE);}
#endif
/* -------------------------------------------------------------------------- */
namespace
BlackDynamite
{
class
Pusher
{
/* ------------------------------------------------------------------------ */
/* Typedefs */
/* ------------------------------------------------------------------------ */
// See chapter 10 section "Designing transactor<>-based Applications" in Douglas: "PostgreSQL" (0-672-32756-2
// this object represents a transaction
template
<
typename
type
,
bool
is_scalar
=
true
>
struct
transaction_input
{
std
::
string
tablename
;
UInt
run_id
;
UInt
quantity_id
;
UInt
step
;
const
type
*
value
;
};
// transaction for non scalar types
template
<
typename
type
>
struct
transaction_input
<
type
,
false
>
{
std
::
string
tablename
;
UInt
run_id
;
UInt
quantity_id
;
UInt
step
;
const
std
::
vector
<
type
>
*
value
;
};
// pqxx transaction type
typedef
pqxx
::
robusttransaction
<
pqxx
::
serializable
>
transaction
;
// obect to really do the push to database
template
<
typename
type
,
bool
is_scalar
>
class
SqlPusher
;
// obect to really do the push to database
class
SqlStateUpdater
;
enum
RunState
{
UNDEF
=
0
,
STARTED
=
1
,
ENDED
=
2
};
/* ------------------------------------------------------------------------ */
/* Constructors/Destructors */
/* ------------------------------------------------------------------------ */
public
:
Pusher
();
virtual
~
Pusher
();
/* ------------------------------------------------------------------------ */
/* Methods */
/* ------------------------------------------------------------------------ */
//! initialisation method
void
init
();
//! initialisation method
void
init
(
const
std
::
string
&
dbname
,
const
std
::
string
&
user
,
const
std
::
string
&
host
,
const
std
::
string
&
schema
,
UInt
run_id
// const std::string & hostaddr,
// const std::string & port,
// const std::string & connect_timeout,
// const std::string & slmode,
// const std::string & service
);
//! function to be called to update the state of the job at the end of the run
void
endRun
();
//! template method that pushes a value associated with a quantity at a particular timestep
template
<
typename
valuetype
>
inline
void
push
(
valuetype
value
,
const
std
::
string
&
quantity
,
const
UInt
&
step
);
//! template method that pushes an int associated with a quantity at a particular timestep
void
push
(
const
int
&
value
,
const
UInt
&
quantity_id
,
const
UInt
&
step
);
//! template method that pushes a real associated with a quantity at a particular timestep
void
push
(
const
Real
&
value
,
const
UInt
&
quantity_id
,
const
UInt
&
step
);
//! template method that pushes an array of int associated with a quantity at a particular timestep
void
push
(
const
std
::
vector
<
int
>
&
values
,
const
UInt
&
quantity_id
,
const
UInt
&
step
);
//! template method that pushes an array of real associated with a quantity at a particular timestep
void
push
(
const
std
::
vector
<
Real
>
&
values
,
const
UInt
&
quantity_id
,
const
UInt
&
step
);
//! return the integer id of a quantity identified with a string
UInt
getQuantityID
(
const
std
::
string
&
name
);
private
:
//! simple function to concatenate the keyword and parameter for forging a request
inline
std
::
string
request_increment
(
std
::
string
keyword
,
std
::
string
parameter
);
/* ------------------------------------------------------------------------ */
/* Class Members */
/* ------------------------------------------------------------------------ */
protected
:
UInt
run_id
;
transaction_input
<
int
>
scalar_integer
;
transaction_input
<
Real
>
scalar_real
;
transaction_input
<
int
,
false
>
vector_integer
;
transaction_input
<
Real
,
false
>
vector_real
;
//standard postgresql connection parameters
// std::string sql_dbname, sql_user, sql_host, sql_password, sql_hostaddr, sql_port, sql_connect_timeout, sql_slmode, sql_service;
std
::
string
sql_schema
;
std
::
string
quantities_tablename
;
static
pqxx
::
connection
*
sql_connection
;
static
UInt
connection_counter
;
std
::
map
<
std
::
string
,
UInt
>
quantity_ids
;
bool
sql_mode
;
SqlStateUpdater
*
state_updater
;
};
/* -------------------------------------------------------------------------- */
template
<
typename
valuetype
>
inline
void
Pusher
::
push
(
valuetype
value
,
const
std
::
string
&
quantity
,
const
UInt
&
step
){
if
(
!
this
->
quantity_ids
.
count
(
quantity
))
this
->
quantity_ids
[
quantity
]
=
getQuantityID
(
quantity
);
UInt
quantity_id
=
this
->
quantity_ids
[
quantity
];
this
->
push
(
value
,
quantity_id
,
step
);
}
/* -------------------------------------------------------------------------- */
template
<
typename
num
>
inline
std
::
string
nan_avoider
(
const
num
&
val
)
{
return
pqxx
::
to_string
(
val
);
}
/* -------------------------------------------------------------------------- */
template
<>
inline
std
::
string
nan_avoider
(
const
double
&
val
)
{
if
(
isnan
(
val
))
{
return
"'nan'::double precision"
;
}
else
{
std
::
stringstream
val_rep
;
val_rep
.
precision
(
16
);
val_rep
<<
std
::
scientific
<<
val
;
return
val_rep
.
str
();
}
}
/* -------------------------------------------------------------------------- */
}
#endif
/* __BLACK_DYNAMITE_PUSHER_HH__ */
Event Timeline
Log In to Comment