diff options
| author | Jason Kirtland <jek@discorporate.us> | 2007-08-16 19:58:24 +0000 |
|---|---|---|
| committer | Jason Kirtland <jek@discorporate.us> | 2007-08-16 19:58:24 +0000 |
| commit | 0339042815dad143d60fc236ae9905cf2a137a3c (patch) | |
| tree | 5af8d02d100a0fb09e3d1e9b15975ab63201ad06 /lib/sqlalchemy/orm/unitofwork.py | |
| parent | 13af7230dcb0bf720a7a46f701404fa16b298c13 (diff) | |
| download | sqlalchemy-0339042815dad143d60fc236ae9905cf2a137a3c.tar.gz | |
Added session.prune(), releases unused objects in strong-ref identity maps.
Diffstat (limited to 'lib/sqlalchemy/orm/unitofwork.py')
| -rw-r--r-- | lib/sqlalchemy/orm/unitofwork.py | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/unitofwork.py b/lib/sqlalchemy/orm/unitofwork.py index 9cf3da639..95ed950e1 100644 --- a/lib/sqlalchemy/orm/unitofwork.py +++ b/lib/sqlalchemy/orm/unitofwork.py @@ -19,12 +19,11 @@ new, dirty, or deleted and provides the capability to flush all those changes at once. """ +import gc, StringIO, weakref from sqlalchemy import util, logging, topological, exceptions from sqlalchemy.orm import attributes, interfaces from sqlalchemy.orm import util as mapperutil from sqlalchemy.orm.mapper import object_mapper -import StringIO -import weakref # Load lazily object_session = None @@ -220,6 +219,24 @@ class UnitOfWork(object): if session.extension is not None: session.extension.after_flush_postexec(session, flush_context) + def prune_identity_map(self): + """Removes unreferenced instances cached in the identity map. + + Removes any object in the identity map that is not referenced + in user code or scheduled for a unit of work operation. Returns + the number of objects pruned. + """ + + if isinstance(self.identity_map, weakref.WeakValueDictionary): + return 0 + ref_count = len(self.identity_map) + dirty = self.locate_dirty() + keepers = weakref.WeakValueDictionary(self.identity_map) + self.identity_map.clear() + gc.collect() + self.identity_map.update(keepers) + return ref_count - len(self.identity_map) + class UOWTransaction(object): """Handles the details of organizing and executing transaction tasks during a UnitOfWork object's flush() operation. |
