Skip to content

Commit

Permalink
exception type and other util
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod committed Aug 11, 2023
1 parent 3d066f2 commit 95eda36
Show file tree
Hide file tree
Showing 20 changed files with 849 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
package io.optimism.batcher.compressor;

import java.io.Closeable;
import java.io.Flushable;

/**
* Tx data bytes compressor interface.
*
* @author thinkAfCod
* @since 0.1.1
*/
public interface Compressor extends Closeable, Readable, Flushable {
public interface Compressor extends Closeable {

/**
* write uncompressed data which will be compressed. Should return CompressorFullException if the
Expand Down Expand Up @@ -59,6 +58,8 @@ public interface Compressor extends Closeable, Readable, Flushable {
* returns CompressorFullException if the compressor is known to be full. Note that calls to Write
* will fail if an error is returned from this method, but calls to Write can still return
* CompressorFullErr even if this does not.
*
* @return return true if compressed data length reached the limit, otherwise return false
*/
void fullErr();
boolean isFull();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,37 @@

package io.optimism.batcher.compressor;

import io.optimism.batcher.config.Config;

/**
* Compressor Config.
*
* @author thinkAfCod
* @since 0.1.1
* @param targetFrameSize To target when creating channel frames. Note that if the realized
* compression ratio is worse than the approximate, more frames may actually be created. This
* also depends on how close the target is to the max frame size.
* @param targetNumFrame To create in this channel. If the realized compression ratio is worse than
* @param targetNumFrames To create in this channel. If the realized compression ratio is worse than
* approxComprRatio, additional leftover frame(s) might get created.
* @param approxComprRatio ApproxComprRatio to assume. Should be slightly smaller than average from
* experiments to avoid the chances of creating a small additional leftover frame.
* @param kind Kind of compressor to use. Must be one of KindKeys. If unset, NewCompressor will
* default to RatioKind.
* @author thinkAfCod
* @since 0.1.1
*/
public record Config(
long targetFrameSize, int targetNumFrame, double approxComprRatio, String kind) {}
public record CompressorConfig(
int targetFrameSize, int targetNumFrames, String approxComprRatio, String kind) {

/**
* Create CompressorConfig instance from Config instance.
*
* @param config Config instance
* @return CompressorConfig instance
*/
public static CompressorConfig from(Config config) {
return new CompressorConfig(
config.targetFrameSize(),
config.targetNumFrames(),
config.approxComprRatio(),
Compressors.RatioKind);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,33 @@

package io.optimism.batcher.compressor;

import io.optimism.utilities.ex.UnsupportedException;

/**
* Compressor create tool.
*
* @author thinkAfCod
* @since 0.1.1
*/
public interface Compressors {
public class Compressors {

/** Kind type of ratio. */
String RatioKind = "ratio";
public static final String RatioKind = "ratio";

/** Kind type of shadow. */
String ShadowKind = "shadow";
private Compressors() {}

/**
* Create Compressor by kind.
*
* @param kind Type of Compressor
* @param config Config of compressor
* @return a compressor
*/
default void create(String kind) {}
public static Compressor create(final CompressorConfig config) {
String kind = config.kind();
if (kind.equalsIgnoreCase(RatioKind)) {
return new RatioCompressor(config);
} else {
throw new UnsupportedException(String.format("unsupported kind: %s", kind));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package io.optimism.batcher.compressor;

import io.optimism.batcher.compressor.ex.CompressorFullException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.CharBuffer;
import org.jetbrains.annotations.NotNull;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.zip.Deflater;

/**
* RatioCompressor class.
Expand All @@ -28,39 +31,88 @@
*/
public class RatioCompressor implements Compressor {

RatioCompressor() {
// todo plan to use java.util.zip.Deflater
private final CompressorConfig config;

private final Deflater deflater;

private final int inputThreshold;

private volatile ByteArrayOutputStream bos;

private int pos;

private int inputLength;

RatioCompressor(final CompressorConfig config) {
this.config = config;
this.deflater = new Deflater(Deflater.BEST_COMPRESSION);
this.inputThreshold = inputThreshold();
this.bos = new ByteArrayOutputStream(this.inputThreshold);
this.pos = 0;
this.inputLength = 0;
}

@Override
public int write(byte[] p) {
return 0;
if (this.isFull()) {
throw new CompressorFullException("the target amount of input data has been reached limit");
}
this.inputLength += p.length;
this.deflater.setInput(p);
byte[] compressed = new byte[p.length];
int len = this.deflater.deflate(compressed);
this.bos.write(compressed, 0, len);
this.deflater.reset();
return p.length;
}

@Override
public int read(byte[] p) {
return 0;
byte[] data = this.bos.toByteArray();
int len = this.bos.size();
if (pos > len) {
return -1;
}
int readLen = p.length;
int avail = len - pos;
if (p.length > avail) {
readLen = avail;
}
System.arraycopy(data, pos, p, 0, readLen);
pos += readLen;
return readLen;
}

@Override
public int read(@NotNull CharBuffer cb) throws IOException {
return 0;
public void reset() {
if (this.bos != null) {
this.bos.reset();
}
this.deflater.reset();
this.pos = 0;
this.inputLength = 0;
}

@Override
public void reset() {}

@Override
public int length() {
return 0;
return this.bos.size() - this.pos;
}

@Override
public void fullErr() {}
public boolean isFull() {
return this.inputLength >= this.inputThreshold;
}

@Override
public void close() throws IOException {}
public void close() throws IOException {
this.deflater.finish();
this.deflater.end();
}

@Override
public void flush() throws IOException {}
private int inputThreshold() {
return BigDecimal.valueOf(config.targetNumFrames())
.multiply(BigDecimal.valueOf(config.targetFrameSize()))
.divide(new BigDecimal(config.approxComprRatio()), 2, RoundingMode.HALF_UP)
.intValue();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
* @param subSafetyMargin Sub-safety margin
* @param pollInterval Milliseconds of poll interval
* @param maxL1TxSize Max L1 Tx Size
* @param targetFrameSize Max L1 Tx Size
* @param targetNumFrames Max L1 Tx Size
* @param approxComprRatio Max L1 Tx Size
* @author thinkAfCod
* @since 0.1.1
*/
Expand All @@ -38,4 +41,7 @@ public record Config(
String batchInboxAddress,
Long subSafetyMargin,
Long pollInterval,
Long maxL1TxSize) {}
Long maxL1TxSize,
Integer targetFrameSize,
Integer targetNumFrames,
String approxComprRatio) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 q315xia@163.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.optimism.batcher.ex;

/**
* BlockLoaderException class. Throws this when occurs error while executing BlockLoader.
*
* @author thinkAfCod
* @since 0.1.1
*/
public class BlockLoaderException extends RuntimeException {

/**
* Instantiates a new BlockLoaderException.
*
* @param message the message
*/
public BlockLoaderException(String message) {
super(message);
}

/**
* Instantiates a new BlockLoaderException.
*
* @param message the message
* @param cause the cause
*/
public BlockLoaderException(String message, Throwable cause) {
super(message, cause);
}

/**
* Instantiates a new BlockLoaderException.
*
* @param cause the cause
*/
public BlockLoaderException(Throwable cause) {
super(cause);
}
}
Loading

0 comments on commit 95eda36

Please sign in to comment.