-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.py
72 lines (58 loc) · 2.63 KB
/
objects.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Objects:
def __init__(self, entity_type, entity_id):
valid_entities = ["Database", "Tablespace", "Table", "Page", "Row"]
if not entity_type.isnumeric():
entity_type = valid_entities.index(entity_type)
self.id = entity_id
self.type = valid_entities[entity_type]
self.entity_type = entity_type
self.ancestors = {entity: [] for entity in valid_entities}
self.locks = []
self.version = 0
@staticmethod
def create_schema(num_rows: int, num_pages: int, num_tables: int, num_tablespaces: int):
database = Objects("Database", "DB")
schema = {database.id: database}
for i in range(num_tablespaces):
tablespace = Objects("Tablespace", f"TS{i + 1}")
Objects._link_ancestors(database, tablespace)
schema[tablespace.id] = tablespace
table_counter = 1
for tablespace in database.ancestors["Tablespace"]:
for i in range(num_tables):
table = Objects("Table", f"TB{table_counter}")
table_counter += 1
Objects._link_ancestors(tablespace, table)
schema[table.id] = table
page_counter = 1
for tablespace in database.ancestors["Tablespace"]:
for table in tablespace.ancestors["Table"]:
for i in range(num_pages):
page = Objects("Page", f"PG{page_counter}")
page_counter += 1
Objects._link_ancestors(table, page)
schema[page.id] = page
row_counter = 1
for tablespace in database.ancestors["Tablespace"]:
for table in tablespace.ancestors["Table"]:
for page in table.ancestors["Page"]:
for i in range(num_rows):
row = Objects("Row", f"RW{row_counter}")
row_counter += 1
Objects._link_ancestors(page, row)
schema[row.id] = row
return schema
@staticmethod
def _link_ancestors(parent, child):
entity_hierarchy = ["Database", "Tablespace", "Table", "Page", "Row"]
parent.ancestors[child.type].append(child)
child.ancestors[parent.type].append(parent)
if parent.entity_type > 0:
previous_entity = parent.ancestors[entity_hierarchy[parent.entity_type - 1]][0]
Objects._link_ancestors(previous_entity, child)
def update_version(self):
self.version += 1
def get_id(self):
return self.id
def __repr__(self):
return f"Object ID: {self.id} with Version {self.version}.0"