Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pulsar.client.impl;

import com.google.common.collect.ComparisonChain;
import javax.annotation.Nonnull;
import org.apache.pulsar.client.api.MessageId;

Expand Down Expand Up @@ -72,11 +73,21 @@ public int getBatchIndex() {
public int compareTo(@Nonnull MessageId o) {
if (o instanceof MessageIdImpl) {
MessageIdImpl other = (MessageIdImpl) o;
int batchIndex = (o instanceof BatchMessageIdImpl) ? ((BatchMessageIdImpl) o).batchIndex : NO_BATCH;
return messageIdCompare(
this.ledgerId, this.entryId, this.partitionIndex, this.batchIndex,
other.ledgerId, other.entryId, other.partitionIndex, batchIndex
);
int compareWithoutBatchIndex = messageIdCompare(
this.ledgerId, this.entryId, this.partitionIndex,
other.ledgerId, other.entryId, other.partitionIndex);
if (compareWithoutBatchIndex != 0) {
return compareWithoutBatchIndex;
} else {
if (!(o instanceof BatchMessageIdImpl)) {
throw new UnsupportedOperationException(this.getClass().getName() + " can't compare with "
+ o.getClass().getName()
+ " when they have the same `LedgerId`, `EntryId` and `PartitionIndex`.");
} else {
return ComparisonChain.start().compare(this.batchIndex,
((BatchMessageIdImpl) o).batchIndex).result();
}
}
} else if (o instanceof TopicMessageIdImpl) {
return compareTo(((TopicMessageIdImpl) o).getInnerMessageId());
} else {
Expand Down Expand Up @@ -140,4 +151,16 @@ public BatchMessageAcker getAcker() {
return acker;
}

static int messageIdCompare(
long ledgerId1, long entryId1, int partitionIndex1, int batchIndex1,
long ledgerId2, long entryId2, int partitionIndex2, int batchIndex2
) {
return ComparisonChain.start()
.compare(ledgerId1, ledgerId2)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will be exd uted in hot paths probably.
Is it better to use raw java code? We could save method calls and allocations

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goog point! I think we can use raw java code

.compare(entryId1, entryId2)
.compare(partitionIndex1, partitionIndex2)
.compare(batchIndex1, batchIndex2)
.result();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,17 @@ public byte[] toByteArray() {
public int compareTo(@Nonnull MessageId o) {
if (o instanceof MessageIdImpl) {
MessageIdImpl other = (MessageIdImpl) o;
int batchIndex = (o instanceof BatchMessageIdImpl) ? ((BatchMessageIdImpl) o).getBatchIndex() : NO_BATCH;
return messageIdCompare(
this.ledgerId, this.entryId, this.partitionIndex, NO_BATCH,
other.ledgerId, other.entryId, other.partitionIndex, batchIndex
int compareWithoutBatchIndex = messageIdCompare(
this.ledgerId, this.entryId, this.partitionIndex,
other.ledgerId, other.entryId, other.partitionIndex
);
if (compareWithoutBatchIndex != 0 || !(o instanceof BatchMessageIdImpl)) {
return compareWithoutBatchIndex;
} else {
throw new UnsupportedOperationException(this.getClass().getName() + " can't compare with "
+ o.getClass().getName()
+ " when they have the same `LedgerId`, `EntryId` and `PartitionIndex`.");
}
} else if (o instanceof TopicMessageIdImpl) {
return compareTo(((TopicMessageIdImpl) o).getInnerMessageId());
} else {
Expand All @@ -222,14 +228,13 @@ static int messageIdHashCode(long ledgerId, long entryId, int partitionIndex, in
}

static int messageIdCompare(
long ledgerId1, long entryId1, int partitionIndex1, int batchIndex1,
long ledgerId2, long entryId2, int partitionIndex2, int batchIndex2
long ledgerId1, long entryId1, int partitionIndex1,
long ledgerId2, long entryId2, int partitionIndex2
) {
return ComparisonChain.start()
.compare(ledgerId1, ledgerId2)
.compare(entryId1, entryId2)
.compare(partitionIndex1, partitionIndex2)
.compare(batchIndex1, batchIndex2)
.result();
.compare(ledgerId1, ledgerId2)
.compare(entryId1, entryId2)
.compare(partitionIndex1, partitionIndex2)
.result();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -666,13 +666,13 @@ public synchronized int compareTo(MessageId messageId) {
if (this.messageId instanceof BatchMessageIdImpl && (!(messageId instanceof BatchMessageIdImpl))) {
final BatchMessageIdImpl lhs = (BatchMessageIdImpl) this.messageId;
final MessageIdImpl rhs = (MessageIdImpl) messageId;
return MessageIdImpl.messageIdCompare(
return BatchMessageIdImpl.messageIdCompare(
lhs.getLedgerId(), lhs.getEntryId(), lhs.getPartitionIndex(), lhs.getBatchIndex(),
rhs.getLedgerId(), rhs.getEntryId(), rhs.getPartitionIndex(), Integer.MAX_VALUE);
} else if (messageId instanceof BatchMessageIdImpl && (!(this.messageId instanceof BatchMessageIdImpl))){
final MessageIdImpl lhs = this.messageId;
final BatchMessageIdImpl rhs = (BatchMessageIdImpl) messageId;
return MessageIdImpl.messageIdCompare(
return BatchMessageIdImpl.messageIdCompare(
lhs.getLedgerId(), lhs.getEntryId(), lhs.getPartitionIndex(), Integer.MAX_VALUE,
rhs.getLedgerId(), rhs.getEntryId(), rhs.getPartitionIndex(), rhs.getBatchIndex());
} else {
Expand Down
Loading