Skip to content

Instantly share code, notes, and snippets.

@kelvingakuo
Created November 26, 2021 19:35
Show Gist options
  • Save kelvingakuo/bbdb40d4ebf44d3961f092c17bb20526 to your computer and use it in GitHub Desktop.
Save kelvingakuo/bbdb40d4ebf44d3961f092c17bb20526 to your computer and use it in GitHub Desktop.
def lookup_using_hash_index(self, value: str) -> list:
""" Return all the rows where the indexed column contains a value
Params:
value (str) - The condition
Returns:
rows (list) - The matching rows from our table
"""
# Re-structure the table into a searchable dict
self.redone_table = {row["TID"] : row for row in self.table}
rows = []
hsh, bucket = self.hash_function(value)
items = self.hash_table.get(bucket, None)
if(items is None):
# No match
pass
else:
if(len(items) == 1):
# We have just one TID
rows.append(self.redone_table[items[0]["row_tid"]])
else:
# Multiple TIDs in one slot
relevant_tids = [item["row_tid"] for item in items if item["hash_code"] == hsh]
# Retrieve each relevant row then re-check condition
for relevant in relevant_tids:
rr = self.redone_table[relevant]
if(rr["col_a"] == value):
rows.append(rr)
return rows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment