# HG changeset patch # User Henry S. Thompson # Date 1689015138 -3600 # Node ID f8c8f79b253262991c2a1e5b0cb12181fc383328 # Parent 11a886a84a4951cfa7f2876734b8c0d79a41c66c rework completely to refill as much as possible only when necessary, basic loop working again, but not refill diff -r 11a886a84a49 -r f8c8f79b2532 bin/warc.py --- a/bin/warc.py Mon Jul 10 18:17:35 2023 +0100 +++ b/bin/warc.py Mon Jul 10 19:52:18 2023 +0100 @@ -16,23 +16,28 @@ bufSize=2*1024*1024 hdrMax=16*1024 buf=bytearray(bufSize) - with memoryview(buf)[:hdrMax] as hdrBuf: - fpos=bl=stream.readinto(hdrBuf) + #with memoryview(buf)[:hdrMax] as hdrBuf: + fpos=bl=stream.readinto(buf) + bp=0 + done=False while True: - bp=0 - while buf.startswith(b'\r\n',bp): + while buf.startswith(b'\r\n',bp): # will Fail if buffer (nearly) empty bp+=2 - bob=bp + start_1=bp if not buf.startswith(b'WARC/1.0\r\n',bp): + if done and bl-bp==0: + # really done + return raise ValueError("Not a WARC file? At %s: %s[%s]"%(bp, buf[bp:min(bl,bp+20)].decode('latin-1'), bl-bp)) bp+=10 wtype=None length=None state=1 - done=False tr=None # Was this record truncated? while not buf.startswith(b'\r\n',bp): + # there should always be enough in the buffer to complete this loop, + # because of the buffer update logic below eol=buf.index(b'\r\n',bp)+2 if buf.startswith(b"Content-Length: ",bp): length=wl=int(buf[bp+16:eol-2]) @@ -43,33 +48,45 @@ wtype = bytes(buf[bp+11:eol-2]) bp=eol bp=eol+2 + if (bp+length)>bl: + if done: + raise ValueError("Done but need more! %s + %s > %s",bp,length,bl) + # Need more data + if wtype in types: + # we need to keep from start_1 to bl + keepFrom=start_1 + keepLen=bl-keepFrom + buf[0:keepLen]=buf[keepFrom,bl] + else: + # we can skip the rest of this part + keepLen=0 + fpos=stream.seek(fpos+(pb+length-bl)) + spaceToFill=bufMax-keepLen + with memoryview(buf)[keepLen:bufMax] as xBuf: + nb=stream.readinto(xBuf) + fpos+=nb + bp=keepLen + bl=keepLen+nb + if nbhdrMax pass - print(wtype,bob,bp,eol,length,file=sys.stderr) - jumpTo=bp+length - buf[0:jumpTo]=buf[jumpTo:hdrMax] - _fpos=stream.seek(fpos:=fpos+jumpTo) - print('fp',_fpos,fpos,file=sys.stderr) - if done: - print('finished',file=sys.stderr) - break - with memoryview(buf) as mv: - n=stream.readinto(mv[hdrMax-jumpTo:hdrMax]) - print('read',n,file=sys.stderr) - if n