}
}
List<Long> times = new ArrayList<Long>();
List<Object> keys = new ArrayList<Object>();
DBIterator it = expiredDb.iterator(new ReadOptions().fillCache(false));
long now = ctx.getTimeService().wallClockTime();
try {
for (it.seekToFirst(); it.hasNext();) {
Map.Entry<byte[], byte[]> entry = it.next();
Long time = (Long) unmarshall(entry.getKey());
if (time > now)
break;
times.add(time);
Object key = unmarshall(entry.getValue());
if (key instanceof List)
keys.addAll((List<?>) key);
else
keys.add(key);
}
for (Long time : times) {
expiredDb.delete(marshall(time));
}
if (!keys.isEmpty())
log.debugf("purge (up to) %d entries", keys.size());
int count = 0;
for (Object key : keys) {
byte[] keyBytes = marshall(key);
byte[] b = db.get(keyBytes);
if (b == null)
continue;
MarshalledEntry me = (MarshalledEntry) ctx.getMarshaller().objectFromByteBuffer(b);
// TODO race condition: the entry could be updated between the get and delete!
if (me.getMetadata() != null && me.getMetadata().isExpired(now)) {
// somewhat inefficient to FIND then REMOVE...
db.delete(keyBytes);
purgeListener.entryPurged(key);
count++;
}
}
if (count != 0)
log.debugf("purged %d entries", count);
} catch (Exception e) {
throw new PersistenceException(e);
} finally {
try {
it.close();
} catch (IOException e) {
log.warnUnableToCloseDbIterator(e);
}
}
} catch (PersistenceException e) {