Commit 3ddd351f authored by cvs2svn's avatar cvs2svn
Browse files

This commit was manufactured by cvs2svn to create tag 'COOL_2_7_0'.

git-svn-id: file:///git/lcgcool.svndb/cool/tags/COOL_2_7_0@15242 4525493e-7705-40b1-a816-d608a930855b
parent 2a49fc93
<use name=CoolApplication>
<use name=CoolKernel>
// $Id: cool200API.cpp,v 1.2 2007-02-21 11:08:59 avalassi Exp $
#include <iostream>
#include <stdexcept>
// COOL API include files (CoolKernel)
#include "CoolKernel/IDatabaseSvc.h"
#include "CoolKernel/FolderSpecification.h"
#include "CoolKernel/IDatabase.h"
#include "CoolKernel/IFolder.h"
#include "CoolKernel/IObject.h"
#include "CoolKernel/Record.h"
// COOL API include files (CoolApplication)
#include "CoolApplication/Application.h"
//--------------------------------------------------------------------------
int main ()
{
try {
// Fetch the URL from the environment variable
const char* COOLTESTDB = "COOLTESTDB";
std::string url;
if ( getenv( COOLTESTDB ) ) url = getenv( COOLTESTDB );
else throw std::runtime_error( "COOLTESTDB env variable is not set" );
// Drop the database and recreate it
{
std::cout << "Drop the database and recreate it" << std::endl;
cool::Application app;
app.databaseService().dropDatabase( url );
app.databaseService().createDatabase( url );
}
// Open the database and create a new folder
{
std::cout << "Open the database and create a new folder" << std::endl;
cool::Application app;
bool readOnly = false;
cool::IDatabasePtr db =
app.databaseService().openDatabase( url, readOnly );
cool::RecordSpecification payloadSpec;
payloadSpec.extend( "I64", cool::StorageType::Int64 );
cool::FolderSpecification folderSpec
( cool::FolderVersioning::SINGLE_VERSION, payloadSpec );
cool::IFolderPtr folder = db->createFolder( "/myfolder", folderSpec );
cool::Int64 i64 = cool::Int64Max;
cool::Record payload( payloadSpec );
payload["I64"].setValue( i64 );
folder->storeObject( 0, 100, payload, 1 ); // t=[0,100] in channel#1
}
// Open the database and read back from the folder
{
std::cout << "Open the database and read from the folder" << std::endl;
cool::Application app;
cool::IDatabasePtr db = app.databaseService().openDatabase( url );
cool::IFolderPtr folder = db->getFolder( "/myfolder" );
cool::MSG::Level oldLevel = app.outputLevel();
app.setOutputLevel( cool::MSG::VERBOSE );
cool::IObjectPtr obj = folder->findObject( 50, 1 ); // t=50 in channel#1
app.setOutputLevel( oldLevel );
std::cout << "Payload at t=50 in ch#1: " << obj->payload() << std::endl;
const cool::IField& field = obj->payload()["I64"];
std::cout << "Payload['I64'] at t=50 in ch#1: " << field << std::endl;
cool::Int64 i64 = field.data<cool::Int64>();
std::cout << "I64 at t=50 in ch#1: " << i64 << std::endl;
}
}
catch ( std::exception& e ) {
std::cout << "std::exception caught: '" << e.what() << "'" << std::endl;
return -1;
}
catch (...) {
std::cout << "Unknown exception caught!" << std::endl;
return -1;
}
}
//---------------------------------------------------------------------------
<use name=CoolApplication>
<?xml version="1.0" ?>
<servicelist>
<logicalservice name="CondDB">
<service name="sqlite_file:CondDB.db" accessMode="update" authentication="password" />
<service name="oracle://devdb10/lhcb_lcg_cool_dev" accessMode="update" authentication="password" />
<service name="mysql://pclhcb91.cern.ch/MARCOCLE" accessMode="update" authentication="password" />
</logicalservice>
</servicelist>
// $Id: dbaliases.cpp,v 1.2 2006-05-21 07:53:45 sas Exp $
#include <iostream>
#include <string>
#include <set>
// COOL API: database service bootstrap
#include "CoolApplication/Application.h"
#include "CoolApplication/DatabaseSvcFactory.h"
// COOL API include files (CoolKernel)
#include "CoolKernel/Exception.h"
#include "CoolKernel/IDatabaseSvc.h"
#include "CoolKernel/IDatabase.h"
#include "CoolKernel/IFolder.h"
#include "CoolKernel/IObject.h"
//-----------------------------------------------------------------------------
int main ( int /* argc */, char** /* argv */ ) {
// --------------------------------------------------
// Main part
// --------------------------------------------------
try {
// init COOL application
cool::Application app;
// drop and create a database
app.databaseService().dropDatabase("CondDB/COOLTEST");
cool::IDatabasePtr db = app.databaseService().createDatabase("CondDB/COOLTEST");
// ... do something interesting here ...
}
// COOL, CORAL POOL exceptions inherit from std exceptions: catching
// std::exception will catch all errors from COOL, CORAL and POOL
catch ( std::exception& e ) {
std::cout << "std::exception caught: " << e.what() << std::endl;
return -1;
}
catch (...) {
std::cout << "Unknown exception caught!" << std::endl;
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
<include_path path=.></include_path>
<use name=CoolApplication>
<use name=CoolKernel>
<export autoexport=true>
<lib name=ExampleBase>
</export>
// $Id: ExampleApplication.h,v 1.17 2007-01-19 18:14:12 marcocle Exp $
#ifndef EXAMPLEBASE_EXAMPLEAPPLICATION_H
#define EXAMPLEBASE_EXAMPLEAPPLICATION_H 1
// Include files
#include "CoolApplication/Application.h"
#include "CoolKernel/DatabaseId.h"
#include "CoolKernel/Exception.h"
#include "CoolKernel/IDatabaseSvc.h"
#include "CoolKernel/IObject.h"
#include "CoolKernel/RecordSpecification.h"
#include "CoolKernel/Time.h"
namespace cool
{
/** @class ExampleApplication ExampleApplication.h
*
* Simple application class for COOL example programs.
*
* NB This class is just an example: it is NOT needed by COOL itself.
*
* @author Andrea Valassi and Sven A. Schmidt
* @date 2005-05-17
*/
class ExampleApplication: public cool::Application {
public:
/// Constructor from command line arguments
ExampleApplication( int argc, char** argv );
/// Destructor
virtual ~ExampleApplication();
/// Get a DatabaseId from the arguments of the main method
const DatabaseId dbIdFromArg( int argc, char* argv[] ) const;
/// Drop and recreate the database specified in the dbId.
IDatabasePtr recreateDb( const DatabaseId& dbId );
/// Open an existing database specified in the dbId.
/// If the database does not exist, a DataBaseDoesNotExist
/// exception is thrown
IDatabasePtr openDb( const DatabaseId& dbId, bool readOnly = true );
/// Create a conditions payload specification (cool::ERecordSpecification).
/// All conditions payloads used in this example use this specification
/// (with three fields I - Int32, S - String4k, X - Float: for instance,
/// these may represent a status code, a comment and a temperature).
cool::RecordSpecification createSpec();
/// Create a fake 'indexed' conditions payload for the given specification.
/// A payload with unique values is created for each value of the 'index'.
coral::AttributeList createPayload( int index,
const cool::RecordSpecification &spec );
/// Helper method - pretty printout of cool::IObject table in a tag
void printTagContents( const IFolderPtr& folder,
const std::vector<std::string>& tagNames );
/// Helper method - pretty printout of tag information
void printTag( const std::string& name,
const std::string& desc,
const std::string& tagTime );
// Pretty printout of a cool::Time
const std::string timeToString( const cool::Time& time );
/// Exception thrown if dbIdFromArg is not called with one argument
class CommandLineArgumentException : public cool::Exception
{
public:
CommandLineArgumentException() : Exception
( "Wrong #arguments to dbIdFromArg", "cool::ExampleApplication" ) {}
virtual ~CommandLineArgumentException() throw() {}
void usage( char* command, std::ostream& s ) {
s << "Usage: " << command
<< " '<dbIdUrl>'" << std::endl;
s << "Example: " << command
<< " 'oracle://devdb10;schema=lcg_cool;dbname=COOLTEST'"
<< std::endl;
s << "Example: " << command
<< " 'mysql://pcitdb59;schema=COOLDB;dbname=COOLTEST'"
<< std::endl;
s << "Example: " << command
<< " 'sqlite://none;schema=sqliteTest.db;dbname=COOLTEST'"
<< std::endl;
}
};
// Header for formatted IObjectPtr output:
// "IObject: 3 ( 0) [2,+inf[ "
// "[2|Object 2|0.002] 2005-07-07_09:14:58.987869000 GMT"
static const std::string& headerRow();
private:
/// Standard constructor is private
ExampleApplication();
/// Copy constructor is private
ExampleApplication( const ExampleApplication& rhs );
/// Assignment operator is private
ExampleApplication& operator=( const ExampleApplication& rhs );
private:
// Command line arguments
std::vector<std::string> m_cmdLineArgs;
};
}
#endif // EXAMPLEBASE_EXAMPLEAPPLICATION_H
// $Id: ExampleApplication.cpp,v 1.23 2008-10-30 16:07:38 avalassi Exp $
// Include files
#include <iomanip>
#include <iostream>
#include <sstream>
#include "CoolKernel/Exception.h"
#include "CoolKernel/IDatabase.h"
#include "CoolKernel/IFolder.h"
#include "CoolKernel/IObjectIterator.h"
#include "CoolKernel/Record.h"
#include "CoolKernel/Time.h"
#include "CoralBase/Attribute.h"
#include "ExampleBase/ExampleApplication.h"
//-----------------------------------------------------------------------------
cool::ExampleApplication::ExampleApplication( int, char** ) {
}
//-----------------------------------------------------------------------------
cool::ExampleApplication::~ExampleApplication()
{
}
//-----------------------------------------------------------------------------
const std::string
cool::ExampleApplication::dbIdFromArg( int argc, char* argv[] ) const
{
if ( argc != 2 ) {
std::stringstream msg;
std::cerr << "ERROR! Wrong number of arguments (" << argc-1
<< ") to cool::ExampleApplication::dbIdFromArg" << std::endl;
throw CommandLineArgumentException();
}
else {
DatabaseId dbId = argv[1];
return dbId;
}
}
//---------------------------------------------------------------------------
cool::IDatabasePtr
cool::ExampleApplication::recreateDb( const DatabaseId& dbId )
{
std::cout << "Recreating database '" << dbId << "'" << std::endl;
IDatabaseSvc& dbSvc = databaseService();
dbSvc.dropDatabase( dbId ); // do nothing if the database does not exist
IDatabasePtr db = dbSvc.createDatabase( dbId );
return db;
}
//---------------------------------------------------------------------------
cool::IDatabasePtr
cool::ExampleApplication::openDb( const DatabaseId& dbId, bool readOnly )
{
try {
std::cout << "Opening database '" << dbId << "'" << std::endl;
IDatabaseSvc& dbSvc = databaseService();
IDatabasePtr db = dbSvc.openDatabase( dbId, readOnly );
return db;
}
// If the database does not exist, an exception is thrown
catch ( cool::DatabaseDoesNotExist& /*e*/ ) {
std::cerr << "Error! Database " << dbId << " does not exist!" << std::endl;
throw;
}
}
//---------------------------------------------------------------------------
cool::RecordSpecification
cool::ExampleApplication::createSpec()
{
std::cout << "Preparing RecordSpecification" << std::endl;
RecordSpecification spec;
spec.extend("I",StorageType::Int32);
spec.extend("S",StorageType::String4k);
spec.extend("X",StorageType::Float);
return spec;
}
//---------------------------------------------------------------------------
coral::AttributeList
cool::ExampleApplication::createPayload
( int index,
const cool::RecordSpecification &spec )
{
coral::AttributeList payload = Record( spec ).attributeList();
payload["I"].data<cool::Int32>() = index;
std::stringstream s; s << "Object " << index;
payload["S"].data<cool::String4k>() = s.str();
payload["X"].data<cool::Float>() = (float)(index/1000.);
return payload;
}
//-----------------------------------------------------------------------------
const std::string
cool::ExampleApplication::timeToString( const cool::Time& time )
{
int year = time.year( );
int month = time.month( ) + 1; // Months are in [0-11]
int day = time.day( );
int hour = time.hour( );
int min = time.minute( );
int sec = time.second( );
long nsec = time.nanosecond();
char timeString[] = "yyyy-mm-dd_hh:mm:ss.nnnnnnnnn GMT";
int nChar = std::string(timeString).size();
if ( sprintf( timeString, "%4.4d-%2.2d-%2.2d_%2.2d:%2.2d:%2.2d.%9.9ld GMT",
year, month, day, hour, min, sec, nsec) == nChar ) {
return std::string(timeString);
} else {
std::ostringstream msg;
msg << "Error encoding cool::Time into string: " << time;
throw cool::Exception( msg.str(), "cool::timeToString" );
}
}
//-----------------------------------------------------------------------------
void cool::ExampleApplication::printTag( const std::string& name,
const std::string& desc,
const std::string& tagTime )
{
unsigned int tagNameWidth = 7;
unsigned int tagDescWidth = 17;
std::cout << std::setiosflags(std::ios::left)
<< std::setw(tagNameWidth) << name
<< std::setw(tagDescWidth) << desc
<< " " << tagTime
<< std::resetiosflags(std::ios::left) << std::endl;
}
//---------------------------------------------------------------------------
void cool::ExampleApplication::printTagContents
( const IFolderPtr& folder,
const std::vector<std::string>& tagNames )
{
std::cout << headerRow() << std::endl;
for ( std::vector<std::string>::const_iterator t = tagNames.begin();
t != tagNames.end(); ++ t ) {
ValidityKey since = 0;
ValidityKey until = ValidityKeyMax;
ChannelId channel = 0;
std::string tagName( *t );
IObjectIteratorPtr objects =
folder->browseObjects( since, until, channel, tagName );
std::cout << tagName << ":" << std::endl;
while ( objects->goToNext() ) {
std::cout << objects->currentRef() << std::endl;
}
}
}
//---------------------------------------------------------------------------
const std::string& cool::ExampleApplication::headerRow() {
static std::string header( " Id Ch IOV "
"Payload Insertion time" );
return header;
}
//---------------------------------------------------------------------------
<use name=Examples/Libraries/ExampleBase>
// $Id: mcBulkRetrieval.cpp,v 1.8 2008-10-30 16:07:38 avalassi Exp $
#include <iostream>
#include <iomanip>
// COOL API include files (CoolKernel)
#include "CoolKernel/Exception.h"
#include "CoolKernel/IDatabaseSvc.h"
#include "CoolKernel/IDatabase.h"
#include "CoolKernel/IFolder.h"
#include "CoolKernel/IObject.h"
#include "CoolKernel/IObjectIterator.h"
#include "CoolKernel/ChannelSelection.h"
// COOL example library include files (ExampleBase)
#include "ExampleBase/ExampleApplication.h"
// COOL namespace
using namespace cool;
//-----------------------------------------------------------------------------
/** @class McBulkRetrieval mcBulkRetrieval.cpp
*
* User example that shows the use of browsing using a
* channel selection (multi channel bulk retrieval).
*
* @author Andrea Valassi, Sven A. Schmidt, Ulrich Moosbrugger
* @date 2005-09-08
*/
class McBulkRetrieval {
private:
ExampleApplication m_app;
DatabaseId m_dbId;
public:
//---------------------------------------------------------------------------
/// Constructor from the command line arguments of 'main( argc, argv )'.
/// Expect exactly one argument that ExampleApplication interprets
/// as a COOL database connection identifier (cool::DatabaseId).
McBulkRetrieval( int argc, char* argv[] )
: m_app( argc, argv )
{
m_dbId = m_app.dbIdFromArg( argc, argv );
}
//---------------------------------------------------------------------------
/// This example shows how to store conditions objects into
/// single version folders in different channels.
void example_channels_write()
{
IDatabasePtr db = m_app.recreateDb( m_dbId );
cool::RecordSpecification spec = m_app.createSpec();
IFolderPtr f1 = db->createFolder( "/folder_1", spec );
std::cout << std::endl
<< "\nStoring objects in different channels (Channel 0-6)"
<< std::endl;
for ( int i = 0; i < 20; ++i )
{
ValidityKey since = i;
ValidityKey until = ValidityKeyMax;
ChannelId channel = i / 3;
coral::AttributeList payload = m_app.createPayload( i, spec );
f1->storeObject( since, until, payload, (ChannelId)channel );
}
}
//---------------------------------------------------------------------------
/// This example shows how to retrieve conditions objects from
/// single version folders in a channel selection.
void example_channels_browse()
{
try
{
IDatabasePtr db = m_app.openDb( m_dbId );
IFolderPtr f1 = db->getFolder( "/folder_1" );
std::cout << std::endl
<< "Browsing objects (multi channel bulk: Channel 2-5):"
<< std::endl;
ValidityKey since = 0;
ValidityKey until = ValidityKeyMax;
// define a channel selection range
ChannelSelection channels( 2, 5 );
IObjectIteratorPtr objects = f1->browseObjects( since,
until,
channels );
std::cout << m_app.headerRow() << std::endl;
while ( objects->goToNext() )
{
std::cout << objects->currentRef() << std::endl;
}
}
catch ( cool::Exception& e )
{
std::cout << e.what() << std::endl;
}
}
//---------------------------------------------------------------------------
}; // class McBulkRetrieval
//-----------------------------------------------------------------------------
int main ( int argc, char* argv[] )
{
try {
// instantiate new McBulkRetrieval object and set m_dbId string
McBulkRetrieval app( argc, argv );
std::cout << "\n*** Example: Multi Channel Bulk Retrieval ***\n" << std::endl;
app.example_channels_write();
app.example_channels_browse();
}
// Wrong number of command line arguments: print usage
catch ( cool::ExampleApplication::CommandLineArgumentException& e ) {