-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement scanAndVerify, relieves memory pressure on startup
- Loading branch information
Showing
4 changed files
with
110 additions
and
11 deletions.
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
55 changes: 55 additions & 0 deletions
55
runtime/src/main/java/org/corfudb/util/MappedByteBufInputStream.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,55 @@ | ||
package org.corfudb.util; | ||
|
||
import com.google.common.io.ByteStreams; | ||
import io.netty.buffer.ByteBufInputStream; | ||
import io.netty.buffer.Unpooled; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.channels.FileChannel.MapMode; | ||
import javax.annotation.Nonnull; | ||
|
||
public class MappedByteBufInputStream extends | ||
ByteBufInputStream implements AutoCloseable { | ||
|
||
final AutoCleanableMappedBuffer buffer; | ||
|
||
/** Generate a new {@link MappedByteBufInputStream} over the entire file. | ||
* | ||
* @param fc The FileChannel to map the stream over. | ||
* @throws IOException If the stream cannot be mapped. | ||
*/ | ||
public MappedByteBufInputStream(@Nonnull FileChannel fc) | ||
throws IOException { | ||
this(new AutoCleanableMappedBuffer(fc.map(MapMode.READ_ONLY, 0, fc.size()))); | ||
} | ||
|
||
/** Given an existing {@link AutoCleanableMappedBuffer}, generate a new | ||
* {@link MappedByteBufInputStream} over it. | ||
* @param buf The {@link AutoCleanableMappedBuffer} to base this stream off of. | ||
*/ | ||
private MappedByteBufInputStream(@Nonnull AutoCleanableMappedBuffer buf) { | ||
super(Unpooled.wrappedBuffer(buf.getBuffer())); | ||
this.buffer = buf; | ||
} | ||
|
||
/** Return a limited version of this {@link MappedByteBufInputStream} which only returns | ||
* {@code limit} bytes. | ||
* | ||
* @param limit The number of bytes to limit to. | ||
* @return An {@link InputStream} which only permits reading up to the | ||
* given number of bytes. | ||
*/ | ||
public InputStream limited(int limit) { | ||
return ByteStreams.limit(this, limit); | ||
} | ||
|
||
/** {@inheritDoc} | ||
* | ||
* Also closes the underlying {@link AutoCleanableMappedBuffer}. | ||
*/ | ||
@Override | ||
public void close() { | ||
buffer.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