Skip to content
Snippets Groups Projects
Commit 8b9e7a6c authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Implement ICacheFile reading/writing from/to stream.

Also there was a bug when reading ICacheFile from stream, while
providing the header. We didn't set the header of the read file so it
was null and caused some NREs.
parent baeb1853
No related branches found
No related tags found
No related merge requests found
package azgracompress.cache; package azgracompress.cache;
import azgracompress.compression.CompressionOptions;
import azgracompress.data.V3i; import azgracompress.data.V3i;
import azgracompress.fileformat.QuantizationType; import azgracompress.fileformat.QuantizationType;
import azgracompress.quantization.scalar.SQCodebook; import azgracompress.quantization.scalar.SQCodebook;
...@@ -133,6 +134,7 @@ public class QuantizationCacheManager { ...@@ -133,6 +134,7 @@ public class QuantizationCacheManager {
return fileName; return fileName;
} }
/** /**
* Save VQ codebook to cache. * Save VQ codebook to cache.
* *
...@@ -206,7 +208,7 @@ public class QuantizationCacheManager { ...@@ -206,7 +208,7 @@ public class QuantizationCacheManager {
* @param codebookSize Codebook size. * @param codebookSize Codebook size.
* @return SQ cache file. * @return SQ cache file.
*/ */
private SQCacheFile loadSQCacheFile(final String imageFile, final int codebookSize) { public SQCacheFile loadSQCacheFile(final String imageFile, final int codebookSize) {
final File path = getCacheFilePathForSQ(imageFile, codebookSize); final File path = getCacheFilePathForSQ(imageFile, codebookSize);
try { try {
return (SQCacheFile) readCacheFile(path, new SQCacheFile()); return (SQCacheFile) readCacheFile(path, new SQCacheFile());
...@@ -225,9 +227,9 @@ public class QuantizationCacheManager { ...@@ -225,9 +227,9 @@ public class QuantizationCacheManager {
* @param vDim Quantization vector dimension. * @param vDim Quantization vector dimension.
* @return VQ cache file. * @return VQ cache file.
*/ */
private VQCacheFile loadVQCacheFile(final String trainFile, public VQCacheFile loadVQCacheFile(final String trainFile,
final int codebookSize, final int codebookSize,
final V3i vDim) { final V3i vDim) {
final File path = getCacheFilePathForVQ(trainFile, codebookSize, vDim); final File path = getCacheFilePathForVQ(trainFile, codebookSize, vDim);
try { try {
return (VQCacheFile) readCacheFile(path, new VQCacheFile()); return (VQCacheFile) readCacheFile(path, new VQCacheFile());
...@@ -282,9 +284,36 @@ public class QuantizationCacheManager { ...@@ -282,9 +284,36 @@ public class QuantizationCacheManager {
return null; return null;
} }
public static ICacheFile readCacheFile(final String path) { public ICacheFile loadCacheFile(final CompressionOptions compressionParams) {
try (FileInputStream fis = new FileInputStream(path); String path;
DataInputStream dis = new DataInputStream(fis)) { int codebookSize = (int) Math.pow(2, compressionParams.getBitsPerCodebookIndex());
switch (compressionParams.getQuantizationType()) {
case Scalar:
path = getCacheFilePathForSQ(compressionParams.getInputDataInfo().getCacheFileName(),
codebookSize).getAbsolutePath();
break;
case Vector1D:
case Vector2D:
case Vector3D:
path = getCacheFilePathForVQ(compressionParams.getInputDataInfo().getCacheFileName(),
codebookSize,
compressionParams.getQuantizationVector()).getAbsolutePath();
break;
default:
return null;
}
return readCacheFile(path);
}
/**
* Read cache file by DataInputStream.
*
* @param inputStream Input stream.
* @return Cache file or null, if exception occurs.
*/
private static ICacheFile readCacheFileImpl(final InputStream inputStream) {
try (DataInputStream dis = new DataInputStream(inputStream)) {
CacheFileHeader header = new CacheFileHeader(); CacheFileHeader header = new CacheFileHeader();
header.readFromStream(dis); header.readFromStream(dis);
...@@ -297,6 +326,31 @@ public class QuantizationCacheManager { ...@@ -297,6 +326,31 @@ public class QuantizationCacheManager {
} }
} }
/**
* Read cache file from input stream.
*
* @param inputStream Input data stream.
* @return Cache file or null if reading fails.
*/
public static ICacheFile readCacheFile(final InputStream inputStream) {
return readCacheFileImpl(inputStream);
}
/**
* Read cache file from file.
*
* @param path File path.
* @return Cache file or null if reading fails.
*/
public static ICacheFile readCacheFile(final String path) {
try (FileInputStream fis = new FileInputStream(path)) {
return readCacheFileImpl(fis);
} catch (IOException e) {
return null;
}
}
/** /**
* Inspect cache file specified by the path. * Inspect cache file specified by the path.
* *
......
...@@ -39,6 +39,7 @@ public class SQCacheFile implements ICacheFile { ...@@ -39,6 +39,7 @@ public class SQCacheFile implements ICacheFile {
} }
public void readFromStream(DataInputStream inputStream, CacheFileHeader header) throws IOException { public void readFromStream(DataInputStream inputStream, CacheFileHeader header) throws IOException {
this.header = header;
final int codebookSize = header.getCodebookSize(); final int codebookSize = header.getCodebookSize();
final int[] centroids = new int[codebookSize]; final int[] centroids = new int[codebookSize];
final long[] frequencies = new long[codebookSize]; final long[] frequencies = new long[codebookSize];
......
...@@ -43,6 +43,7 @@ public class VQCacheFile implements ICacheFile { ...@@ -43,6 +43,7 @@ public class VQCacheFile implements ICacheFile {
@Override @Override
public void readFromStream(DataInputStream inputStream, CacheFileHeader header) throws IOException { public void readFromStream(DataInputStream inputStream, CacheFileHeader header) throws IOException {
this.header = header;
final int codebookSize = header.getCodebookSize(); final int codebookSize = header.getCodebookSize();
final int entrySize = header.getVectorSizeX() * header.getVectorSizeY() * header.getVectorSizeZ(); final int entrySize = header.getVectorSizeX() * header.getVectorSizeY() * header.getVectorSizeZ();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment