Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q1 -
- class HashTable:
- def __init__(self, rawEvents):
- """ Build a HashTable from a list of raw pipe-delimited DML Events """
- self.timestamp = datetime.datetime.fromtimestamp(0)
- self.d = {}
- for e in rawEvents:
- lis = e.split("|")
- self.timestamp = datetime.datetime.fromtimestamp(int(lis[0])/ 1000.0)
- key = lis[2]
- if lis[1] == "INSERT":
- if key in self.d:
- continue
- else:
- self.d[key] = lis[3]
- elif lis[1] == "UPSERT":
- self.d[key] = lis[3]
- else:
- if key in self.d:
- del self.d[key]
- @property
- def table(self):
- """ Retrieve the current state of the HashTable
- Returns
- -------
- dict(str, str): the key-value pairs
- """
- return self.d
- @property
- def high_watermark(self):
- """ Retrieve the high-watermark of the system -- the UTC epoch millisecond timestamp of the latest
- event read as a datetime or datetime.datetime.utcfromtimestamp(0) (Epoch 0) if no event occurred
- Returns
- -------
- datetime.datetime: the high-watermark
- """
- return self.timestamp
- Q2 -
- # Add any imports you need here
- class WAL:
- def __init__(self, binary_wal):
- self.ans = []
- offset = 0
- while offset<len(binary_wal):
- num = int.from_bytes(binary_wal[offset:offset+8],byteorder = 'big', signed = 'False')
- mbit = binary_wal[offset + 8]
- msg = ["INSERT", "UPSERT", "DELETE"][mbit]
- key_len = int.from_bytes(binary_wal[offset+9:offset+11],byteorder = 'big', signed = 'False')
- key_str = binary_wal[offset+11:offset+key_len + 11].decode()
- if mbit == 2:
- self.ans.append("{}|{}|{}".format(num, msg, key_str))
- offset+=key_len+11
- else:
- val_len = int.from_bytes(binary_wal[offset+key_len + 11: offset+key_len + 13], byteorder = 'big', signed = 'False')
- val_str = binary_wal[offset+key_len + 13: offset+ key_len + 13 + val_len].decode()
- self.ans.append("{}|{}|{}|{}".format(num, msg, key_str, val_str))
- offset+=key_len+13+val_len
- def get_events(self):
- """ Retrieve all events contained within the WAL as their string values in time order
- DML Event String Format: "<epoch_milli>|<message_name>|<key>|<value>"
- Returns
- -------
- list(str): a time-ordered list of DML Event strings
- """
- return self.ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement