Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. # leveldb json store
  2.  
  3. import leveldb
  4. import struct
  5.  
  6. def i2s(i):
  7. res = ''
  8. while i > 255:
  9. res += chr(i % 256)
  10. i = i >> 8
  11. res += chr(i)
  12. return res
  13.  
  14. def s2i(s):
  15. res = 0
  16. for e, c in enumerate(s):
  17. res += ord(c) << e * 8
  18. return res
  19.  
  20. def f2s(f):
  21. return struct.pack('d', f)
  22.  
  23. def s2f(s):
  24. return struct.unpack('d',s)[0]
  25.  
  26.  
  27.  
  28.  
  29. class LJS(object):
  30.  
  31. _NONE = chr(1)
  32. _TRUE = chr(2)
  33. _FALSE = chr(3)
  34. _LIST = chr(4)
  35. _DICT = chr(5)
  36. _STR = chr(6)
  37. _INT = chr(7)
  38. _DBL = chr(8)
  39.  
  40. _TDIC = {
  41. _NONE : None,
  42. _FALSE : False,
  43. _TRUE : True,
  44. _LIST : [],
  45. _DICT : {},
  46. _STR : '',
  47. _INT : 0,
  48. _DBL : 0.0
  49. }
  50.  
  51.  
  52. def __init__(self, dbpath, root):
  53. self.db = leveldb.LevelDB(dbpath)
  54. self.root = root
  55. self.cursor = root
  56.  
  57. def check(self, v):
  58. vtype = type(v)
  59. if vtype in (type(None), bool, unicode, str, int, float):
  60. return True
  61. elif vtype in (list, tuple):
  62. for e in v:
  63. if not self.check(e):
  64. return False
  65. return True
  66. elif vtype == dict:
  67. for k in v:
  68. if not type(k) == str:
  69. return False
  70. if not self.check(v[k]):
  71. return False
  72. return True
  73. return False
  74.  
  75.  
  76. def delete(self, K):
  77. keys = [k for k, _ in self.db.RangeIter(key_from = K, key_to = K + chr(47) )]
  78. batch = leveldb.WriteBatch()
  79. for key in keys:
  80. batch.Delete(key)
  81. self.db.Write(batch, sync = True)
  82.  
  83.  
  84.  
  85.  
  86. def _write(self, K, v):
  87. self.delete(K)
  88.  
  89. vtype = type(v)
  90. if vtype == type(None):
  91. self.db.Put(K, LJS._NONE)
  92. elif vtype == bool:
  93. self.db.Put(K, LJS._TRUE) if v else self.db.Put(K, LJS._FALSE)
  94. elif vtype == unicode:
  95. self.db.Put(K, LJS._STR + v.encode('utf-8'))
  96. elif vtype == str:
  97. self.db.Put(K, LJS._STR + v)
  98. elif vtype == int:
  99. self.db.Put(K, LJS._INT + i2s(v))
  100. elif vtype == float:
  101. self.db.Put(K, LJS._DBL + f2s(v))
  102. elif vtype in (list, tuple):
  103. self.db.Put(K, LJS._LIST)
  104. for e, x in enumerate(v):
  105. nK = K + '.' + '%08d'%e
  106. self._write(nK, x)
  107. elif vtype == dict:
  108. self.db.Put(K, LJS._DICT)
  109. for k in v:
  110. nK = K + '.' + k
  111. self._write(nK, v[k])
  112.  
  113.  
  114.  
  115. def write(self, k, v):
  116. assert(self.check(v))
  117. assert(type(k)==str)
  118. K = '.'.join([self.cursor, k]) if k else self.cursor
  119. self._write(K, v)
  120.  
  121.  
  122. def _rread(self, K):
  123.  
  124. Kcomps = K.split('.')
  125. cd = len(Kcomps)
  126. print Kcomps
  127. tstack = [5]
  128.  
  129. res = {}
  130. collptrstack = [res]
  131.  
  132. for _k, v in self.db.RangeIter(key_from = K, key_to = K + chr(47) ):
  133. kcomps = _k.split('.')
  134. print kcomps
  135.  
  136. #print res
  137. kd = len(kcomps)
  138. while kd < cd + len(tstack) - 1 :
  139. if chr(tstack.pop()) in (LJS._LIST, LJS._DICT):
  140. collptrstack.pop()
  141.  
  142. tbit = v[0]
  143. supertype = tstack[-1] # must be collection
  144. tstack.append(ord(tbit))
  145. thistype = tstack[-1]
  146.  
  147. #print supertype, thistype, tbit, tbit == LJS._DICT
  148.  
  149. if tbit == LJS._STR: arg = v[1:]
  150. elif tbit == LJS._STR: arg = v[1:]
  151. elif tbit == LJS._INT: arg = s2i(v[1:])
  152. elif tbit == LJS._DBL: arg = s2f(v[1:])
  153. elif tbit == LJS._LIST: arg = []
  154. elif tbit == LJS._DICT: arg = {}
  155. elif tbit == LJS._NONE: arg = None
  156. elif tbit == LJS._TRUE: arg = True
  157. elif tbit == LJS._FALSE: arg = False
  158.  
  159. #print arg
  160.  
  161. if supertype == ord(LJS._LIST):
  162. #print collptrstack, arg
  163. collptrstack[-1].append(arg)
  164. if thistype in (ord(LJS._LIST), ord(LJS._DICT)):
  165. collptrstack.append(collptrstack[-1][len(collptrstack[-1])-1])
  166. elif supertype == ord(LJS._DICT):
  167. #print collptrstack, arg
  168. collptrstack[-1][kcomps[-1]] = arg
  169. if thistype in (ord(LJS._LIST), ord(LJS._DICT)):
  170. collptrstack.append(collptrstack[-1][kcomps[-1]])
  171. else:
  172. print "ERRRRROR"
  173.  
  174. return res[Kcomps[-1]]
  175.  
  176.  
  177. def read(self, k = None):
  178. assert(type(k)==str)
  179. if k == None:
  180. K = self.cursor
  181. else:
  182. K = '.'.join([self.cursor, k])
  183. return self._rread(K)
  184.  
  185.  
  186.  
  187.  
  188. def set_cursor(self, k):
  189. self.cursor = '.'.join([self.root, k])
  190.  
  191.  
  192. if __name__ == '__main__':
  193. store = LJS('test', 'ROOT')
  194. smpl = {'none':None, 'int':123, 'true':True, 'false':False, 'float':123.123, 'string':'abc', 'list':[None,True,False,[],{},'2',1,1.1], 'dict':{'a':1,'b':2}, 'emptydic':{}, 'emptylist':[]}
  195. store.write('jsom', smpl)
  196. store.write('json', smpl)
  197. store.write('jsona', smpl)
  198. store.write('json2', smpl)
  199.  
  200. store.write('json.false',[1,2,3.333])
  201. store.write('json.falsee',[4,5,6])
  202. store.write('json.falseee',[77777, None])
  203.  
  204. rsmpl = store.read('json')
  205. print smpl
  206. print rsmpl
  207. print smpl == rsmpl
  208.  
  209. #for k,v in store.db.RangeIter():
  210. # print k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement