-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/kiwi/storage/bitcask/sync/LazySegmentWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package kiwi.storage.bitcask.sync; | ||
|
||
import kiwi.error.KiwiWriteException; | ||
import kiwi.storage.bitcask.log.LogSegment; | ||
import kiwi.storage.bitcask.log.Record; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A {@link SegmentWriter} that appends records to the active segment and defers | ||
* fsync to operating system. This is useful for performance sensitive applications but | ||
* may result in data loss in case of a crash. | ||
*/ | ||
public class LazySegmentWriter extends SegmentWriter { | ||
public LazySegmentWriter(Supplier<LogSegment> activeSegmentSupplier) { | ||
super(activeSegmentSupplier); | ||
} | ||
|
||
@Override | ||
protected void append(Record record) throws KiwiWriteException { | ||
activeSegment().append(record); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/kiwi/storage/bitcask/sync/SegmentWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package kiwi.storage.bitcask.sync; | ||
|
||
import kiwi.error.KiwiWriteException; | ||
import kiwi.storage.bitcask.log.LogSegment; | ||
import kiwi.storage.bitcask.log.Record; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Supplier; | ||
|
||
public abstract class SegmentWriter implements AutoCloseable { | ||
private final AtomicBoolean closed = new AtomicBoolean(false); | ||
private final Supplier<LogSegment> activeSegmentSupplier; | ||
|
||
public SegmentWriter(Supplier<LogSegment> activeSegmentSupplier) { | ||
this.activeSegmentSupplier = activeSegmentSupplier; | ||
} | ||
|
||
abstract protected void append(Record record) throws KiwiWriteException; | ||
|
||
protected void sync() { | ||
activeSegment().sync(); | ||
} | ||
|
||
protected LogSegment activeSegment() { | ||
return activeSegmentSupplier.get(); | ||
} | ||
|
||
public void close() { | ||
if (closed.compareAndSet(false, true)) { | ||
activeSegment().close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters