def write(self): if self.isloaded: self.count = len(self.keys) leafjson = {} leafjson["version"] = 1 if len(self) > 0: if self.key_class in [int, dict, list, str, unicode]: leafjson["keys"] = self.keys else: leafjson["keys"] = [key.to_JSON() for key in self.keys] if self.value_class in [int, dict, list, str, unicode]: leafjson["values"] = self.values else: leafjson["values"] = [value.to_JSON() for value in self.values] else: leafjson["keys"] = [] leafjson["values"] = [] s = json.dumps(leafjson, indent=3) self.hash = sstr(hashlib.md5(s).hexdigest()) f = open(self.filename, 'w') f.write(s + "\n") f.close() self.leaffactory.page_changed(self, None)
def ensure_load(self): if not(self.isloaded): if os.path.isfile(self.filename): leafjson = json.loads(open(self.filename).read()) if leafjson["version"] != 1: raise IOException("Version of B+ tree page file " + sstr(self.filename) + " is not compatible.") if self.key_class in [int, dict, list, str, unicode]: self.keys = leafjson["keys"] else: self.keys = [self.key_class.from_JSON(keyjson) for keyjson in leafjson["keys"]] if self.value_class in [int, dict, list, str, unicode]: self.values = leafjson["values"] else: self.values = [self.value_class.from_JSON(value_json) for value_json in leafjson["values"]] self.isloaded = True self.count = len(self.keys) else: self.isloaded = True self.count = 0
def ensure_load(self): if not(self.isloaded): if os.path.isfile(self.filename): with open(self.filename) as f: leafjson = json.loads(f.read()) if leafjson["version"] != 1: raise IOException("Version of B+ tree page file " + sstr(self.filename) + " is not compatible.") if self.key_class in [int, dict, list, str, str]: self.keys = leafjson["keys"] else: self.keys = [self.key_class.from_JSON(keyjson) for keyjson in leafjson["keys"]] if self.value_class in [int, dict, list, str, str]: self.values = leafjson["values"] else: self.values = [self.value_class.from_JSON(value_json) for value_json in leafjson["values"]] self.isloaded = True self.count = len(self.keys) else: self.isloaded = True self.count = 0
def write(self): if self.isloaded: self.count = len(self.keys) leafjson = {} leafjson["version"] = 1 if len(self) > 0: if self.key_class in [int, dict, list, str, str]: leafjson["keys"] = self.keys else: leafjson["keys"] = [key.to_JSON() for key in self.keys] if self.value_class in [int, dict, list, str, str]: leafjson["values"] = self.values else: leafjson["values"] = [value.to_JSON() for value in self.values] else: leafjson["keys"] = [] leafjson["values"] = [] s = json.dumps(leafjson, indent=3) self.hash = sstr(hashlib.md5(s.encode('utf-8')).hexdigest()) with open(self.filename, 'w') as f: f.write(s + "\n") self.leaffactory.page_changed(self, None)