ATLAS Offline Software
Loading...
Searching...
No Matches
dictionary.h File Reference
#include <Python.h>
#include <CoolKernel/ChannelSelection.h>
#include <CoolKernel/IObject.h>
#include <CoolKernel/IObjectIterator.h>
#include <CoolKernel/IFolder.h>
#include <CoolKernel/IDatabase.h>
#include <CoolKernel/IDatabaseSvc.h>
#include <CoolKernel/IRecordSelection.h>
#include <CoolKernel/FieldSelection.h>
#include <CoolApplication/DatabaseSvcFactory.h>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

struct  quick_retrieve_dict::dict

Namespaces

namespace  quick_retrieve_dict

Macros

#define register
#define likely(x)
#define unlikely(x)

Functions

const cool::RecordSpecification get_coracool_payload_spec (IDatabasePtr cooldb, const string &folder)
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)
PyObjectquick_retrieve (const IObjectIteratorPtr &objects, PyObject *object_converter, PyObject *to_fetch=NULL, const long max_records=-1, const bool with_channel=true, const bool loud=false, PyObject *iovkey_wrapper=NULL, PyObject *channel_name_mapping=NULL, const bool with_time=false, const bool as_unicode=false)
cool::FieldSelection * make_fieldselection (const std::string &name, const cool::StorageType::TypeId typeId, cool::FieldSelection::Relation relation, PyObject *refValue)
vector< const cool::IRecordSelection * > make_selection_vector ()

Macro Definition Documentation

◆ likely

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

Definition at line 40 of file dictionary.h.

◆ register

#define register

Definition at line 21 of file dictionary.h.

◆ unlikely

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

Definition at line 41 of file dictionary.h.

Function Documentation

◆ 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
_object PyObject
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)

◆ 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_fieldselection()

cool::FieldSelection * make_fieldselection ( const std::string & name,
const cool::StorageType::TypeId typeId,
cool::FieldSelection::Relation relation,
PyObject * refValue )

Definition at line 53 of file quick_retrieve.cxx.

58{
59 #define MAKE_FS(type, converter) \
60 if (typeId == cool::StorageType::type) \
61 return new cool::FieldSelection(name, typeId, relation, \
62 static_cast<cool::type>(converter(refValue)));
63
64 MAKE_FS(Bool, PyLong_AsLong)
65 MAKE_FS(Float, PyFloat_AsDouble)
66 MAKE_FS(Double, PyFloat_AsDouble)
67
68 MAKE_FS(UChar, PyLong_AsUnsignedLong)
69 MAKE_FS(Int16, PyLong_AsLong)
70 MAKE_FS(UInt16, PyLong_AsUnsignedLong)
71 MAKE_FS(Int32, PyLong_AsLong)
72 MAKE_FS(UInt32, PyLong_AsUnsignedLong)
73 MAKE_FS(Int64, PyLong_AsLongLong)
74 MAKE_FS(UInt63, PyLong_AsUnsignedLongLong)
75
76 MAKE_FS(String255, _PyUnicode_AsString)
77 MAKE_FS(String4k, _PyUnicode_AsString)
78 MAKE_FS(String64k, _PyUnicode_AsString)
79 MAKE_FS(String16M, _PyUnicode_AsString)
80
81 //MAKE_FS(Blob16M, PyString_AsString)
82 //MAKE_FS(Blob64k, PyString_AsString)
83 throw (std::runtime_error("Unsupported cool type encountered in python conversion"));
84}
#define MAKE_FS(type, converter)

◆ make_selection_vector()

vector< const cool::IRecordSelection * > make_selection_vector ( )

Definition at line 86 of file quick_retrieve.cxx.

87{
88 return vector<const cool::IRecordSelection*>();
89
90}

◆ quick_retrieve()

PyObject * quick_retrieve ( const IObjectIteratorPtr & objects,
PyObject * object_converter,
PyObject * to_fetch = NULL,
const long max_records = -1,
const bool with_channel = true,
const bool loud = false,
PyObject * iovkey_wrapper = NULL,
PyObject * channel_name_mapping = NULL,
const bool with_time = false,
const bool as_unicode = false )

Definition at line 219 of file quick_retrieve.cxx.

229{
230 IObjectIterator_Guard closeiterator_guard(objects);
231
232 PyObject* result = PyList_New(0);
233
234 const long long records = objects->size();
235 const Py_ssize_t count = to_fetch ? PySequence_Size(to_fetch) : 0;
236 const unsigned int fetch_start = 2 + with_channel + with_time;
237 bool first = true;
238 int j = 0;
239 PyObject *py_datetime_class = NULL;
240
241 vector<payload_fetcher_t> payload_fetchers;
242
243 while (objects->goToNext())
244 {
245 const IObject& object = objects->currentRef();
246
247 if (unlikely(first))
248 {
249 // On the first iteration, compute the types in to_fetch, and bind
250 // them into the payload_fetchers.
251 first = false;
252 for (Py_ssize_t i = 0; i < count; i++)
253 {
254 PyObject *py_name = PySequence_GetItem(to_fetch, i);
255 const char *name = _PyUnicode_AsString(py_name);
256 const string type = (object.payload()
257 .specification()[name]
258 .storageType()
259 .name());
260
262 as_unicode);
263 auto pff = pf.target<PyObject* (*)(const IObject&)>();
264 if ( pff && *pff == &no_conversion_available)
265 return NULL;
266 payload_fetchers.push_back(std::move(pf));
267 Py_DECREF(py_name);
268 }
269
270 if (with_time)
271 {
272 PyObject *py_datetime_module = PyImport_ImportModule("datetime");
273 if (!py_datetime_module)
274 throw (std::runtime_error("Could not import python datetime_module"));
275
276 py_datetime_class = PyObject_GetAttrString(py_datetime_module,
277 "datetime");
278
279 Py_DECREF(py_datetime_module);
280 }
281 }
282
283 PyObject *one = PyTuple_New(fetch_start + count);
284
285 // Fetch (since, until, channel[optional])
286
287 PyTuple_SET_ITEM(one, 0, make_iov_key(iovkey_wrapper, object.since()));
288 PyTuple_SET_ITEM(one, 1, make_iov_key(iovkey_wrapper, object.until()));
289
290 if (with_time)
291 {
292 const ITime& t = object.insertionTime();
293 static const char * const argtypes = const_cast<char *>("iiiiiil");
294
295 PyObject *py_record_time =
296 PyObject_CallFunction(py_datetime_class, argtypes,
297 t.year(), t.month(), t.day(),
298 t.hour(), t.minute(), t.second(), t.nanosecond() / 1000);
299
300 PyTuple_SetItem(one, 2, py_record_time);
301 }
302
303 if (with_channel)
304 {
305 if (channel_name_mapping)
306 {
307 // Use a channel name if it is available, otherwise fall back
308 // to standard channelid.
309 PyObject *channelId = PyLong_FromUnsignedLong(object.channelId());
310 PyObject *channelName = PyDict_GetItem(channel_name_mapping,
311 channelId);
312 if (channelName)
313 {
314 Py_INCREF(channelName);
315 Py_DECREF(channelId);
316 PyTuple_SET_ITEM(one, 2+with_time, channelName);
317 } else
318 PyTuple_SET_ITEM(one, 2+with_time, channelId);
319
320 } else
321 PyTuple_SET_ITEM(one, 2+with_time,
322 PyLong_FromLong(object.channelId()));
323 }
324
325 // Fetch the objects specified in to_fetch
326 for (Py_ssize_t i = 0; i < count; i++)
327 PyTuple_SET_ITEM(one, fetch_start + i, payload_fetchers[i](object));
328
329 one = apply_function(object_converter, one);
330 if (!one)
331 {
332 // apply_function returned an error
333 // We won't return the list to python so we need to tell python it
334 // can be deleted
335 Py_DECREF(result);
336 return NULL;
337 }
338
339 PyList_Append(result, one);
340 Py_DECREF(one);
341
342 // Print status if loud is active
343 if (unlikely(loud && ((++j) % 1000 == 0)))
344 {
345 cout << "Progress: " << j << " / " << records << "\r";
346 cout.flush();
347 }
348
349 if (unlikely(max_records > 0 && j >= max_records))
350 break;
351 }
352
353 if (loud)
354 cout << "Done fetching " << j << " records.\x1B[K" << endl;
355
356 if (py_datetime_class)
357 {
358 Py_DECREF(py_datetime_class);
359 }
360
361 return result;
362}
PyObject * no_conversion_available(const IObject &)
payload_fetcher_t create_payload_fetcher(const char *name, const string &type_name, bool string_to_unicode=false)
std::function< PyObject *(const IObject &)> payload_fetcher_t
PyObject * apply_function(PyObject *function, PyObject *object)
#define unlikely(x)
PyObject * make_iov_key(PyObject *iovkey_wrapper, unsigned long long value)