2023-03-06 17:32:13 +00:00
|
|
|
package nu.marginalia.rwf;
|
2022-05-19 15:45:26 +00:00
|
|
|
|
2023-03-06 17:32:13 +00:00
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
2022-05-19 15:45:26 +00:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.RandomAccessFile;
|
2023-03-06 17:32:13 +00:00
|
|
|
import java.nio.file.Files;
|
2022-05-19 15:45:26 +00:00
|
|
|
import java.nio.file.Path;
|
2023-03-06 17:32:13 +00:00
|
|
|
import java.nio.file.StandardOpenOption;
|
2022-05-19 15:45:26 +00:00
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
|
|
|
class RandomWriteFunnelTest {
|
|
|
|
|
2023-03-06 17:32:13 +00:00
|
|
|
Path testFile;
|
|
|
|
@BeforeEach
|
|
|
|
public void setUp() throws IOException {
|
|
|
|
testFile = Files.createTempFile(getClass().getSimpleName(), "bin");
|
|
|
|
}
|
|
|
|
@AfterEach
|
|
|
|
public void tearDown() throws IOException {
|
|
|
|
Files.delete(testFile);
|
|
|
|
}
|
2022-05-19 15:45:26 +00:00
|
|
|
@Test
|
|
|
|
public void test() {
|
2023-01-08 10:11:44 +00:00
|
|
|
try (var funnel = new RandomWriteFunnel(Path.of("/tmp"), 5001);
|
2023-03-06 17:32:13 +00:00
|
|
|
var out = Files.newByteChannel(testFile, StandardOpenOption.WRITE)) {
|
2022-05-19 15:45:26 +00:00
|
|
|
for (int i = 10_000-1; i >= 0; i--) {
|
|
|
|
System.out.println(i);
|
|
|
|
funnel.put(i, 10_000-i);
|
|
|
|
}
|
2023-03-06 17:32:13 +00:00
|
|
|
funnel.write(out);
|
2022-05-19 15:45:26 +00:00
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2023-03-06 17:32:13 +00:00
|
|
|
try (var in = new RandomAccessFile(testFile.toFile(), "r")) {
|
2022-05-19 15:45:26 +00:00
|
|
|
for (int i = 0; i < 10_000; i++) {
|
2024-02-06 11:12:26 +00:00
|
|
|
assertEquals(10_000-i, Long.reverseBytes(in.readLong()));
|
2022-05-19 15:45:26 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSparse() {
|
|
|
|
for (int j = 1; j <= 20; j++) {
|
2023-01-08 10:11:44 +00:00
|
|
|
try (var funnel = new RandomWriteFunnel(Path.of("/tmp"), j);
|
2023-03-06 17:32:13 +00:00
|
|
|
var out = Files.newByteChannel(testFile, StandardOpenOption.WRITE)) {
|
2022-05-19 15:45:26 +00:00
|
|
|
for (int i = 10 - 1; i >= 0; i -= 2) {
|
|
|
|
funnel.put(i, 10 - i);
|
|
|
|
}
|
2023-03-06 17:32:13 +00:00
|
|
|
funnel.write(out);
|
2022-05-19 15:45:26 +00:00
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2023-03-06 17:32:13 +00:00
|
|
|
try (var in = new RandomAccessFile(testFile.toFile(), "r")) {
|
2024-02-06 11:12:26 +00:00
|
|
|
assertEquals(0, Long.reverseBytes(in.readLong()));
|
|
|
|
assertEquals(9, Long.reverseBytes(in.readLong()));
|
|
|
|
assertEquals(0, Long.reverseBytes(in.readLong()));
|
|
|
|
assertEquals(7, Long.reverseBytes(in.readLong()));
|
|
|
|
assertEquals(0, Long.reverseBytes(in.readLong()));
|
|
|
|
assertEquals(5, Long.reverseBytes(in.readLong()));
|
|
|
|
assertEquals(0, Long.reverseBytes(in.readLong()));
|
|
|
|
assertEquals(3, Long.reverseBytes(in.readLong()));
|
|
|
|
assertEquals(0, Long.reverseBytes(in.readLong()));
|
|
|
|
assertEquals(1, Long.reverseBytes(in.readLong()));
|
2022-05-19 15:45:26 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-18 13:54:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testYuge() {
|
|
|
|
for (int j = 1; j <= 20; j++) {
|
2023-01-08 10:11:44 +00:00
|
|
|
try (var funnel = new RandomWriteFunnel(Path.of("/tmp"), j);
|
2023-03-06 17:32:13 +00:00
|
|
|
var out = Files.newByteChannel(testFile, StandardOpenOption.WRITE)) {
|
2022-06-18 13:54:58 +00:00
|
|
|
for (int i = 10 - 1; i >= 0; i -= 2) {
|
|
|
|
funnel.put(i, Long.MAX_VALUE - i);
|
|
|
|
}
|
2023-03-06 17:32:13 +00:00
|
|
|
funnel.write(out);
|
2022-06-18 13:54:58 +00:00
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2023-03-06 17:32:13 +00:00
|
|
|
try (var in = new RandomAccessFile(testFile.toFile(), "r")) {
|
2022-06-18 13:54:58 +00:00
|
|
|
in.readLong();
|
|
|
|
in.readLong();
|
|
|
|
in.readLong();
|
|
|
|
in.readLong();
|
|
|
|
in.readLong();
|
|
|
|
in.readLong();
|
|
|
|
in.readLong();
|
|
|
|
in.readLong();
|
|
|
|
in.readLong();
|
|
|
|
in.readLong();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-19 15:45:26 +00:00
|
|
|
}
|