Source

django-dbindexer / dbindexer / backends.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
from django.db import models
from django.db.models.fields import FieldDoesNotExist
from django.db.models.sql.constants import JOIN_TYPE, LHS_ALIAS, LHS_JOIN_COL, \
    TABLE_NAME, RHS_JOIN_COL
from django.utils.tree import Node
from djangotoolbox.fields import ListField
from dbindexer.lookups import StandardLookup

OR = 'OR'

# TODO: optimize code (JOIN backend and in memory-JOINs are rather slow, cache?)
class BaseResolver(object):
    def __init__(self):
        # mapping from lookups to indexes
        self.index_map = {}
        # mapping from column names to field names
        self.column_to_name = {}
        
    ''' API called by resolver''' 
    
    def create_index(self, lookup):
        field_to_index = self.get_field_to_index(lookup.model, lookup.field_name)
        
        if not field_to_index:
            return 
        
        index_field = lookup.get_field_to_add(field_to_index)        
        config_field = index_field.item_field if \
            isinstance(index_field, ListField) else index_field  
        if hasattr(field_to_index, 'max_length') and \
                isinstance(config_field, models.CharField):
            config_field.max_length = field_to_index.max_length
            
        # don't install a field if it already exists
        try:
            lookup.model._meta.get_field(self.index_name(lookup))
        except:
            lookup.model.add_to_class(self.index_name(lookup), index_field)
            self.index_map[lookup] = index_field
            self.add_column_to_name(lookup.model, lookup.field_name)

    def convert_query(self, query):
        '''Converts a database saving query.'''
        
        for lookup in self.index_map.keys():
            if not lookup.model == query.model:
                continue
            
            position = self.get_query_position(query, lookup)
            if position is None:
                return
            
            value = self.get_value(lookup.model, lookup.field_name, query)
            value = lookup.convert_value(value)
            query.values[position] = (self.get_index(lookup), value)
    
    def convert_filters(self, query):
        self._convert_filters(query, query.where)

    ''' helper methods '''
    
    def _convert_filters(self, query, filters):
        for index, child in enumerate(filters.children[:]):
            if isinstance(child, Node):
                self._convert_filters(query, child)
                continue

            self.convert_filter(query, filters, child, index)

    def convert_filter(self, query, filters, child, index):
        constraint, lookup_type, annotation, value = child
        field_name = self.column_to_name.get(constraint.field.column)
        if field_name and constraint.field is not None and \
                constraint.alias == query.table_map[query.model._meta.db_table][0]:
            for lookup in self.index_map.keys():
                if lookup.matches_filter(query.model, field_name, lookup_type,
                                         value):
                    new_lookup_type, new_value = lookup.convert_lookup(value,
                                                                       lookup_type)
                    index_name = self.index_name(lookup)
                    self._convert_filter(query, filters, child, index,
                                         new_lookup_type, new_value, index_name)
        
    def _convert_filter(self, query, filters, child, index, new_lookup_type,
                        new_value, index_name):
        constraint, lookup_type, annotation, value = child
        lookup_type, value = new_lookup_type, new_value
        constraint.field = query.get_meta().get_field(index_name)
        constraint.col = constraint.field.column
        child = constraint, lookup_type, annotation, value
        filters.children[index] = child
    
    def index_name(self, lookup):
        return lookup.index_name
    
    def get_field_to_index(self, model, field_name):
        try:
            return model._meta.get_field(field_name)
        except:
            return None
    
    def get_value(self, model, field_name, query):
        field_to_index = self.get_field_to_index(model, field_name)
        for index, (query_field, value) in enumerate(query.values[:]):
            if field_to_index == query_field:
                return value
        raise FieldDoesNotExist('Cannot find field in query.')
    
    def add_column_to_name(self, model, field_name):
        column_name = model._meta.get_field(field_name).column
        self.column_to_name[column_name] = field_name
    
    def get_index(self, lookup):
        return self.index_map[lookup]
    
    def get_query_position(self, query, lookup):
        for index, (field, query_value) in enumerate(query.values[:]):
            if field is self.get_index(lookup):
                return index
        return None

def unref_alias(query, alias):
    table_name = query.alias_map[alias][TABLE_NAME]
    query.alias_refcount[alias] -= 1
    if query.alias_refcount[alias] < 1:
        # Remove all information about the join
        del query.alias_refcount[alias]
        del query.join_map[query.rev_join_map[alias]]
        del query.rev_join_map[alias]
        del query.alias_map[alias]
        query.table_map[table_name].remove(alias)
        if len(query.table_map[table_name]) == 0:
            del query.table_map[table_name]
        query.used_aliases.discard(alias)

class PKNullFix(BaseResolver):
    '''
        Django doesn't generate correct code for ForeignKey__isnull.
        It becomes a JOIN with pk__isnull which won't work on nonrel DBs,
        so we rewrite the JOIN here.
    '''
     
    def create_index(self, lookup):
        pass
    
    def convert_query(self, query):
        pass
    
    def convert_filter(self, query, filters, child, index):
        constraint, lookup_type, annotation, value = child
        if constraint.field is not None and lookup_type == 'isnull' and \
                        isinstance(constraint.field, models.ForeignKey):
            self.fix_fk_null_filter(query, constraint)
            
    def unref_alias(self, query, alias):
        unref_alias(query, alias)
            
    def fix_fk_null_filter(self, query, constraint):
        alias = constraint.alias
        table_name = query.alias_map[alias][TABLE_NAME]
        lhs_join_col = query.alias_map[alias][LHS_JOIN_COL]
        rhs_join_col = query.alias_map[alias][RHS_JOIN_COL]
        if table_name != constraint.field.rel.to._meta.db_table or \
                rhs_join_col != constraint.field.rel.to._meta.pk.column or \
                lhs_join_col != constraint.field.column:
            return
        next_alias = query.alias_map[alias][LHS_ALIAS]
        if not next_alias:
            return
        self.unref_alias(query, alias)
        alias = next_alias
        constraint.col = constraint.field.column
        constraint.alias = alias

class JOINResolver(BaseResolver):
    def create_index(self, lookup):
        if '__' in lookup.field_name:
            BaseResolver.create_index(self, lookup)
    
    def convert_query(self, query):
        for lookup in self.index_map.keys():
            if lookup.model == query.model and '__' in lookup.field_name:
                BaseResolver.convert_query(self, query)
    
    def convert_filter(self, query, filters, child, index):
        constraint, lookup_type, annotation, value = child
        field_chain = self.get_field_chain(query, constraint)

        if field_chain is None:
            return
        
        for lookup in self.index_map.keys():
            if lookup.matches_filter(query.model, field_chain, lookup_type,
                                     value):
                self.resolve_join(query, child)
                new_lookup_type, new_value = lookup.convert_lookup(value,
                                                                   lookup_type)
                index_name = self.index_name(lookup)
                self._convert_filter(query, filters, child, index,
                                     new_lookup_type, new_value, index_name)
    
    def get_field_to_index(self, model, field_name):
        model = self.get_model_chain(model, field_name)[-1]
        field_name = field_name.split('__')[-1]
        return BaseResolver.get_field_to_index(self, model, field_name)
    
    def get_value(self, model, field_name, query):
        value = BaseResolver.get_value(self, model, field_name.split('__')[0],
                                    query)
        if value is not None:
            value = self.get_target_value(model, field_name, value)
        return value        

    def get_field_chain(self, query, constraint):
        if constraint.field is None:
            return

        column_index = self.get_column_index(query, constraint)
        return self.column_to_name.get(column_index)

    def get_model_chain(self, model, field_chain):
        model_chain = [model, ]
        for value in field_chain.split('__')[:-1]:
            model = model._meta.get_field(value).rel.to
            model_chain.append(model)
        return model_chain
       
    def get_target_value(self, start_model, field_chain, pk):
        fields = field_chain.split('__')
        foreign_key = start_model._meta.get_field(fields[0])
        
        if not foreign_key.rel:
            # field isn't a related one, so return the value itself
            return pk
        
        target_model = foreign_key.rel.to
        foreignkey = target_model.objects.all().get(pk=pk)
        for value in fields[1:-1]:
            foreignkey = getattr(foreignkey, value)
        return getattr(foreignkey, fields[-1])
    
    def add_column_to_name(self, model, field_name):
        model_chain = self.get_model_chain(model, field_name)
        column_chain = ''
        field_names = field_name.split('__')
        for model, name in zip(model_chain, field_names):
            column_chain += model._meta.get_field(name).column + '__'
        self.column_to_name[column_chain[:-2]] = field_name
        
    def unref_alias(self, query, alias):
        unref_alias(query, alias)
        
    def get_column_index(self, query, constraint):
        if constraint.field:
            column_chain = constraint.field.column
            alias = constraint.alias
            while alias:
                join = query.alias_map.get(alias)
                if join and join[JOIN_TYPE] == 'INNER JOIN':
                    column_chain += '__' + join[LHS_JOIN_COL]
                    alias = query.alias_map[alias][LHS_ALIAS]
                else:
                    alias = None
        return '__'.join(reversed(column_chain.split('__')))

    def resolve_join(self, query, child):
        constraint, lookup_type, annotation, value = child
        if not constraint.field:
            return

        alias = constraint.alias
        while True:
            next_alias = query.alias_map[alias][LHS_ALIAS]
            if not next_alias:
                break
            self.unref_alias(query, alias)
            alias = next_alias
        
        constraint.alias = alias

# TODO: distinguish in memory joins from standard joins somehow
class InMemoryJOINResolver(JOINResolver):
    def __init__(self):
        self.field_chains = []
        JOINResolver.__init__(self)

    def create_index(self, lookup):
        if '__' in lookup.field_name:
            field_to_index = self.get_field_to_index(lookup.model, lookup.field_name)
        
            if not field_to_index:
                return 
            
            # save old column_to_name so we can make in memory queries later on 
            self.add_column_to_name(lookup.model, lookup.field_name)
            
            # don't add an extra field for standard lookups!
            if isinstance(lookup, StandardLookup):
                return 
             
            # install lookup on target model
            model = self.get_model_chain(lookup.model, lookup.field_name)[-1]
            lookup.model = model
            lookup.field_name = lookup.field_name.split('__')[-1]
            BaseResolver.create_index(self, lookup)

    def convert_query(self, query):
        BaseResolver.convert_query(self, query)
        
    def _convert_filters(self, query, filters):
        # or queries are not supported for in-memory-JOINs
        if self.contains_OR(query.where, OR):
            return
        
        # start with the deepest JOIN level filter!
        all_filters = self.get_all_filters(query, filters)
        all_filters.sort(key=lambda item: self.get_field_chain(query, item[1][0]) and \
                         -len(self.get_field_chain(query, item[1][0])) or 0)
        
        for filters, child, index in all_filters:
            if not self.contains_child(query.where, child):
                continue
            self.convert_filter(query, filters, child, index)
    
    def convert_filter(self, query, filters, child, index):
        constraint, lookup_type, annotation, value = child
        field_chain = self.get_field_chain(query, constraint)

        if field_chain is None:
            return
        
        if '__' not in field_chain:
            return BaseResolver.convert_filter(self, query, filters, child, index)
        
        pks = self.get_pks(query, field_chain, lookup_type, value)
        self.resolve_join(query, child)
        # TODO: what happens if pks is empty?
        self._convert_filter(query, filters, child, index, 'in',
                             (pk for pk in pks), field_chain.split('__')[0])
        
    def tree_contains(self, filters, to_find, func):
        result = False
        for index, child in enumerate(filters.children[:]):
            if func(child, to_find):
                result = True
                break
            if isinstance(child, Node):
                 result = self.tree_contains(child, to_find, func)
                 if result:
                     break
        return result
    
    def contains_OR(self, filters, or_):
        return self.tree_contains(filters, or_,
            lambda c, f: isinstance(c, Node) and c.connector == f)

    def contains_child(self, filters, to_find):
        return self.tree_contains(filters, to_find, lambda c, f: c is f)
    
    def get_all_filters(self, query, filters):
        all_filters = []
        for index, child in enumerate(filters.children[:]):
            if isinstance(child, Node):
                all_filters.extend(self.get_all_filters(query, child))
                continue

            all_filters.append((filters, child, index))
        return all_filters
    
    def index_name(self, lookup):
        # use another index_name to avoid conflicts with lookups defined on the
        # target model which are handled by the BaseBackend
        return lookup.index_name + '_in_memory_join'
    
    def get_pks(self, query, field_chain, lookup_type, value):
        model_chain = self.get_model_chain(query.model, field_chain)
        field_names = field_chain.split('__')
        
        first_lookup = {'%s__%s' %(field_names[-1], lookup_type): value}
        self.combine_with_other_filter(first_lookup, query, field_chain)
        pks = model_chain[-1].objects.all().filter(**first_lookup).values_list(
            'id', flat=True)
        #print first_lookup
        for model, field_name in reversed(zip(model_chain[1:-1], field_names[1:-1])):
            lookup = {'%s__%s' %(field_name, 'in'):(pk for pk in pks)}
            #lookup.update(self.combine_with_other_filter(query, field_chain))
#            print lookup
            pks = model.objects.all().filter(**lookup).values_list('id', flat=True)
        return pks
    
    def combine_with_other_filter(self, lookup, query, field_chain):
        lookup_updates = {}
        field_chains = self.get_all_field_chains(query, query.where)

        for chain, child in field_chains.items():
            if chain == field_chain:
                continue
            if field_chain.rsplit('__', 1)[0] == chain.rsplit('__', 1)[0]:
                lookup_updates ['%s__%s' %(chain.rsplit('__', 1)[1], child[1])] \
                    = child[3]
                
                self.remove_child(query.where, child)
                self.resolve_join(query, child)
                # TODO: update query.alias_refcount correctly!
        lookup.update(lookup_updates)
                
    def remove_child(self, filters, to_remove):
        ''' Removes a child object from filters. If filters doesn't contain
            children afterwoods, filters will be removed from its parent. '''
            
        for child in filters.children[:]:
            if child is to_remove:
                self._remove_child(filters, to_remove)
                return
            elif isinstance(child, Node):
                self.remove_child(child, to_remove)
            
            if hasattr(child, 'children') and not child.children:
                self.remove_child(filters, child)
    
    def _remove_child(self, filters, to_remove):
        result = []
        for child in filters.children[:]:
            if child is to_remove:
                continue
            result.append(child)
        filters.children = result
    
    def get_all_field_chains(self, query, filters):
        ''' Returns a dict mapping from field_chains to the corresponding child.'''

        field_chains = {}
        all_filters = self.get_all_filters(query, filters)
        for filters, child, index in all_filters:
            field_chain = self.get_field_chain(query, child[0])
            # field_chain can be None if the user didn't specified an index for it
            if field_chain:
                field_chains[field_chain] = child
        return field_chains