Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix set0 with LongArrayDisk #185

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix set0 with LongArrayDisk
  • Loading branch information
ate47 committed Jan 4, 2023
commit 2eb9ca881988806e94395e332ea1e6edbb955255
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package org.rdfhdt.hdt.util.disk;

import org.rdfhdt.hdt.util.BitUtil;
import org.rdfhdt.hdt.util.io.CloseMappedByteBuffer;
import org.rdfhdt.hdt.util.io.IOUtil;

Expand Down Expand Up @@ -139,9 +140,11 @@ public long length() {
return size;
}

public void set0(long start, long end) {
public void set0(long startIndex, long endIndex) {
long start = startIndex * 8L;
long end = endIndex * 8L;

if (start >= end) {
// TODO: trim the file after end
return;
}
int startBlock = (int) (start / MAPPING_SIZE);
Expand All @@ -166,15 +169,21 @@ public void set0(long start, long end) {
startBuffer = 0;
}
if (i == endBlock) {
endBuffer = (int) (end % MAPPING_SIZE);
if (end % MAPPING_SIZE == 0) {
endBuffer = (int) MAPPING_SIZE;
} else {
endBuffer = (int) (end % MAPPING_SIZE);
}
} else {
endBuffer = capacity;
}

int toWrite = (endBuffer - startBuffer) * 8;
assert endBuffer > startBuffer : "start after end " + startBuffer + " -> " + endBuffer;

int toWrite = endBuffer - startBuffer;

while (toWrite > 0) {
mapping.position(startBuffer * 8);
mapping.position(startBuffer);
int w = Math.min(toWrite, zeros.length);

mapping.put(zeros, 0, w);
Expand Down Expand Up @@ -214,6 +223,9 @@ public void resize(long newSize) throws IOException {
IOUtil.closeAll(this.mappings);
this.mappings = null;

// resize the file to the new size
channel.truncate(sizeBit);

for (int block = 0; block < blocks; block++) {
long sizeMapping;
if (block + 1 == blocks && sizeBit % MAPPING_SIZE != 0) {
Expand All @@ -223,6 +235,7 @@ public void resize(long newSize) throws IOException {
}
mappings[block] = IOUtil.mapChannel(location.toAbsolutePath().toString(), channel, FileChannel.MapMode.READ_WRITE, block * MAPPING_SIZE, sizeMapping);
}

// close previous mapping
this.mappings = mappings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.stream.IntStream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class LongArrayDiskTest extends AbstractMapMemoryTest {

Expand Down Expand Up @@ -103,5 +104,55 @@ public void resizeTest() throws IOException {
assertEquals(array.get(3), 4);
}
}
@Test
public void largeResizeTest() throws IOException {
Path root = tempDir.getRoot().toPath();

long[] tests = {7, 5, 9, 2};

Path arrFile = root.resolve("arr");
try {
long endSize;
try (LongArrayDisk array = new LongArrayDisk(arrFile, 100)) {
// add check value after the resizes/loading
for (int i = 0; i < tests.length; i++) {
array.set(i, tests[i]);
}

// resize from 10^3 to 10^9 to see the usage of multiple mappings in set0
for (int i = 10; i < 30; i++) {
array.resize(1L << i);
}

// store the size
endSize = array.length();
}

assertEquals("array size and file size aren't matching!", endSize * 8L, Files.size(arrFile));

long endSize2;

try (LongArrayDisk array = new LongArrayDisk(arrFile, endSize, false)) {
// resize to a lower size to test the file truncating
array.resize(1L << 10);

// store the new size
endSize2 = array.length();
}

assertEquals("new array size and file size aren't matching!", endSize2 * 8L, Files.size(arrFile));

assertTrue("old array size isn't bigger than the new size, bad test?", endSize2 < endSize);

try (LongArrayDisk array = new LongArrayDisk(arrFile, endSize2, false)) {
// assert that the reloading/resizing didn't cancelled some values
for (int i = 0; i < tests.length; i++) {
assertEquals(tests[i], array.get(i));
}
}
} finally {
Files.deleteIfExists(arrFile);
}

}
}