Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
askwang committed Aug 23, 2024
1 parent 3848268 commit 78b9cff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ public static List<Snapshot> findOverlappedSnapshots(
List<Snapshot> overlappedSnapshots = new ArrayList<>();
int right = findPreviousSnapshot(sortedSnapshots, endExclusive);
if (right >= 0) {
int left = Math.max(findPreviousOrEqualSnapshot(sortedSnapshots, beginInclusive), 0);
int left = Math.max(findNextOrEqualTag(sortedSnapshots, beginInclusive), 0);
for (int i = left; i <= right; i++) {
overlappedSnapshots.add(sortedSnapshots.get(i));
}
Expand All @@ -682,10 +682,10 @@ public static int findPreviousSnapshot(List<Snapshot> sortedSnapshots, long targ
return -1;
}

private static int findPreviousOrEqualSnapshot(
List<Snapshot> sortedSnapshots, long targetSnapshotId) {
for (int i = sortedSnapshots.size() - 1; i >= 0; i--) {
if (sortedSnapshots.get(i).id() <= targetSnapshotId) {
private static int findNextOrEqualTag(
List<Snapshot> taggedSnapshots, long targetSnapshotId) {
for (int i = 0; i < taggedSnapshots.size(); i++) {
if (taggedSnapshots.get(i).id() >= targetSnapshotId) {
return i;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void testlaterOrEqualWatermark() throws IOException {
assertThat(snapshotManager.laterOrEqualWatermark(millis + 999)).isNull();
}

public Snapshot createSnapshotWithMillis(long id, long millis) {
private Snapshot createSnapshotWithMillis(long id, long millis) {
return new Snapshot(
id,
0L,
Expand Down Expand Up @@ -366,4 +366,23 @@ public void testLongLivedChangelog() throws Exception {
Assertions.assertThat(snapshotManager.latestSnapshotId()).isEqualTo(10);
Assertions.assertThat(snapshotManager.changelog(1)).isNotNull();
}

@Test
public void testOverlappedSnapshots() {
SnapshotManagerTest snapshotManagerTest = new SnapshotManagerTest();
List<Snapshot> taggedSnapshot = new ArrayList<>();
long millis = System.currentTimeMillis();
long[] snapshotId = new long[] {8, 9, 11, 12, 15};
for (long id : snapshotId) {
taggedSnapshot.add(snapshotManagerTest.createSnapshotWithMillis(id, millis));
}

int beginInclusive = 10, endExclusive = 15;
// overlapped snapshot is [11,12]
List<Snapshot> overlappedSnapshots =
SnapshotManager.findOverlappedSnapshots(taggedSnapshot, beginInclusive, endExclusive);
List<Long> overlappedIds =
overlappedSnapshots.stream().map(Snapshot::id).collect(Collectors.toList());
assertThat(overlappedIds).containsExactly(11L, 12L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,6 @@ public void testCreateTagWithTimeRetained() throws Exception {
assertThat(tags.get(0).getValue()).contains("tag");
}

@Test
public void testOverlappedSnapshots() {
SnapshotManagerTest snapshotManagerTest = new SnapshotManagerTest();
List<Snapshot> taggedSnapshot = new ArrayList<>();
long millis = System.currentTimeMillis();
long[] snapshotId = new long[] {7, 9, 11, 12};
for (long id : snapshotId) {
taggedSnapshot.add(snapshotManagerTest.createSnapshotWithMillis(id, millis));
}

int beginInclusive = 10, endExclusive = 15;
// overlapped snapshot is [11,12]
List<Snapshot> overlappedSnapshots =
TagManager.findOverlappedSnapshots(taggedSnapshot, beginInclusive, endExclusive);
List<Long> overlappedIds =
overlappedSnapshots.stream().map(Snapshot::id).collect(Collectors.toList());
assertThat(overlappedIds).containsExactly(11L, 12L);
}

private TestFileStore createStore(TestKeyValueGenerator.GeneratorMode mode, int buckets)
throws Exception {
ThreadLocalRandom random = ThreadLocalRandom.current();
Expand Down

0 comments on commit 78b9cff

Please sign in to comment.