Example #1
0
            class TestModelTwo(models.Model):
                id = TableShardedIDField(source_table=self.id_table)
                random_string = models.CharField(max_length=120)
                user_pk = models.PositiveIntegerField(primary_key=True)

                def get_shard(self):
                    pass
class Post(models.Model):
    id = TableShardedIDField(
        primary_key=True,
        source_table_name='api.PostIDs')  # 샤드된 Post들이 고유의 ID를 갖게 됨.
    title = models.CharField(null=False,
                             blank=False,
                             unique=True,
                             max_length=128)
    user_id = models.BigIntegerField(
        null=False, blank=False
    )  # 자동으로 테이블을 복제시키는 방법을 몰라서 일단 수동으로 넣는 것으로..(라이브러리에서는 지원 안 함.)

    def get_shard(
        self
    ):  # 각 Post가 자신의 버켓팅 전략에 맞게 샤드를 갖고 올 수 있도록 get_shard 메소드를 작성. 샤딩할 모델에는 get_shard 메소드를 반드시 작성해야 함.
        shard_group = getattr(self, 'django_sharding__shard_group')
        django_sharding_app = apps.get_app_config('django_sharding')
        bucketer = django_sharding_app.get_bucketer(shard_group)

        return bucketer.get_shard(self.user_id)

    class Meta:
        managed = True
        db_table = 'posts'
        app_label = 'api'
Example #3
0
class UnshardedTestModel(models.Model):
    id = TableShardedIDField(primary_key=True, source_table_name='tests.ShardedTestModelIDs')
    random_string = models.CharField(max_length=120)
    user_pk = models.PositiveIntegerField()

    def get_shard(self):
        from django.contrib.auth import get_user_model
        return get_user_model().objects.get(pk=self.user_pk).shard
Example #4
0
            class TestModelOne(models.Model):
                id = TableShardedIDField(primary_key=True,
                                         source_table_name="blah")
                random_string = models.CharField(max_length=120)
                user_pk = models.PositiveIntegerField()

                def get_shard(self):
                    pass
Example #5
0
class Book(models.Model):
    id = TableShardedIDField(primary_key=True, source_table_name='django_sharding_example.ShardedBookID')
    user = models.ForeignKey(ShardedUser, on_delete=models.CASCADE)
    title = models.CharField(max_length=120)

    def __str__(self):
        return 'Book ({})'.format(self.title)

    def get_shard(self):
        return self.user.shard
Example #6
0
class ShardedUser(models.Model):
    """
        This table has user_id.
        This user_id is supposed to be stored same shard db as user.shard in default db.
    """
    id = TableShardedIDField(primary_key=True, source_table_name='django_sharding_example.ShardedBookID')
    user_id = models.PositiveIntegerField()

    def get_shard(self):
        from django.contrib.auth import get_user_model
        return get_user_model().objects.get(pk=self.user_id).shard
Example #7
0
            class TestModelOne(models.Model):
                id = TableShardedIDField(primary_key=True,
                                         source_table=self.id_table)
                random_string = models.CharField(max_length=120)
                user_pk = models.PositiveIntegerField()

                def get_shard(self):
                    pass

                @staticmethod
                def get_shard_from_id(id):
                    pass
Example #8
0
class PostgresCustomAutoIDModel(models.Model):
    if settings.DATABASES['default']['ENGINE'] in Backends.POSTGRES:
        id = PostgresShardGeneratedIDAutoField(primary_key=True)
    else:
        id = TableShardedIDField(primary_key=True, source_table_name='tests.PostgresCustomIDModelBackupField')
    random_string = models.CharField(max_length=120)
    user_pk = models.PositiveIntegerField()

    def get_shard(self):
        return PostgresShardUser.objects.get(pk=self.user_pk).shard

    @staticmethod
    def get_shard_from_id(user_pk):
        return PostgresShardUser.objects.get(pk=user_pk).shard
Example #9
0
            class TestModelOne(models.Model):
                id = TableShardedIDField(primary_key=True,
                                         source_table_name="blah")
                random_string = models.CharField(max_length=120)
                user_pk = models.PositiveIntegerField()

                objects = CustomManager()

                def get_shard(self):
                    pass

                @staticmethod
                def get_shard_from_id(id):
                    pass
Example #10
0
class TestModel(models.Model):
    id = TableShardedIDField(primary_key=True,
                             source_table=ShardedTestModelIDs)
    random_string = models.CharField(max_length=120)
    user_pk = models.PositiveIntegerField()

    def get_shard(self):
        from django.contrib.auth import get_user_model
        return get_user_model().objects.get(pk=self.user_pk).shard

    @staticmethod
    def get_shard_from_id(user_pk):
        from django.contrib.auth import get_user_model
        user = get_user_model()
        return user.objects.get(pk=user_pk).shard
Example #11
0
        class TestModelTwo(models.Model):
            id = TableShardedIDField(primary_key=True,
                                     source_table_name="blah")
            random_string = models.CharField(max_length=120)
            user_pk = models.PositiveIntegerField()

            class Meta:
                abstract = True

            def get_shard(self):
                pass

            @staticmethod
            def get_shard_from_id(id):
                pass
Example #12
0
File: set.py Project: rafis/gmv
class Set(models.Model):
    id = TableShardedIDField(primary_key=True,
                             source_table_name='core.ShardedSetIDs')
    owner_user = models.OneToOneField(User,
                                      null=True,
                                      blank=True,
                                      on_delete=models.PROTECT)
    name = models.CharField(max_length=255)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    items = models.ManyToManyField('Item', blank=True)

    def get_shard(self):
        if self.pk is not None:
            shard_group = getattr(self, 'django_sharding__shard_group')
            django_sharding_app = apps.get_app_config('django_sharding')
            bucketer = django_sharding_app.get_bucketer(shard_group)

            return bucketer.get_shard(self)
        else:
            sharded_by_field = getattr(self,
                                       'django_sharding__sharded_by_field')
            sharded_by_field = self._meta.get_field(sharded_by_field)
            self.pk = sharded_by_field.strategy.get_next_id()

            return self.get_shard()

    @staticmethod
    def get_shard_from_id(pk):
        import pdb
        pdb.set_trace()
        if pk is not None:
            shard_group = getattr(Set, 'django_sharding__shard_group')
            django_sharding_app = apps.get_app_config('django_sharding')
            bucketer = django_sharding_app.get_bucketer(shard_group)

            obj = Set()
            obj.pk = pk

            return bucketer.get_shard(obj)
        else:
            sharded_by_field = getattr(Set,
                                       'django_sharding__sharded_by_field')

            sharded_by_field = Set._meta.get_field(sharded_by_field)
            pk = sharded_by_field.strategy.get_next_id()

            return get_shard_from_id(pk)
Example #13
0
        class TestModelThree(models.Model):
            id = TableShardedIDField(primary_key=True,
                                     source_table=self.id_table)
            random_string = models.CharField(max_length=120)
            user_pk = models.PositiveIntegerField()

            objects = ShardManager()

            class Meta:
                abstract = True

            def get_shard(self):
                pass

            @staticmethod
            def get_shard_from_id(id):
                pass
Example #14
0
File: item.py Project: rafis/gmv
class Item(models.Model):
    id = TableShardedIDField(primary_key=True,
                             source_table_name='core.ShardedItemIDs')
    name = models.CharField(max_length=255)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def get_shard(self):
        if self.pk is not None:
            shard_group = getattr(self, 'django_sharding__shard_group')
            django_sharding_app = apps.get_app_config('django_sharding')
            bucketer = django_sharding_app.get_bucketer(shard_group)

            return bucketer.get_shard(self)
        else:
            sharded_by_field = getattr(self,
                                       'django_sharding__sharded_by_field')
            sharded_by_field = self._meta.get_field(sharded_by_field)
            self.pk = sharded_by_field.strategy.get_next_id()

            return self.get_shard()

    @staticmethod
    def get_shard_from_id(pk):
        if pk is not None:
            shard_group = getattr(Item, 'django_sharding__shard_group')
            django_sharding_app = apps.get_app_config('django_sharding')
            bucketer = django_sharding_app.get_bucketer(shard_group)

            obj = Item()
            obj.pk = pk

            return bucketer.get_shard(obj)
        else:
            sharded_by_field = getattr(Item,
                                       'django_sharding__sharded_by_field')

            sharded_by_field = Item._meta.get_field(sharded_by_field)
            pk = sharded_by_field.strategy.get_next_id()

            return get_shard_from_id(pk)