ATLAS Offline Software
Loading...
Searching...
No Matches
pythonic_coracool.cxx File Reference
#include <Python.h>
#include <CoolKernel/ChannelSelection.h>
#include <CoolKernel/IFolder.h>
#include <CoolKernel/IDatabase.h>
#include "CoralBase/Attribute.h"
#include "CoralBase/AttributeList.h"
#include "CoralBase/AttributeListSpecification.h"
#include <CoraCool/CoraCoolDatabaseSvcFactory.h>
#include <CoraCool/CoraCoolDatabaseSvc.h>
#include <CoraCool/CoraCoolDatabase.h>
#include <CoraCool/CoraCoolFolder.h>
#include <CoraCool/CoraCoolObjectIter.h>
#include <CoraCool/CoraCoolObject.h>
#include <functional>
#include <string>
#include <iostream>
#include "CxxUtils/checker_macros.h"

Go to the source code of this file.

Macros

#define likely(x)
#define unlikely(x)
#define MAKE_FETCHER(type, converter)

Typedefs

typedef std::function< PyObject *(const AttributeList &)> coral_attribute_fetcher_t

Functions

PyObjectno_coral_conversion_available (const AttributeList &)
PyObjectqr_PyString_FromBlob (const coral::Blob &blob)
PyObjectqr_PyString_FromStdString (const string &str)
template<typename T>
const T & fetch_attribute_data (const coral::Attribute &A)
coral_attribute_fetcher_t create_attribute_fetcher (const char *name, const string &type_name)
bool make_fetchers (PyObject *to_fetch, const AttributeList &attribute_list, vector< coral_attribute_fetcher_t > &payload_fetchers)
PyObjectapply_function (PyObject *function, PyObject *object)
CoraCoolFolderPtr fetch_coracool_folder (IDatabasePtr cooldb, const string &folder)
const cool::RecordSpecification get_coracool_payload_spec (IDatabasePtr cooldb, const string &folder)
PyObjectmake_iov_key (PyObject *iovkey_wrapper, unsigned long long value)
PyObjectbrowse_coracool (IDatabasePtr cooldb, const string &folder, ValidityKey since, ValidityKey until, const ChannelSelection &cs=ChannelSelection::all(), const char *tag="", PyObject *to_fetch=NULL, PyObject *object_converter=NULL, PyObject *inner_object_converter=NULL, PyObject *iovkey_wrapper=NULL)

Variables

 ATLAS_NO_CHECK_FILE_THREAD_SAFETY

Macro Definition Documentation

◆ likely

#define likely ( x)
Value:
__builtin_expect((x),1)
#define x

Definition at line 8 of file pythonic_coracool.cxx.

◆ MAKE_FETCHER

#define MAKE_FETCHER ( type,
converter )
Value:
if (type_name == #type) { \
/*coverity[copy_constructor_call] */ \
return [sname] (const AttributeList& l) -> PyObject* \
{ return converter(fetch_attribute_data<type>(l[sname])); }; \
}
_object PyObject
const T & fetch_attribute_data(const coral::Attribute &A)

◆ unlikely

#define unlikely ( x)
Value:
__builtin_expect((x),0)

Definition at line 9 of file pythonic_coracool.cxx.

Typedef Documentation

◆ coral_attribute_fetcher_t

typedef std::function<PyObject* (const AttributeList&)> coral_attribute_fetcher_t

Definition at line 52 of file pythonic_coracool.cxx.

Function Documentation

◆ apply_function()

PyObject * apply_function ( PyObject * function,
PyObject * object )
inline

Definition at line 152 of file pythonic_coracool.cxx.

153{
154 // Convert object according to function, taking care of references
155 // If function is null, return unmodified object
156 if (!function || function == Py_None) return object;
157 PyObject *old_object = object;
158 PyObject *new_object = PyObject_CallObject(function, object);
159 Py_DECREF(old_object);
160 return new_object;
161}

◆ browse_coracool()

PyObject * browse_coracool ( IDatabasePtr cooldb,
const string & folder,
ValidityKey since,
ValidityKey until,
const ChannelSelection & cs = ChannelSelection::all(),
const char * tag = "",
PyObject * to_fetch = NULL,
PyObject * object_converter = NULL,
PyObject * inner_object_converter = NULL,
PyObject * iovkey_wrapper = NULL )

Definition at line 189 of file pythonic_coracool.cxx.

197{
198 // Browse CoraCool objects
199 CoraCoolFolderPtr coralFolder = fetch_coracool_folder(std::move(cooldb), folder);
200 CoraCoolObjectIterPtr objects = coralFolder->browseObjects(since, until,
201 cs, tag);
202
203 // Number of attributes to be fetched
204 const Py_ssize_t count = to_fetch ? PySequence_Size(to_fetch) : 0;
205
206 PyObject* result = PyList_New(0); // List which is returned by this function
207 bool first = true; // Is this the first iteration?
208 CoraCoolObject::const_iterator payload; // Current payload
209
210 // List of functors which convert an Attribute into a python value
211 vector<coral_attribute_fetcher_t> payload_fetchers;
212
213 // Loop over IoVs
214 while (objects->hasNext())
215 {
216 const CoraCoolObjectPtr& object = objects->next();
217
218 PyObject *py_payload_tuple = PyTuple_New(object->size());
219 unsigned int payload_index = 0;
220
221 // Loop over multiple payloads per record
222 for (payload = object->begin();
223 payload != object->end();
224 ++payload, ++payload_index)
225 {
226 if (unlikely(first))
227 {
228 // On the first iteration, figure out the types and create
229 // specialised functions for retrieving them
230 first = false;
231 if (!make_fetchers(to_fetch, *payload, payload_fetchers))
232 return NULL; // Failure, throw python exception from above
233 }
234
235 // Create a tuple for the payload and fill it from payload fetchers
236 PyObject *py_payload = PyTuple_New(count);
237 for (Py_ssize_t i = 0; i < count; i++)
238 PyTuple_SET_ITEM(py_payload, i, payload_fetchers[i](*payload));
239
240 py_payload = apply_function(inner_object_converter, py_payload);
241
242 if (!py_payload)
243 {
244 // apply_function returned an error
245 // We won't return the list to python so we need to tell python it
246 // can be deleted
247 Py_DECREF(py_payload_tuple);
248 Py_DECREF(result);
249 return NULL;
250 }
251
252 PyTuple_SET_ITEM(py_payload_tuple, payload_index, py_payload);
253 }
254
255 PyObject *one = PyTuple_New(4);
256
257 PyTuple_SET_ITEM(one, 0, make_iov_key(iovkey_wrapper, object->since()));
258 PyTuple_SET_ITEM(one, 1, make_iov_key(iovkey_wrapper, object->until()));
259
260 PyTuple_SET_ITEM(one, 2, PyLong_FromLong(object->channelId()));
261
262 // SET_ITEM steals reference, no decref needed
263 PyTuple_SET_ITEM(one, 3, py_payload_tuple);
264
265 one = apply_function(object_converter, one);
266 if (!one)
267 {
268 // apply_function returned an error
269 // We won't return the list to python so we need to tell python it
270 // can be deleted
271 Py_DECREF(result);
272 return NULL;
273 }
274
275 PyList_Append(result, one);
276 Py_DECREF(one);
277 }
278
279 return result;
280}
boost::shared_ptr< CoraCoolObject > CoraCoolObjectPtr
boost::shared_ptr< CoraCoolObjectIter > CoraCoolObjectIterPtr
boost::shared_ptr< CoraCoolFolder > CoraCoolFolderPtr
AttrListVec::const_iterator const_iterator
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
bool first
Definition DeMoScan.py:534
CoraCoolFolderPtr fetch_coracool_folder(IDatabasePtr cooldb, const string &folder)
PyObject * apply_function(PyObject *function, PyObject *object)
bool make_fetchers(PyObject *to_fetch, const AttributeList &attribute_list, vector< coral_attribute_fetcher_t > &payload_fetchers)
#define unlikely(x)
PyObject * make_iov_key(PyObject *iovkey_wrapper, unsigned long long value)

◆ create_attribute_fetcher()

coral_attribute_fetcher_t create_attribute_fetcher ( const char * name,
const string & type_name )

Definition at line 70 of file pythonic_coracool.cxx.

72{
73 // Test type against type_name. If true, return a functor for this type.
74 // Be sure to use only a copy of name --- the original may not be valid
75 // by the time the lambda is called.
76 std::string sname =name;
77 #define MAKE_FETCHER(type, converter) \
78 if (type_name == #type) { \
79 /*coverity[copy_constructor_call] */ \
80 return [sname] (const AttributeList& l) -> PyObject* \
81 { return converter(fetch_attribute_data<type>(l[sname])); }; \
82 }
83
84 // See the python c-api reference for python conversion functions
85 // Python/C API Reference Manual >> Concrete Objects Layer
86 // http://docs.python.org/c-api/concrete.html
87
88 MAKE_FETCHER(short, PyLong_FromLong)
89 MAKE_FETCHER(int, PyLong_FromLong)
90 MAKE_FETCHER(long long, PyLong_FromLongLong)
91
92 /*
93
94 Types from COOL:
95 We need to build up a similar list from CORAL, it would be nice to know
96 where they came from.
97
98 MAKE_FETCHER(Bool, PyBool_FromLong)
99 MAKE_FETCHER(Float, PyFloat_FromDouble)
100 MAKE_FETCHER(Double, PyFloat_FromDouble)
101 MAKE_FETCHER(UChar, PyLong_FromLong)
102 MAKE_FETCHER(UInt16, PyLong_FromUnsignedLong)
103 MAKE_FETCHER(UInt32, PyLong_FromUnsignedLong)
104 MAKE_FETCHER(UInt63, PyLong_FromUnsignedLongLong)
105 MAKE_FETCHER(UInt64, PyLong_FromUnsignedLongLong)
106 MAKE_FETCHER(String255, qr_PyString_FromStdString)
107 MAKE_FETCHER(String4k, qr_PyString_FromStdString)
108 MAKE_FETCHER(String64k, qr_PyString_FromStdString)
109 MAKE_FETCHER(String16M, qr_PyString_FromStdString)
110 MAKE_FETCHER(Blob16M, qr_PyString_FromBlob)
111 MAKE_FETCHER(Blob64k, qr_PyString_FromBlob)
112 */
113
114 PyErr_Format(PyExc_RuntimeError,
115 "Type '%s' is not in type conversion table. "
116 "Please add it to pythonic_coracool.cxx. "
117 "Can't convert field '%s'.",
118 type_name.c_str(),
119 name);
121}
#define MAKE_FETCHER(type, converter)
PyObject * no_coral_conversion_available(const AttributeList &)

◆ fetch_attribute_data()

template<typename T>
const T & fetch_attribute_data ( const coral::Attribute & A)

Definition at line 62 of file pythonic_coracool.cxx.

63{
64 return A.data<T>();
65}
hold the test vectors and ease the comparison

◆ fetch_coracool_folder()

CoraCoolFolderPtr fetch_coracool_folder ( IDatabasePtr cooldb,
const string & folder )

Definition at line 163 of file pythonic_coracool.cxx.

164{
165 CoraCoolDatabaseSvc& corasvc = CoraCoolDatabaseSvcFactory::
166 databaseService();
167
168 CoraCoolDatabasePtr coradb = corasvc.openDatabase(
169 cooldb->databaseId(), cooldb, true);
170
171 return coradb->getFolder(folder);
172}
boost::shared_ptr< CoraCoolDatabase > CoraCoolDatabasePtr
CoraCoolDatabasePtr openDatabase(const std::string &dbconn, cool::IDatabasePtr cooldb, bool readonly=false)

◆ get_coracool_payload_spec()

const cool::RecordSpecification get_coracool_payload_spec ( IDatabasePtr cooldb,
const string & folder )

Definition at line 175 of file pythonic_coracool.cxx.

176{
177 return fetch_coracool_folder(std::move(cooldb), folder)->payloadSpecification();
178}

◆ make_fetchers()

bool make_fetchers ( PyObject * to_fetch,
const AttributeList & attribute_list,
vector< coral_attribute_fetcher_t > & payload_fetchers )

Definition at line 126 of file pythonic_coracool.cxx.

130{
131 const Py_ssize_t count = to_fetch ? PySequence_Size(to_fetch) : 0;
132
133 for (Py_ssize_t i = 0; i < count; i++)
134 {
135 PyObject *py_name = PySequence_GetItem(to_fetch, i);
136 const char *name = _PyUnicode_AsString(py_name);
137 const string type = attribute_list[name].specification().typeName();
138
140
141 auto pff = pf.target<PyObject* (*)(const AttributeList&)>();
142 if ( pff && *pff == &no_coral_conversion_available)
143 return false; // Failure: A python exception was thrown above
144
145 payload_fetchers.push_back(std::move(pf));
146 Py_DECREF(py_name);
147 }
148
149 return true; // Success
150}
coral_attribute_fetcher_t create_attribute_fetcher(const char *name, const string &type_name)
std::function< PyObject *(const AttributeList &)> coral_attribute_fetcher_t

◆ make_iov_key()

PyObject * make_iov_key ( PyObject * iovkey_wrapper,
unsigned long long value )
inline

Definition at line 180 of file pythonic_coracool.cxx.

182{
183 static const char * const argtypes = const_cast<char *>("K");
184 if (iovkey_wrapper && iovkey_wrapper != Py_None)
185 return PyObject_CallFunction(iovkey_wrapper, argtypes, value);
186 return PyLong_FromUnsignedLongLong(value);
187}

◆ no_coral_conversion_available()

PyObject * no_coral_conversion_available ( const AttributeList & )

Definition at line 55 of file pythonic_coracool.cxx.

55{return NULL;}

◆ qr_PyString_FromBlob()

PyObject * qr_PyString_FromBlob ( const coral::Blob & blob)

Definition at line 99 of file quick_retrieve.cxx.

100{
101 const char* data = reinterpret_cast<const char*>(blob.startingAddress());
102 return PyBytes_FromStringAndSize(data, blob.size());
103}
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11

◆ qr_PyString_FromStdString()

PyObject * qr_PyString_FromStdString ( const string & str)

Definition at line 105 of file quick_retrieve.cxx.

106{
107 return PyUnicode_FromStringAndSize(str.c_str(), str.size());
108}

Variable Documentation

◆ ATLAS_NO_CHECK_FILE_THREAD_SAFETY

ATLAS_NO_CHECK_FILE_THREAD_SAFETY

Definition at line 34 of file pythonic_coracool.cxx.