Added ChunkedUpdate to leverage update_many

The ChunkedInsert object is great for speeding up inserts. This commit
does the same for updates. It chunks up updates then sends them to the
table's `update_many` in bundles that contain updates to the same
fields.

In doing this the ChunkedInsert and new ChunkedUpdate were refactored to
inherit from a common `_Chunker` object that does most of the chunking
logic. This should also make it simple to add a ChunkedUpsert object.
This commit is contained in:
Micha Gorelick
2020-03-13 19:00:54 +01:00
parent 03b7532997
commit ff98cfc5d4
2 changed files with 77 additions and 22 deletions
+16
View File
@@ -426,6 +426,22 @@ class TableTestCase(unittest.TestCase):
# Ensure data has been updated.
assert tbl.find_one(id=1)['temp'] == tbl.find_one(id=3)['temp']
def test_chunked_update(self):
tbl = self.db['update_many_test']
tbl.insert_many([
dict(temp=10, location='asdf'), dict(temp=20, location='qwer'), dict(temp=30, location='asdf')
])
chunked_tbl = chunked.ChunkedUpdate(tbl, 'id')
chunked_tbl.update(dict(id=1, temp=50))
chunked_tbl.update(dict(id=2, location='asdf'))
chunked_tbl.update(dict(id=3, temp=50))
chunked_tbl.flush()
# Ensure data has been updated.
assert tbl.find_one(id=1)['temp'] == tbl.find_one(id=3)['temp'] == 50
assert tbl.find_one(id=2)['location'] == tbl.find_one(id=3)['location'] == 'asdf'
def test_upsert_many(self):
# Also tests updating on records with different attributes
tbl = self.db['upsert_many_test']