Skip to content

Commit

Permalink
Update KffFile to use FileChannelFactory instead of RandomAccessFile
Browse files Browse the repository at this point in the history
Still need to create more robust unit test to demonstrate thread safety.
  • Loading branch information
drivenflywheel committed Jul 31, 2023
1 parent 250c7bc commit 361f9d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
55 changes: 31 additions & 24 deletions src/main/java/emissary/kff/KffFile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package emissary.kff;

import emissary.core.channels.FileChannelFactory;
import emissary.core.channels.SeekableByteChannelFactory;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -8,6 +11,7 @@
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.annotation.Nonnull;
Expand All @@ -31,7 +35,8 @@ public class KffFile implements KffFilter {
private final Logger logger;

/** File containing SHA-1/CRC32 results of known files */
protected RandomAccessFile knownFile;
// protected RandomAccessFile knownFile;
protected SeekableByteChannelFactory knownFileFactory;

/** Byte buffer that is mapped to the above file */
protected ByteBuffer mappedBuf;
Expand Down Expand Up @@ -82,10 +87,11 @@ public KffFile(String filename, String filterName, FilterType ftype, int recordL
logger = LoggerFactory.getLogger(this.getClass());

// Open file in read-only mode
knownFile = new RandomAccessFile(filename, "r");
// knownFile = new RandomAccessFile(filename, "r");
knownFileFactory = FileChannelFactory.create(Paths.get(filename));

// Initial high value for binary search is the largest index
bSearchInitHigh = ((int) knownFile.length() / recordLength) - 1;
bSearchInitHigh = ((int) knownFileFactory.create().size() / recordLength) - 1;

logger.debug("KFF File {} has {} records", filename, (bSearchInitHigh + 1));
}
Expand Down Expand Up @@ -147,33 +153,34 @@ private boolean binaryFileSearch(@Nonnull byte[] hash, long crc) {

/* Buffer to hold a record */
byte[] rec = new byte[recordLength];

ByteBuffer byteBuffer = ByteBuffer.wrap(rec);
// Search until the indexes cross
while (low <= high) {
// Calculate the midpoint
int mid = (low + high) >> 1;
try (SeekableByteChannel knownFile = knownFileFactory.create()) {
while (low <= high) {
byteBuffer.clear();

try {
knownFile.seek(rec.length * (long) mid);
int count = knownFile.read(rec);
// Calculate the midpoint
int mid = (low + high) >> 1;

knownFile.position(rec.length * (long) mid);
int count = knownFile.read(byteBuffer);
if (count != rec.length) {
logger.warn("Short read on KffFile at {} read {} expected {}", (rec.length * mid), count, rec.length);
logger.warn("Short read on KffFile at {} read {} expected {}", (recordLength * mid), count, recordLength);
return false;
}
} catch (IOException x) {
logger.warn("Exception reading KffFile: {}", x.getMessage());
return false;
}

// Compare the record with the target. Adjust the indexes accordingly.
int c = compare(rec, hash, crc);
if (c < 0) {
high = mid - 1;
} else if (c > 0) {
low = mid + 1;
} else {
return true;
// Compare the record with the target. Adjust the indexes accordingly.
int c = compare(rec, hash, crc);
if (c < 0) {
high = mid - 1;
} else if (c > 0) {
low = mid + 1;
} else {
return true;
}
}
} catch (IOException e) {
logger.warn("Exception reading KffFile: {}", e.getMessage());
return false;
}

// not found
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/emissary/kff/KffFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class KffFileTest extends UnitTest {

private static final String expectedShaHash = "000000206738748EDD92C4E3D2E823896700F849";
private static final String ITEM_NAME = "Some_item_name"; // "000000206738748EDD92C4E3D2E823896700F849";
private static final byte[] expectedSha1Bytes = {(byte) 0, (byte) 0, (byte) 0, (byte) 32, (byte) 103, (byte) 56, (byte) 116,
(byte) -114, (byte) -35, (byte) -110, (byte) -60, (byte) -29, (byte) -46, (byte) -24, (byte) 35, (byte) -119,
(byte) 103, (byte) 0, (byte) -8, (byte) 73};
Expand Down Expand Up @@ -44,7 +44,7 @@ void testKffFileCheck() {
results.setHash("SHA-1", expectedSha1Bytes);
results.setHash("CRC32", expectedCrcBytes);
try {
assertTrue(kffFile.check(expectedShaHash, results));
assertTrue(kffFile.check(ITEM_NAME, results));
} catch (Exception e) {
fail(e);
}
Expand All @@ -54,7 +54,7 @@ void testKffFileCheck() {
results = new ChecksumResults();
results.setHash("SHA-1", incorrectSha1Bytes);
try {
assertFalse(kffFile.check(expectedShaHash, results));
assertFalse(kffFile.check(ITEM_NAME, results));
} catch (Exception e) {
fail(e);
}
Expand Down

0 comments on commit 361f9d8

Please sign in to comment.