Skip to content

Commit

Permalink
debugging routines
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Moore committed Feb 5, 2022
1 parent a463eee commit cd801e6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
5 changes: 1 addition & 4 deletions innoldb/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ def getLogger(name: str) -> logging.Logger:
logger = logging.getLogger(name)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
logger.setLevel(logging.DEBUG)
logger.setLevel(logging.INFO)
logger.addHandler(consoleHandler)

logger.log_dict = lambda input_dict: [ logger.info('%s : %s', key, value) for key, value in input_dict.items() ]

return logger
30 changes: 21 additions & 9 deletions innoldb/qldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def update(driver, document, table, index):
saved_value = saved_document.get(key, None)
log.debug('Comparing saved value: %s \n\t\t\t\t\t\t\t to buffer value: %s', saved_value, buffer_value)
if saved_value != buffer_value:
log.debug('Not equal')
update_statement = 'UPDATE {} SET {} = ? WHERE {} = ?'.format(table, key, index)
results += driver.execute_lambda(lambda executor: Driver.execute(
executor, update_statement, buffer_value, lookup
Expand Down Expand Up @@ -160,45 +159,58 @@ def _init_fixtures(self):
try:
Driver.create_table(self.driver, self.table)
except Exception as e:
log.debug(e)
log.warn(e)
try:
Driver.create_index(self.driver, self.table, self.index)
except Exception as e:
log.debug(e)
log.warn(e)

def _insert(self, document):
log.debug("Inserting DOCUMENT(%s = %s)", self.index, document[self.index])
log.info("Inserting DOCUMENT(%s = %s)", self.index, document[self.index])
return Driver.insert(self.driver, document, self.table)

def _update(self, document):
log.debug("Updating DOCUMENT(%s = %s)", self.index, document[self.index])
log.info("Updating DOCUMENT(%s = %s)", self.index, document[self.index])
return Driver.update(self.driver, document, self.table, self.index)

def exists(self, id):
log.debug("Checking existence of DOCUMENT(%s = %s)", self.index, id)
log.info("Checking existence of DOCUMENT(%s = %s)", self.index, id)
result = Driver.query_by_field(self.driver, self.index, id, self.table)
# for row in result:
# log.debug("Query returned with row %s", row)
if next(result, None):
return True
return False

def get(self, id):
log.info("Returning DOCUMENT(%s = %s)", self.index, id)
return loads(dumps(next(Driver.query_by_field(self.driver, self.index, id, self.table))))

def save(self, document):
log.debug("Saving Document(%s = %s)", self.index, document[self.index])
log.info("Saving DOCUMENT(%s = %s)", self.index, document[self.index])
if self.exists(document[self.index]):
return self._update(document)
return self._insert(document)


class Document(Table):
def __init__(self, name, id = str(uuid.uuid1()), ledger=settings.LEDGER):
def __init__(self, name, id = None, ledger=settings.LEDGER):
super().__init__(table=name, ledger=ledger)
if id is None:
id = str(uuid.uuid1())
else:
self._load(id)
self.id = id

def _load(self, id):
snapshot = super().get(id)
for key, value in snapshot.items():
setattr(self, key, value)

def fields(self):
return {key: value for key, value in vars(self).items() if key not in ['table', 'driver', 'index']}

def save(self):
result = super().save(self.fields())
for row in result:
log.debug('Transaction result: \n\t\t\t\t\t\t\t %s', loads(dumps(row)))
log.info('Transaction result: \n\t\t\t\t\t\t\t %s', loads(dumps(row)))
28 changes: 20 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

log = getLogger('main')


departments = ['Business Development', 'Research and Development', 'Innovation and Technology']
locations = ['Virgina', 'Maryland', 'Florida', 'Minnesota', 'West Virginia']
teams = ['Innovation', 'InnoLab', 'Innovation Lab', 'Inno Lab', 'Laboratory']
Expand All @@ -14,14 +13,19 @@
{'Developer #1': 'Thomas'}, {'Developer #2': 'Aurora'}, {'DevSecOps': 'Grant'},
{'Scrum': 'Selah'}, { 'Architect': 'Tariq' }]

def create(document):
def create():
document = Document('innolab')
document.company = 'Makpar'
document.department = departments[random.randint(0, len(departments) - 1)]
document.location = locations[random.randint(0, len(locations) - 1)]
document.team = teams[random.randint(0, len(teams) - 1)]
document.specialty = specialities[random.randint(0, len(specialities) - 1)]
document.members = members[:random.randint(0, len(members) - 1)]
document.save()
return document

def load(id):
return Document(name='innolab', id=id)

def update_speciality(document):
document.specialty = specialities[random.randint(0, len(specialities) - 1)]
Expand All @@ -32,22 +36,30 @@ def update_prop(document, key, value):
document.save()

if __name__=="__main__":
document = Document('innolab')
routines = ['create']
if len(sys.argv) > 1:
routines += sys.argv[1:]
routines = sys.argv[1:]

if routines[0] == 'load':
document = load('0434ade7-86bb-11ec-935a-34735aabbbc4')
log.info("Loaded DOCUMENT: \n\t\t\t\t\t\t\t %s", document.fields())

else:
document = create()
log.info("Created DOCUMENT: \n\t\t\t\t\t\t\t %s", document.fields())

for routine in routines:
if routine == 'create':
create(document)
elif routine == 'update_spec':
if routine == 'update_spec':
update_speciality(document)
elif routine == 'update_prop_1':
update_prop(document, 'favorite_movie', 'children of men')
elif routine == 'update_prop_2':
update_prop(document, 'favorite_movie', 'pulp fiction')
elif routine == 'update_prop_3':
update_prop(document, 'favorite_movie', 'lords of salem')
elif routine == 'update_prop_4':
update_prop(document, 'favorite_movie', 'the big lebowski')
elif routine == 'update_prop_5':
update_prop(document, 'favorite_movie', 'the evil dead')

# if __name__==:""
# from qldb.qldb import Driver
Expand Down

0 comments on commit cd801e6

Please sign in to comment.