Commit 7a73e8ed authored by Jim Fulton's avatar Jim Fulton

Added some comments.

Added seek before write in invalidate method. Without this, strange
things happen on NT and solaris. (I was probably breaking a rule of
thumb that says never to do a write() right after a read() even though
tell() would indicate I was in the right place.)

Added logic to update the cache index on an invalidate.

Added a separate call to check the cache size so that we only check
when a transaction is about to be written. It would be bad if we
"flipped" the cache twice during a transaction, as we would have
incomplete transaction data.  Note that this means that we could
exceed the target cache size if the data for a transaction exceeded
the target size (/2).
parent 94153587
...@@ -144,7 +144,7 @@ file 0 and file 1. ...@@ -144,7 +144,7 @@ file 0 and file 1.
""" """
__version__ = "$Revision: 1.5 $"[11:-2] __version__ = "$Revision: 1.6 $"[11:-2]
import os, tempfile import os, tempfile
from struct import pack, unpack from struct import pack, unpack
...@@ -155,11 +155,16 @@ class ClientCache: ...@@ -155,11 +155,16 @@ class ClientCache:
def __init__(self, storage='', size=20000000, client=None, var=None): def __init__(self, storage='', size=20000000, client=None, var=None):
if client: if client:
# Create a persistent cache
if var is None: var=os.path.join(INSTANCE_HOME,'var') if var is None: var=os.path.join(INSTANCE_HOME,'var')
# Get the list of cache file names
self._p=p=map(lambda i, p=storage, var=var, c=client: self._p=p=map(lambda i, p=storage, var=var, c=client:
os.path.join(var,'c%s-%s-%s.zec' % (p, c, i)), os.path.join(var,'c%s-%s-%s.zec' % (p, c, i)),
(0,1)) (0,1))
# get the list of cache files
self._f=f=[None, None] self._f=f=[None, None]
# initialize cache serial numbers
s=['\0\0\0\0\0\0\0\0', '\0\0\0\0\0\0\0\0'] s=['\0\0\0\0\0\0\0\0', '\0\0\0\0\0\0\0\0']
for i in 0,1: for i in 0,1:
if os.path.exists(p[i]): if os.path.exists(p[i]):
...@@ -169,18 +174,22 @@ class ClientCache: ...@@ -169,18 +174,22 @@ class ClientCache:
if fi.tell() > 30: if fi.tell() > 30:
fi.seek(22) fi.seek(22)
s[i]=fi.read(8) s[i]=fi.read(8)
if s[i]!='\0\0\0\0\0\0\0\0': f[i]=fi # If we found a non-zero serial, then use the file
if s[i] != '\0\0\0\0\0\0\0\0': f[i]=fi
fi=None fi=None
# Whoever has the larger serial is the current
if s[1] > s[0]: current=1 if s[1] > s[0]: current=1
elif s[0] > s[1]: current=0 elif s[0] > s[1]: current=0
else: else:
if f[0] is None: if f[0] is None:
# We started, open the first cache file
f[0]=open(p[0], 'w+b') f[0]=open(p[0], 'w+b')
f[0].write(magic) f[0].write(magic)
current=0 current=0
f[1]=None f[1]=None
else: else:
# Create a temporary cache
self._p=p=map( self._p=p=map(
lambda i, p=storage: lambda i, p=storage:
tempfile.mktemp('.zec'), tempfile.mktemp('.zec'),
...@@ -212,7 +221,12 @@ class ClientCache: ...@@ -212,7 +221,12 @@ class ClientCache:
f.seek(ap) f.seek(ap)
h=f.read(8) h=f.read(8)
if h != oid: return if h != oid: return
f.write(version and 'n' or 'i') f.seek(8,1) # Dang, we shouldn't have to do this. Bad Solaris & NT
if version:
f.write('n')
else:
del self._index[oid]
f.write('i')
def load(self, oid, version): def load(self, oid, version):
p=self._get(oid, None) p=self._get(oid, None)
...@@ -292,6 +306,17 @@ class ClientCache: ...@@ -292,6 +306,17 @@ class ClientCache:
seek(dlen, 1) seek(dlen, 1)
return read(vlen) return read(vlen)
def checkSize(self, size):
# Make sure we aren't going to exceed the target size.
# If we are, then flip the cache.
if self._pos+size > self._limit:
current=not current
self._current=current
self._f[current]=open(self._p[current],'w+b')
self._f[current].write(magic)
self._pos=pos=4
def store(self, oid, p, s, version, pv, sv): def store(self, oid, p, s, version, pv, sv):
tlen=31+len(p) tlen=31+len(p)
if version: if version:
...@@ -302,13 +327,6 @@ class ClientCache: ...@@ -302,13 +327,6 @@ class ClientCache:
pos=self._pos pos=self._pos
current=self._current current=self._current
if pos+tlen > self._limit:
current=not current
self._current=current
self._f[current]=open(self._p[current],'w+b')
self._f[current].write(magic)
self._pos=pos=4
f=self._f[current] f=self._f[current]
f.seek(pos) f.seek(pos)
stlen=pack(">I",tlen) stlen=pack(">I",tlen)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment