1 #ifndef CYCLUS_SRC_QUERY_BACKEND_H_ 2 #define CYCLUS_SRC_QUERY_BACKEND_H_ 8 #include <boost/version.hpp> 10 #if BOOST_VERSION / 100 % 1000 <= 67 11 #include <boost/uuid/sha1.hpp> 13 #include <boost/uuid/detail/sha1.hpp> 20 #define CYCLUS_UUID_SIZE 16 21 #define CYCLUS_SHA1_SIZE 20 22 #define CYCLUS_SHA1_NINT 5 407 throw ValueError(
"operation '" + op +
"' not valid for field '" + \
424 typedef std::vector<boost::spirit::hold_any>
QueryRow;
462 throw StateError(
"No rows found during query for field " + field);
464 if (row >= rows.size()) {
465 throw KeyError(
"index larger than number of query rows for field " 470 for (
int i = 0; i < fields.size(); ++i) {
471 if (fields[i] == field) {
476 if (field_idx == -1) {
477 throw KeyError(
"query result has no such field " + field);
480 return rows[row][field_idx].cast<T>();
488 DbTypes default_dbtype, std::vector<int> default_shape) :
489 table(default_table), col(default_col), index(default_index), dbtype(default_dbtype),
490 shape(default_shape) {};
509 virtual std::map<std::string, DbTypes> ColumnTypes(
std::string table) = 0;
512 virtual std::list<ColumnInfo> Schema(
std::string table) = 0;
515 virtual std::set<std::string> Tables() = 0;
530 to_inject_(to_inject) {}
534 return b_->Query(table, &to_inject_);
537 std::vector<Cond> c = *conds;
538 for (
int i = 0; i < to_inject_.size(); ++i) {
539 c.push_back(to_inject_[i]);
541 return b_->Query(table, &c);
545 return b_->ColumnTypes(table);
549 return b_->Schema(table);
552 virtual std::set<std::string>
Tables() {
return b_->Tables(); }
556 std::vector<Cond> to_inject_;
570 return b_->Query(prefix_ + table, conds);
574 return b_->ColumnTypes(table);
578 return b_->Schema(table);
581 virtual std::set<std::string>
Tables() {
return b_->Tables(); }
589 template <
typename T>
594 rtn = (*x) < cond->
val.
cast<T>();
598 rtn = (*x) > cond->
val.
cast<T>();
602 rtn = (*x) <= cond->
val.
cast<T>();
606 rtn = (*x) >= cond->
val.
cast<T>();
610 rtn = (*x) == cond->
val.
cast<T>();
614 rtn = (*x) != cond->
val.
cast<T>();
622 template <
typename T>
623 inline bool CmpConds(T* x, std::vector<Cond*>* conds) {
625 for (i = 0; i < conds->size(); ++i)
626 if (!CmpCond<T>(&(*x), (*conds)[i]))
652 template <
typename T>
653 inline std::vector<T>
cast()
const {
656 rtn[i] = static_cast<T>(val[i]);
662 return out <<
"[" << val[0] <<
", " << val[1] <<
", " << val[2] << \
663 ", " << val[3] <<
", " << val[4] <<
"]";
669 if (val[i] < rhs.
val[i]) {
672 }
else if (val[i] > rhs.
val[i]) {
685 return !operator>(rhs);
695 if (val[i] != rhs.
val[i]) {
710 Sha1() { hash_ = boost::uuids::detail::sha1(); }
713 inline void Clear() { hash_.reset(); }
718 hash_.process_bytes(s.c_str(), s.size());
723 inline void Update(
const std::vector<int>& x) {
724 hash_.process_bytes(&x[0], x.size() *
sizeof(int));
727 inline void Update(
const std::vector<float>& x) {
728 hash_.process_bytes(&x[0], x.size() *
sizeof(float));
731 inline void Update(
const std::vector<double>& x) {
732 hash_.process_bytes(&x[0], x.size() *
sizeof(double));
735 inline void Update(
const std::vector<std::string>& x) {
736 for (
unsigned int i = 0; i < x.size(); ++i)
737 hash_.process_bytes(x[i].c_str(), x[i].size());
740 inline void Update(
const std::vector<cyclus::Blob>& x) {
741 for (
unsigned int i = 0; i < x.size(); ++i)
742 hash_.process_bytes(x[i].str().c_str(), x[i].str().size());
745 inline void Update(
const std::vector<boost::uuids::uuid>& x) {
746 std::vector<boost::uuids::uuid>::const_iterator it = x.begin();
747 for (; it != x.end(); ++it)
751 inline void Update(
const std::set<int>& x) {
752 std::set<int>::iterator it = x.begin();
753 for (; it != x.end(); ++it)
754 hash_.process_bytes(&(*it),
sizeof(
int));
757 inline void Update(
const std::set<bool>& x) {
758 std::set<bool>::iterator it = x.begin();
759 for (; it != x.end(); ++it)
760 hash_.process_bytes(&(*it),
sizeof(
bool));
763 inline void Update(
const std::set<double>& x) {
764 std::set<double>::iterator it = x.begin();
765 for (; it != x.end(); ++it)
766 hash_.process_bytes(&(*it),
sizeof(
double));
769 inline void Update(
const std::set<float>& x) {
770 std::set<float>::iterator it = x.begin();
771 for (; it != x.end(); ++it)
772 hash_.process_bytes(&(*it),
sizeof(
float));
775 inline void Update(
const std::set<cyclus::Blob>& x) {
776 std::set<cyclus::Blob>::iterator it = x.begin();
777 for (; it != x.end(); ++it)
778 hash_.process_bytes(it->str().c_str(), it->str().size());
781 inline void Update(
const std::set<boost::uuids::uuid>& x) {
782 std::set<boost::uuids::uuid>::iterator it = x.begin();
783 for (; it != x.end(); ++it)
787 inline void Update(
const std::set<std::string>& x) {
788 std::set<std::string>::iterator it = x.begin();
789 for (; it != x.end(); ++it)
790 hash_.process_bytes(it->c_str(), it->size());
793 inline void Update(
const std::list<int>& x) {
794 std::list<int>::const_iterator it = x.begin();
795 for (; it != x.end(); ++it)
796 hash_.process_bytes(&(*it),
sizeof(
int));
799 inline void Update(
const std::list<bool>& x) {
800 std::list<bool>::const_iterator it = x.begin();
801 for (; it != x.end(); ++it)
802 hash_.process_bytes(&(*it),
sizeof(
bool));
805 inline void Update(
const std::list<double>& x) {
806 std::list<double>::const_iterator it = x.begin();
807 for (; it != x.end(); ++it)
808 hash_.process_bytes(&(*it),
sizeof(
double));
811 inline void Update(
const std::list<float>& x) {
812 std::list<float>::const_iterator it = x.begin();
813 for (; it != x.end(); ++it)
814 hash_.process_bytes(&(*it),
sizeof(
float));
817 inline void Update(
const std::list<std::string>& x) {
818 std::list<std::string>::const_iterator it = x.begin();
819 for (; it != x.end(); ++it)
820 hash_.process_bytes(it->c_str(), it->size());
823 inline void Update(
const std::list<cyclus::Blob>& x) {
824 std::list<cyclus::Blob>::const_iterator it = x.begin();
825 for (; it != x.end(); ++it)
826 hash_.process_bytes(it->str().c_str(), it->str().size());
829 inline void Update(
const std::list<boost::uuids::uuid>& x) {
830 std::list<boost::uuids::uuid>::const_iterator it = x.begin();
831 for (; it != x.end(); ++it)
835 inline void Update(
const std::pair<int, int>& x) {
836 hash_.process_bytes(&(x.first),
sizeof(
int));
837 hash_.process_bytes(&(x.second),
sizeof(
int));
840 inline void Update(
const std::pair<int, std::string>& x) {
841 hash_.process_bytes(&(x.first),
sizeof(
int));
842 hash_.process_bytes(x.second.c_str(), x.second.size());
845 inline void Update(
const std::map<int, int>& x) {
846 std::map<int, int>::const_iterator it = x.begin();
847 for (; it != x.end(); ++it) {
848 hash_.process_bytes(&(it->first),
sizeof(
int));
849 hash_.process_bytes(&(it->second),
sizeof(
int));
853 inline void Update(
const std::map<int, bool>& x) {
854 std::map<int, bool>::const_iterator it = x.begin();
855 for (; it != x.end(); ++it) {
856 hash_.process_bytes(&(it->first),
sizeof(
int));
857 hash_.process_bytes(&(it->second),
sizeof(
bool));
861 inline void Update(
const std::map<int, double>& x) {
862 std::map<int, double>::const_iterator it = x.begin();
863 for (; it != x.end(); ++it) {
864 hash_.process_bytes(&(it->first),
sizeof(
int));
865 hash_.process_bytes(&(it->second),
sizeof(
double));
869 inline void Update(
const std::map<int, float>& x) {
870 std::map<int, float>::const_iterator it = x.begin();
871 for (; it != x.end(); ++it) {
872 hash_.process_bytes(&(it->first),
sizeof(
int));
873 hash_.process_bytes(&(it->second),
sizeof(
float));
877 inline void Update(
const std::map<int, cyclus::Blob>& x) {
878 std::map<int, cyclus::Blob>::const_iterator it = x.begin();
879 for (; it != x.end(); ++it) {
880 hash_.process_bytes(&(it->first),
sizeof(
int));
881 hash_.process_bytes(it->second.str().c_str(), it->second.str().size());
885 inline void Update(
const std::map<int, boost::uuids::uuid>& x) {
886 std::map<int, boost::uuids::uuid>::const_iterator it = x.begin();
887 for (; it != x.end(); ++it) {
888 hash_.process_bytes(&(it->first),
sizeof(
int));
893 inline void Update(
const std::map<int, std::string>& x) {
894 std::map<int, std::string>::const_iterator it = x.begin();
895 for (; it != x.end(); ++it) {
896 hash_.process_bytes(&(it->first),
sizeof(
int));
897 hash_.process_bytes(it->second.c_str(), it->second.size());
901 inline void Update(
const std::map<std::string, int>& x) {
902 std::map<std::string, int>::const_iterator it = x.begin();
903 for (; it != x.end(); ++it) {
904 hash_.process_bytes(it->first.c_str(), it->first.size());
905 hash_.process_bytes(&(it->second),
sizeof(
int));
909 inline void Update(
const std::map<std::string, double>& x) {
910 std::map<std::string, double>::const_iterator it = x.begin();
911 for (; it != x.end(); ++it) {
912 hash_.process_bytes(it->first.c_str(), it->first.size());
913 hash_.process_bytes(&(it->second),
sizeof(
double));
917 inline void Update(
const std::map<std::string, float>& x) {
918 std::map<std::string, float>::const_iterator it = x.begin();
919 for (; it != x.end(); ++it) {
920 hash_.process_bytes(it->first.c_str(), it->first.size());
921 hash_.process_bytes(&(it->second),
sizeof(
float));
925 inline void Update(
const std::map<std::string, bool>& x) {
926 std::map<std::string, bool>::const_iterator it = x.begin();
927 for (; it != x.end(); ++it) {
928 hash_.process_bytes(it->first.c_str(), it->first.size());
929 hash_.process_bytes(&(it->second),
sizeof(
bool));
933 inline void Update(
const std::map<std::string, cyclus::Blob>& x) {
934 std::map<std::string, cyclus::Blob>::const_iterator it = x.begin();
935 for (; it != x.end(); ++it) {
936 hash_.process_bytes(it->first.c_str(), it->first.size());
937 hash_.process_bytes(it->second.str().c_str(), it->second.str().size());
941 inline void Update(
const std::map<std::string, boost::uuids::uuid>& x) {
942 std::map<std::string, boost::uuids::uuid>::const_iterator it = x.begin();
943 for (; it != x.end(); ++it) {
944 hash_.process_bytes(it->first.c_str(), it->first.size());
949 inline void Update(
const std::map<std::string, std::string>& x) {
950 std::map<std::string, std::string>::const_iterator it = x.begin();
951 for (; it != x.end(); ++it) {
952 hash_.process_bytes(it->first.c_str(), it->first.size());
953 hash_.process_bytes(it->second.c_str(), it->second.size());
957 inline void Update(
const std::map<std::pair<int, std::string>,
double>& x) {
958 std::map<std::pair<int, std::string>,
double>::const_iterator it = x.begin();
959 for (; it != x.end(); ++it) {
960 hash_.process_bytes(&(it->first.first),
sizeof(
int));
961 hash_.process_bytes(it->first.second.c_str(), it->first.second.size());
962 hash_.process_bytes(&(it->second),
sizeof(
double));
966 inline void Update(
const std::map<std::pair<std::string, std::string>,
int>& x) {
967 std::map<std::pair<std::string, std::string>,
int>::const_iterator it = x.begin();
968 for (; it != x.end(); ++it) {
969 hash_.process_bytes(it->first.first.c_str(), it->first.first.size());
970 hash_.process_bytes(it->first.second.c_str(), it->first.second.size());
971 hash_.process_bytes(&(it->second),
sizeof(
int));
976 std::map<std::string, std::vector<double>>::const_iterator it = x.begin();
977 for (; it != x.end(); ++it) {
978 hash_.process_bytes(it->first.c_str(), it->first.size());
984 std::map<std::string, std::map<int, double>>::const_iterator it = x.begin();
985 for (; it != x.end(); ++it) {
986 hash_.process_bytes(it->first.c_str(), it->first.size());
991 inline void Update(
const std::map<
int, std::map<std::string, double>>& x) {
992 std::map<int, std::map<std::string, double>>::const_iterator it = x.begin();
993 for (; it != x.end(); ++it) {
994 hash_.process_bytes(&(it->first),
sizeof(
int));
999 inline void Update(
const std::pair<
double, std::map<int, double>>& x) {
1000 hash_.process_bytes(&(x.first),
sizeof(
double));
1005 std::map<std::string, std::pair<double, std::map<int, double>>>::const_iterator it = x.begin();
1006 for (; it != x.end(); ++it) {
1007 hash_.process_bytes(&(it->first), it->first.size());
1012 inline void Update(
const std::pair<
int, std::pair<std::string, std::string>>& x) {
1013 hash_.process_bytes(&(x.first),
sizeof(
int));
1014 hash_.process_bytes(x.second.first.c_str(), x.second.first.size());
1015 hash_.process_bytes(x.second.second.c_str(), x.second.second.size());
1018 inline void Update(
const std::vector<std::pair<
int, std::pair<std::string, std::string>>>& x) {
1019 std::vector<std::pair<int, std::pair<std::string, std::string>>>::const_iterator it = x.begin();
1020 for (; it != x.end(); ++it) {
1025 inline void Update(
const std::map<
std::string, std::vector<std::pair<
int, std::pair<std::string, std::string>>>>& x) {
1026 std::map<std::string, std::vector<std::pair<int, std::pair<std::string, std::string>>>>::const_iterator it = x.begin();
1027 for (; it != x.end(); ++it) {
1028 hash_.process_bytes(it->first.c_str(), it->first.size());
1033 inline void Update(
const std::list<std::pair<int, int>>& x){
1034 std::list<std::pair<int, int>>::const_iterator it = x.begin();
1035 for (; it != x.end(); ++it) {
1041 hash_.process_bytes(x.first.c_str(), x.first.size());
1046 std::map<std::string, std::pair<std::string, std::vector<double>>>::const_iterator it = x.begin();
1047 for(; it != x.end(); ++it) {
1048 hash_.process_bytes(it->first.c_str(), it->first.size());
1054 std::map<std::string, std::map<std::string, int>>::const_iterator it = x.begin();
1055 for(; it != x.end(); ++it) {
1056 hash_.process_bytes(it->first.c_str(), it->first.size());
1061 inline void Update(
const std::pair<double, double>& x) {
1062 hash_.process_bytes(&(x.first),
sizeof(
double));
1063 hash_.process_bytes(&(x.second),
sizeof(
double));
1066 inline void Update(
const std::pair<std::pair<double, double>, std::map<std::string, double>>& x) {
1071 inline void Update(
const std::vector<std::pair<std::pair<double, double>, std::map<std::string, double>>>& x) {
1072 std::vector<std::pair<std::pair<double, double>, std::map<std::string, double>>>::const_iterator it = x.begin();
1073 for(; it != x.end(); ++it) {
1079 std::map<std::string, std::map<std::string, double>>::const_iterator it = x.begin();
1080 for(; it != x.end(); ++it) {
1081 hash_.process_bytes(it->first.c_str(), it->first.size());
1090 hash_.get_digest(d.
val);
1095 boost::uuids::detail::sha1 hash_;
1100 #endif // CYCLUS_SRC_QUERY_BACKEND_H_ T Query(InfileTree *tree, std::string query, int index=0)
a query method for required parameters
void Update(const std::map< std::pair< int, std::string >, double > &x)
unsigned int val[CYCLUS_SHA1_NINT]
void Update(const std::map< std::string, int > &x)
void Update(const Blob &b)
void Update(const std::map< int, double > &x)
bool CmpConds(T *x, std::vector< Cond *> *conds)
Compares all condiontions for a value.
std::string op
One of: "<", ">", "<=", ">=", "==", "!=".
void Update(const std::pair< std::string, std::vector< double >> &x)
void Update(const std::map< int, int > &x)
virtual QueryResult Query(std::string table, std::vector< Cond > *conds)
Return a set of rows from the specificed table that match all given conditions.
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
void Update(const std::map< std::string, bool > &x)
For failed object state expectations.
CondInjector(QueryableBackend *b, std::vector< Cond > to_inject)
Interface implemented by backends that support recording and querying.
Meta data and results of a query.
virtual std::map< std::string, DbTypes > ColumnTypes(std::string table)
Return a map of column names of the specified table to the associated database type.
bool operator<(const CapacityConstraint< T > &lhs, const CapacityConstraint< T > &rhs)
CapacityConstraint-CapacityConstraint comparison operator, allows usage in ordered containers...
void Update(const std::list< double > &x)
void Update(const std::vector< std::string > &x)
ColumnInfo(std::string default_table, std::string default_col, int default_index, DbTypes default_dbtype, std::vector< int > default_shape)
boost::spirit::hold_any val
value supported by backend(s) in use
For values that are too big, too small, etc.
void Update(const std::map< std::string, std::pair< double, std::map< int, double >>> &x)
bool operator<=(const cyclus::Digest &rhs) const
void Update(const std::pair< int, int > &x)
void Update(const std::list< bool > &x)
void Update(const std::pair< int, std::string > &x)
void Update(const std::map< std::string, float > &x)
bool operator>=(const cyclus::Digest &rhs) const
void Update(const std::list< boost::uuids::uuid > &x)
void Update(const std::pair< int, std::pair< std::string, std::string >> &x)
void Update(const std::map< std::string, std::vector< double >> &x)
void Update(const std::map< int, std::string > &x)
DbTypes
This is the master list of all supported database types.
void Update(const std::map< int, cyclus::Blob > &x)
void Update(const std::map< std::string, std::map< int, double >> &x)
void Update(const std::map< std::string, std::map< std::string, double >> &x)
void Update(const std::set< cyclus::Blob > &x)
T GetVal(std::string field, int row=0)
Convenience method for retrieving a value from a specific row and named field (column).
void Update(const std::pair< std::pair< double, double >, std::map< std::string, double >> &x)
void Update(const std::map< std::string, cyclus::Blob > &x)
virtual std::map< std::string, DbTypes > ColumnTypes(std::string table)
Return a map of column names of the specified table to the associated database type.
void Update(const std::map< std::pair< std::string, std::string >, int > &x)
void Update(const std::map< std::string, std::pair< std::string, std::vector< double >>> &x)
void Update(const std::pair< double, std::map< int, double >> &x)
virtual QueryResult Query(std::string table, std::vector< Cond > *conds)
Return a set of rows from the specificed table that match all given conditions.
void Update(const std::map< int, boost::uuids::uuid > &x)
void Update(const std::set< std::string > &x)
bool operator==(const cyclus::Digest &rhs) const
void Update(const std::map< std::string, boost::uuids::uuid > &x)
void Update(const std::set< int > &x)
The digest type for SHA1s.
void Update(const std::list< int > &x)
std::vector< boost::spirit::hold_any > QueryRow
std::vector< std::string > fields
names of each field returned by a query
CmpOpCode opcode
The CmpOpCode cooresponding to op.
Cond(std::string field, std::string op, boost::spirit::hold_any val)
void Update(const std::pair< double, double > &x)
void Update(const std::vector< cyclus::Blob > &x)
Wrapper class for QueryableBackends that injects a set of Cond's into every query before being execut...
virtual std::set< std::string > Tables()
Return a set of all table names currently in the database.
void Update(const std::list< float > &x)
const std::string & str() const
virtual std::list< ColumnInfo > Schema(std::string table)
Return information about all columns of a table.
std::vector< DbTypes > types
types of each field returned by a query.
An abstract base class for listeners (e.g.
void Update(const std::vector< double > &x)
std::string field
table column name
Wrapper class for QueryableBackends that injects prefix in front of the title/table for every query b...
bool operator!=(const cyclus::Digest &rhs) const
bool operator==(const CapacityConstraint< T > &lhs, const CapacityConstraint< T > &rhs)
CapacityConstraint-CapacityConstraint equality operator.
std::ostream & operator<<(std::ostream &out) const
virtual std::list< ColumnInfo > Schema(std::string table)
Return information about all columns of a table.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
void Update(const std::map< int, float > &x)
PrefixInjector(QueryableBackend *b, std::string prefix)
A type to represent variable-length array of bytes for dumping to a cyclus output database...
void Update(const std::set< float > &x)
bool CmpCond(T *x, Cond *cond)
Compares a condiontion for a single value.
void Update(const std::vector< int > &x)
std::vector< QueryRow > rows
ordered results of a query
Represents a condition used to filter rows returned by a query.
std::vector< T > cast() const
Casts the value of this digest to a vector of the templated type.
void Update(const std::set< bool > &x)
void Update(const std::map< std::string, std::vector< std::pair< int, std::pair< std::string, std::string >>>> &x)
void Update(const std::map< int, bool > &x)
void Update(const std::vector< float > &x)
CmpOpCode
Represents operation codes for condition checking.
void Update(const std::vector< boost::uuids::uuid > &x)
virtual std::set< std::string > Tables()
Return a set of all table names currently in the database.
void Update(const std::list< std::string > &x)
void Update(const std::set< boost::uuids::uuid > &x)
Interface implemented by backends that support rudimentary querying.
void Update(const std::vector< std::pair< std::pair< double, double >, std::map< std::string, double >>> &x)
void Update(const std::map< std::string, std::string > &x)
void Update(const std::set< double > &x)
void Update(const std::map< std::string, double > &x)
void Update(const std::map< int, std::map< std::string, double >> &x)
For failed retrieval/insertion of key-based data into/from data structures.
void Update(const std::vector< std::pair< int, std::pair< std::string, std::string >>> &x)
void Update(const std::list< cyclus::Blob > &x)
Represents column information.
virtual ~QueryableBackend()
void Update(const std::list< std::pair< int, int >> &x)
void Update(const std::map< std::string, std::map< std::string, int >> &x)
void Clear()
Clears the current hash value to its default state.
void Update(const std::string &s)
Updates the hash value in-place.