Beispiel #1
0
def get_keyspace(name, durable_writes, strategy_class, strategy_options):
    keyspace = KeyspaceMetadata(name, durable_writes, strategy_class,
                                strategy_options)
    # build some tables
    tables = {}
    for t in range(3):
        table = get_table(keyspace, "my_table_%s" % t)
        tables[table.name] = table

    keyspace.tables = tables
    return keyspace
Beispiel #2
0
    def test_getting_replicas(self):
        tokens = [MD5Token(str(i)) for i in range(0, (2**127 - 1), 2**125)]
        hosts = [
            Host("ip%d" % i, SimpleConvictionPolicy)
            for i in range(len(tokens))
        ]
        token_to_primary_replica = dict(zip(tokens, hosts))
        keyspace = KeyspaceMetadata("ks", True, "SimpleStrategy",
                                    {"replication_factor": "1"})
        metadata = Mock(spec=Metadata, keyspaces={'ks': keyspace})
        token_map = TokenMap(MD5Token, token_to_primary_replica, tokens,
                             metadata)

        # tokens match node tokens exactly
        for i, token in enumerate(tokens):
            expected_host = hosts[(i + 1) % len(hosts)]
            replicas = token_map.get_replicas("ks", token)
            self.assertEqual(set(replicas), set([expected_host]))

        # shift the tokens back by one
        for token, expected_host in zip(tokens, hosts):
            replicas = token_map.get_replicas("ks",
                                              MD5Token(str(token.value - 1)))
            self.assertEqual(set(replicas), set([expected_host]))

        # shift the tokens forward by one
        for i, token in enumerate(tokens):
            replicas = token_map.get_replicas("ks",
                                              MD5Token(str(token.value + 1)))
            expected_host = hosts[(i + 1) % len(hosts)]
            self.assertEqual(set(replicas), set([expected_host]))
Beispiel #3
0
    def test_export_as_string_user_types(self):
        keyspace_name = 'test'
        keyspace = KeyspaceMetadata(keyspace_name, True, 'SimpleStrategy',
                                    dict(replication_factor=3))
        keyspace.user_types['a'] = UserType(keyspace_name, 'a', ['one', 'two'],
                                            ['c', 'int'])
        keyspace.user_types['b'] = UserType(keyspace_name, 'b',
                                            ['one', 'two', 'three'],
                                            ['d', 'int', 'a'])
        keyspace.user_types['c'] = UserType(keyspace_name, 'c', ['one'],
                                            ['int'])
        keyspace.user_types['d'] = UserType(keyspace_name, 'd', ['one'], ['c'])

        self.assertEqual(
            """CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}  AND durable_writes = true;

CREATE TYPE test.c (
    one int
);

CREATE TYPE test.a (
    one c,
    two int
);

CREATE TYPE test.d (
    one c
);

CREATE TYPE test.b (
    one d,
    two int,
    three a
);""", keyspace.export_as_string())
Beispiel #4
0
 def create_keyspace_simple(cls,
                            name,
                            replication_factor=1,
                            durable_writes=True,
                            connections=[]):
     for connection in connections:
         assert connection in cls.connections
         if name not in cls.get_cluster(connection).metadata.keyspaces:
             cls.get_cluster(
                 connection).metadata.keyspaces[name] = KeyspaceMetadata(
                     name, durable_writes, None, None)
             MockCluster.database[name] = {}
Beispiel #5
0
    def test_export_as_string_user_types(self):
        keyspace_name = 'test'
        keyspace = KeyspaceMetadata(keyspace_name, True, 'SimpleStrategy', dict(replication_factor=3))
        keyspace.user_types['a'] = UserType(keyspace_name, 'a', ['one', 'two'],
                                            [self.mock_user_type('UserType', 'c'),
                                             self.mock_user_type('IntType', 'int')])
        keyspace.user_types['b'] = UserType(keyspace_name, 'b', ['one', 'two', 'three'],
                                            [self.mock_user_type('UserType', 'd'),
                                             self.mock_user_type('IntType', 'int'),
                                             self.mock_user_type('UserType', 'a')])
        keyspace.user_types['c'] = UserType(keyspace_name, 'c', ['one'],
                                            [self.mock_user_type('IntType', 'int')])
        keyspace.user_types['d'] = UserType(keyspace_name, 'd', ['one'],
                                            [self.mock_user_type('UserType', 'c')])

        self.assertEqual("""CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}  AND durable_writes = true;

CREATE TYPE test.c (
    one int
);

CREATE TYPE test.a (
    one c,
    two int
);

CREATE TYPE test.d (
    one c
);

CREATE TYPE test.b (
    one d,
    two int,
    three a
);""", keyspace.export_as_string())
Beispiel #6
0
    def test_export_as_string_user_types(self):
        keyspace_name = "test"
        keyspace = KeyspaceMetadata(keyspace_name, True, "SimpleStrategy", dict(replication_factor=3))
        keyspace.user_types["a"] = UserType(keyspace_name, "a", ["one", "two"], ["c", "int"])
        keyspace.user_types["b"] = UserType(keyspace_name, "b", ["one", "two", "three"], ["d", "int", "a"])
        keyspace.user_types["c"] = UserType(keyspace_name, "c", ["one"], ["int"])
        keyspace.user_types["d"] = UserType(keyspace_name, "d", ["one"], ["c"])

        self.assertEqual(
            """CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}  AND durable_writes = true;

CREATE TYPE test.c (
    one int
);

CREATE TYPE test.a (
    one c,
    two int
);

CREATE TYPE test.d (
    one c
);

CREATE TYPE test.b (
    one d,
    two int,
    three a
);""",
            keyspace.export_as_string(),
        )
 def test_keyspace_name(self):
     km = KeyspaceMetadata(self.name, False, 'SimpleStrategy', {'replication_factor': 1})
     km.export_as_string()
Beispiel #8
0
 def test_keyspace_name(self):
     km = KeyspaceMetadata(self.name, False, 'SimpleStrategy',
                           {'replication_factor': 1})
     km.export_as_string()
Beispiel #9
0
 def _create_keyspace_metadata(self, graph_engine):
     return KeyspaceMetadata('keyspace',
                             True,
                             'org.apache.cassandra.locator.SimpleStrategy',
                             {'replication_factor': 1},
                             graph_engine=graph_engine)