Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. Q1 -
  2.  
  3.  
  4. class HashTable:
  5.     def __init__(self, rawEvents):
  6.         """ Build a HashTable from a list of raw pipe-delimited DML Events """
  7.         self.timestamp = datetime.datetime.fromtimestamp(0)
  8.         self.d = {}
  9.         for e in rawEvents:
  10.             lis = e.split("|")
  11.            
  12.             self.timestamp = datetime.datetime.fromtimestamp(int(lis[0])/ 1000.0)  
  13.            
  14.            
  15.             key = lis[2]
  16.             if lis[1] == "INSERT":
  17.                
  18.                 if key in self.d:
  19.                     continue
  20.                 else:
  21.                     self.d[key] = lis[3]
  22.                    
  23.             elif lis[1] == "UPSERT":
  24.                 self.d[key] = lis[3]
  25.            
  26.             else:
  27.                 if key in self.d:
  28.                     del self.d[key]
  29.                    
  30.     @property
  31.     def table(self):
  32.         """ Retrieve the current state of the HashTable
  33.  
  34.        Returns
  35.        -------
  36.        dict(str, str): the key-value pairs
  37.        """
  38.         return self.d
  39.  
  40.     @property
  41.     def high_watermark(self):
  42.         """ Retrieve the high-watermark of the system  -- the UTC epoch millisecond timestamp of the latest
  43.        event read as a datetime or datetime.datetime.utcfromtimestamp(0) (Epoch 0) if no event occurred
  44.  
  45.        Returns
  46.        -------
  47.        datetime.datetime: the high-watermark
  48.        """
  49.        
  50.         return self.timestamp
  51.  
  52. Q2 -
  53.  
  54. # Add any imports you need here
  55.  
  56. class WAL:
  57.     def __init__(self, binary_wal):
  58.        
  59.         self.ans = []
  60.         offset = 0
  61.         while offset<len(binary_wal):
  62.             num = int.from_bytes(binary_wal[offset:offset+8],byteorder = 'big', signed = 'False')
  63.             mbit = binary_wal[offset + 8]
  64.             msg = ["INSERT", "UPSERT", "DELETE"][mbit]
  65.             key_len = int.from_bytes(binary_wal[offset+9:offset+11],byteorder = 'big', signed = 'False')
  66.             key_str = binary_wal[offset+11:offset+key_len + 11].decode()
  67.             if mbit == 2:
  68.                 self.ans.append("{}|{}|{}".format(num, msg, key_str))
  69.                 offset+=key_len+11
  70.             else:
  71.                 val_len = int.from_bytes(binary_wal[offset+key_len + 11: offset+key_len + 13], byteorder = 'big', signed = 'False')
  72.                 val_str = binary_wal[offset+key_len + 13: offset+ key_len + 13 + val_len].decode()
  73.                 self.ans.append("{}|{}|{}|{}".format(num, msg, key_str, val_str))
  74.                 offset+=key_len+13+val_len
  75.    
  76.     def get_events(self):
  77.         """ Retrieve all events contained within the WAL as their string values in time order
  78.        DML Event String Format: "<epoch_milli>|<message_name>|<key>|<value>"
  79.  
  80.        Returns
  81.        -------
  82.        list(str): a time-ordered list of DML Event strings
  83.        """
  84.         return self.ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement