Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander.A,Utkin committed Dec 12, 2024
1 parent 2fc87d9 commit 7984ea1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 46 deletions.
19 changes: 0 additions & 19 deletions pyreindexer/lib/include/pyobjtools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,6 @@ reindexer::h_vector<std::string, 2> PyObjectToJson(PyObject** obj) {
return values;
}

reindexer::h_vector<std::string, 2> ParseStrListToStrVec(PyObject** list) {
reindexer::h_vector<std::string, 2> result;

Py_ssize_t sz = PyList_Size(*list);
result.reserve(sz);
for (Py_ssize_t i = 0; i < sz; i++) {
PyObject* item = PyList_GetItem(*list, i);

if (!PyUnicode_Check(item)) {
throw reindexer::Error(ErrorCode::errParseJson,
std::string("String expected, got ") + Py_TYPE(item)->tp_name);
}

result.push_back(PyUnicode_AsUTF8(item));
}

return result;
}

reindexer::Variant convert(PyObject** value) {
if (PyFloat_Check(*value)) {
double v = PyFloat_AsDouble(*value);
Expand Down
24 changes: 23 additions & 1 deletion pyreindexer/lib/include/pyobjtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,29 @@

namespace pyreindexer {

reindexer::h_vector<std::string, 2> ParseStrListToStrVec(PyObject** list);
template <typename T>
using HVectorT = reindexer::h_vector<T, 2>;

template <template <typename...> class VectorT = HVectorT>
VectorT<std::string> ParseStrListToStrVec(PyObject** list) {
VectorT<std::string> result;

Py_ssize_t sz = PyList_Size(*list);
result.reserve(sz);
for (Py_ssize_t i = 0; i < sz; i++) {
PyObject* item = PyList_GetItem(*list, i);

if (!PyUnicode_Check(item)) {
throw reindexer::Error(ErrorCode::errParseJson,
std::string("String expected, got ") + Py_TYPE(item)->tp_name);
}

result.emplace_back(PyUnicode_AsUTF8(item));
}

return result;
}

reindexer::VariantArray ParseListToVec(PyObject** list);

void PyObjectToJson(PyObject** obj, reindexer::WrSerializer& wrSer);
Expand Down
10 changes: 5 additions & 5 deletions pyreindexer/lib/include/query_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ Error QueryWrapper::OpenBracket() {
nextOperation_ = OpType::OpAnd;
++queriesCount_;

return errOK;
return {};
}

reindexer::Error QueryWrapper::CloseBracket() {
Error QueryWrapper::CloseBracket() {
if (nextOperation_ != OpType::OpAnd) {
return reindexer::Error(ErrorCode::errLogic, "Operation before close bracket");
}
Expand All @@ -100,7 +100,7 @@ reindexer::Error QueryWrapper::CloseBracket() {
ser_.PutVarUint(QueryItemType::QueryCloseBracket);
openedBrackets_.pop_back();

return errOK;
return {};
}

void QueryWrapper::DWithin(std::string_view index, double x, double y, double distance) {
Expand Down Expand Up @@ -307,7 +307,7 @@ void QueryWrapper::Merge(QueryWrapper* mergeQuery) {
mergedQueries_.push_back(mergeQuery);
}

reindexer::Error QueryWrapper::On(std::string_view joinField, CondType condition, std::string_view joinIndex) {
Error QueryWrapper::On(std::string_view joinField, CondType condition, std::string_view joinIndex) {
ser_.PutVarUint(QueryItemType::QueryJoinOn);
ser_.PutVarUint(nextOperation_);
ser_.PutVarUint(condition);
Expand All @@ -316,7 +316,7 @@ reindexer::Error QueryWrapper::On(std::string_view joinField, CondType condition

nextOperation_ = OpType::OpAnd;

return errOK;
return {};
}

void QueryWrapper::SelectFilter(const reindexer::h_vector<std::string, 2>& fields) {
Expand Down
32 changes: 15 additions & 17 deletions pyreindexer/lib/src/rawpyreindexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,16 @@ PyObject* itemModify(PyObject* self, PyObject* args, ItemModifyMode mode) {
}

if (preceptsList != nullptr && mode != ModeDelete) {
reindexer::h_vector<std::string, 2> itemPrecepts;
std::vector<std::string> precepts;
try {
itemPrecepts = ParseStrListToStrVec(&preceptsList);
precepts = ParseStrListToStrVec<std::vector>(&preceptsList);
} catch (const Error& err) {
Py_DECREF(preceptsList);

return pyErr(err);
}

std::vector<std::string> prets(itemPrecepts.begin(), itemPrecepts.end());
item.SetPrecepts(prets); // ToDo after migrate on v.4, do std::move
item.SetPrecepts(precepts); // ToDo after migrate on v.4, do std::move
}

Py_XDECREF(preceptsList);
Expand Down Expand Up @@ -511,21 +510,21 @@ namespace {
PyObject* modifyTransaction(PyObject* self, PyObject* args, ItemModifyMode mode) {
uintptr_t transactionWrapperAddr = 0;
PyObject* defDict = nullptr; // borrowed ref after ParseTuple
PyObject* precepts = nullptr; // borrowed ref after ParseTuple if passed
if (!PyArg_ParseTuple(args, "kO!|O!", &transactionWrapperAddr, &PyDict_Type, &defDict, &PyList_Type, &precepts)) {
PyObject* preceptsList = nullptr; // borrowed ref after ParseTuple if passed
if (!PyArg_ParseTuple(args, "kO!|O!", &transactionWrapperAddr, &PyDict_Type, &defDict, &PyList_Type, &preceptsList)) {
return nullptr;
}

Py_INCREF(defDict);
Py_XINCREF(precepts);
Py_XINCREF(preceptsList);

auto transaction = getWrapper<TransactionWrapper>(transactionWrapperAddr);

auto item = transaction->NewItem();
auto err = item.Status();
if (!err.ok()) {
Py_DECREF(defDict);
Py_XDECREF(precepts);
Py_XDECREF(preceptsList);

return pyErr(err);
}
Expand All @@ -536,7 +535,7 @@ PyObject* modifyTransaction(PyObject* self, PyObject* args, ItemModifyMode mode)
PyObjectToJson(&defDict, wrSer);
} catch (const Error& err) {
Py_DECREF(defDict);
Py_XDECREF(precepts);
Py_XDECREF(preceptsList);

return pyErr(err);
}
Expand All @@ -545,27 +544,26 @@ PyObject* modifyTransaction(PyObject* self, PyObject* args, ItemModifyMode mode)

err = item.Unsafe().FromJSON(wrSer.Slice(), 0, mode == ModeDelete);
if (!err.ok()) {
Py_XDECREF(precepts);
Py_XDECREF(preceptsList);

return pyErr(err);
}

if (precepts != nullptr && mode != ModeDelete) {
reindexer::h_vector<std::string, 2> itemPrecepts;
if (preceptsList != nullptr && mode != ModeDelete) {
std::vector<std::string> precepts;

try {
itemPrecepts = ParseStrListToStrVec(&precepts);
precepts = ParseStrListToStrVec<std::vector>(&preceptsList);
} catch (const Error& err) {
Py_DECREF(precepts);
Py_DECREF(preceptsList);

return pyErr(err);
}

std::vector<std::string> prets(itemPrecepts.begin(), itemPrecepts.end());
item.SetPrecepts(prets); // ToDo after migrate on v.4, do std::move
item.SetPrecepts(precepts); // ToDo after migrate on v.4, do std::move
}

Py_XDECREF(precepts);
Py_XDECREF(preceptsList);

switch (mode) {
case ModeInsert:
Expand Down
8 changes: 4 additions & 4 deletions pyreindexer/lib/src/reindexerinterface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ template <typename DBT>
Error ReindexerInterface<DBT>::FetchResults(QueryResultsWrapper& result) {
return execute([&result] {
result.FetchResults();
return errOK;
return Error();
});
}

Expand All @@ -88,7 +88,7 @@ template <>
Error ReindexerInterface<reindexer::Reindexer>::modify(reindexer::Transaction& transaction,
reindexer::Item&& item, ItemModifyMode mode) {
transaction.Modify(std::move(item), mode);
return errOK;
return {};
}
template <>
Error ReindexerInterface<reindexer::client::CoroReindexer>::modify(reindexer::client::CoroTransaction& transaction,
Expand Down Expand Up @@ -156,14 +156,14 @@ Error ReindexerInterface<reindexer::client::CoroReindexer>::connect(const std::s

template <>
Error ReindexerInterface<reindexer::Reindexer>::stop() {
return errOK;
return {};
}

template <>
Error ReindexerInterface<reindexer::client::CoroReindexer>::stop() {
db_.Stop();
stopCh_.close();
return errOK;
return {};
}

#ifdef PYREINDEXER_CPROTO
Expand Down

0 comments on commit 7984ea1

Please sign in to comment.