@Override
public synchronized void setMatrices(SparseMatrix features, SparseMatrix transpose, File dataDir) throws IOException {
this.features = features;
this.transpose = transpose;
File idCacheFile = new File(dataDir, "cosineSimilarity-ids.bin");
File lengthCacheFile = new File(dataDir, "cosineSimilarity-lengths.bin");
File maxCacheFile = new File(dataDir, "cosineSimilarity-maxResults.bin");
if (lengthCacheFile.exists() && lengthCacheFile.lastModified() >= features.lastModified()
&& idCacheFile.exists() && idCacheFile.lastModified() >= transpose.lastModified()) {
LOG.info("reading matrix information from cache");
lengths = (TIntFloatHashMap) WpIOUtils.readObjectFromFile(lengthCacheFile);
idsInResults = (TIntSet) WpIOUtils.readObjectFromFile(idCacheFile);
maxResults = (Integer) WpIOUtils.readObjectFromFile(maxCacheFile);
} else {
LOG.info("building cached matrix information");
lengths.clear();
idsInResults.clear();
maxResults = 0;
for (SparseMatrixRow row : features) {
lengths.put(row.getRowIndex(), (float) row.getNorm());
maxResults = Math.max(maxResults, row.getNumCols());
}
idsInResults.addAll(transpose.getRowIds());
WpIOUtils.writeObjectToFile(lengthCacheFile, lengths);
WpIOUtils.writeObjectToFile(idCacheFile, idsInResults);
WpIOUtils.writeObjectToFile(maxCacheFile, maxResults);
}
}