Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indexes.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from django.db.models import Index
  2. __all__ = ['BrinIndex', 'GinIndex', 'GistIndex']
  3. class MaxLengthMixin:
  4. # Allow an index name longer than 30 characters since the suffix is 4
  5. # characters (usual limit is 3). Since this index can only be used on
  6. # PostgreSQL, the 30 character limit for cross-database compatibility isn't
  7. # applicable.
  8. max_name_length = 31
  9. class BrinIndex(MaxLengthMixin, Index):
  10. suffix = 'brin'
  11. def __init__(self, *, pages_per_range=None, **kwargs):
  12. if pages_per_range is not None and pages_per_range <= 0:
  13. raise ValueError('pages_per_range must be None or a positive integer')
  14. self.pages_per_range = pages_per_range
  15. super().__init__(**kwargs)
  16. def deconstruct(self):
  17. path, args, kwargs = super().deconstruct()
  18. if self.pages_per_range is not None:
  19. kwargs['pages_per_range'] = self.pages_per_range
  20. return path, args, kwargs
  21. def create_sql(self, model, schema_editor, using=''):
  22. statement = super().create_sql(model, schema_editor, using=' USING brin')
  23. if self.pages_per_range is not None:
  24. statement.parts['extra'] = ' WITH (pages_per_range={})'.format(
  25. schema_editor.quote_value(self.pages_per_range)
  26. ) + statement.parts['extra']
  27. return statement
  28. class GinIndex(Index):
  29. suffix = 'gin'
  30. def __init__(self, *, fastupdate=None, gin_pending_list_limit=None, **kwargs):
  31. self.fastupdate = fastupdate
  32. self.gin_pending_list_limit = gin_pending_list_limit
  33. super().__init__(**kwargs)
  34. def deconstruct(self):
  35. path, args, kwargs = super().deconstruct()
  36. if self.fastupdate is not None:
  37. kwargs['fastupdate'] = self.fastupdate
  38. if self.gin_pending_list_limit is not None:
  39. kwargs['gin_pending_list_limit'] = self.gin_pending_list_limit
  40. return path, args, kwargs
  41. def create_sql(self, model, schema_editor, using=''):
  42. statement = super().create_sql(model, schema_editor, using=' USING gin')
  43. with_params = []
  44. if self.gin_pending_list_limit is not None:
  45. with_params.append('gin_pending_list_limit = %d' % self.gin_pending_list_limit)
  46. if self.fastupdate is not None:
  47. with_params.append('fastupdate = {}'.format('on' if self.fastupdate else 'off'))
  48. if with_params:
  49. statement.parts['extra'] = 'WITH ({}) {}'.format(', '.join(with_params), statement.parts['extra'])
  50. return statement
  51. class GistIndex(MaxLengthMixin, Index):
  52. suffix = 'gist'
  53. def __init__(self, *, buffering=None, fillfactor=None, **kwargs):
  54. self.buffering = buffering
  55. self.fillfactor = fillfactor
  56. super().__init__(**kwargs)
  57. def deconstruct(self):
  58. path, args, kwargs = super().deconstruct()
  59. if self.buffering is not None:
  60. kwargs['buffering'] = self.buffering
  61. if self.fillfactor is not None:
  62. kwargs['fillfactor'] = self.fillfactor
  63. return path, args, kwargs
  64. def create_sql(self, model, schema_editor):
  65. statement = super().create_sql(model, schema_editor, using=' USING gist')
  66. with_params = []
  67. if self.buffering is not None:
  68. with_params.append('buffering = {}'.format('on' if self.buffering else 'off'))
  69. if self.fillfactor is not None:
  70. with_params.append('fillfactor = %s' % self.fillfactor)
  71. if with_params:
  72. statement.parts['extra'] = 'WITH ({}) {}'.format(', '.join(with_params), statement.parts['extra'])
  73. return statement