added insert_many

This commit is contained in:
Gregor Aisch 2013-04-04 15:43:05 +02:00
parent fea6eda35a
commit ec0d2d61f7
3 changed files with 34 additions and 1 deletions

View File

@ -61,6 +61,34 @@ class Table(object):
self._ensure_columns(row, types=types) self._ensure_columns(row, types=types)
self.database.engine.execute(self.table.insert(row)) self.database.engine.execute(self.table.insert(row))
def insert_many(self, rows, ensure=True, types={}):
"""
Add many rows at a time, which is significantly faster than adding
them one by one. The rows are automatically processed in chunks of
1000 per commit.
::
rows = [dict(name='Dolly')] * 10000
table.insert_many(rows)
"""
def _process_chunk(chunk):
if ensure:
for row in chunk:
self._ensure_columns(row, types=types)
self.table.insert().execute(chunk)
chunk_size = 1000
chunk = []
i = 0
for row in rows:
chunk.append(row)
i += 1
if i == chunk_size:
_process_chunk(chunk)
chunk = []
i = 0
if i > 0:
_process_chunk(chunk)
def update(self, row, keys, ensure=True, types={}): def update(self, row, keys, ensure=True, types={}):
""" """
Update a row in the table. The update is managed via Update a row in the table. The update is managed via

View File

@ -16,5 +16,5 @@ Table
----- -----
.. autoclass:: dataset.Table .. autoclass:: dataset.Table
:members: columns, drop, insert, update, upsert, find, find_one, distinct, create_column, create_index, all :members: columns, drop, insert, update, upsert, find, find_one, distinct, create_column, create_index, all, insert_many
:special-members: __len__, __iter__ :special-members: __len__, __iter__

View File

@ -96,6 +96,11 @@ class TableTestCase(unittest.TestCase):
x = list(self.tbl.distinct('place', 'date')) x = list(self.tbl.distinct('place', 'date'))
assert len(x)==6, x assert len(x)==6, x
def test_insert_many(self):
data = TEST_DATA * 5000
self.tbl.insert_many(data)
assert len(self.tbl) == len(data) + 6
if __name__ == '__main__': if __name__ == '__main__':