Пример #1
0
def create_realm(realm, shard_field, collection_name, default_dest):
    _get_realm_coll().insert({
        'name': realm,
        'shard_field': shard_field,
        'collection': collection_name,
        'default_dest': default_dest
    })
Пример #2
0
def create_indices():
    realm_coll = _get_realm_coll()
    realm_coll.ensure_index([('name', 1)], unique=True)
    realm_coll.ensure_index([('collection', 1)], unique=True)

    shards_coll = _get_shards_coll()
    shards_coll.ensure_index(
        [('realm', 1), ('shard_key', 1)], unique=True)

    cluster_coll = _get_cluster_coll()
    cluster_coll.ensure_index([('name', 1)], unique=True)
Пример #3
0
    def test_ensure_realm_exists(self):
        # Trying to get a none-existent realm should blow up
        with self.assertRaises(Exception) as catcher:
            realm = _get_realm_for_collection('some_collection')
        self.assertEquals(
            catcher.exception.message,
            'Realm for collection some_collection does not exist')

        ensure_realm_exists('some_realm', 'some_field', 'some_collection')
        realm = _get_realm_for_collection('some_collection')
        self.assertEquals('some_realm', realm['name'])

        # Try creating the realm again, ensure it doesn't blow up or create a
        # duplicate
        ensure_realm_exists('some_realm', 'some_field', 'some_collection')
        realm = _get_realm_for_collection('some_collection')
        self.assertEquals('some_realm', realm['name'])

        coll = _get_realm_coll()
        self.assertEquals(2, coll.count())  # One realm exists due to test base
Пример #4
0
    def test_ensure_realm_exists(self):
        # Trying to get a none-existent realm should blow up
        with self.assertRaises(Exception) as catcher:
            realm = _get_realm_for_collection('some_collection')
        self.assertEquals(
            catcher.exception.message,
            'Realm for collection some_collection does not exist')

        ensure_realm_exists('some_realm', 'some_field', 'some_collection')
        realm = _get_realm_for_collection('some_collection')
        self.assertEquals('some_realm', realm['name'])

        # Try creating the realm again, ensure it doesn't blow up or create a
        # duplicate
        ensure_realm_exists('some_realm', 'some_field', 'some_collection')
        realm = _get_realm_for_collection('some_collection')
        self.assertEquals('some_realm', realm['name'])

        coll = _get_realm_coll()
        self.assertEquals(2, coll.count()) # One realm exists due to test base
Пример #5
0
def ensure_realm_exists(name, shard_field, collection_name, default_dest):
    """Ensures that a realm of the given name exists and matches the expected
    settings.

    :param str name: The name of the realm
    :param shard_field: The field in documents that should be used as the shard
        field. The only supported values that can go in this field are strings
        and integers.
    :param str collection_name: The name of the collection that this realm
        corresponds to. In general, the collection name should match the realm
        name.
    :param str default_dest: The default destination for any data that isn't
        explicitly sharded to a specific location.
    :return: None
    """
    coll = _get_realm_coll()

    cursor = coll.find({'name': name})
    if cursor.count():
        # realm with this name already exists
        existing = cursor[0]
        if (existing['shard_field'] != shard_field
            or existing['collection'] != collection_name
            or existing['default_dest'] != default_dest):
            raise Exception('Cannot change realm')
        else:
            return
        
    cursor = coll.find({'collection': collection_name})
    if cursor.count():
        # realm for this collection already exists
        existing = cursor[0]
        if (existing['shard_field'] != shard_field
            or existing['name'] != name
            or existing['default_dest'] != default_dest):
            raise Exception(
                'Realm for collection %s already exists' % collection_name)
        else:
            return

    create_realm(name, shard_field, collection_name, default_dest)
Пример #6
0
def create_realm(realm, shard_field, collection_name, default_dest):
    _get_realm_coll().insert({
        'name': realm,
        'shard_field': shard_field,
        'collection': collection_name,
        'default_dest': default_dest})
Пример #7
0
def _reset_sharding_info():
    """Wipes all shard info. For internal test use only.
    """
    _get_realm_coll().remove({})
    _get_shards_coll().remove({})
Пример #8
0
def create_realm(realm, shard_field, collection_name):
    _get_realm_coll().insert({
        'name': realm,
        'shard_field': shard_field,
        'collection': collection_name
    })
Пример #9
0
def create_realm(realm, shard_field, collection_name):
    _get_realm_coll().insert({
        'name': realm,
        'shard_field': shard_field,
        'collection': collection_name})