Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sync Transaction support #17

Merged
merged 32 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6bbe288
[fix] Rework code according to Valgrind's recommendations
Oct 8, 2024
d27da93
Clear code
Oct 9, 2024
81ca870
Avoid PEP's warnings
Oct 9, 2024
2f9edd5
Update code
Oct 9, 2024
0ded690
Support sync transactions
Oct 14, 2024
de64dac
Rewrite query results wrapper
Oct 14, 2024
0392983
Properly use NewItem method. Reorganize code
Oct 15, 2024
1cbd051
Implement transaction py API
Oct 15, 2024
1a77b8e
Add transaction py-tests
Oct 15, 2024
3ac3485
Update Readme.md
Oct 16, 2024
0d44728
[fix] Fix build pyreindexer with reindexer v.4.15
Oct 16, 2024
2f01596
Try fix MacOS tests
Oct 17, 2024
ce25efa
Merge branch 'fix/rework_code_according_to_valgrinds_recommendations'…
Oct 17, 2024
da03def
Correct style
Oct 17, 2024
e016c33
Update tests
Oct 17, 2024
a7b03c2
Reorganize README
Oct 17, 2024
d0d732d
Correct test_item
Oct 18, 2024
afa4770
Merge branch 'fix/build_pyreindexer_with_v4.15.0_GetAggregationResult…
Oct 18, 2024
99099fa
Update transaction test
Oct 23, 2024
ae9dcd4
Simplify code
Oct 23, 2024
5454cf9
Style changes
Oct 23, 2024
f403305
Merge branch 'master' into feature/add_transaction_support
Oct 25, 2024
cd35c30
Add commit_with_count()
Oct 28, 2024
8f7542d
Add commit_with_count(). Fix code and check in test
Oct 29, 2024
2450019
Merge branch 'master' into feature/add_transaction_support
Nov 8, 2024
3a0485a
Merge branch 'master' into feature/add_transaction_support
Dec 2, 2024
111278b
Upgrade version
Dec 2, 2024
8e30837
Fix transaction tests
Dec 2, 2024
030effe
Fix build
Dec 2, 2024
1e70130
Update transaction tests
Dec 2, 2024
fda67a0
Update transaction tests. Fix build
Dec 2, 2024
ed49111
Review changes
Dec 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 195 additions & 43 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyreindexer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif()
enable_testing()

set(PY_MIN_VERSION 3.6)
set(RX_MIN_VERSION 3.24.0)
set(RX_MIN_VERSION 3.30.0)
find_package(PythonInterp ${PY_MIN_VERSION})
find_package(PythonLibs ${PY_MIN_VERSION})
find_package(reindexer CONFIG ${RX_MIN_VERSION})
Expand Down
1 change: 0 additions & 1 deletion pyreindexer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
"""

from pyreindexer.rx_connector import RxConnector
# from pyreindexer.index_definition import IndexDefinition
MadSchemas marked this conversation as resolved.
Show resolved Hide resolved
38 changes: 35 additions & 3 deletions pyreindexer/example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create_index_example(db, namespace):

try:
db.index_add(namespace, index_definition)
except Exception:
except (Exception,):
db.index_drop(namespace, 'id')
db.index_add(namespace, index_definition)

Expand All @@ -46,7 +46,7 @@ def create_items_example(db, namespace):
items_count = 10

for i in range(0, items_count):
item = {'id': i + 1, 'name': 'item_' + str(i % 2)}
item = {'id': i + 1, 'name': 'item_' + str(i % 2), 'value': 'check'}
db.item_upsert(namespace, item)


Expand All @@ -56,9 +56,37 @@ def select_item_query_example(db, namespace):
return db.select("SELECT * FROM " + namespace + " WHERE name='" + item_name_for_lookup + "'")


def transaction_example(db, namespace, items_in_base):
transaction = db.new_transaction(namespace)

items_count = len(items_in_base)

# delete first few items
for i in range(int(items_count/2)):
transaction.delete(items_in_base[i])

# update last one item, overwrite field 'value'
item = items_in_base[items_count - 1]
item['value'] = 'the transaction was here'
transaction.update(item)

# stop transaction and commit changes to namespace
transaction.commit()

# print records from namespace
selected_items_tr = select_item_query_example(db, namespace)

res_count = selected_items_tr.count()
print('Transaction results count: ', res_count)

# disposable QueryResults iterator
for item in selected_items_tr:
print('Item: ', item)


def rx_example():
db = RxConnector('builtin:///tmp/pyrx')
#db = RxConnector('cproto://127.0.0.1:6534/pyrx')
# db = RxConnector('cproto://127.0.0.1:6534/pyrx')

namespace = 'test_table'

Expand All @@ -75,13 +103,17 @@ def rx_example():
print('Results count: ', res_count)

# disposable QueryResults iterator
items_copy = []
for item in selected_items:
items_copy.append(item)
print('Item: ', item)

# won't be iterated again
for item in selected_items:
print('Item: ', item)

transaction_example(db, namespace, items_copy)

db.close()


Expand Down
4 changes: 2 additions & 2 deletions pyreindexer/index_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class IndexDefinition(dict):
`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) .
[More](https://github.com/Restream/reindexer/blob/master/fulltext.md).
"""

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

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

Expand Down
48 changes: 36 additions & 12 deletions pyreindexer/lib/include/queryresults_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,49 @@ using QueryResultsT = reindexer::QueryResults;

class QueryResultsWrapper {
public:
QueryResultsWrapper() : qresPtr(kResultsJson) {}
size_t Count() const { return qresPtr.Count(); }
void GetItemJSON(reindexer::WrSerializer& wrser, bool withHdrLen) { itPtr.GetJSON(wrser, withHdrLen); }
void Next() {
QueryResultsWrapper(DBInterface* db) : db_{db}, qres_{kResultsJson} {
assert(db_);
}

void Wrap(QueryResultsT&& qres) {
qres_ = std::move(qres);
it_ = qres_.begin();
wrap_ = true;
}

Error Select(const std::string& query) {
return db_->Select(query, *this);
}

size_t Count() const {
assert(wrap_);
return qres_.Count();
}

void GetItemJSON(reindexer::WrSerializer& wrser, bool withHdrLen) {
assert(wrap_);
it_.GetJSON(wrser, withHdrLen);
}

void Next() {
assert(wrap_);
db_->FetchResults(*this);
}

const std::vector<reindexer::AggregationResult>& GetAggregationResults() & { return qresPtr.GetAggregationResults(); }
void FetchResults() {
assert(wrap_);
// when results are fetched iterator closes and frees a memory of results buffer of Reindexer
++it_;
}

const std::vector<reindexer::AggregationResult>& GetAggregationResults() & { return qres_.GetAggregationResults(); }
const std::vector<reindexer::AggregationResult>& GetAggregationResults() && = delete;

private:
friend DBInterface;

void iterInit() { itPtr = qresPtr.begin(); }

DBInterface* db_ = nullptr;
QueryResultsT qresPtr;
QueryResultsT::Iterator itPtr;
DBInterface* db_{nullptr};
QueryResultsT qres_;
QueryResultsT::Iterator it_;
bool wrap_{false};
};

} // namespace pyreindexer
66 changes: 66 additions & 0 deletions pyreindexer/lib/include/transaction_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include "reindexerinterface.h"

#ifdef PYREINDEXER_CPROTO
#include "client/cororeindexer.h"
#else
#include "core/reindexer.h"
#endif

namespace pyreindexer {

#ifdef PYREINDEXER_CPROTO
using DBInterface = ReindexerInterface<reindexer::client::CoroReindexer>;
using TransactionT = reindexer::client::CoroTransaction;
using QueryResultsT = reindexer::client::CoroQueryResults;
using ItemT = reindexer::client::Item;
#else
using DBInterface = ReindexerInterface<reindexer::Reindexer>;
using TransactionT = reindexer::Transaction;
using QueryResultsT = reindexer::QueryResults;
using ItemT = reindexer::Item;
#endif

class TransactionWrapper {
public:
TransactionWrapper(DBInterface* db) : db_{db} {
assert(db_);
}

void Wrap(TransactionT&& transaction) {
transaction_ = std::move(transaction);
wrap_ = true;
}

Error Start(std::string_view ns) {
return db_->StartTransaction(ns, *this);
}

ItemT NewItem() {
assert(wrap_);
return db_->NewItem(transaction_);
}

Error Modify(ItemT&& item, ItemModifyMode mode) {
assert(wrap_);
return db_->Modify(transaction_, std::move(item), mode);
}

Error Commit(size_t& count) {
assert(wrap_);
return db_->CommitTransaction(transaction_, count);
}

Error Rollback() {
assert(wrap_);
return db_->RollbackTransaction(transaction_);
}

private:
DBInterface* db_{nullptr};
TransactionT transaction_;
bool wrap_{false};
};

} // namespace pyreindexer
Loading
Loading