File: sharding.py

package info (click to toggle)
django-cacheops 7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: python: 3,189; sh: 7; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,223 bytes parent folder | download | duplicates (2)
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
from funcy import cached_property
from django.core.exceptions import ImproperlyConfigured

from .conf import settings


def get_prefix(**kwargs):
    return settings.CACHEOPS_PREFIX(PrefixQuery(**kwargs))


class PrefixQuery(object):
    def __init__(self, **kwargs):
        assert set(kwargs) <= {'func', '_queryset', '_cond_dnfs', 'dbs', 'tables'}
        kwargs.setdefault('func', None)
        self.__dict__.update(kwargs)

    @cached_property
    def dbs(self):
        return [self._queryset.db]

    @cached_property
    def db(self):
        if len(self.dbs) > 1:
            dbs_str = ', '.join(self.dbs)
            raise ImproperlyConfigured('Single db required, but several used: ' + dbs_str)
        return self.dbs[0]

    # TODO: think if I should expose it and how. Same for queryset.
    @cached_property
    def _cond_dnfs(self):
        return self._queryset._cond_dnfs

    @cached_property
    def tables(self):
        return list(self._cond_dnfs)

    @cached_property
    def table(self):
        if len(self.tables) > 1:
            tables_str = ', '.join(self.tables)
            raise ImproperlyConfigured('Single table required, but several used: ' + tables_str)
        return self.tables[0]