5 #define likely(x) __builtin_expect((x),1)
6 #define unlikely(x) __builtin_expect((x),0)
10 #include <CoolKernel/ChannelSelection.h>
11 #include <CoolKernel/ITime.h>
12 #include <CoolKernel/IField.h>
13 #include <CoolKernel/FieldSelection.h>
14 #include <CoolKernel/CompositeSelection.h>
15 #include <CoolKernel/IObject.h>
16 #include <CoolKernel/IObjectIterator.h>
17 #include <CoolKernel/IFolder.h>
18 #include <CoolKernel/IDatabase.h>
19 #include <CoolKernel/IDatabaseSvc.h>
21 #include <CoolApplication/DatabaseSvcFactory.h>
23 #include <boost/typeof/typeof.hpp>
24 #include <boost/bind/bind.hpp>
25 #include <boost/function.hpp>
41 using boost::placeholders::_1;
43 using cool::DatabaseSvcFactory;
44 using cool::IDatabasePtr;
48 using cool::IFolderPtr;
49 using cool::ChannelSelection;
50 using cool::IObjectIteratorPtr;
51 using cool::IObjectVectorPtr;
52 using cool::ValidityKey;
56 const std::string&
name,
58 cool::FieldSelection::Relation relation,
61 #define MAKE_FS(type, converter) \
62 if (typeId == cool::StorageType::type) \
63 return new cool::FieldSelection(name, typeId, relation, \
64 static_cast<cool::type>(converter(refValue)));
67 MAKE_FS(Float, PyFloat_AsDouble)
68 MAKE_FS(Double, PyFloat_AsDouble)
70 MAKE_FS(UChar, PyLong_AsUnsignedLong)
72 MAKE_FS(UInt16, PyLong_AsUnsignedLong)
74 MAKE_FS(UInt32, PyLong_AsUnsignedLong)
75 MAKE_FS(Int64, PyLong_AsLongLong)
76 MAKE_FS(UInt63, PyLong_AsUnsignedLongLong)
78 MAKE_FS(String255, _PyUnicode_AsString)
79 MAKE_FS(String4k, _PyUnicode_AsString)
80 MAKE_FS(String64k, _PyUnicode_AsString)
81 MAKE_FS(String16M, _PyUnicode_AsString)
85 throw (std::runtime_error(
"Unsupported cool type encountered in python conversion"));
90 return vector<const cool::IRecordSelection*>();
103 const char*
data =
reinterpret_cast<const char*
>(
blob.startingAddress());
104 return PyBytes_FromStringAndSize(
data,
blob.size());
109 return PyUnicode_FromStringAndSize(
str.c_str(),
str.size());
114 return PyUnicode_DecodeUTF8(
str.c_str(),
str.size(),
"backslashreplace");
120 template<
typename T,
typename FUNC_TYPE>
122 FUNC_TYPE* converter_function)
130 return converter_function(
field.data<
T>());
136 const string& type_name,
137 bool string_to_unicode =
false)
141 #define MAKE_FETCHER(type, converter) \
142 if (type_name == #type) \
143 return bind(payload_fetcher<cool::type, BOOST_TYPEOF(converter)>, \
144 _1, name, &converter);
161 if (string_to_unicode)
177 PyErr_Format(PyExc_RuntimeError,
178 "Type '%s' is not in type conversion table. "
179 "Please add it to quick_retrieve.cxx. "
180 "Can't convert field '%s'.",
197 unsigned long long value)
199 static const char *
const argtypes =
const_cast<char *
>(
"K");
200 if (iovkey_wrapper && iovkey_wrapper != Py_None)
201 return PyObject_CallFunction(iovkey_wrapper,
argtypes,
value);
202 return PyLong_FromUnsignedLongLong(
value);
209 if (!
function ||
function == Py_None)
return object;
211 PyObject *new_object = PyObject_CallObject(
function,
object);
212 Py_DECREF(old_object);
224 const long max_records = -1,
225 const bool with_channel =
true,
226 const bool loud =
false,
228 PyObject *channel_name_mapping = NULL,
229 const bool with_time =
false,
230 const bool as_unicode =
false)
236 const long long records =
objects->size();
237 const Py_ssize_t
count = to_fetch ? PySequence_Size(to_fetch) : 0;
238 const unsigned int fetch_start = 2 + with_channel + with_time;
243 vector<payload_fetcher_t> payload_fetchers;
247 const IObject&
object =
objects->currentRef();
254 for (Py_ssize_t
i = 0;
i <
count;
i++)
256 PyObject *py_name = PySequence_GetItem(to_fetch,
i);
257 const char *
name = _PyUnicode_AsString(py_name);
258 const string type = (
object.payload()
259 .specification()[
name]
267 payload_fetchers.push_back(
pf);
273 PyObject *py_datetime_module = PyImport_ImportModule(
"datetime");
274 if (!py_datetime_module)
275 throw (std::runtime_error(
"Could not import python datetime_module"));
277 py_datetime_class = PyObject_GetAttrString(py_datetime_module,
280 Py_DECREF(py_datetime_module);
293 const ITime&
t =
object.insertionTime();
294 static const char *
const argtypes =
const_cast<char *
>(
"iiiiiil");
297 PyObject_CallFunction(py_datetime_class,
argtypes,
298 t.year(),
t.month(),
t.day(),
299 t.hour(),
t.minute(),
t.second(),
t.nanosecond() / 1000);
301 PyTuple_SetItem(
one, 2, py_record_time);
306 if (channel_name_mapping)
311 PyObject *channelName = PyDict_GetItem(channel_name_mapping,
315 Py_INCREF(channelName);
317 PyTuple_SET_ITEM(
one, 2+with_time, channelName);
322 PyTuple_SET_ITEM(
one, 2+with_time,
327 for (Py_ssize_t
i = 0;
i <
count;
i++)
328 PyTuple_SET_ITEM(
one, fetch_start +
i, payload_fetchers[
i](
object));
344 if (
unlikely(loud && ((++j) % 1000 == 0)))
346 cout <<
"Progress: " << j <<
" / " << records <<
"\r";
350 if (
unlikely(max_records > 0 && j >= max_records))
355 cout <<
"Done fetching " << j <<
" records.\x1B[K" << endl;
357 if (py_datetime_class)
359 Py_DECREF(py_datetime_class);