DB Class Reference

Ratpac-two: RAT::DB Class Reference
Ratpac-two
Inheritance diagram for RAT::DB:
RAT::DBFieldCallback

Public Member Functions

 DB ()
 
int Load (std::string filename, bool printPath=false)
 
int LoadTable (DBTable *table)
 
int LoadFile (std::string filename)
 
int LoadAll (std::string dirname, std::string pattern="*.ratdb")
 
int LoadDefaults ()
 
void Clear ()
 
void SetServer (std::string url)
 
void SetDefaultRun (int _run)
 
int GetDefaultRun () const
 
DBLinkPtr GetLink (std::string tblname, std::string index="")
 
DBLinkPtr GetLink (std::string tblname, std::string index, int run)
 
DBLinkGroup GetLinkGroup (std::string tblname)
 
const DBTableSet & GetAllTables ()
 
template<typename T >
void Set (const std::string &tblname, const std::string &fieldname, const T &val)
 
template<typename T >
void Set (const std::string &tblname, const std::string &index, const std::string &fieldname, const T &val)
 
template<typename T >
void SetArrayIndex (const std::string &tblname, const std::string &fieldname, size_t idx, const T &val)
 
template<typename T >
void SetArrayIndex (const std::string &tblname, const std::string &index, const std::string &fieldname, size_t idx, const T &val)
 
void DumpContentsToJson (std::ostream &stream)
 
DBTableGetUserTable (std::string tblname, std::string index)
 
DBTableGetRunTable (std::string tblname, std::string index, int runNumber)
 
DBTableGetDefaultTable (std::string tblname, std::string index)
 
void RemoveLink (DBLink *link)
 
int NumLinks ()
 
virtual std::vector< int > FetchIArray (const std::string &tableID, const std::string &fieldname)
 
virtual std::vector< double > FetchDArray (const std::string &tableID, const std::string &fieldname)
 

Static Public Member Functions

static DBGet ()
 
static bool ParseTableName (std::string descriptor, std::string &table, std::string &index)
 
static std::vector< RAT::DBTable * > ReadRATDBFile (const std::string &filename)
 

Protected Member Functions

DBTableFindTable (std::string tblname, std::string index, int runNumber)
 
DBTableFindOrCreateTable (std::string tblname, std::string index, int runNumber)
 

Protected Attributes

std::list< RAT::DBLink * > links
 
std::string server
 
std::set< std::string > tableNamesOnServer
 
std::set< RAT::DBTableKeytablesNotOnServer
 
HTTPDownloader downloader
 
DBTableSet tables
 
std::deque< std::pair< RAT::DBTableKey, bool > > tablesFromServer
 
int run
 

Static Protected Attributes

static DBprimary
 

Constructor & Destructor Documentation

◆ DB()

DB::DB ( )

Create new database in memory with no tables.

Member Function Documentation

◆ Clear()

void DB::Clear ( )

Delete all tables from memory.

◆ DumpContentsToJson()

void DB::DumpContentsToJson ( std::ostream &  stream)

Dump all tables in the db to a JSON file.

Todo:
: Currently doesn't support server tables

◆ FindOrCreateTable()

DBTable * DB::FindOrCreateTable ( std::string  tblname,
std::string  index,
int  runNumber 
)
protected

Obtain a pointer to a table in memory, or create it if not found.

Returns
Pointer to existing table, or pointer to new table, which is also added to tblset.

◆ FindTable()

DBTable * DB::FindTable ( std::string  tblname,
std::string  index,
int  runNumber 
)
protected

Obtain a pointer to a table loaded in memory.

Returns
Pointer to table if found, otherwise 0.

◆ Get()

static DB* RAT::DB::Get ( )
inlinestatic

Obtain pointer to global database

Unless you want a private database, this is the method you should use to obtain a pointer to DB. If the global database has not yet been constructed, it will be constructed when you call this function.

◆ GetAllTables()

const DBTableSet& RAT::DB::GetAllTables ( )
inline

Obtain a reference to all tables stored in the databse.

This is useful if you want to iterate over all the tables in the database.

Todo:
: Should also implement this for server tables.

◆ GetDefaultRun()

int DB::GetDefaultRun ( ) const

Get the default run number used for new links

◆ GetDefaultTable()

DBTable* RAT::DB::GetDefaultTable ( std::string  tblname,
std::string  index 
)
inline

Get pointer to a table in default plane: do not use unless you know what you are doing!

◆ GetLink() [1/2]

DBLinkPtr DB::GetLink ( std::string  tblname,
std::string  index,
int  run 
)

Obtain a link to a particular table and index with given run.

If you have an empty index, pass "" as the index value.

◆ GetLink() [2/2]

DBLinkPtr DB::GetLink ( std::string  tblname,
std::string  index = "" 
)

Obtain a link to a particular table and index with default run.

Note that even if your table does not have an index value, you should still use this method, but with an empty index. (The default value)

◆ GetLinkGroup()

DBLinkGroup DB::GetLinkGroup ( std::string  tblname)

Obtain a link to all tables with the same name, but different indexes.

LinkGroups are useful if you want to step through all the instances of the same kind of table, like GEO.

◆ GetRunTable()

DBTable* RAT::DB::GetRunTable ( std::string  tblname,
std::string  index,
int  runNumber 
)
inline

Get pointer to a table in run plane: do not use unless you know what you are doing!

◆ GetUserTable()

DBTable* RAT::DB::GetUserTable ( std::string  tblname,
std::string  index 
)
inline

Get pointer to a table in user plane: do not use unless you know what you are doing!

◆ Load()

int DB::Load ( std::string  filename,
bool  printPath = false 
)

Load a DB text file or directory of text files in search path.

If passed the name of a file, this function will load all the DB tables it contains into memory. If passed the name of a directory, all of the files in that directory ending in .ratdb will be loaded.

The current directory is searched first, then the $RATSHARE/ratdb directory.

If printFullPath is true, then an info message is printed to stdout (and logged) indicating the path of the file that is loaded. Shows whether $RATSHARE/ratdb was used.

◆ LoadAll()

int DB::LoadAll ( std::string  dirname,
std::string  pattern = "*.ratdb" 
)

Load all of the DB files in a directory.

Searches dirname for all files matching pattern. Does not search $RATSHARE/ratdb automatically, so a path relative to current directory or an absolute path much be given.

◆ LoadDefaults()

int DB::LoadDefaults ( )

Load standard tables into memory.

Currently, the standard tables are $RATSHARE/ratdb/ *.ratdb.

◆ LoadFile()

int DB::LoadFile ( std::string  filename)

Load DB text file of tables into memory.

This function does not search in $RATSHARE/ratdb automatically, so it must be given a file name relative to the current directory or an absolute path.

◆ LoadTable()

int DB::LoadTable ( DBTable table)

Put a DBTable into the database. Returns 1 if success and 0 if failure. The DB class will take over responsibility for freeing the table memory when needed.

◆ NumLinks()

int RAT::DB::NumLinks ( )
inline

Number of still active links to this database.

This is a count of the number of links which have been given out GetLink() and GetLinkGroup(). See DB::links member for more information about what this means. This method is really just for debugging purposes.

◆ ParseTableName()

bool DB::ParseTableName ( std::string  descriptor,
std::string &  table,
std::string &  index 
)
static

Parse a table name written in the "TABLE" or "TABLE[index]" format.

Handy method for when you are getting a table name from user input. Separates the table name from the index name.

Parameters
[in]descriptorString in the form "TABLE" or "TABLE[index]"
[out]tableJust the table name
[out]indexJust the index name. Empty string if no index.
Returns
True if descriptor was a properly formatted table name.

◆ ReadRATDBFile()

std::vector< DBTable * > DB::ReadRATDBFile ( const std::string &  filename)
static

Reads a file and returns all the RATDB tables found inside. Works with both RATDB native and JSON files

◆ RemoveLink()

void DB::RemoveLink ( DBLink link)

Remove a table link from the list of outstanding links: do not use unless you know what you are doing!

This method should only be used by DBLink objects to remove themselves from the list during their destructor call. See DB::links member for more information.

◆ Set() [1/2]

template<typename T >
void DB::Set ( const std::string &  tblname,
const std::string &  fieldname,
const T &  val 
)

Set field in user plane, no table index.

◆ Set() [2/2]

template<typename T >
void DB::Set ( const std::string &  tblname,
const std::string &  index,
const std::string &  fieldname,
const T &  val 
)

Set field in user plane, with table index.

◆ SetArrayIndex() [1/2]

template<typename T >
void DB::SetArrayIndex ( const std::string &  tblname,
const std::string &  fieldname,
size_t  idx,
const T &  val 
)

Set array field index in user plane, no table index.

◆ SetArrayIndex() [2/2]

template<typename T >
void DB::SetArrayIndex ( const std::string &  tblname,
const std::string &  index,
const std::string &  fieldname,
size_t  idx,
const T &  val 
)

Set array field index in user plane, with table index.

◆ SetDefaultRun()

void DB::SetDefaultRun ( int  _run)

Set default run number for new links.

◆ SetServer()

void DB::SetServer ( std::string  url)

Set URL of CouchDB server containing RATDB tables

Member Data Documentation

◆ downloader

HTTPDownloader RAT::DB::downloader
protected

Helper class to download files over HTTP/HTTPS

◆ links

std::list<RAT::DBLink *> RAT::DB::links
protected

List of DBLinks which have been issued by GetLink() or GetLinkGroup().

In order to allow a DBLink to do local caching of values, the DB object must retain pointers to all of the previously issued link objects. Then, if something changes, like new tables are loaded or become valid, DB can go out and expire the relevant cached values inside the affected DBLinks.

In the DBLink destructor, RemoveLink() is called to remove the link from the list before its memory is reclaimed.

◆ primary

DB * DB::primary
staticprotected

Pointer to global DB instance

◆ run

int RAT::DB::run
protected

Current run number, used to set all new DBLinks.

◆ server

std::string RAT::DB::server
protected

URL to CouchDB server. Empty string means no server will be used.

◆ tableNamesOnServer

std::set<std::string> RAT::DB::tableNamesOnServer
protected

Cache of table names present on the CouchDB server. Check this before issuing a query.

◆ tables

DBTableSet RAT::DB::tables
protected

Set of all tables. Run 0 signifies default and run -1 is user-override.

◆ tablesFromServer

std::deque<std::pair<RAT::DBTableKey, bool> > RAT::DB::tablesFromServer
protected

FIFO of tables fetched from the server. Second element of pair is indicates if the size from this key "is real". When the same table is added for multiple runs, only the last run should be set to true.

◆ tablesNotOnServer

std::set<RAT::DBTableKey> RAT::DB::tablesNotOnServer
protected

Cache of full table keys (name, index, run) that are not on the server. Important to cut down on needless network traffic from future queries once a rejection has been received.


The documentation for this class was generated from the following files:
  • /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/db/include/RAT/DB.hh
  • /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/db/src/DB.cc