Skip to content

Commit

Permalink
avoid create comparable object
Browse files Browse the repository at this point in the history
  • Loading branch information
lifepuzzlefun1 committed Jan 28, 2024
1 parent 4620c7d commit 9072539
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
package com.automq.stream.s3.model;

import com.automq.stream.s3.StreamRecordBatchCodec;
import com.automq.stream.utils.biniarysearch.StreamRecordBatchList;
import com.automq.stream.utils.biniarysearch.primitive.LongComparableItem;
import io.netty.buffer.ByteBuf;

public class StreamRecordBatch implements Comparable<StreamRecordBatch> {
public class StreamRecordBatch implements Comparable<StreamRecordBatch>, LongComparableItem {
private final long streamId;
private final long epoch;
private final long baseOffset;
Expand Down Expand Up @@ -113,4 +115,14 @@ public String toString() {
", count=" + count +
", size=" + size() + '}';
}

@Override
public boolean isLessThan(long value) {
return getLastOffset() <= value;
}

@Override
public boolean isGreaterThan(long value) {
return getBaseOffset() > value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.automq.stream.utils.biniarysearch.primitive.LongOrderedCollection;

import java.util.List;
import java.util.Objects;

public class StreamRecordBatchList extends LongOrderedCollection {

Expand All @@ -41,51 +40,7 @@ public int size() {

@Override
protected LongComparableItem get(int index) {
return new ComparableStreamRecordBatch(records.get(index));
return records.get(index);
}


private static final class ComparableStreamRecordBatch implements LongComparableItem {
private final StreamRecordBatch recordBatch;

private ComparableStreamRecordBatch(StreamRecordBatch recordBatch) {
this.recordBatch = recordBatch;
}

@Override
public boolean isLessThan(long value) {
return recordBatch.getLastOffset() <= value;
}

@Override
public boolean isGreaterThan(long value) {
return recordBatch.getBaseOffset() > value;
}

public StreamRecordBatch recordBatch() {
return recordBatch;
}

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (ComparableStreamRecordBatch) obj;
return Objects.equals(this.recordBatch, that.recordBatch);
}

@Override
public int hashCode() {
return recordBatch.hashCode();
}

@Override
public String toString() {
return "ComparableStreamRecordBatch[" +
"recordBatch=" + recordBatch + ']';
}

}
}

0 comments on commit 9072539

Please sign in to comment.