1 | # identity.py |
---|
2 | # Copyright (C) the SQLAlchemy authors and contributors |
---|
3 | # |
---|
4 | # This module is part of SQLAlchemy and is released under |
---|
5 | # the MIT License: http://www.opensource.org/licenses/mit-license.php |
---|
6 | |
---|
7 | import weakref |
---|
8 | |
---|
9 | from sqlalchemy import util as base_util |
---|
10 | from sqlalchemy.orm import attributes |
---|
11 | |
---|
12 | |
---|
13 | class IdentityMap(dict): |
---|
14 | def __init__(self): |
---|
15 | self._mutable_attrs = set() |
---|
16 | self._modified = set() |
---|
17 | self._wr = weakref.ref(self) |
---|
18 | |
---|
19 | def replace(self, state): |
---|
20 | raise NotImplementedError() |
---|
21 | |
---|
22 | def add(self, state): |
---|
23 | raise NotImplementedError() |
---|
24 | |
---|
25 | def remove(self, state): |
---|
26 | raise NotImplementedError() |
---|
27 | |
---|
28 | def update(self, dict): |
---|
29 | raise NotImplementedError("IdentityMap uses add() to insert data") |
---|
30 | |
---|
31 | def clear(self): |
---|
32 | raise NotImplementedError("IdentityMap uses remove() to remove data") |
---|
33 | |
---|
34 | def _manage_incoming_state(self, state): |
---|
35 | state._instance_dict = self._wr |
---|
36 | |
---|
37 | if state.modified: |
---|
38 | self._modified.add(state) |
---|
39 | if state.manager.mutable_attributes: |
---|
40 | self._mutable_attrs.add(state) |
---|
41 | |
---|
42 | def _manage_removed_state(self, state): |
---|
43 | del state._instance_dict |
---|
44 | self._mutable_attrs.discard(state) |
---|
45 | self._modified.discard(state) |
---|
46 | |
---|
47 | def _dirty_states(self): |
---|
48 | return self._modified.union(s for s in list(self._mutable_attrs) |
---|
49 | if s.modified) |
---|
50 | |
---|
51 | def check_modified(self): |
---|
52 | """return True if any InstanceStates present have been marked as 'modified'.""" |
---|
53 | |
---|
54 | if self._modified: |
---|
55 | return True |
---|
56 | else: |
---|
57 | for state in list(self._mutable_attrs): |
---|
58 | if state.modified: |
---|
59 | return True |
---|
60 | return False |
---|
61 | |
---|
62 | def has_key(self, key): |
---|
63 | return key in self |
---|
64 | |
---|
65 | def popitem(self): |
---|
66 | raise NotImplementedError("IdentityMap uses remove() to remove data") |
---|
67 | |
---|
68 | def pop(self, key, *args): |
---|
69 | raise NotImplementedError("IdentityMap uses remove() to remove data") |
---|
70 | |
---|
71 | def setdefault(self, key, default=None): |
---|
72 | raise NotImplementedError("IdentityMap uses add() to insert data") |
---|
73 | |
---|
74 | def copy(self): |
---|
75 | raise NotImplementedError() |
---|
76 | |
---|
77 | def __setitem__(self, key, value): |
---|
78 | raise NotImplementedError("IdentityMap uses add() to insert data") |
---|
79 | |
---|
80 | def __delitem__(self, key): |
---|
81 | raise NotImplementedError("IdentityMap uses remove() to remove data") |
---|
82 | |
---|
83 | class WeakInstanceDict(IdentityMap): |
---|
84 | |
---|
85 | def __getitem__(self, key): |
---|
86 | state = dict.__getitem__(self, key) |
---|
87 | o = state.obj() |
---|
88 | if o is None: |
---|
89 | o = state._is_really_none() |
---|
90 | if o is None: |
---|
91 | raise KeyError, key |
---|
92 | return o |
---|
93 | |
---|
94 | def __contains__(self, key): |
---|
95 | try: |
---|
96 | if dict.__contains__(self, key): |
---|
97 | state = dict.__getitem__(self, key) |
---|
98 | o = state.obj() |
---|
99 | if o is None: |
---|
100 | o = state._is_really_none() |
---|
101 | else: |
---|
102 | return False |
---|
103 | except KeyError: |
---|
104 | return False |
---|
105 | return o is not None |
---|
106 | |
---|
107 | def contains_state(self, state): |
---|
108 | return dict.get(self, state.key) is state |
---|
109 | |
---|
110 | def replace(self, state): |
---|
111 | if dict.__contains__(self, state.key): |
---|
112 | existing = dict.__getitem__(self, state.key) |
---|
113 | if existing is not state: |
---|
114 | self._manage_removed_state(existing) |
---|
115 | else: |
---|
116 | return |
---|
117 | |
---|
118 | dict.__setitem__(self, state.key, state) |
---|
119 | self._manage_incoming_state(state) |
---|
120 | |
---|
121 | def add(self, state): |
---|
122 | if state.key in self: |
---|
123 | if dict.__getitem__(self, state.key) is not state: |
---|
124 | raise AssertionError("A conflicting state is already present in the identity map for key %r" % (state.key, )) |
---|
125 | else: |
---|
126 | dict.__setitem__(self, state.key, state) |
---|
127 | self._manage_incoming_state(state) |
---|
128 | |
---|
129 | def remove_key(self, key): |
---|
130 | state = dict.__getitem__(self, key) |
---|
131 | self.remove(state) |
---|
132 | |
---|
133 | def remove(self, state): |
---|
134 | if dict.pop(self, state.key) is not state: |
---|
135 | raise AssertionError("State %s is not present in this identity map" % state) |
---|
136 | self._manage_removed_state(state) |
---|
137 | |
---|
138 | def discard(self, state): |
---|
139 | if self.contains_state(state): |
---|
140 | dict.__delitem__(self, state.key) |
---|
141 | self._manage_removed_state(state) |
---|
142 | |
---|
143 | def get(self, key, default=None): |
---|
144 | try: |
---|
145 | return self[key] |
---|
146 | except KeyError: |
---|
147 | return default |
---|
148 | |
---|
149 | def items(self): |
---|
150 | return list(self.iteritems()) |
---|
151 | |
---|
152 | def iteritems(self): |
---|
153 | for state in dict.itervalues(self): |
---|
154 | value = state.obj() |
---|
155 | if value is not None: |
---|
156 | yield state.key, value |
---|
157 | |
---|
158 | def itervalues(self): |
---|
159 | for state in dict.itervalues(self): |
---|
160 | instance = state.obj() |
---|
161 | if instance is not None: |
---|
162 | yield instance |
---|
163 | |
---|
164 | def values(self): |
---|
165 | return list(self.itervalues()) |
---|
166 | |
---|
167 | def all_states(self): |
---|
168 | return dict.values(self) |
---|
169 | |
---|
170 | def prune(self): |
---|
171 | return 0 |
---|
172 | |
---|
173 | class StrongInstanceDict(IdentityMap): |
---|
174 | def all_states(self): |
---|
175 | return [attributes.instance_state(o) for o in self.values()] |
---|
176 | |
---|
177 | def contains_state(self, state): |
---|
178 | return state.key in self and attributes.instance_state(self[state.key]) is state |
---|
179 | |
---|
180 | def replace(self, state): |
---|
181 | if dict.__contains__(self, state.key): |
---|
182 | existing = dict.__getitem__(self, state.key) |
---|
183 | existing = attributes.instance_state(existing) |
---|
184 | if existing is not state: |
---|
185 | self._manage_removed_state(existing) |
---|
186 | else: |
---|
187 | return |
---|
188 | |
---|
189 | dict.__setitem__(self, state.key, state.obj()) |
---|
190 | self._manage_incoming_state(state) |
---|
191 | |
---|
192 | def add(self, state): |
---|
193 | if state.key in self: |
---|
194 | if attributes.instance_state(dict.__getitem__(self, state.key)) is not state: |
---|
195 | raise AssertionError("A conflicting state is already present in the identity map for key %r" % (state.key, )) |
---|
196 | else: |
---|
197 | dict.__setitem__(self, state.key, state.obj()) |
---|
198 | self._manage_incoming_state(state) |
---|
199 | |
---|
200 | def remove(self, state): |
---|
201 | if attributes.instance_state(dict.pop(self, state.key)) is not state: |
---|
202 | raise AssertionError("State %s is not present in this identity map" % state) |
---|
203 | self._manage_removed_state(state) |
---|
204 | |
---|
205 | def discard(self, state): |
---|
206 | if self.contains_state(state): |
---|
207 | dict.__delitem__(self, state.key) |
---|
208 | self._manage_removed_state(state) |
---|
209 | |
---|
210 | def remove_key(self, key): |
---|
211 | state = attributes.instance_state(dict.__getitem__(self, key)) |
---|
212 | self.remove(state) |
---|
213 | |
---|
214 | def prune(self): |
---|
215 | """prune unreferenced, non-dirty states.""" |
---|
216 | |
---|
217 | ref_count = len(self) |
---|
218 | dirty = [s.obj() for s in self.all_states() if s.check_modified()] |
---|
219 | keepers = weakref.WeakValueDictionary(self) |
---|
220 | dict.clear(self) |
---|
221 | dict.update(self, keepers) |
---|
222 | self.modified = bool(dirty) |
---|
223 | return ref_count - len(self) |
---|
224 | |
---|