LSSTApplications  11.0-24-g0a022a1,14.0+77,15.0,15.0+1
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
lsst::daf::persistence::DbTsvStorage Class Reference

Class for database storage with data loading from TSV files. More...

#include <DbTsvStorage.h>

Inheritance diagram for lsst::daf::persistence::DbTsvStorage:
lsst::daf::persistence::DbStorage lsst::daf::persistence::FormatterStorage lsst::daf::base::Citizen

Public Types

typedef std::shared_ptr< DbTsvStoragePtr
 
typedef std::vector< PtrList
 
enum  { magicSentinel = 0xdeadbeef }
 
typedef unsigned long memId
 Type of the block's ID. More...
 
typedef memId(* memNewCallback) (const memId cid)
 A function used to register a callback. More...
 
typedef memId(* memCallback) (const Citizen *ptr)
 

Public Member Functions

 DbTsvStorage (void)
 Constructor. More...
 
 ~DbTsvStorage (void)
 Minimal destructor. More...
 
virtual void setPolicy (lsst::pex::policy::Policy::Ptr policy)
 Allow a policy to be used to configure the DbTsvStorage. More...
 
virtual void setPersistLocation (LogicalLocation const &location)
 Set the database location to persist to. More...
 
virtual void setRetrieveLocation (LogicalLocation const &location)
 Set the database location to retrieve from. More...
 
virtual void startTransaction (void)
 Start a transaction. More...
 
virtual void endTransaction (void)
 End a transaction. More...
 
virtual void createTableFromTemplate (std::string const &tableName, std::string const &templateName, bool mayAlreadyExist=false)
 Create a new table from an existing template table. More...
 
virtual void dropTable (std::string const &tableName)
 Drop a table. More...
 
virtual void truncateTable (std::string const &tableName)
 Truncate a table. More...
 
virtual void setTableForInsert (std::string const &tableName)
 Set the table to insert rows into. More...
 
template<typename T >
void setColumn (std::string const &columnName, T const &value)
 Set the value to insert in a given column. More...
 
virtual void setColumnToNull (std::string const &columnName)
 Set a given column to NULL. More...
 
virtual void insertRow (void)
 Insert the row. More...
 
template<typename T >
void outParam (std::string const &columnName, T *location, bool isExpr=false)
 Request a column in the query output and bind a destination location. More...
 
template<typename T >
void condParam (std::string const &paramName, T const &value)
 Bind a value to a WHERE condition parameter. More...
 
template<typename T >
T const & getColumnByPos (int pos)
 Get the value of a column of the query result row by position. More...
 
template<>
void setColumn (std::string const &columnName, char const &value)
 
template<>
void setColumn (std::string const &columnName, double const &value)
 
template<>
void setColumn (std::string const &columnName, float const &value)
 
template<>
void setColumn (std::string const &columnName, DateTime const &value)
 
virtual void executeSql (std::string const &sqlStatement)
 Execute an arbitrary SQL statement. More...
 
virtual void setTableForQuery (std::string const &tableName, bool isExpr=false)
 Set the table to query (single-table queries only). More...
 
virtual void setTableListForQuery (std::vector< std::string > const &tableNameList)
 Set a list of tables to query (multiple-table queries). More...
 
virtual void outColumn (std::string const &columnName, bool isExpr=false)
 Request a column in the query output. More...
 
virtual void orderBy (std::string const &expression)
 Request that the query output be sorted by an expression. More...
 
virtual void groupBy (std::string const &expression)
 Request that the query output be grouped by an expression. More...
 
virtual void setQueryWhere (std::string const &whereClause)
 Set the condition for the WHERE clause of the query. More...
 
virtual void query (void)
 Execute the query. More...
 
virtual bool next (void)
 Move to the next (first) row of the query result. More...
 
bool columnIsNull (int pos)
 Determine if the value of a column is NULL. More...
 
virtual void finishQuery (void)
 Indicate that query processing is finished. More...
 
std::string repr () const
 Return a string representation of a Citizen. More...
 
void markPersistent (void)
 Mark a Citizen as persistent and not destroyed until process end. More...
 
memId getId () const
 Return the Citizen's ID. More...
 

Static Public Member Functions

static Ptr createInstance (std::string const &name, LogicalLocation const &location, bool persist, lsst::pex::policy::Policy::Ptr policy)
 Create and configure a FormatterStorage subclass instance. More...
 
static bool hasBeenCorrupted ()
 Check all allocated blocks for corruption. More...
 
static memId getNextMemId ()
 Return the memId of the next object to be allocated. More...
 
static int init ()
 Called once when the memory system is being initialised. More...
 
Census

Provide a list of current Citizens

static int census (int, memId startingMemId=0)
 How many active Citizens are there? More...
 
static void census (std::ostream &stream, memId startingMemId=0)
 Print a list of all active Citizens to stream, sorted by ID. More...
 
static const std::vector< const Citizen * > * census ()
 Return a (newly allocated) std::vector of active Citizens sorted by ID. More...
 
callbackIDs

Set callback Ids.

The old Id is returned

static memId setNewCallbackId (memId id)
 Call the NewCallback when block is allocated. More...
 
static memId setDeleteCallbackId (memId id)
 Call the current DeleteCallback when block is deleted. More...
 
callbacks

Set the New/Delete callback functions; in each case the previously installed callback is returned.

These callback functions return a value which is Added to the previously registered id.

The default callback functions are called default{New,Delete}Callback; you may want to set a break point in these callbacks from your favourite debugger

static memNewCallback setNewCallback (memNewCallback func)
 Set the NewCallback function. More...
 
static memCallback setDeleteCallback (memCallback func)
 Set the DeleteCallback function. More...
 
static memCallback setCorruptionCallback (memCallback func)
 Set the CorruptionCallback function. More...
 

Protected Member Functions

void verifyPathName (std::string const &pathName)
 Ensure that all directories along a path exist, creating them if necessary. More...
 

Private Member Functions

int _getColumnIndex (std::string const &columnName)
 Get the index of a given column. More...
 

Private Attributes

bool _persisting
 
bool _saveTemp
 Do not delete temporary TSV file if true. More...
 
std::string _tempPath
 Directory pathname for temporary TSV file. More...
 
char * _fileName
 Full pathname for temporary TSV file. More...
 
std::string _location
 Database location URL. More...
 
std::string _tableName
 
std::map< std::string, int > _colMap
 Map from column names to positions. More...
 
std::ostringstream _convertStream
 Stream to convert to text. More...
 
std::vector< std::string_rowBuffer
 
std::unique_ptr< std::ofstream_osp
 Output TSV stream. More...
 

Detailed Description

Class for database storage with data loading from TSV files.

Subclass of DbStorage, overriding persistence methods.

Persists data to a database using TSV files as an intermediary for performance. Provides methods for writing rows to a table and retrieving rows from a query.

Definition at line 66 of file DbTsvStorage.h.

Member Typedef Documentation

◆ List

Definition at line 63 of file FormatterStorage.h.

◆ memCallback

typedef memId(* lsst::daf::base::Citizen::memCallback) (const Citizen *ptr)
inherited

Definition at line 59 of file Citizen.h.

◆ memId

typedef unsigned long lsst::daf::base::Citizen::memId
inherited

Type of the block's ID.

Definition at line 56 of file Citizen.h.

◆ memNewCallback

typedef memId(* lsst::daf::base::Citizen::memNewCallback) (const memId cid)
inherited

A function used to register a callback.

Definition at line 58 of file Citizen.h.

◆ Ptr

Definition at line 68 of file DbTsvStorage.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
inherited
Enumerator
magicSentinel 

Definition at line 86 of file Citizen.h.

Constructor & Destructor Documentation

◆ DbTsvStorage()

lsst::daf::persistence::DbTsvStorage::DbTsvStorage ( void  )

Constructor.

Definition at line 67 of file DbTsvStorage.cc.

67  : _saveTemp(false) {
68 }
bool _saveTemp
Do not delete temporary TSV file if true.
Definition: DbTsvStorage.h:101

◆ ~DbTsvStorage()

lsst::daf::persistence::DbTsvStorage::~DbTsvStorage ( void  )

Minimal destructor.

Definition at line 72 of file DbTsvStorage.cc.

72  {
73 }

Member Function Documentation

◆ _getColumnIndex()

int lsst::daf::persistence::DbTsvStorage::_getColumnIndex ( std::string const &  columnName)
private

Get the index of a given column.

Create a new entry in the row buffer if the column hasn't already been seen. May modify the row buffer, so do not call this inside "_rowBuffer[]".

Parameters
[in]columnNameName of the column
Returns
Index of the column in the row buffer

Definition at line 252 of file DbTsvStorage.cc.

252  {
254  if (i == _colMap.end()) {
256  _rowBuffer.size()));
258  return _rowBuffer.size() - 1;
259  }
260  else {
261  return i->second;
262  }
263 }
std::vector< std::string > _rowBuffer
Definition: DbTsvStorage.h:108
T end(T... args)
STL class.
STL class.
std::map< std::string, int > _colMap
Map from column names to positions.
Definition: DbTsvStorage.h:106
T push_back(T... args)
T insert(T... args)
T find(T... args)
T size(T... args)

◆ census() [1/3]

int lsst::daf::base::Citizen::census ( int  ,
memId  startingMemId = 0 
)
staticinherited

How many active Citizens are there?

Parameters
startingMemIdDon't print Citizens with lower IDs

Definition at line 267 of file Citizen.cc.

270  {
271  if (startingMemId == 0) { // easy
272  ReadGuard guard(citizenLock);
273  return _activeCitizens.size();
274  }
275 
276  int n = 0;
277  ReadGuard guard(citizenLock);
278  for (table::iterator cur = _activeCitizens.begin();
279  cur != _activeCitizens.end(); cur++) {
280  if (cur->first->_CitizenId >= startingMemId) {
281  n++;
282  }
283  }
284 
285  return n;
286 }
T end(T... args)
static table _activeCitizens
Definition: Citizen.h:102
T size(T... args)
T begin(T... args)

◆ census() [2/3]

void lsst::daf::base::Citizen::census ( std::ostream stream,
memId  startingMemId = 0 
)
staticinherited

Print a list of all active Citizens to stream, sorted by ID.

Parameters
streamstream to print to
startingMemIdDon't print Citizens with lower IDs

Definition at line 290 of file Citizen.cc.

293  {
294  ReadGuard guard(citizenLock);
295 
297 
298  for (std::vector<Citizen const *>::const_iterator citizen = leaks->begin(), end = leaks->end();
299  citizen != end; ++citizen) {
300  if ((*citizen)->getId() >= startingMemId) {
301  stream << (*citizen)->repr() << "\n";
302  }
303  }
304 }
int end
STL class.
STL class.
T begin(T... args)
static const std::vector< const Citizen * > * census()
Return a (newly allocated) std::vector of active Citizens sorted by ID.
Definition: Citizen.cc:322

◆ census() [3/3]

std::vector< dafBase::Citizen const * > const * lsst::daf::base::Citizen::census ( )
staticinherited

Return a (newly allocated) std::vector of active Citizens sorted by ID.

You are responsible for deleting it; or you can say std::unique_ptr<std::vector<Citizen const*> const> leaks(Citizen::census()); and not bother (that becomes std::unique_ptr in C++11)

Definition at line 322 of file Citizen.cc.

322  {
325  ReadGuard guard(citizenLock);
326  vec->reserve(_activeCitizens.size());
327 
328  for (table::iterator cur = _activeCitizens.begin();
329  cur != _activeCitizens.end(); cur++) {
330  vec->push_back(dynamic_cast<Citizen const*>(cur->first));
331  }
332 
333  std::sort(vec->begin(), vec->end(), cmpId);
334 
335  return vec;
336 }
T end(T... args)
static table _activeCitizens
Definition: Citizen.h:102
T push_back(T... args)
T size(T... args)
STL class.
T begin(T... args)
T sort(T... args)
T reserve(T... args)

◆ columnIsNull()

bool lsst::daf::persistence::DbStorage::columnIsNull ( int  pos)
inherited

Determine if the value of a column is NULL.

Parameters
[in]posPosition of the column (starts at 0)

Definition at line 270 of file DbStorage.cc.

270  {
271  return _impl->columnIsNull(pos);
272 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ condParam()

template<typename T >
void lsst::daf::persistence::DbTsvStorage::condParam ( std::string const &  paramName,
T const &  value 
)

Bind a value to a WHERE condition parameter.

Parameters
[in]paramNameName of the parameter (prefixed by ":" in the WHERE clause)
[in]valueValue to be bound to the parameter.

Definition at line 368 of file DbTsvStorage.cc.

368  {
369  DbStorage::condParam<T>(paramName, value);
370 }

◆ createInstance()

FormatterStorage::Ptr lsst::daf::persistence::FormatterStorage::createInstance ( std::string const &  name,
LogicalLocation const &  location,
bool  persist,
lsst::pex::policy::Policy::Ptr  policy 
)
staticinherited

Create and configure a FormatterStorage subclass instance.

Parameters
[in]nameName of subclass
[in]locationLocation to persist to or retrieve from
[in]persistTrue if persisting, false if retrieving
[in]policyPolicy used to configure the FormatterStorage
Returns
Shared pointer to FormatterStorage subclass instance

Definition at line 75 of file FormatterStorage.cc.

77  {
79  storage->setPolicy(policy);
80  if (persist) {
81  storage->setPersistLocation(location);
82  }
83  else {
84  storage->setRetrieveLocation(location);
85  }
86  return storage;
87 }
table::Key< std::string > name
Definition: ApCorrMap.cc:71
static StorageRegistry & getRegistry(void)
Return a reference to a subclass registry.
FormatterStorage::Ptr createInstance(std::string const &name)
Create a FormatterStorage subclass instance by name.
std::shared_ptr< FormatterStorage > Ptr

◆ createTableFromTemplate()

void lsst::daf::persistence::DbTsvStorage::createTableFromTemplate ( std::string const &  tableName,
std::string const &  templateName,
bool  mayAlreadyExist = false 
)
virtual

Create a new table from an existing template table.

Parameters
[in]tableNameName of the new table
[in]templateNameName of the existing template table
[in]mayAlreadyExistFalse (default) if the table must not be present

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 183 of file DbTsvStorage.cc.

185  {
186  if (_persisting) {
187  DbStorage dbs;
188  dbs.setPersistLocation(LogicalLocation(_location));
189  dbs.startTransaction();
190  dbs.createTableFromTemplate(tableName, templateName, mayAlreadyExist);
191  dbs.endTransaction();
192  }
193  else {
194  DbStorage::createTableFromTemplate(tableName, templateName,
195  mayAlreadyExist);
196  }
197 }
std::string _location
Database location URL.
Definition: DbTsvStorage.h:104
DbStorage(void)
Constructor.
Definition: DbStorage.cc:57
virtual void createTableFromTemplate(std::string const &tableName, std::string const &templateName, bool mayAlreadyExist=false)
Create a new table from an existing template table.
Definition: DbStorage.cc:112

◆ dropTable()

void lsst::daf::persistence::DbTsvStorage::dropTable ( std::string const &  tableName)
virtual

Drop a table.

Parameters
[in]tableNameName of the table to drop

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 202 of file DbTsvStorage.cc.

202  {
203  if (_persisting) {
204  DbStorage dbs;
205  dbs.setPersistLocation(LogicalLocation(_location));
206  dbs.startTransaction();
207  dbs.dropTable(tableName);
208  dbs.endTransaction();
209  }
210  else {
211  DbStorage::dropTable(tableName);
212  }
213 }
std::string _location
Database location URL.
Definition: DbTsvStorage.h:104
virtual void dropTable(std::string const &tableName)
Drop a table.
Definition: DbStorage.cc:121
DbStorage(void)
Constructor.
Definition: DbStorage.cc:57

◆ endTransaction()

void lsst::daf::persistence::DbTsvStorage::endTransaction ( void  )
virtual

End a transaction.

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 114 of file DbTsvStorage.cc.

114  {
115  if (!_persisting) {
117  return;
118  }
119 
120  // close stream
121  _osp->close();
122 
123  MYSQL* db = mysql_init(0);
124  if (db == 0) {
125  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unable to allocate MySQL connection");
126  }
127  DbStorageLocation dbLoc(_location);
128  unsigned int port = strtoul(dbLoc.getPort().c_str(), 0, 10);
129  if (mysql_real_connect(db,
130  dbLoc.getHostname().c_str(),
131  dbLoc.getUsername().c_str(),
132  dbLoc.getPassword().c_str(),
133  dbLoc.getDbName().c_str(),
134  port, 0,
135  CLIENT_COMPRESS | CLIENT_LOCAL_FILES) == 0) {
137  "Unable to connect to MySQL database: " + _location);
138  }
139  if (mysql_options(db, MYSQL_OPT_LOCAL_INFILE, 0) != 0) {
141  std::string("Unable to set LOCAL INFILE option - * ") +
142  mysql_error(db));
143  }
144 
145  std::string query = "LOAD DATA LOCAL INFILE";
146  query += " '";
147  query += _fileName;
148  query += "'";
149  query += " REPLACE";
150  query += " INTO TABLE `" + _tableName;
151  query += "` (";
153  it != _colMap.end(); ++it) {
154  _rowBuffer[it->second] = it->first;
155  }
157  it != _rowBuffer.end(); ++it) {
158  if (it != _rowBuffer.begin()) query += " ,";
159  query += *it;
160  }
161  query += ")";
162 
163  if (mysql_query(db, query.c_str()) != 0) {
164  mysql_close(db);
166  "Unable to load data into database table: " + _tableName +
167  "- * " + mysql_error(db));
168  }
169  mysql_close(db);
170 
171  // unlink file
172  if (!_saveTemp) {
173  unlink(_fileName);
174  }
175  delete[] _fileName;
176 }
std::vector< std::string > _rowBuffer
Definition: DbTsvStorage.h:108
char * _fileName
Full pathname for temporary TSV file.
Definition: DbTsvStorage.h:103
T end(T... args)
std::string _location
Database location URL.
Definition: DbTsvStorage.h:104
STL class.
STL class.
std::map< std::string, int > _colMap
Map from column names to positions.
Definition: DbTsvStorage.h:106
bool _saveTemp
Do not delete temporary TSV file if true.
Definition: DbTsvStorage.h:101
T close(T... args)
virtual void endTransaction(void)
End a transaction.
Definition: DbStorage.cc:101
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
virtual void query(void)
Execute the query.
Definition: DbStorage.cc:248
STL class.
T begin(T... args)
T strtoul(T... args)
T c_str(T... args)
std::unique_ptr< std::ofstream > _osp
Output TSV stream.
Definition: DbTsvStorage.h:109

◆ executeSql()

void lsst::daf::persistence::DbStorage::executeSql ( std::string const &  sqlStatement)
virtualinherited

Execute an arbitrary SQL statement.

Use primarily to perform server-side computations or complex DDL.

Parameters
[in]sqlStatementSQL statement to be executed. Must not end in ";".

Definition at line 136 of file DbStorage.cc.

136  {
137  _impl->executeSql(sqlStatement);
138 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ finishQuery()

void lsst::daf::persistence::DbStorage::finishQuery ( void  )
virtualinherited

Indicate that query processing is finished.

Must be called after next() returns false; no getColumnByPos() or columnIsNull() calls may be made after this method is called.

Definition at line 278 of file DbStorage.cc.

278  {
279  _impl->finishQuery();
280 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ getColumnByPos()

template<typename T >
T const & lsst::daf::persistence::DbTsvStorage::getColumnByPos ( int  pos)

Get the value of a column of the query result row by position.

Parameters
[in]posPosition of the column (starts at 0)

Definition at line 376 of file DbTsvStorage.cc.

376  {
377  return DbStorage::getColumnByPos<T>(pos);
378 }

◆ getId()

dafBase::Citizen::memId lsst::daf::base::Citizen::getId ( ) const
inherited

Return the Citizen's ID.

Definition at line 224 of file Citizen.cc.

224  {
225  return _CitizenId;
226 }

◆ getNextMemId()

dafBase::Citizen::memId lsst::daf::base::Citizen::getNextMemId ( )
staticinherited

Return the memId of the next object to be allocated.

Definition at line 229 of file Citizen.cc.

229  {
230  return _nextMemId();
231 }
static memId _nextMemId(void)
Return the memId of the next object to be allocated.
Definition: Citizen.cc:234

◆ groupBy()

void lsst::daf::persistence::DbStorage::groupBy ( std::string const &  expression)
virtualinherited

Request that the query output be grouped by an expression.

Parameters
[in]expressionText of the SQL expression

Definition at line 233 of file DbStorage.cc.

233  {
234  _impl->groupBy(expression);
235 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ hasBeenCorrupted()

bool lsst::daf::base::Citizen::hasBeenCorrupted ( )
staticinherited

Check all allocated blocks for corruption.

Definition at line 353 of file Citizen.cc.

353  {
354  ReadGuard guard(citizenLock);
355  for (table::iterator cur = _activeCitizens.begin();
356  cur != _activeCitizens.end(); cur++) {
357  if (cur->first->_hasBeenCorrupted()) {
358  return true;
359  }
360  }
361  for (table::iterator cur = _persistentCitizens.begin();
362  cur != _persistentCitizens.end(); cur++) {
363  if (cur->first->_hasBeenCorrupted()) {
364  return true;
365  }
366  }
367 
368  return false;
369 }
T end(T... args)
static table _activeCitizens
Definition: Citizen.h:102
static table _persistentCitizens
Definition: Citizen.h:103
T begin(T... args)

◆ init()

int lsst::daf::base::Citizen::init ( )
staticinherited

Called once when the memory system is being initialised.

Definition at line 214 of file Citizen.cc.

214  {
215  volatile int dummy = 1;
216  return dummy;
217 }

◆ insertRow()

void lsst::daf::persistence::DbTsvStorage::insertRow ( void  )
virtual

Insert the row.

Row values must have been set with setColumn() calls.

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 334 of file DbTsvStorage.cc.

334  {
335  // Output row to stream
337  i != _rowBuffer.end(); ++i) {
338  if (i != _rowBuffer.begin()) *_osp << '\t';
339  *_osp << *i;
340  }
341  // \todo Optimization: if columns all outputted, just put out endl
342  *_osp << std::endl;
343 }
std::vector< std::string > _rowBuffer
Definition: DbTsvStorage.h:108
T endl(T... args)
T end(T... args)
STL class.
T begin(T... args)
std::unique_ptr< std::ofstream > _osp
Output TSV stream.
Definition: DbTsvStorage.h:109

◆ markPersistent()

void lsst::daf::base::Citizen::markPersistent ( void  )
inherited

Mark a Citizen as persistent and not destroyed until process end.

Definition at line 254 of file Citizen.cc.

254  {
255  WriteGuard guard(citizenLock);
256  _persistentCitizens[this] = _activeCitizens[this];
257  _activeCitizens.erase(this);
258 }
static table _activeCitizens
Definition: Citizen.h:102
T erase(T... args)
static table _persistentCitizens
Definition: Citizen.h:103

◆ next()

bool lsst::daf::persistence::DbStorage::next ( void  )
virtualinherited

Move to the next (first) row of the query result.

Returns
false if no more rows

Definition at line 255 of file DbStorage.cc.

255  {
256  return _impl->next();
257 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ orderBy()

void lsst::daf::persistence::DbStorage::orderBy ( std::string const &  expression)
virtualinherited

Request that the query output be sorted by an expression.

Multiple expressions may be specified, in order.

Parameters
[in]expressionText of the SQL expression

Definition at line 226 of file DbStorage.cc.

226  {
227  _impl->orderBy(expression);
228 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ outColumn()

void lsst::daf::persistence::DbStorage::outColumn ( std::string const &  columnName,
bool  isExpr = false 
)
virtualinherited

Request a column in the query output.

Parameters
[in]columnNameName of the column
[in]isExprTrue if the name is actually an expression

The order of outColumn() calls is the order of appearance in the output row. Use either outColumn() or outParam() but not both.

Definition at line 194 of file DbStorage.cc.

194  {
195  _impl->outColumn(columnName, isExpr);
196 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ outParam()

template<typename T >
void lsst::daf::persistence::DbTsvStorage::outParam ( std::string const &  columnName,
T *  location,
bool  isExpr = false 
)

Request a column in the query output and bind a destination location.

Parameters
[in]columnNameName of the column
[in]locationPointer to the destination
[in]isExprTrue if the name is actually an expression

The order of outParam() calls is the order of appearance in the output row. Use either outColumn() or outParam() but not both.

Definition at line 357 of file DbTsvStorage.cc.

358  {
359  DbStorage::outParam<T>(columnName, location, isExpr);
360 }

◆ query()

void lsst::daf::persistence::DbStorage::query ( void  )
virtualinherited

Execute the query.

Definition at line 248 of file DbStorage.cc.

248  {
249  _impl->query();
250 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ repr()

std::string lsst::daf::base::Citizen::repr ( ) const
inherited

Return a string representation of a Citizen.

Definition at line 245 of file Citizen.cc.

245  {
246  return boost::str(boost::format("%d: %08x %s")
247  % _CitizenId
248  % this
250  );
251 }
std::string demangleType(std::string const _typeName)
Definition: Demangle.cc:113
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:134
const char * _typeName
Definition: Citizen.h:94

◆ setColumn() [1/5]

template<typename T >
void lsst::daf::persistence::DbTsvStorage::setColumn ( std::string const &  columnName,
T const &  value 
)

Set the value to insert in a given column.

Parameters
[in]columnNameName of the column
[in]valueValue to set in the column

Definition at line 270 of file DbTsvStorage.cc.

270  {
271  int colIndex = _getColumnIndex(columnName);
272 
273  // set value in row buffer
274  // \todo quote value as appropriate
276  _convertStream << value;
277  _rowBuffer[colIndex] = _convertStream.str();
278 
279  // \todo Optimization: if next column, output now, plus any others saved
280 }
int _getColumnIndex(std::string const &columnName)
Get the index of a given column.
std::vector< std::string > _rowBuffer
Definition: DbTsvStorage.h:108
STL class.
std::ostringstream _convertStream
Stream to convert to text.
Definition: DbTsvStorage.h:107
T str(T... args)

◆ setColumn() [2/5]

template<>
void lsst::daf::persistence::DbTsvStorage::setColumn ( std::string const &  columnName,
char const &  value 
)

Definition at line 284 of file DbTsvStorage.cc.

285  {
286  int colIndex = _getColumnIndex(columnName);
288  _convertStream << static_cast<int>(value);
289  _rowBuffer[colIndex] = _convertStream.str();
290 }
int _getColumnIndex(std::string const &columnName)
Get the index of a given column.
std::vector< std::string > _rowBuffer
Definition: DbTsvStorage.h:108
STL class.
std::ostringstream _convertStream
Stream to convert to text.
Definition: DbTsvStorage.h:107
T str(T... args)

◆ setColumn() [3/5]

template<>
void lsst::daf::persistence::DbTsvStorage::setColumn ( std::string const &  columnName,
double const &  value 
)

Definition at line 294 of file DbTsvStorage.cc.

295  {
296  int colIndex = _getColumnIndex(columnName);
298  _convertStream << std::setprecision(17) << value;
299  _rowBuffer[colIndex] = _convertStream.str();
300 }
int _getColumnIndex(std::string const &columnName)
Get the index of a given column.
std::vector< std::string > _rowBuffer
Definition: DbTsvStorage.h:108
STL class.
std::ostringstream _convertStream
Stream to convert to text.
Definition: DbTsvStorage.h:107
T str(T... args)
T setprecision(T... args)

◆ setColumn() [4/5]

template<>
void lsst::daf::persistence::DbTsvStorage::setColumn ( std::string const &  columnName,
float const &  value 
)

Definition at line 303 of file DbTsvStorage.cc.

304  {
305  int colIndex = _getColumnIndex(columnName);
307  _convertStream << std::setprecision(9) << value;
308  _rowBuffer[colIndex] = _convertStream.str();
309 }
int _getColumnIndex(std::string const &columnName)
Get the index of a given column.
std::vector< std::string > _rowBuffer
Definition: DbTsvStorage.h:108
STL class.
std::ostringstream _convertStream
Stream to convert to text.
Definition: DbTsvStorage.h:107
T str(T... args)
T setprecision(T... args)

◆ setColumn() [5/5]

template<>
void lsst::daf::persistence::DbTsvStorage::setColumn ( std::string const &  columnName,
DateTime const &  value 
)

Definition at line 313 of file DbTsvStorage.cc.

314  {
315  int colIndex = _getColumnIndex(columnName);
317  struct tm t = value.gmtime(DateTime::UTC);
318  char buf[20];
319  strftime(buf, sizeof(buf), "%F %T", &t);
320  _rowBuffer[colIndex] = std::string(buf);
321 }
int _getColumnIndex(std::string const &columnName)
Get the index of a given column.
std::vector< std::string > _rowBuffer
Definition: DbTsvStorage.h:108
STL class.
std::ostringstream _convertStream
Stream to convert to text.
Definition: DbTsvStorage.h:107
T strftime(T... args)
T str(T... args)

◆ setColumnToNull()

void lsst::daf::persistence::DbTsvStorage::setColumnToNull ( std::string const &  columnName)
virtual

Set a given column to NULL.

Parameters
[in]columnNameName of the column

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 326 of file DbTsvStorage.cc.

326  {
327  int colIndex = _getColumnIndex(columnName);
328  _rowBuffer[colIndex] = "\\N";
329 }
int _getColumnIndex(std::string const &columnName)
Get the index of a given column.
std::vector< std::string > _rowBuffer
Definition: DbTsvStorage.h:108

◆ setCorruptionCallback()

dafBase::Citizen::memCallback lsst::daf::base::Citizen::setCorruptionCallback ( Citizen::memCallback  func)
staticinherited

Set the CorruptionCallback function.

Parameters
funcfunction be called when block is found to be corrupted

Definition at line 432 of file Citizen.cc.

434  {
436  _corruptionCallback = func;
437 
438  return old;
439 }
memId(* memCallback)(const Citizen *ptr)
Definition: Citizen.h:59
static memCallback _corruptionCallback
Definition: Citizen.h:113

◆ setDeleteCallback()

dafBase::Citizen::memCallback lsst::daf::base::Citizen::setDeleteCallback ( Citizen::memCallback  func)
staticinherited

Set the DeleteCallback function.

Parameters
funcfunction be called when desired block is deleted

Definition at line 422 of file Citizen.cc.

424  {
426  _deleteCallback = func;
427 
428  return old;
429 }
static memCallback _deleteCallback
Definition: Citizen.h:112
memId(* memCallback)(const Citizen *ptr)
Definition: Citizen.h:59

◆ setDeleteCallbackId()

dafBase::Citizen::memId lsst::daf::base::Citizen::setDeleteCallbackId ( Citizen::memId  id)
staticinherited

Call the current DeleteCallback when block is deleted.

Parameters
idDesired ID

Definition at line 387 of file Citizen.cc.

389  {
390  WriteGuard guard(citizenLock);
391  Citizen::memId oldId = _deleteId;
392  _deleteId = id;
393 
394  return oldId;
395 }
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:56
int id
Definition: CR.cc:155
static memId _deleteId
Definition: Citizen.h:109

◆ setNewCallback()

dafBase::Citizen::memNewCallback lsst::daf::base::Citizen::setNewCallback ( Citizen::memNewCallback  func)
staticinherited

Set the NewCallback function.

Parameters
funcThe new function to be called when a designated block is allocated

Definition at line 412 of file Citizen.cc.

414  {
416  _newCallback = func;
417 
418  return old;
419 }
memId(* memNewCallback)(const memId cid)
A function used to register a callback.
Definition: Citizen.h:58
static memNewCallback _newCallback
Definition: Citizen.h:111

◆ setNewCallbackId()

dafBase::Citizen::memId lsst::daf::base::Citizen::setNewCallbackId ( Citizen::memId  id)
staticinherited

Call the NewCallback when block is allocated.

Parameters
idDesired ID

Definition at line 376 of file Citizen.cc.

378  {
379  WriteGuard guard(citizenLock);
380  Citizen::memId oldId = _newId;
381  _newId = id;
382 
383  return oldId;
384 }
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:56
static memId _newId
Definition: Citizen.h:108
int id
Definition: CR.cc:155

◆ setPersistLocation()

void lsst::daf::persistence::DbTsvStorage::setPersistLocation ( LogicalLocation const &  location)
virtual

Set the database location to persist to.

Parameters
[in]locationDatabase connection string to insert to.

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 91 of file DbTsvStorage.cc.

91  {
92  _persisting = true;
93  _location = location.locString();
94  // Set the timezone for any DATE/TIME/DATETIME fields.
95  setenv("TZ", "UTC", 1);
96 }
std::string _location
Database location URL.
Definition: DbTsvStorage.h:104

◆ setPolicy()

void lsst::daf::persistence::DbTsvStorage::setPolicy ( lsst::pex::policy::Policy::Ptr  policy)
virtual

Allow a policy to be used to configure the DbTsvStorage.

Parameters
[in]policy

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 78 of file DbTsvStorage.cc.

78  {
79  _tempPath = "/tmp";
80  if (policy && policy->exists("TempPath")) {
81  _tempPath = policy->getString("TempPath");
82  }
83  if (policy && policy->exists("SaveTemp") && policy->getBool("SaveTemp")) {
84  _saveTemp = true;
85  }
86 }
std::string _tempPath
Directory pathname for temporary TSV file.
Definition: DbTsvStorage.h:102
bool _saveTemp
Do not delete temporary TSV file if true.
Definition: DbTsvStorage.h:101

◆ setQueryWhere()

void lsst::daf::persistence::DbStorage::setQueryWhere ( std::string const &  whereClause)
virtualinherited

Set the condition for the WHERE clause of the query.

Parameters
[in]whereClauseSQL text of the WHERE clause

May include join conditions.

Definition at line 242 of file DbStorage.cc.

242  {
243  _impl->setQueryWhere(whereClause);
244 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ setRetrieveLocation()

void lsst::daf::persistence::DbTsvStorage::setRetrieveLocation ( LogicalLocation const &  location)
virtual

Set the database location to retrieve from.

Parameters
[in]locationDatabase connection string to retrieve from.

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 101 of file DbTsvStorage.cc.

101  {
102  _persisting = false;
104 }
virtual void setRetrieveLocation(LogicalLocation const &location)
Set the database location to retrieve from.
Definition: DbStorage.cc:89

◆ setTableForInsert()

void lsst::daf::persistence::DbTsvStorage::setTableForInsert ( std::string const &  tableName)
virtual

Set the table to insert rows into.

Parameters
[in]tableNameName of the table

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 234 of file DbTsvStorage.cc.

234  {
235  _tableName = tableName;
236  std::string templ = _tempPath + "/" + tableName + ".XXXXXX";
237  _fileName = new char[templ.size() + 1];
238  strncpy(_fileName, templ.c_str(), templ.size());
239  _fileName[templ.size()] = '\0';
240  int fd = mkstemp(_fileName);
241  // \todo check for errors
242  close(fd);
243  _osp.reset(new std::ofstream(_fileName));
244 }
char * _fileName
Full pathname for temporary TSV file.
Definition: DbTsvStorage.h:103
STL class.
STL class.
std::string _tempPath
Directory pathname for temporary TSV file.
Definition: DbTsvStorage.h:102
T strncpy(T... args)
T reset(T... args)
T size(T... args)
T c_str(T... args)
std::unique_ptr< std::ofstream > _osp
Output TSV stream.
Definition: DbTsvStorage.h:109

◆ setTableForQuery()

void lsst::daf::persistence::DbStorage::setTableForQuery ( std::string const &  tableName,
bool  isExpr = false 
)
virtualinherited

Set the table to query (single-table queries only).

Parameters
[in]tableNameName of the table
[in]isExprTrue if the name is actually a table expression

Definition at line 175 of file DbStorage.cc.

175  {
176  _impl->setTableForQuery(tableName, isExpr);
177 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ setTableListForQuery()

void lsst::daf::persistence::DbStorage::setTableListForQuery ( std::vector< std::string > const &  tableNameList)
virtualinherited

Set a list of tables to query (multiple-table queries).

Parameters
[in]tableNameListVector of names of tables

Definition at line 182 of file DbStorage.cc.

183  {
184  _impl->setTableListForQuery(tableNameList);
185 }
std::unique_ptr< DbStorageImpl > _impl
Implementation class for isolation.
Definition: DbStorage.h:113

◆ startTransaction()

void lsst::daf::persistence::DbTsvStorage::startTransaction ( void  )
virtual

Start a transaction.

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 108 of file DbTsvStorage.cc.

108  {
110 }
virtual void startTransaction(void)
Start a transaction.
Definition: DbStorage.cc:95

◆ truncateTable()

void lsst::daf::persistence::DbTsvStorage::truncateTable ( std::string const &  tableName)
virtual

Truncate a table.

Parameters
[in]tableNameName of the table to truncate

Reimplemented from lsst::daf::persistence::DbStorage.

Definition at line 218 of file DbTsvStorage.cc.

218  {
219  if (_persisting) {
220  DbStorage dbs;
221  dbs.setPersistLocation(LogicalLocation(_location));
222  dbs.startTransaction();
223  dbs.truncateTable(tableName);
224  dbs.endTransaction();
225  }
226  else {
227  DbStorage::truncateTable(tableName);
228  }
229 }
std::string _location
Database location URL.
Definition: DbTsvStorage.h:104
DbStorage(void)
Constructor.
Definition: DbStorage.cc:57
virtual void truncateTable(std::string const &tableName)
Truncate a table.
Definition: DbStorage.cc:128

◆ verifyPathName()

void lsst::daf::persistence::FormatterStorage::verifyPathName ( std::string const &  name)
protectedinherited

Ensure that all directories along a path exist, creating them if necessary.

Parameters
[in]namePathname to file to be created

Definition at line 93 of file FormatterStorage.cc.

93  {
94  // Get the directory by stripping off anything after the last slash.
95  std::string::size_type pos = name.find_last_of('/');
96  if (pos == std::string::npos) return;
97  std::string dirName = name.substr(0, pos);
98 
99  // Check to see if the directory exists.
100  struct stat buf;
101  int ret = ::stat(dirName.c_str(), &buf);
102 
103  if (ret == -1 && errno == ENOENT) {
104  // It doesn't; check its parent and then create it.
105  verifyPathName(dirName);
106 
107  ret = ::mkdir(dirName.c_str(), 0777);
108 
109  // If it already exists, we're OK; otherwise, throw an exception.
110  if (ret == -1 && errno != EEXIST) {
112  dirName + ": Error creating directory = " + std::strerror(errno));
113  }
114  }
115  else if (ret == -1) {
116  // We couldn't read the (existing) directory for some reason.
118  dirName + ": Error searching for directory = " + std::strerror(errno));
119  }
120  else if (!S_ISDIR(buf.st_mode)) {
121  // It's not a directory.
123  dirName + ": Non-directory in path");
124  }
125 }
table::Key< std::string > name
Definition: ApCorrMap.cc:71
T strerror(T... args)
STL class.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
T c_str(T... args)
void verifyPathName(std::string const &pathName)
Ensure that all directories along a path exist, creating them if necessary.

Member Data Documentation

◆ _colMap

std::map<std::string, int> lsst::daf::persistence::DbTsvStorage::_colMap
private

Map from column names to positions.

Definition at line 106 of file DbTsvStorage.h.

◆ _convertStream

std::ostringstream lsst::daf::persistence::DbTsvStorage::_convertStream
private

Stream to convert to text.

Definition at line 107 of file DbTsvStorage.h.

◆ _fileName

char* lsst::daf::persistence::DbTsvStorage::_fileName
private

Full pathname for temporary TSV file.

Definition at line 103 of file DbTsvStorage.h.

◆ _location

std::string lsst::daf::persistence::DbTsvStorage::_location
private

Database location URL.

Definition at line 104 of file DbTsvStorage.h.

◆ _osp

std::unique_ptr<std::ofstream> lsst::daf::persistence::DbTsvStorage::_osp
private

Output TSV stream.

Definition at line 109 of file DbTsvStorage.h.

◆ _persisting

bool lsst::daf::persistence::DbTsvStorage::_persisting
private

Definition at line 100 of file DbTsvStorage.h.

◆ _rowBuffer

std::vector<std::string> lsst::daf::persistence::DbTsvStorage::_rowBuffer
private

Definition at line 108 of file DbTsvStorage.h.

◆ _saveTemp

bool lsst::daf::persistence::DbTsvStorage::_saveTemp
private

Do not delete temporary TSV file if true.

Definition at line 101 of file DbTsvStorage.h.

◆ _tableName

std::string lsst::daf::persistence::DbTsvStorage::_tableName
private

Definition at line 105 of file DbTsvStorage.h.

◆ _tempPath

std::string lsst::daf::persistence::DbTsvStorage::_tempPath
private

Directory pathname for temporary TSV file.

Definition at line 102 of file DbTsvStorage.h.


The documentation for this class was generated from the following files: