Skip to content

Commit

Permalink
Rework API py: removes commit(), adds delete_meta() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander.A.Utkin committed Aug 27, 2024
1 parent 4e10bdc commit c337c34
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 166 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Gets a list of namespaces available

__Arguments:__

enum_not_opeden (bool, optional): An enumeration mode flag. If it is
enum_not_opened (bool, optional): An enumeration mode flag. If it is
set then closed namespaces are in result list too. Defaults to False.

__Returns:__
Expand Down Expand Up @@ -238,58 +238,59 @@ __Raises:__
Exception: Raises with an error message of API return on non-zero error code.


<h3 id="pyreindexer.rx_connector.RxConnector.commit">commit</h3>
<h3 id="pyreindexer.rx_connector.RxConnector.meta_put">meta_put</h3>

```python
RxConnector.commit(self, namespace)
RxConnector.meta_put(self, namespace, key, value)
```
Flushes changes to a storage of Reindexer
Puts metadata to a storage of Reindexer by key

__Arguments:__

namespace (string): A name of a namespace
key (string): A key in a storage of Reindexer for metadata keeping
value (string): A metadata for storage

__Raises:__

Exception: Raises with an error message of API return if Reindexer instance is not initialized yet.
Exception: Raises with an error message of API return on non-zero error code.


<h3 id="pyreindexer.rx_connector.RxConnector.meta_put">meta_put</h3>
<h3 id="pyreindexer.rx_connector.RxConnector.meta_get">meta_get</h3>

```python
RxConnector.meta_put(self, namespace, key, value)
RxConnector.meta_get(self, namespace, key)
```
Puts metadata to a storage of Reindexer by key
Gets metadata from a storage of Reindexer by key specified

__Arguments:__

namespace (string): A name of a namespace
key (string): A key in a storage of Reindexer for metadata keeping
value (string): A metadata for storage
key (string): A key in a storage of Reindexer where metadata is kept

__Returns:__

string: A metadata value

__Raises:__

Exception: Raises with an error message of API return if Reindexer instance is not initialized yet.
Exception: Raises with an error message of API return on non-zero error code.


<h3 id="pyreindexer.rx_connector.RxConnector.meta_get">meta_get</h3>
<h3 id="pyreindexer.rx_connector.RxConnector.meta_delete">meta_delete</h3>

```python
RxConnector.meta_get(self, namespace, key)
RxConnector.meta_delete(self, namespace, key)
```
Gets metadata from a storage of Reindexer by key specified
Deletes metadata from a storage of Reindexer by key specified

__Arguments:__

namespace (string): A name of a namespace
key (string): A key in a storage of Reindexer where metadata is kept

__Returns:__

string: A metadata value

__Raises:__

Exception: Raises with an error message of API return if Reindexer instance is not initialized yet.
Expand Down
11 changes: 9 additions & 2 deletions pyreindexer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ cmake_minimum_required(VERSION 3.0..3.13)

project(pyreindexer)

set(CMAKE_CXX_STANDARD 17)
# Configure cmake options
if(MSVC)
# Enable C++20 for windows build to be able to use designated initializers.
# GCC/Clang support them even with C++17.
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
Expand All @@ -11,7 +18,7 @@ endif()
enable_testing()

set(PY_MIN_VERSION 3.6)
set(RX_MIN_VERSION 3.2.2)
set(RX_MIN_VERSION 3.24.0)
find_package(PythonInterp ${PY_MIN_VERSION})
find_package(PythonLibs ${PY_MIN_VERSION})
find_package(reindexer CONFIG ${RX_MIN_VERSION})
Expand Down
19 changes: 12 additions & 7 deletions pyreindexer/index_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ class IndexDefinition(dict):
# Arguments:
name (str): An index name.
json_paths (:obj:`list` of :obj:`str`): A name for mapping a value to a json field.
field_type (str): A type of a field. Possible values are: `int`, `int64`, `double`, `string`, `bool`, `composite`.
field_type (str): A type of field. Possible values are: `int`, `int64`, `double`, `string`, `bool`, `composite`.
index_type (str): An index type. Possible values are: `hash`, `tree`, `text`, `-`.
is_pk (bool): True if a field is a primary key.
is_array (bool): True if an index is an array.
is_dense (bool): True if an index is dense. reduce index size. Saves 8 bytes per unique key value for 'hash' and 'tree' index types.
For '-' index type saves 4-8 bytes per each element. Useful for indexes with high selectivity, but for tree and hash indexes with low selectivity could
is_dense (bool): True if an index is dense. reduce index size. Saves 8 bytes per unique key value for 'hash'
and 'tree' index types.
For '-' index type saves 4-8 bytes per each element. Useful for indexes with high selectivity,
but for tree and hash indexes with low selectivity could
significantly decrease update performance.
is_sparse (bool): True if a value of an index may be not presented.
collate_mode (str): Sets an order of values by collate mode. Possible values are: `none`, `ascii`, `utf8`, `numeric`, `custom`.
collate_mode (str): Sets an order of values by collate mode. Possible values are:
`none`, `ascii`, `utf8`, `numeric`, `custom`.
sort_order_letters (str): Order for a sort sequence for a custom collate mode.
config (dict): A config for a fulltext engine. [More](https://github.com/Restream/reindexer/blob/master/fulltext.md) .
config (dict): A config for a fulltext engine.
[More](https://github.com/Restream/reindexer/blob/master/fulltext.md) .
"""

def __getitem__(self, attr):
Expand All @@ -33,11 +37,12 @@ def __setitem__(self, attr, value):
super(IndexDefinition, self).update({attr: value})
return self

def update(self, dict_part={}):
def update(self, dict_part=None):
raise NotImplementedError(
'Bulk update is not implemented for IndexDefinition instance')

def _get_known_attrs(self):
@staticmethod
def _get_known_attrs() -> list[str]:
return ['name', 'json_paths', 'field_type', 'index_type', 'is_pk',
'is_array', 'is_dense', 'is_sparse', 'collate_mode', 'sort_order_letters', 'expire_after', 'config']

Expand Down
10 changes: 4 additions & 6 deletions pyreindexer/lib/include/pyobjtools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@ void pyDictSerialize(PyObject **dict, reindexer::WrSerializer &wrSer) {
}

void PyObjectToJson(PyObject **obj, reindexer::WrSerializer &wrSer) {
if (!PyList_Check(*obj) && !PyDict_Check(*obj)) {
throw reindexer::Error(errParseJson,
std::string("PyObject must be a dictionary or a list for JSON serializing, got ") + Py_TYPE(*obj)->tp_name);
}

if (PyDict_Check(*obj)) {
pyDictSerialize(obj, wrSer);
} else {
} else if (PyList_Check(*obj) ) {
pyListSerialize(obj, wrSer);
} else {
throw reindexer::Error(errParseJson,
std::string("PyObject must be a dictionary or a list for JSON serializing, got ") + Py_TYPE(*obj)->tp_name);
}
}

Expand Down
25 changes: 13 additions & 12 deletions pyreindexer/lib/src/rawpyreindexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,35 +269,37 @@ static PyObject *ItemUpsert(PyObject *self, PyObject *args) { return itemModify(

static PyObject *ItemDelete(PyObject *self, PyObject *args) { return itemModify(self, args, ModeDelete); }

static PyObject *Commit(PyObject *self, PyObject *args) {
static PyObject *PutMeta(PyObject *self, PyObject *args) {
uintptr_t rx;
char *ns;
char *key;
char *value;

if (!PyArg_ParseTuple(args, "ks", &rx, &ns)) {
if (!PyArg_ParseTuple(args, "ksss", &rx, &ns, &key, &value)) {
return NULL;
}

Error err = getDB(rx)->Commit(ns);
Error err = getDB(rx)->PutMeta(ns, key, value);

return pyErr(err);
}

static PyObject *PutMeta(PyObject *self, PyObject *args) {
static PyObject *GetMeta(PyObject *self, PyObject *args) {
uintptr_t rx;
char *ns;
char *key;
char *value;

if (!PyArg_ParseTuple(args, "ksss", &rx, &ns, &key, &value)) {
if (!PyArg_ParseTuple(args, "kss", &rx, &ns, &key)) {
return NULL;
}

Error err = getDB(rx)->PutMeta(ns, key, value);
std::string value;
Error err = getDB(rx)->GetMeta(ns, key, value);

return pyErr(err);
return Py_BuildValue("iss", err.code(), err.what().c_str(), value.c_str());
}

static PyObject *GetMeta(PyObject *self, PyObject *args) {
static PyObject *DeleteMeta(PyObject *self, PyObject *args) {
uintptr_t rx;
char *ns;
char *key;
Expand All @@ -306,10 +308,9 @@ static PyObject *GetMeta(PyObject *self, PyObject *args) {
return NULL;
}

std::string value;
Error err = getDB(rx)->GetMeta(ns, key, value);
Error err = getDB(rx)->DeleteMeta(ns, key);

return Py_BuildValue("iss", err.code(), err.what().c_str(), value.c_str());
return pyErr(err);
}

static PyObject *Select(PyObject *self, PyObject *args) {
Expand Down
4 changes: 2 additions & 2 deletions pyreindexer/lib/src/rawpyreindexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ static PyObject *ItemInsert(PyObject *self, PyObject *args);
static PyObject *ItemUpdate(PyObject *self, PyObject *args);
static PyObject *ItemUpsert(PyObject *self, PyObject *args);
static PyObject *ItemDelete(PyObject *self, PyObject *args);
static PyObject *Commit(PyObject *self, PyObject *args);
static PyObject *PutMeta(PyObject *self, PyObject *args);
static PyObject *GetMeta(PyObject *self, PyObject *args);
static PyObject *DeleteMeta(PyObject *self, PyObject *args);
static PyObject *Select(PyObject *self, PyObject *args);
static PyObject *EnumMeta(PyObject *self, PyObject *args);
static PyObject *EnumNamespaces(PyObject *self, PyObject *args);
Expand All @@ -90,9 +90,9 @@ static PyMethodDef module_methods[] = {
{"item_update", ItemUpdate, METH_VARARGS, "update item"},
{"item_upsert", ItemUpsert, METH_VARARGS, "upsert item"},
{"item_delete", ItemDelete, METH_VARARGS, "delete item"},
{"commit", Commit, METH_VARARGS, "commit"},
{"meta_put", PutMeta, METH_VARARGS, "put meta"},
{"meta_get", GetMeta, METH_VARARGS, "get meta"},
{"meta_delete", DeleteMeta, METH_VARARGS, "delete meta"},
{"meta_enum", EnumMeta, METH_VARARGS, "enum meta"},
{"select", Select, METH_VARARGS, "select query"},

Expand Down
8 changes: 4 additions & 4 deletions pyreindexer/lib/src/reindexerinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ class ReindexerInterface {
Error Delete(std::string_view ns, typename DBT::ItemT& item) {
return execute([this, ns, &item] { return deleteImpl(ns, item); });
}
Error Commit(std::string_view ns) {
return execute([this, ns] { return commit(ns); });
}
Error PutMeta(std::string_view ns, const std::string& key, std::string_view data) {
return execute([this, ns, &key, data] { return putMeta(ns, key, data); });
}
Error GetMeta(std::string_view ns, const std::string& key, std::string& data) {
return execute([this, ns, &key, &data] { return getMeta(ns, key, data); });
}
Error DeleteMeta(std::string_view ns, const std::string& key) {
return execute([this, ns, &key] { return deleteMeta(ns, key); });
}
Error EnumMeta(std::string_view ns, std::vector<std::string>& keys) {
return execute([this, ns, &keys] { return enumMeta(ns, keys); });
}
Expand All @@ -125,9 +125,9 @@ class ReindexerInterface {
Error upsert(std::string_view ns, typename DBT::ItemT& item) { return db_.Upsert({ns.data(), ns.size()}, item); }
Error update(std::string_view ns, typename DBT::ItemT& item) { return db_.Update({ns.data(), ns.size()}, item); }
Error deleteImpl(std::string_view ns, typename DBT::ItemT& item) { return db_.Delete({ns.data(), ns.size()}, item); }
Error commit(std::string_view ns) { return db_.Commit({ns.data(), ns.size()}); }
Error putMeta(std::string_view ns, const std::string& key, std::string_view data) { return db_.PutMeta({ns.data(), ns.size()}, key, {data.data(), data.size()}); }
Error getMeta(std::string_view ns, const std::string& key, std::string& data) { return db_.GetMeta({ns.data(), ns.size()}, key, data); }
Error deleteMeta(std::string_view ns, const std::string& key) { return db_.DeleteMeta({ns.data(), ns.size()}, key); }
Error enumMeta(std::string_view ns, std::vector<std::string>& keys) { return db_.EnumMeta({ns.data(), ns.size()}, keys); }
Error select(const std::string &query, typename DBT::QueryResultsT& result) { return db_.Select(query, result); }
Error enumNamespaces(std::vector<NamespaceDef>& defs, EnumNamespacesOpts opts) { return db_.EnumNamespaces(defs, opts); }
Expand Down
4 changes: 2 additions & 2 deletions pyreindexer/query_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def __next__(self):
self.pos += 1
self.err_code, self.err_msg, res = self.api.query_results_iterate(
self.qres_wrapper_ptr)
if (self.err_code):
if self.err_code:
raise Exception(self.err_msg)
return res
else:
del(self)
del self
raise StopIteration

def __del__(self):
Expand Down
Loading

0 comments on commit c337c34

Please sign in to comment.