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

implement bulk_add for applying several SQLite operations per transaction #3300

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
22 changes: 14 additions & 8 deletions beets/dbcore/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def __delattr__(self, key):

# Database interaction (CRUD methods).

def store(self, fields=None):
def store(self, fields=None, txn=None):
"""Save the object's metadata into the library database.
:param fields: the fields to be stored. If not specified, all fields
will be.
Expand All @@ -500,7 +500,7 @@ def store(self, fields=None):
subvars.append(value)
assignments = ','.join(assignments)

with self._db.transaction() as tx:
with self._db.transaction(txn) as tx:
# Main table update.
if assignments:
query = 'UPDATE {0} SET {1} WHERE id=?'.format(
Expand Down Expand Up @@ -541,11 +541,11 @@ def load(self):
self.update(dict(stored_obj))
self.clear_dirty()

def remove(self):
def remove(self, txn=None):
"""Remove the object's associated rows from the database.
"""
self._check_db()
with self._db.transaction() as tx:
with self._db.transaction(txn) as tx:
tx.mutate(
'DELETE FROM {0} WHERE id=?'.format(self._table),
(self.id,)
Expand All @@ -555,7 +555,7 @@ def remove(self):
(self.id,)
)

def add(self, db=None):
def add(self, db=None, txn=None):
"""Add the object to the library database. This object must be
associated with a database; you can provide one via the `db`
parameter or use the currently associated database.
Expand All @@ -567,7 +567,7 @@ def add(self, db=None):
self._db = db
self._check_db(False)

with self._db.transaction() as tx:
with self._db.transaction(txn) as tx:
new_id = tx.mutate(
'INSERT INTO {0} DEFAULT VALUES'.format(self._table)
)
Expand Down Expand Up @@ -933,6 +933,12 @@ def _close(self):
with self._shared_map_lock:
self._connections.clear()

def bulk_add(self, models):
"""Add a series of items within a single SQLite transaction."""
with Transaction(self) as txn:
for model in models:
model.add(self, txn)

@contextlib.contextmanager
def _tx_stack(self):
"""A context manager providing access to the current thread's
Expand All @@ -943,11 +949,11 @@ def _tx_stack(self):
with self._shared_map_lock:
yield self._tx_stacks[thread_id]

def transaction(self):
def transaction(self, txn=None):
"""Get a :class:`Transaction` object for interacting directly
with the underlying SQLite database.
"""
return Transaction(self)
return txn or Transaction(self)

def load_extension(self, path):
"""Load an SQLite extension into all open connections."""
Expand Down