Skip to content

Commit

Permalink
Merge pull request #1154 from b2ihealthcare/issue/short-ipv6-parsing-…
Browse files Browse the repository at this point in the history
…from-docval

Short ipv6 format parsing from doc values
  • Loading branch information
cmark authored May 5, 2023
2 parents a8f0404 + e42c85e commit 9a879b9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.runners.MethodSorters;

import com.b2international.commons.exceptions.BadRequestException;
import com.b2international.index.query.Query;
import com.b2international.index.revision.RevisionBranch.BranchState;
import com.b2international.index.revision.RevisionFixtures.RevisionData;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -185,14 +186,19 @@ public void fastForwardMergeBranchWithNewRevisionToParent() throws Exception {
// after commit child branch becomes FORWARD
assertState(child, MAIN, BranchState.FORWARD);
// do the merge
branching().prepareMerge(child, MAIN).merge();
Commit mergeCommit = branching().prepareMerge(child, MAIN).merge();
// after fast-forward merge
// 1. MAIN should be in UP_TO_DATE state compared to the child
assertState(MAIN, child, BranchState.UP_TO_DATE);
// 2. Child should be UP_TO_DATE state compared to the MAIN
assertState(child, MAIN, BranchState.UP_TO_DATE);
// 3. revision should be visible from MAIN branch
assertNotNull(getRevision(MAIN, RevisionData.class, STORAGE_KEY1));
// 4. verify merge commit details, especially merge source value (this verifies deserialization and proper loading of IP fields via field selection)
Commit mergeCommitFromIndex = rawIndex().read(searcher -> Query.select(Commit.class)
.fields(Commit.Fields.ID, Commit.Fields.MERGE_SOURCE)
.where(Commit.Expressions.id(mergeCommit.getId())).build().search(searcher).first());
assertEquals(mergeCommit.getMergeSource(), mergeCommitFromIndex.getMergeSource());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2021 B2i Healthcare Pte Ltd, http://b2i.sg
* Copyright 2018-2023 B2i Healthcare Pte Ltd, http://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,10 +24,21 @@
*/
public class RevisionBranchPointTest {

@Test
public void serializeNull() throws Exception {
assertNull(RevisionBranchPoint.valueOf(null));
}

@Test
public void serialization() throws Exception {
RevisionBranchPoint original = new RevisionBranchPoint(1024, 2048);
assertEquals(original, RevisionBranchPoint.valueOf(original.toIpAddress()));
}

@Test
public void deserShortIPv6Address() throws Exception {
RevisionBranchPoint addr = RevisionBranchPoint.valueOf("::1:0:187:eb43:177e");
assertEquals(1, addr.getBranchId());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2022 B2i Healthcare Pte Ltd, http://b2i.sg
* Copyright 2018-2023 B2i Healthcare Pte Ltd, http://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,7 +60,7 @@ static final class Builder {
private String branch;
private String author;
private String comment;
private long timestamp;
private Long timestamp;
private List<CommitDetail> details;
private String groupId;
private RevisionBranchPoint mergeSource;
Expand All @@ -86,7 +86,7 @@ public Builder comment(final String comment) {
return this;
}

public Builder timestamp(final long timestamp) {
public Builder timestamp(final Long timestamp) {
this.timestamp = timestamp;
return this;
}
Expand Down Expand Up @@ -222,7 +222,7 @@ public static final class Fields {
}
)
private final String comment;
private final long timestamp;
private final Long timestamp;
private final String groupId;
private final List<CommitDetail> details;
private final RevisionBranchPoint mergeSource;
Expand All @@ -238,7 +238,7 @@ private Commit(
final String branch,
final String author,
final String comment,
final long timestamp,
final Long timestamp,
final String groupId,
final List<CommitDetail> details,
final RevisionBranchPoint mergeSource,
Expand Down Expand Up @@ -281,7 +281,7 @@ public String getComment() {
return comment;
}

public long getTimestamp() {
public Long getTimestamp() {
return timestamp;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2020 B2i Healthcare Pte Ltd, http://b2i.sg
* Copyright 2018-2023 B2i Healthcare Pte Ltd, http://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.ComparisonChain;
import com.google.common.net.InetAddresses;

/**
* @since 6.5
Expand Down Expand Up @@ -90,22 +91,34 @@ public RevisionBranchPoint atFuture() {

@JsonCreator
public static RevisionBranchPoint valueOf(String value) {
String[] addrArray = value.split(":");//a IPv6 adress is of form 2607:f0d0:1002:0051:0000:0000:0000:0004
long[] num = new long[addrArray.length];

for (int i=0; i<addrArray.length; i++) {
num[i] = Long.parseLong(addrArray[i], 16);
}
long branchId = num[0];
for (int i=1;i<4;i++) {
branchId = (branchId<<16) + num[i];
}
long timestamp = num[4];
for (int i=5;i<8;i++) {
timestamp = (timestamp<<16) + num[i];
}

return new RevisionBranchPoint(branchId, timestamp);
if (value == null) return null;

try {
// quick hack to parse short form of IPv6 addresses by passing it through the java InetAddress first then back to String and parsing the long values
if (value.contains("::")) {
return valueOf(InetAddresses.forString(value).getHostAddress());
}

// a IPv6 adress is of form 2607:f0d0:1002:0051:0000:0000:0000:0004
String[] addrArray = value.split(":");
long[] num = new long[addrArray.length];

for (int i=0; i<addrArray.length; i++) {
num[i] = Long.parseLong(addrArray[i], 16);
}
long branchId = num[0];
for (int i=1;i<4;i++) {
branchId = (branchId<<16) + num[i];
}
long timestamp = num[4];
for (int i=5;i<8;i++) {
timestamp = (timestamp<<16) + num[i];
}

return new RevisionBranchPoint(branchId, timestamp);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(String.format("Couldn't convert IPv6 value '%s' to RevisionBranchPoint instance", value), e);
}
}

@Override
Expand Down

0 comments on commit 9a879b9

Please sign in to comment.