|
1
|
|
|
import collections |
|
2
|
|
|
import datetime |
|
3
|
|
|
import logging |
|
4
|
|
|
|
|
5
|
|
|
from goblin import properties |
|
6
|
|
|
|
|
7
|
|
|
logger = logging.getLogger(__name__) |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
CARD_MAPPING = {'Cardinality.single': 'Cardinality.SINGLE', |
|
11
|
|
|
'Cardinality.list_':'Cardinality.LIST', |
|
12
|
|
|
'Cardinality.set_': 'Cardinality.SET'} |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
DATA_TYPE_MAPPING = {properties.Integer: 'Integer.class', |
|
16
|
|
|
properties.Float: 'Float.class', |
|
17
|
|
|
properties.String: 'String.class', |
|
18
|
|
|
properties.Boolean: 'Boolean.class'} |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
prop_keys = {} |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
PropertyKey = collections.namedtuple('PropertyKey', ['name', 'data_type', 'card']) |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
async def create_schema(app, indices, cluster): |
|
28
|
|
|
client = await cluster.connect() |
|
29
|
|
|
schema_definition = get_schema(app, indices) |
|
30
|
|
|
start_time = datetime.datetime.now() |
|
31
|
|
|
logger.info("Processing schema....") |
|
32
|
|
|
resp = await client.submit(schema_definition) |
|
33
|
|
|
await resp.all() |
|
34
|
|
|
logger.info("Processed schema in {}".format(datetime.datetime.now() - start_time)) |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
def get_schema(app, indices=None): |
|
38
|
|
|
if not indices: |
|
39
|
|
|
indices = [] |
|
40
|
|
|
schema_definition = """graph.tx().rollback() |
|
41
|
|
|
mgmt = graph.openManagement()\n""" |
|
42
|
|
|
for label, vertex in app.vertices.items(): |
|
43
|
|
|
schema_definition += get_vertex_schema(label, vertex) |
|
44
|
|
|
schema_definition += "// Edge schema\n" |
|
45
|
|
|
for label, edge in app.edges.items(): |
|
46
|
|
|
schema_definition += get_edge_schema(label, edge) |
|
47
|
|
|
# Need to register vertex props with app TODO Fix in Goblin |
|
48
|
|
|
|
|
49
|
|
|
schema_definition += get_indices_schema(indices) |
|
50
|
|
|
schema_definition += "mgmt.commit()" |
|
51
|
|
|
return schema_definition |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def get_vertex_schema(label, vertex): |
|
55
|
|
|
vertex_schema = "// Schema for vertex label: {}\n".format(label) |
|
56
|
|
|
vertex_schema += "{} = mgmt.makeVertexLabel('{}').make()\n".format(label, label) |
|
57
|
|
|
mapping = vertex.__mapping__ |
|
58
|
|
|
properties = vertex.__properties__ |
|
59
|
|
|
for db_name, (ogm_name, _) in mapping.db_properties.items(): |
|
60
|
|
|
prop = properties[ogm_name] |
|
61
|
|
|
|
|
62
|
|
|
# Get cardinality |
|
63
|
|
|
if hasattr(prop, 'cardinality'): |
|
64
|
|
|
card = str(prop.cardinality) |
|
65
|
|
|
else: |
|
66
|
|
|
card = 'Cardinality.single' |
|
67
|
|
|
mapped_card = CARD_MAPPING[card] |
|
68
|
|
|
|
|
69
|
|
|
# Get data type |
|
70
|
|
|
data_type = prop.data_type |
|
71
|
|
|
mapped_data_type = DATA_TYPE_MAPPING[data_type.__class__] |
|
72
|
|
|
prop_key = PropertyKey(db_name, mapped_data_type, mapped_card) |
|
73
|
|
|
if db_name in prop_keys: |
|
|
|
|
|
|
74
|
|
|
assert prop_key == prop_keys[db_name] |
|
75
|
|
|
else: |
|
76
|
|
|
prop_keys[db_name] = prop_key |
|
77
|
|
|
prop_key_string = "{} = mgmt.makePropertyKey('{}').dataType({}).cardinality({}).make()\n".format( |
|
78
|
|
|
prop_key.name, prop_key.name, prop_key.data_type, prop_key.card) |
|
79
|
|
|
vertex_schema += prop_key_string |
|
80
|
|
|
vertex_schema += "\n" |
|
81
|
|
|
return vertex_schema |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def get_indices_schema(indices): |
|
85
|
|
|
indices_schema = "// Indices ...\n" |
|
86
|
|
|
for index in indices: |
|
87
|
|
|
indices_schema += "mgmt.buildIndex('by_{}', Vertex.class).addKey({}).buildCompositeIndex()\n".format(index, index) |
|
88
|
|
|
return indices_schema |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
def get_edge_schema(label, edge): |
|
92
|
|
|
edge_schema = "{} = mgmt.makeEdgeLabel('{}').multiplicity(SIMPLE).make()\n".format(label, label) |
|
93
|
|
|
#TODO edge prop keys |
|
94
|
|
|
return edge_schema |
|
95
|
|
|
|