only read from or write to disk if there is new data to transfer

(and set the file modification date to the 'published on' date in the netDb object)
This commit is contained in:
jrandom
2004-04-27 08:44:23 +00:00
committed by zzz
parent a78df1a152
commit 5c41be3470

View File

@@ -97,6 +97,7 @@ class PersistentDataStore extends TransientDataStore {
public void runJob() { public void runJob() {
_log.info("Writing key " + _key); _log.info("Writing key " + _key);
FileOutputStream fos = null; FileOutputStream fos = null;
File dbFile = null;
try { try {
String filename = null; String filename = null;
File dbDir = getDbDir(); File dbDir = getDbDir();
@@ -108,13 +109,22 @@ class PersistentDataStore extends TransientDataStore {
else else
throw new IOException("We don't know how to write objects of type " + _data.getClass().getName()); throw new IOException("We don't know how to write objects of type " + _data.getClass().getName());
fos = new FileOutputStream(new File(dbDir, filename)); dbFile = new File(dbDir, filename);
try { long dataPublishDate = getPublishDate();
_data.writeBytes(fos); if (dbFile.lastModified() < dataPublishDate) {
} catch (DataFormatException dfe) { // our filesystem is out of date, lets replace it
_log.error("Error writing out malformed object as " + _key + ": " + _data, dfe); fos = new FileOutputStream(dbFile);
File f = new File(dbDir, filename); try {
f.delete(); _data.writeBytes(fos);
fos.close();
dbFile.setLastModified(dataPublishDate);
} catch (DataFormatException dfe) {
_log.error("Error writing out malformed object as " + _key + ": "
+ _data, dfe);
dbFile.delete();
}
} else {
// we've already written the file, no need to waste our time
} }
} catch (IOException ioe) { } catch (IOException ioe) {
_log.error("Error writing out the object", ioe); _log.error("Error writing out the object", ioe);
@@ -122,6 +132,15 @@ class PersistentDataStore extends TransientDataStore {
if (fos != null) try { fos.close(); } catch (IOException ioe) {} if (fos != null) try { fos.close(); } catch (IOException ioe) {}
} }
} }
private long getPublishDate() {
if (_data instanceof RouterInfo) {
return ((RouterInfo)_data).getPublished();
} else if (_data instanceof LeaseSet) {
return ((LeaseSet)_data).getEarliestLeaseDate();
} else {
return -1;
}
}
} }
private class ReadJob extends JobImpl { private class ReadJob extends JobImpl {
@@ -143,7 +162,7 @@ class PersistentDataStore extends TransientDataStore {
for (int i = 0; i < leaseSetFiles.length; i++) { for (int i = 0; i < leaseSetFiles.length; i++) {
Hash key = getLeaseSetHash(leaseSetFiles[i].getName()); Hash key = getLeaseSetHash(leaseSetFiles[i].getName());
if ( (key != null) && (!isKnown(key)) ) if ( (key != null) && (!isKnown(key)) )
PersistentDataStore.this._context.jobQueue().addJob(new ReadLeaseJob(leaseSetFiles[i])); PersistentDataStore.this._context.jobQueue().addJob(new ReadLeaseJob(leaseSetFiles[i], key));
} }
} }
File routerInfoFiles[] = dbDir.listFiles(RouterInfoFilter.getInstance()); File routerInfoFiles[] = dbDir.listFiles(RouterInfoFilter.getInstance());
@@ -151,24 +170,41 @@ class PersistentDataStore extends TransientDataStore {
for (int i = 0; i < routerInfoFiles.length; i++) { for (int i = 0; i < routerInfoFiles.length; i++) {
Hash key = getRouterInfoHash(routerInfoFiles[i].getName()); Hash key = getRouterInfoHash(routerInfoFiles[i].getName());
if ( (key != null) && (!isKnown(key)) ) if ( (key != null) && (!isKnown(key)) )
PersistentDataStore.this._context.jobQueue().addJob(new ReadRouterJob(routerInfoFiles[i])); PersistentDataStore.this._context.jobQueue().addJob(new ReadRouterJob(routerInfoFiles[i], key));
} }
} }
} catch (IOException ioe) { } catch (IOException ioe) {
_log.error("Error reading files in the db dir", ioe); _log.error("Error reading files in the db dir", ioe);
} }
} }
} }
private class ReadLeaseJob extends JobImpl { private class ReadLeaseJob extends JobImpl {
private File _leaseFile; private File _leaseFile;
public ReadLeaseJob(File leaseFile) { private Hash _key;
public ReadLeaseJob(File leaseFile, Hash key) {
super(PersistentDataStore.this._context); super(PersistentDataStore.this._context);
_leaseFile = leaseFile; _leaseFile = leaseFile;
_key = key;
} }
public String getName() { return "Read LeaseSet"; } public String getName() { return "Read LeaseSet"; }
private boolean shouldRead() {
DataStructure data = get(_key);
if (data == null) return true;
if (data instanceof LeaseSet) {
long knownDate = ((LeaseSet)data).getEarliestLeaseDate();
long fileDate = _leaseFile.lastModified();
if (fileDate > knownDate)
return true;
else
return false;
} else {
// wtf
return true;
}
}
public void runJob() { public void runJob() {
if (!shouldRead()) return;
try { try {
FileInputStream fis = null; FileInputStream fis = null;
boolean corrupt = false; boolean corrupt = false;
@@ -201,12 +237,31 @@ class PersistentDataStore extends TransientDataStore {
private class ReadRouterJob extends JobImpl { private class ReadRouterJob extends JobImpl {
private File _routerFile; private File _routerFile;
public ReadRouterJob(File routerFile) { private Hash _key;
public ReadRouterJob(File routerFile, Hash key) {
super(PersistentDataStore.this._context); super(PersistentDataStore.this._context);
_routerFile = routerFile; _routerFile = routerFile;
_key = key;
} }
public String getName() { return "Read RouterInfo"; } public String getName() { return "Read RouterInfo"; }
private boolean shouldRead() {
DataStructure data = get(_key);
if (data == null) return true;
if (data instanceof RouterInfo) {
long knownDate = ((RouterInfo)data).getPublished();
long fileDate = _routerFile.lastModified();
if (fileDate > knownDate)
return true;
else
return false;
} else {
// wtf
return true;
}
}
public void runJob() { public void runJob() {
if (!shouldRead()) return;
try { try {
FileInputStream fis = null; FileInputStream fis = null;
boolean corrupt = false; boolean corrupt = false;