Improve buffer efficiency

master
Ringo Wantanabe 6 years ago
parent f6a8da49c3
commit 1a9c4a5698
No known key found for this signature in database
GPG Key ID: C1C1CD34CF2907B2

@ -0,0 +1 @@
pylzma

@ -1,3 +1,4 @@
import sys import sys
import os import os
import os.path import os.path
@ -11,7 +12,7 @@ import datetime
import json import json
import re import re
import time import time
import threading from threading import Thread, Lock
import binascii import binascii
import pylzma import pylzma
from cffi import FFI from cffi import FFI
@ -76,8 +77,14 @@ class StatBuffer:
SLV_NOUI = 3 #Remove all user inputed information (sub, com,name,trip, file info) SLV_NOUI = 3 #Remove all user inputed information (sub, com,name,trip, file info)
SLV_HIGH = 0xFF #Keep only post number SLV_HIGH = 0xFF #Keep only post number
def __init__(self): def __init__(self):
self._mutex = Lock()
pass pass
def _lock(self):
self._mutex.acquire()
def _unlock(self):
self._mutex.release()
def _decode(self, data): def _decode(self, data):
if(isinstance(data, int)): if(isinstance(data, int)):
return data return data
@ -128,75 +135,132 @@ class StatBuffer:
class MemoryBuffer(StatBuffer): class MemoryBuffer(StatBuffer):
def __init__(self, level): def __init__(self, level):
super().__init__()
self.lvl = level self.lvl = level
self.store = list() self.store = list()
def write(self, post): def write(self, post):
super()._lock()
data = super()._encode(post, self.lvl) data = super()._encode(post, self.lvl)
self.store.append(data) self.store.append(data)
super()._unlock()
def raw(self): def raw(self):
return json.dumps(self.store) super()._lock()
try:
return json.dumps(self.store)
finally:
super()._unlock()
def clear(self): def clear(self):
super()._lock()
self.store = list() self.store = list()
super()._unlock()
def length(self): def length(self):
return len(self.store) super()._lock()
try:
return len(self.store)
finally:
super()._unlock()
def read(self): def read(self):
base = super() super()._lock()
return list(base._decode(d) for d in self.store) try:
base = super()
return list(base._decode(d) for d in self.store)
finally:
super()._unlock()
def findMax(self): def findMax(self):
mx=0 super()._lock()
for ent in self.store: try:
post = super()._decode(ent) if len(self.store)<1: return 0
if(post["no"]>mx): mx = post["no"] return super()._decode(self.store[-1])["no"]
return mx finally:
super()._unlock()
def readno(self, floor):
super()._lock()
posts = list()
nl = len(self.store)-1
while(nl>=0):
entry = super()._decode(self.store[nl])
if(entry["no"]<=floor): break
posts.append(ent)
nl-=1
super()._unlock()
return posts
class FileBuffer(StatBuffer): class FileBuffer(StatBuffer):
def __init__(self, fn, level): def __init__(self, fn, level):
super().__init__()
self.lvl = level self.lvl = level
self.file = open(fn, "a+b") self.file = open(fn, "a+b")
def write(self, post): def write(self, post):
super()._lock()
data = super()._encode(post, self.lvl) data = super()._encode(post, self.lvl)
self.file.write(struct.pack("I", len(data)))
self.file.write(data) self.file.write(data)
self.file.write(struct.pack("I", len(data)))
super()._unlock()
def _readentry(self):
self.file.seek(-4,1)
lentr = self.file.read(4)
if len(lentr)<4: return None
tl = struct.unpack("I", lentr)[0]
self.file.seek(-(tl+4), 1)
ret = super()._decode(self.file.read(tl))
self.file.seek(-tl,1)
return ret
def _skipentry(self):
self.file.seek(-4,1)
lentr = self.file.read(4)
if len(lentr)<4: return False
tl = struct.unpack("I", lentr)[0]
self.file.seek(-(tl+4),1)
return True
def read(self): def read(self):
self.file.seek(0) super()._lock()
posts = list() posts = list()
lentr=self.file.read(4) ent = self._skipentry()
while( lentr != None): while self.file.tell()>0 and ent!=None:
if(len(lentr)<4): break posts.append(ent)
tl = struct.unpack("I", lentr)[0] ent = self._readentry()
#pprint.pprint(tl) self.file.seek(0,2)
posts.append(super()._decode(self.file.read(tl))) super()._unlock()
lentr=self.file.read(4)
self.file.seek(0, 2)
return posts return posts
def length(self): def length(self):
self.file.seek(0) super()._lock()
maxln = 0 ent = self._skipentry()
lentr=self.file.read(4) maxl =0
while( lentr != None): while self.file.tell()>0 and ent:
if(len(lentr)<4): break maxl+=1
tl = struct.unpack("I", lentr)[0] ent = self._skipentry()
self.file.seek(tl, 1) self.file.seek(0,2)
maxln+=1 super()._unlock()
lentr=self.file.read(4)#wonderful language lmao return maxl
self.file.seek(0, 2)
return maxln
def close(self): def close(self):
super()._lock()
self.file.close() self.file.close()
super()._unlock()
def clear(self): def clear(self):
super()._lock()
self.file.truncate() self.file.truncate()
super()._unlock()
def findMax(self): def findMax(self):
self.file.seek(0) super()._lock()
mx=0 try:
lentr=self.file.read(4) if(self.file.tell()<1): return 0
while( lentr != None): sk = self._readentry()
if(len(lentr)<4): break self.file.seek(0,2)
tl = struct.unpack("I", lentr)[0] if sk!=None: return sk["no"]
post = super()._decode(self.file.read(tl)) else: return 0
if(post["no"] > mx): mx = post["no"] finally:
lentr=self.file.read(4)#wonderful language lmao super()._unlock()
self.file.seek(0, 2) def readno(self, floor):
return mx super()._lock()
posts = list()
ent = self._skipentry()
while self.file.tell()>0 and ent!=None:
if(ent["no"]<=floor): break
posts.append(ent)
ent = self._readentry()
self.file.seek(0,2)
super()._unlock()
return posts
def parse_post(post): def parse_post(post):
res = dict() res = dict()

Binary file not shown.
Loading…
Cancel
Save