The stor data storage library
C++ version
Marty Olevitch
Laboratory for Experimental Astrophysics
Physics Dept, Washington University in St. Louis
St. Louis, MO 63130 USA
marty-stor@cosray.wustl.edu
Contents
The stor data storage library is a C++ class to aid in the
storage of data acquired, especially in real-time situations such as saving
data from balloon flight or accelerator experiments. Use of this class
frees the programmer from dealing with the mechanics of saving the incoming
data. After initialization, one simple function call saves data. Other
calls can be used to find out how much data has been stored, etc.
Version 2.05 of the stor library has been run under UNIX
(Linux) using the GNU C++ compiler.
The diagram below shows a representation of the data directory tree. The
root of the tree is noted in the diagram by datadir. This directory
contains other directories, one for each ``run''. In the diagram, the runs
are 5-digit numbers. In general, a new run is started each time your
application program starts up. The root directory also contains a file
called .stor_cur_run (in the DOS version, this file is called
_cur_run) which contains the current run number. The library
maintains this file to keep track of the run numbers. Delete it only at the
risk of already saved data!
Inside each run directory are one or more data files. The names of these
files are represented as 6-digit numbers. The size of each data file is
specified by the programmer in the constructor call. When this
limit is reached, the library will automatically open the next data file.
Typically, a data file will hold many events.
This code fragment illustrates the typical use of the library routines.
#include <iostream>
#include <string>
#include <stor.hpp>
...
unsigned char dp[DSIZE];
int count;
int ret;
...
/* initialize stor to save the data in the directory
* rooted at /data/misc/exp1. Each data file will be
* a maximum of 100,000 bytes */
try {
stor st("/data/misc/exp1", 100000);
} catch (string e) {
std::cerr << e << "\n";
exit(1);
}
...
while (1) {
/* read some data from the telemetry (not part of
* the stor library! */
count = read_telemetry_data(dp);
if (count > 0) {
/* save count bytes of data in the current data file */
try {
ret = st.save_data(dp, count);
} catch (std::string s) {
std::cerr << s << "\n";
exit(1);
}
switch (ret) {
case 0:
/* okay */
break;
case 1:
/* okay and started new data file */
cout << "started new file " << st.cur_file() << endl;
break;
default:
cerr << "Bad return code " << ret << endl;
break;
}
/* print some statistics */
cout << "Total bytes saved: " << st.total_bytes() << endl;
cout << "Bytes in current file: " << st.bytes_cur_file() << endl;
}
}
The routines which may have problems opening files or writing data, etc all
throw a std::string as an exception.
For example, see the code fragment above.
Each stor object maintains files of comments. One file is located in the
root data directory and another is located in the current run directory.
Both have the name ``stor_comments''.
By default, the constructor
and destructor both insert a comment into the each file.
The default comment can be suppressed by calling the
constructor with the third parameter set to ``false''.
Additionally, an application program can insert its own comments using the
member function stor::comment.
You can get by using just the following constructor and the
save_data member function.
-
stor::stor(string datadir, long maxfilesize, bool
do_default_comment = true)
- datadir is a string containing the name of the
root data directory
- maxfilesize should contain the maximum possible
file size for each data file.
- do_default_comment controls whether the stor
object will write
default comments or not. The default is
true, to write the comments.
Construct a stor object. If there are any problems, the
constructor throws an exception. It throws a string
containing an error message.
-
int stor::save_data(const char *dp, int nbytes)
- dp is a pointer to the data to be saved
- nbytes is the size in bytes of each data item
Write the data pointed to by dp to the current data
file. Returns 0 if all went well, 1 if the amount of data
caused us to open a new data file, or throws a string
containing an error message if there is an
error.
The following routines are not absolutely necessary, but can be useful if
you want to know some of the library's internal state.
-
long stor::cur_run()
-
Returns the integer value of the current run.
-
long stor::cur_file()
-
Returns the integer value of the current data file.
-
long stor::bytes_cur_file()
-
Returns the number of bytes written so far to the current
data file.
-
unsigned long stor::total_bytes()
-
Returns the number of bytes written since the stor object
was constructed.
-
string stor::cur_run_str()
-
Returns a string containing
the five-character name of the current run (for example
"00345").
-
string stor::cur_file_str()
-
Returns a string containing
the six-character name of the current data file (for example
"001429").
-
string stor::file_path()
-
Returns a string containing
the full path name of the current data file
(for example "/data/tiger/00345/001429").
The following routines are not necessarily needed in ordinary use.
However, in special situtaions, they may be useful.
-
void stor::comment(string& s)
void stor::comment(const char *s)
-
The string s contains, or the pointer s points to, a
comment. This member function writes the comment (after
prepending the date to it) to the file called stor_comments
in the data directory, and also the file stor_comments in
the run directory.
See comments.
-
void stor::flush()
-
Flushes any data to the current file.
Throws a std::string
containing an error message if
there is an error.
-
long stor::next_file()
-
Closes the current data file and opens the next one.
Returns the new data file number (an non-negative integer)
if successful, Throws a std::string containing an error
message if
there is an error.
-
void stor::next_run()
-
Closes the current run and starts the next one. The current
data file is closed and the new data file number is set to
0. Throws a std::string if
there is an error.
-
string stor::version()
-
Returns a string containing the stor software version.