Skip to content

Commit 8903c3c

Browse files
committed
tuning performance
1 parent 1070fff commit 8903c3c

File tree

6 files changed

+27
-34
lines changed

6 files changed

+27
-34
lines changed

benchmark.png

-17 Bytes
Loading

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = "io.github.gc-garcol"
10-
version = "0.3.0"
10+
version = "0.4.0"
1111

1212
java {
1313
withJavadocJar()

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Cafe WAL
22

3+
Append over 180,000 records per second.
4+
35
[![Maven Central Version](https://img.shields.io/maven-central/v/io.github.gc-garcol/cafe-wal?logo=sonatype&logoColor=red&style=flat&label=Maven%20Central)](https://central.sonatype.com/artifact/io.github.gc-garcol/cafe-wal?)
46
[![javadoc](https://javadoc.io/badge2/io.github.gc-garcol/cafe-wal/javadoc.svg?)](https://javadoc.io/doc/io.github.gc-garcol/cafe-wal?)
57

68
## Benchmark
79

8-
140k records per second (not batching)
10+
180k records per second (not batching)
911

1012
![benchmark.png](benchmark.png)

wal-benchmark/src/main/java/gc/garcol/libbenchmark/LogRepositoryAppendBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
@State(Scope.Thread)
1414
@BenchmarkMode({ Mode.Throughput, Mode.AverageTime })
15-
@OutputTimeUnit(TimeUnit.SECONDS)
15+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
1616
@Fork(1)
1717
public class LogRepositoryAppendBenchmark
1818
{

wal-core/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = "io.github.gc-garcol"
7-
version = "0.3.0"
7+
version = "0.4.0"
88

99
java {
1010
toolchain {

wal-core/src/main/java/gc/garcol/walcore/LogRepository.java

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
* @author thaivc
1919
* @since 2024
2020
*/
21-
public class LogRepository
22-
{
21+
public class LogRepository {
2322
/**
2423
* The first segment number.
2524
*/
@@ -38,13 +37,15 @@ public class LogRepository
3837
FileChannel indexChannel;
3938
FileChannel logChannel;
4039

40+
long logOffset;
41+
long indexOffset;
42+
4143
/**
4244
* Constructs a `LogRepository` with the specified base log directory.
4345
*
4446
* @param baseLogDir the base directory for log files
4547
*/
46-
public LogRepository(String baseLogDir)
47-
{
48+
public LogRepository(String baseLogDir) {
4849
LogUtil.createDirectoryNX(baseLogDir);
4950
this.baseLogDir = baseLogDir;
5051
}
@@ -59,21 +60,21 @@ public LogRepository(String baseLogDir)
5960
* @return the segment number of the latest log file, or 0 if no log files are found
6061
* @throws IOException if an I/O error occurs while accessing the log directory
6162
*/
62-
public long getLatestSegment() throws IOException
63-
{
63+
public long getLatestSegment() throws IOException {
6464
Optional<Path> lastFile = Files.list(Paths.get(baseLogDir))
6565
.filter(Files::isRegularFile)
6666
.max(Comparator.comparing(Path::getFileName));
6767
return lastFile.map(path -> LogUtil.segment(path.getFileName().toString())).orElse(-1L);
6868
}
6969

70-
private void generateFiles(long segment) throws IOException
71-
{
70+
private void generateFiles(long segment) throws IOException {
7271
indexFile = new RandomAccessFile(indexPath(segment), "rw");
7372
logFile = new RandomAccessFile(logPath(segment), "rw");
7473
currentIndex = totalRecords(currentSegment);
7574
indexChannel = indexFile.getChannel();
7675
logChannel = logFile.getChannel();
76+
logOffset = logFile.length();
77+
indexOffset = indexFile.length();
7778
}
7879

7980
/**
@@ -86,11 +87,9 @@ private void generateFiles(long segment) throws IOException
8687
* @throws IOException if an I/O error occurs during the file operations
8788
* @throws IllegalArgumentException if the specified segment is older than the current segment
8889
*/
89-
public void switchToSegment(long segment) throws IOException
90-
{
90+
public void switchToSegment(long segment) throws IOException {
9191
long latestSegment = getLatestSegment();
92-
if (segment < latestSegment)
93-
{
92+
if (segment < latestSegment) {
9493
throw new IllegalArgumentException("Cannot switch to an older segment");
9594
}
9695
currentSegment = segment;
@@ -106,8 +105,7 @@ public void switchToSegment(long segment) throws IOException
106105
* @param readerBuffer the buffer to read the log entry into
107106
* @throws IOException if an I/O error occurs
108107
*/
109-
public void read(long segment, long index, ByteBuffer readerBuffer) throws IOException
110-
{
108+
public void read(long segment, long index, ByteBuffer readerBuffer) throws IOException {
111109
RandomAccessFile indexFileRead = indexFile != null && segment == currentSegment ? indexFile : new RandomAccessFile(indexPath(segment), "r");
112110
RandomAccessFile logFileRead = logFile != null && segment == currentSegment ? logFile : new RandomAccessFile(logPath(segment), "r");
113111

@@ -134,17 +132,16 @@ public void read(long segment, long index, ByteBuffer readerBuffer) throws IOExc
134132
* @param logs the buffer containing the log entries to append
135133
* @throws IOException if an I/O error occurs during the write operation
136134
*/
137-
public void append(ByteBuffer logs) throws IOException
138-
{
135+
public void append(ByteBuffer logs) throws IOException {
139136
indexBufferWriter.clear();
140137
indexBufferWriter.putLong(logFile.length());
141138
indexBufferWriter.putInt(logs.limit());
142139
indexBufferWriter.flip();
143140

144-
var logOffset = logFile.length();
145-
var indexOffset = indexFile.length();
146141
logChannel.write(logs, logOffset);
147142
indexChannel.write(indexBufferWriter, indexOffset);
143+
logOffset += logs.limit();
144+
indexOffset += LogIndex.SIZE;
148145
currentIndex++;
149146
}
150147

@@ -157,19 +154,16 @@ public void append(ByteBuffer logs) throws IOException
157154
* @param fromIndex the index from which to truncate
158155
* @throws IOException if an I/O error occurs
159156
*/
160-
public void truncate(long segment, long fromIndex) throws IOException
161-
{
157+
public void truncate(long segment, long fromIndex) throws IOException {
162158
File indexFileSys = new File(indexPath(segment));
163-
if (!indexFileSys.exists())
164-
{
159+
if (!indexFileSys.exists()) {
165160
return;
166161
}
167162

168163
try (
169164
RandomAccessFile indexFile = new RandomAccessFile(indexPath(segment), "rw");
170165
RandomAccessFile logFile = new RandomAccessFile(logPath(segment), "rw")
171-
)
172-
{
166+
) {
173167
indexBufferReader.clear();
174168
var indexChannel = indexFile.getChannel();
175169
indexChannel.read(indexBufferReader, fromIndex * LogIndex.SIZE);
@@ -187,19 +181,16 @@ public void truncate(long segment, long fromIndex) throws IOException
187181
* @return the total number of records
188182
* @throws IOException if an I/O error occurs
189183
*/
190-
public long totalRecords(long segment) throws IOException
191-
{
184+
public long totalRecords(long segment) throws IOException {
192185
RandomAccessFile indexFileRead = segment == currentSegment ? indexFile : new RandomAccessFile(indexPath(segment), "r");
193186
return indexFileRead.length() / LogIndex.SIZE;
194187
}
195188

196-
private String indexPath(long segment)
197-
{
189+
private String indexPath(long segment) {
198190
return baseLogDir + "/" + LogUtil.indexName(segment);
199191
}
200192

201-
private String logPath(long segment)
202-
{
193+
private String logPath(long segment) {
203194
return baseLogDir + "/" + LogUtil.logName(segment);
204195
}
205196
}

0 commit comments

Comments
 (0)