高级农民
- 积分
- 2783
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-12-20
- 最后登录
- 1970-1-1
|
本楼: |
👍
100% (3)
|
|
0% (0)
👎
|
全局: |
👍 89% (2686) |
|
10% (306) 👎 |
- class KVStore(object):
- def __init__(self):. 1point3acres.com
- """Initializes the key-value store."""
- # type: () -> None
- self.txid = 0
- self.actual_data = {}
- self.tx_data = defaultdict()
- def begin(self):
- """Starts a transaction, returns a txid."""
- # type: () -> int
- self.txid += 1
- return self.txid
- def read(self, txid, key):
- """Reads a value given a key, within the context of a transaction."""
- # type: (int, Text) -> Optional[int]
- # if txid in self.tx_data:
- val_list = self.tx_data.get(key)
- if not val_list:
- return self.actual_data.get(key)
- . Waral dи,
- if len(val_list) == 1:
- return val_list[0][0]
- else:. Χ
- raise Exception("value is written by multi tx")
- def write(self, txid, key, val):
- """Writes a value to a key within the context of a transaction."""
- # type: (int, Text, int) -> None
- # if key in self.actual_data:
- # del self.actual_data[key]
- if key in self.tx_data:
- if txid != self.tx_data[key][0][1]:.google и
- raise Exception("value is written by another txid")
- Else:. 1point 3 acres
- self.tx_data[key] = list() ..
- self.tx_data[key].append((val, txid))
- def commit(self, txid):
- """Commits writes to the store. The transaction may no longer be used."""-baidu 1point3acres
- # type: (int) -> None
- for key, val_list in self.tx_data.items():
- # if not conflict
- for val, version in val_list:
- if version == txid:
- self.actual_data[key] = val. 1point 3acres
- val_list.remove((val, version))
复制代码 |
|