Skip to content

Commit e8afbe7

Browse files
committed
BelongsTo
1 parent 5042717 commit e8afbe7

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheDirectory.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ public void close() throws IOException {
6060
super.close();
6161
// Ideally we could let the cache evict/remove cached files by itself after the
6262
// directory has been closed.
63-
cacheService.removeFromCache(key ->
64-
Objects.equals(key.getSnapshotId(), snapshotId) &&
65-
Objects.equals(key.getIndexId(), indexId) &&
66-
Objects.equals(key.getShardId(), shardId));
63+
cacheService.removeFromCache(cacheKey -> cacheKey.belongsTo(snapshotId, indexId, shardId));
6764
}
6865

6966
@Override

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheKey.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@ public String toString() {
7070
", fileName='" + fileName + '\'' +
7171
']';
7272
}
73+
74+
boolean belongsTo(SnapshotId snapshotId, IndexId indexId, ShardId shardId) {
75+
return Objects.equals(this.snapshotId, snapshotId)
76+
&& Objects.equals(this.indexId, indexId)
77+
&& Objects.equals(this.shardId, shardId);
78+
}
7379
}

x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/cache/CacheKeyTests.java

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,59 @@
1212
import org.elasticsearch.test.ESTestCase;
1313
import org.elasticsearch.test.EqualsHashCodeTestUtils;
1414

15+
import static org.hamcrest.Matchers.equalTo;
16+
1517
public class CacheKeyTests extends ESTestCase {
1618

1719
public void testEqualsAndHashCode() {
1820
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createInstance(), this::copy, this::mutate);
1921
}
2022

23+
public void testBelongsTo() {
24+
final CacheKey cacheKey = createInstance();
25+
26+
SnapshotId snapshotId = cacheKey.getSnapshotId();
27+
IndexId indexId = cacheKey.getIndexId();
28+
ShardId shardId = cacheKey.getShardId();
29+
30+
final boolean belongsTo;
31+
switch (randomInt(2)) {
32+
case 0:
33+
snapshotId = randomValueOtherThan(cacheKey.getSnapshotId(), this::randomSnapshotId);
34+
belongsTo = false;
35+
break;
36+
case 1:
37+
indexId = randomValueOtherThan(cacheKey.getIndexId(), this::randomIndexId);
38+
belongsTo = false;
39+
break;
40+
case 2:
41+
shardId = randomValueOtherThan(cacheKey.getShardId(), this::randomShardId);
42+
belongsTo = false;
43+
break;
44+
case 3:
45+
belongsTo = true;
46+
break;
47+
default:
48+
throw new AssertionError("Unsupported value");
49+
}
50+
51+
assertThat(cacheKey.belongsTo(snapshotId, indexId, shardId), equalTo(belongsTo));
52+
}
53+
54+
private SnapshotId randomSnapshotId() {
55+
return new SnapshotId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
56+
}
57+
58+
private IndexId randomIndexId() {
59+
return new IndexId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
60+
}
61+
62+
private ShardId randomShardId() {
63+
return new ShardId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10), randomInt(5));
64+
}
65+
2166
private CacheKey createInstance() {
22-
return new CacheKey(
23-
new SnapshotId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10)),
24-
new IndexId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10)),
25-
new ShardId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10), randomInt(5)),
26-
randomAlphaOfLengthBetween(5, 10)
27-
);
67+
return new CacheKey(randomSnapshotId(), randomIndexId(), randomShardId(), randomAlphaOfLengthBetween(5, 10));
2868
}
2969

3070
private CacheKey copy(final CacheKey origin) {
@@ -51,21 +91,20 @@ private CacheKey mutate(CacheKey origin) {
5191

5292
switch (randomInt(3)) {
5393
case 0:
54-
snapshotId = new SnapshotId(randomAlphaOfLength(4), randomAlphaOfLength(4));
94+
snapshotId = randomValueOtherThan(origin.getSnapshotId(), this::randomSnapshotId);
5595
break;
5696
case 1:
57-
indexId = new IndexId(randomAlphaOfLength(4), randomAlphaOfLength(4));
97+
indexId = randomValueOtherThan(origin.getIndexId(), this::randomIndexId);
5898
break;
5999
case 2:
60-
shardId = new ShardId(randomAlphaOfLength(4), randomAlphaOfLength(4), randomIntBetween(6, 10));
100+
shardId = randomValueOtherThan(origin.getShardId(), this::randomShardId);
61101
break;
62102
case 3:
63-
fileName = randomAlphaOfLength(15);
103+
fileName = randomValueOtherThan(origin.getFileName(), () -> randomAlphaOfLengthBetween(5, 10));
64104
break;
65105
default:
66106
throw new AssertionError("Unsupported mutation");
67107
}
68108
return new CacheKey(snapshotId, indexId, shardId, fileName);
69109
}
70-
71110
}

0 commit comments

Comments
 (0)