2017-01-16 03:09:38 +08:00
|
|
|
from django.db.models import Index
|
2016-08-08 19:50:25 +08:00
|
|
|
|
2017-07-01 21:30:34 +08:00
|
|
|
__all__ = ['BrinIndex', 'GinIndex', 'GistIndex']
|
2016-10-13 20:39:44 +08:00
|
|
|
|
|
|
|
|
2017-07-01 21:30:34 +08:00
|
|
|
class MaxLengthMixin:
|
2017-03-20 23:21:01 +08:00
|
|
|
# Allow an index name longer than 30 characters since the suffix is 4
|
|
|
|
# characters (usual limit is 3). Since this index can only be used on
|
|
|
|
# PostgreSQL, the 30 character limit for cross-database compatibility isn't
|
|
|
|
# applicable.
|
|
|
|
max_name_length = 31
|
2016-10-13 20:39:44 +08:00
|
|
|
|
2017-07-01 21:30:34 +08:00
|
|
|
|
|
|
|
class BrinIndex(MaxLengthMixin, Index):
|
|
|
|
suffix = 'brin'
|
|
|
|
|
2017-06-28 01:39:37 +08:00
|
|
|
def __init__(self, *, pages_per_range=None, **kwargs):
|
2017-01-17 00:28:30 +08:00
|
|
|
if pages_per_range is not None and pages_per_range <= 0:
|
|
|
|
raise ValueError('pages_per_range must be None or a positive integer')
|
2016-10-13 20:39:44 +08:00
|
|
|
self.pages_per_range = pages_per_range
|
2017-06-28 01:39:37 +08:00
|
|
|
super().__init__(**kwargs)
|
2016-10-13 20:39:44 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
path, args, kwargs = super().deconstruct()
|
2017-08-26 05:54:36 +08:00
|
|
|
if self.pages_per_range is not None:
|
|
|
|
kwargs['pages_per_range'] = self.pages_per_range
|
2016-10-13 20:39:44 +08:00
|
|
|
return path, args, kwargs
|
|
|
|
|
2017-08-04 05:33:06 +08:00
|
|
|
def create_sql(self, model, schema_editor, using=''):
|
|
|
|
statement = super().create_sql(model, schema_editor, using=' USING brin')
|
2016-10-13 20:39:44 +08:00
|
|
|
if self.pages_per_range is not None:
|
2017-08-04 05:33:06 +08:00
|
|
|
statement.parts['extra'] = ' WITH (pages_per_range={})'.format(
|
|
|
|
schema_editor.quote_value(self.pages_per_range)
|
|
|
|
) + statement.parts['extra']
|
|
|
|
return statement
|
2016-08-08 19:50:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GinIndex(Index):
|
|
|
|
suffix = 'gin'
|
|
|
|
|
2017-06-28 01:39:37 +08:00
|
|
|
def __init__(self, *, fastupdate=None, gin_pending_list_limit=None, **kwargs):
|
2017-05-31 05:17:32 +08:00
|
|
|
self.fastupdate = fastupdate
|
|
|
|
self.gin_pending_list_limit = gin_pending_list_limit
|
2017-06-28 01:39:37 +08:00
|
|
|
super().__init__(**kwargs)
|
2017-05-31 05:17:32 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
path, args, kwargs = super().deconstruct()
|
2017-08-26 22:32:03 +08:00
|
|
|
if self.fastupdate is not None:
|
|
|
|
kwargs['fastupdate'] = self.fastupdate
|
|
|
|
if self.gin_pending_list_limit is not None:
|
|
|
|
kwargs['gin_pending_list_limit'] = self.gin_pending_list_limit
|
2017-05-31 05:17:32 +08:00
|
|
|
return path, args, kwargs
|
|
|
|
|
2017-08-04 05:33:06 +08:00
|
|
|
def create_sql(self, model, schema_editor, using=''):
|
|
|
|
statement = super().create_sql(model, schema_editor, using=' USING gin')
|
2017-05-31 05:17:32 +08:00
|
|
|
with_params = []
|
|
|
|
if self.gin_pending_list_limit is not None:
|
|
|
|
with_params.append('gin_pending_list_limit = %d' % self.gin_pending_list_limit)
|
|
|
|
if self.fastupdate is not None:
|
|
|
|
with_params.append('fastupdate = {}'.format('on' if self.fastupdate else 'off'))
|
|
|
|
if with_params:
|
2017-08-04 05:33:06 +08:00
|
|
|
statement.parts['extra'] = 'WITH ({}) {}'.format(', '.join(with_params), statement.parts['extra'])
|
|
|
|
return statement
|
2017-07-01 21:30:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GistIndex(MaxLengthMixin, Index):
|
|
|
|
suffix = 'gist'
|
|
|
|
|
|
|
|
def __init__(self, *, buffering=None, fillfactor=None, **kwargs):
|
|
|
|
self.buffering = buffering
|
|
|
|
self.fillfactor = fillfactor
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
path, args, kwargs = super().deconstruct()
|
|
|
|
if self.buffering is not None:
|
|
|
|
kwargs['buffering'] = self.buffering
|
|
|
|
if self.fillfactor is not None:
|
|
|
|
kwargs['fillfactor'] = self.fillfactor
|
|
|
|
return path, args, kwargs
|
|
|
|
|
|
|
|
def create_sql(self, model, schema_editor):
|
|
|
|
statement = super().create_sql(model, schema_editor, using=' USING gist')
|
|
|
|
with_params = []
|
|
|
|
if self.buffering is not None:
|
|
|
|
with_params.append('buffering = {}'.format('on' if self.buffering else 'off'))
|
|
|
|
if self.fillfactor is not None:
|
|
|
|
with_params.append('fillfactor = %s' % self.fillfactor)
|
|
|
|
if with_params:
|
|
|
|
statement.parts['extra'] = 'WITH ({}) {}'.format(', '.join(with_params), statement.parts['extra'])
|
|
|
|
return statement
|