Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Migration_Project/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.formatting.provider": "autopep8"
}
40 changes: 40 additions & 0 deletions Migration_Project/Rough.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# # Entertainment_Booking_System
# event_details/event_type/manager
# event_booked
# customer/event_booked



# # ecommerce
# customer/orders
# orders/lineitem
# part/supplier
# supplier/part



# # Exam Question and Hardcoded values
# users/cart
# users/orders
# orders/lineitem
# products

tables=["users","orders","lineitem","shoppingCart","products","added"]
tNo={"users":0,"orders":1,"lineitem":2,"shoppingCart":3,"products":4,"added":5}
adj={"lineitem":["orders"],
"orders":["users"],
"users":["shoppingCart"],
"shoppingCart":[],
"added":["shoppingCart","products"],
"products":[]
}
relations={
"shoppingCart":["users","added"],
"users":["orders"],
"orders":["lineitem"],
"products":["added"],
"lineitem":[],
"added":[]
}

Binary file not shown.
Binary file added Migration_Project/__pycache__/algo.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Migration_Project/__pycache__/home.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions Migration_Project/accessPaths_BT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Admin
Player/Contact_Information
Contact_Information/Player
Trainer/Grades
Salary_Structure/Trainer
Team
4 changes: 4 additions & 0 deletions Migration_Project/add.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
users/cart
users/orders
orders/lineitem
products
168 changes: 168 additions & 0 deletions Migration_Project/algo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import psycopg2
import psycopg2.extras
import pymongo
import ssl
from bson.json_util import dumps, loads
import datetime
import json
from queue import Queue



# function to get order of embedding
def getOrder(tables, work) :
n = len(work)
adj = []
for i in range(n) :
adj.append([])
indeg = [0]*n
vis = {}
relax = []
zeroqueue = Queue(maxsize = n+1)
for i in range(n) :
for j in range(n) :
if work[i][j] == 'e' and work[j][i] == "e" :
work[i][j] = '-'
work[j][i] = '-'
# print("embed " + tables[i] + " in " + tables[j] + " simultaneously")
relax.append([i, j, 1])
for i in range(n) :
for j in range(n) :
if(work[i][j] == 'e') :
indeg[i] = indeg[i] + 1
adj[j].append(i)
for t in range(n) :
if(indeg[t] == 0) :
zeroqueue.put(t)

while zeroqueue.qsize() > 0 :
t = zeroqueue.get()
for c in adj[t] :
indeg[c] = indeg[c] - 1
if(indeg[t] == 0) :
zeroqueue.put(c)
relax.append([c, t])
for pair in relax :
print("embed " + tables[pair[1]] + " in " + tables[pair[0]])
return relax



# dfs on access paths
def dfs1(tables,tNo,adj1,work1,currCollec,vis1,curTable):
vis1.add(curTable)
for nxtTable in adj1[curTable]:
if(nxtTable not in vis1):
dfs1(tables,tNo,adj1,work1,currCollec,vis1,nxtTable)
if(nxtTable in currCollec):
work1[tNo[curTable]][tNo[nxtTable]]="l"
else:
work1[tNo[curTable]][tNo[nxtTable]]="e"



# dfs on adjacency list of Relational Schema
def dfs2(tables,tNo,adj2,work2,currCollec,vis2,relations,curTable):
vis2.add(curTable)
if(len(relations[curTable])==0): # not referred - noone is refering curTable
if(len(adj2[curTable])==0): # has 0 FK
currCollec.add(curTable)
elif(len(adj2[curTable])==1): # has 1 FK
nxtTable=adj2[curTable][0]
if(curTable in currCollec):
work2[tNo[curTable]][tNo[nxtTable]]="l"
else:
work2[tNo[nxtTable]][tNo[curTable]]="e"
dfs2(tables,tNo,adj2,work2,currCollec,vis2,relations,nxtTable)
else: # has more than 1 FKs
for nxtTable in adj2[curTable]:
if(nxtTable not in vis2):
dfs2(tables,tNo,adj2,work2,currCollec,vis2,relations,nxtTable)
if(nxtTable in currCollec):
work2[tNo[curTable]][tNo[nxtTable]]="l"
else:
work2[tNo[curTable]][tNo[nxtTable]]="e"
else: # referred
for nxtTable in adj2[curTable]:
if(nxtTable not in vis2):
dfs2(tables,tNo,adj2,work2,currCollec,vis2,relations,nxtTable)
if(nxtTable in currCollec):
work2[tNo[curTable]][tNo[nxtTable]]="l"
else:
work2[tNo[curTable]][tNo[nxtTable]]="e"
return



# function for main algorithm
def algo(tables,tNo,relations,paths):
print(".....Running Algo.....")

nTables=len(tables)
currCollec = {"dummy"}

#--------------------------------------------------- Part 1 ----------------------------------------------------
vis1 = {"dummy"}
work1= [["-"]*nTables for _ in range(nTables)]

# creating potential collections
for p in paths:
currCollec.add(p[0])
currCollec.remove("dummy")
vis1.remove("dummy")

# creates adjacency from access paths
adj1={}
for t in tables:
adj1[t]=[]

for r in paths:
for i in range(len(r)-1):
adj1[r[i]].append(r[i+1])

# print(adj1)

for t in currCollec:
dfs1(tables,tNo,adj1,work1,currCollec,vis1,t)

# for x in work1:
# print(x)

# ------------------------------------------------ Part 2 -------------------------------------------------------
vis2 = {"dummy"}
work2= [["-"]*nTables for _ in range(nTables)]

# creates adjacency from schema
adj2={}
for t in tables:
adj2[t]=[]

# print(adj2)

for r in relations:
for r1 in relations[r]:
adj2[r1[0]].append(r)

for t in tables:
dfs2(tables,tNo,adj2,work2,currCollec,vis2,relations,t)

# for x in work2:
# print(x)

work = work1
for i in range(nTables):
for j in range(nTables):
if(work1[i][j]=='-'):
work[i][j]=work2[i][j]
else:
work[i][j]=work1[i][j]

# for x in work:
# print(x)

relax = getOrder(tables, work)

print("------Algo Ends------")
return [currCollec,relax, work, adj2]


35 changes: 35 additions & 0 deletions Migration_Project/connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import psycopg2
import psycopg2.extras
import pymongo
import ssl
from bson.json_util import dumps, loads
import datetime
import json

class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'

OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

def connectDB(dbName,mongodb_host):
print(f"{bcolors.HEADER}Initializing database connections...{bcolors.ENDC}")

#Postgres connection
print(f"{bcolors.HEADER}Connecting to PostgreSQL server...{bcolors.ENDC}")
pgsqldb = psycopg2.connect(database=dbName,user="postgres",password="admin")
cursor = pgsqldb.cursor()
print(f"{bcolors.HEADER}Connection to Postgres db succeeded.{bcolors.ENDC}")

#MongoDB connection
print(f"{bcolors.HEADER}Connecting to MongoDB server...{bcolors.ENDC}")
myClient = pymongo.MongoClient(mongodb_host,tls=True, tlsAllowInvalidCertificates=True)
print(f"{bcolors.HEADER}Connection to MongoDB Server succeeded.{bcolors.ENDC}")
print(f"{bcolors.HEADER}Database connections initialized successfully.{bcolors.ENDC}")
return [myClient,cursor]
Loading