Skip to content

Commit 0d0c163

Browse files
committed
Fix a bug in shrinking logic
1 parent bd9666a commit 0d0c163

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/main/java/io/timeandspace/smoothie/SmoothieMapStats.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ final class SmoothieMapStats {
3737
@Nullable OrdinarySegmentStats @Nullable [][] ordinarySegmentStatsPerOrderAndNumFullSlots =
3838
new OrdinarySegmentStats[MAX_SEGMENTS_ARRAY_ORDER + 1][];
3939

40+
int getNumAggregatedSegments() {
41+
return numAggregatedSegments;
42+
}
43+
4044
int getNumInflatedSegments() {
4145
return numInflatedSegments;
4246
}

src/main/javaTemplates/io/timeandspace/smoothie/SmoothieMap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4507,6 +4507,7 @@ private void doShrinkInto(Segment<K, V> fromSegment, final long fromSegment_bitS
45074507
", intoSegment_allocCapacity: " + intoSegment_allocCapacity);
45084508
}
45094509

4510+
this.modCount++;
45104511
byte tag = (byte) tagBits(hash);
45114512
writeEntry(intoSegment,
45124513
/* if Interleaved segments Supported intermediateSegments */
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) The SmoothieMap Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.timeandspace.smoothie;
18+
19+
import org.junit.Assert;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Random;
25+
26+
public class ShrinkTest {
27+
@Test
28+
public void testShrink() {
29+
final SmoothieMap<Integer, Integer> map = SmoothieMap
30+
.<Integer, Integer>newBuilder().doShrink(true).build();
31+
32+
Random r = new Random(1);
33+
List<Integer> keys = new ArrayList<>();
34+
for (int i = 0; i < 10_000; i++) {
35+
int key = r.nextInt();
36+
keys.add(key);
37+
map.put(key, key);
38+
}
39+
40+
for (Integer key : keys) {
41+
map.remove(key);
42+
}
43+
final SmoothieMapStats stats = new SmoothieMapStats();
44+
map.aggregateStats(stats);
45+
Assert.assertEquals(1, stats.getNumAggregatedSegments());
46+
}
47+
}

0 commit comments

Comments
 (0)