Skip to content

Commit 9fde60b

Browse files
add bulk getTotalCount and mergeWith to PaginatedStore
1 parent 9eb8db7 commit 9fde60b

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/main/java/com/datadoghq/sketch/ddsketch/store/PaginatedStore.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ public int getMaxIndex() {
8282
throw new NoSuchElementException();
8383
}
8484

85+
@Override
86+
public double getTotalCount() {
87+
if (isEmpty()) {
88+
return 0D;
89+
}
90+
double total = 0D;
91+
for (double[] page : pages) {
92+
if (null != page) {
93+
for (double count : page) {
94+
total += count;
95+
}
96+
}
97+
}
98+
return total;
99+
}
100+
85101
@Override
86102
public void add(int index, double count) {
87103
if (count > 0) {
@@ -147,6 +163,84 @@ private void extendTo(int pageIndex) {
147163
this.pages = Arrays.copyOf(pages, pages.length + aligned(pageIndex - minPageIndex + 1));
148164
}
149165

166+
@Override
167+
public void mergeWith(Store store) {
168+
if (store.isEmpty()) {
169+
return;
170+
}
171+
if (store instanceof PaginatedStore) {
172+
mergeWith((PaginatedStore) store);
173+
} else {
174+
store.getStream().forEach(this::add);
175+
}
176+
}
177+
178+
private void mergeWith(PaginatedStore store) {
179+
if (isEmpty()) {
180+
this.pages = deepCopy(store.pages);
181+
this.minPageIndex = store.minPageIndex;
182+
} else {
183+
int min = minPageIndex;
184+
int max = minPageIndex + pages.length;
185+
int storeMin = store.minPageIndex;
186+
int storeMax = store.minPageIndex + store.pages.length;
187+
if (max < storeMin) {
188+
extendTo(storeMax);
189+
for (int i = 0; i < store.pages.length; ++i) {
190+
double[] page = store.pages[i];
191+
if (null != page) {
192+
pages[i + storeMin - min] = Arrays.copyOf(page, page.length);
193+
}
194+
}
195+
} else if (min > storeMax) {
196+
shiftPagesRight(storeMin);
197+
for (int i = 0; i < store.pages.length; ++i) {
198+
double[] page = store.pages[i];
199+
if (null != page) {
200+
pages[i] = Arrays.copyOf(page, page.length);
201+
}
202+
}
203+
} else if (min < storeMin) {
204+
if (storeMax > max) {
205+
extendTo(storeMax);
206+
}
207+
for (int i = 0; i < store.pages.length; ++i) {
208+
double[] page = store.pages[i];
209+
if (null != page) {
210+
double[] target = pages[i + storeMin - min];
211+
if (null == target) {
212+
pages[i + storeMin - min] = Arrays.copyOf(page, page.length);
213+
} else {
214+
for (int j = 0; j < page.length; ++j) {
215+
target[j] += page[j];
216+
}
217+
}
218+
}
219+
}
220+
} else {
221+
if (min > storeMin) {
222+
shiftPagesRight(storeMin);
223+
}
224+
if (storeMax > max) {
225+
extendTo(storeMax);
226+
}
227+
for (int i = 0; i < store.pages.length; ++i) {
228+
double[] page = store.pages[i];
229+
if (null != page) {
230+
double[] target = pages[i];
231+
if (null == target) {
232+
pages[i] = Arrays.copyOf(page, page.length);
233+
} else {
234+
for (int j = 0; j < page.length; ++j) {
235+
target[j] += page[j];
236+
}
237+
}
238+
}
239+
}
240+
}
241+
}
242+
}
243+
150244
@Override
151245
public Store copy() {
152246
return new PaginatedStore(this);

src/test/java/com/datadoghq/sketch/ddsketch/store/PaginatedStoreTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.datadoghq.sketch.ddsketch.store;
22

3-
import org.junit.jupiter.api.Test;
43
import org.junit.jupiter.params.ParameterizedTest;
54
import org.junit.jupiter.params.provider.Arguments;
65
import org.junit.jupiter.params.provider.MethodSource;

src/test/java/com/datadoghq/sketch/ddsketch/store/StoreTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ void testMergingFarApart() {
219219
testMerging(new int[]{ -10000 }, new int[]{ 10000 });
220220
testMerging(new int[]{ 10000 }, new int[]{ -10000 });
221221
testMerging(new int[]{ 10000 }, new int[]{ -10000 }, new int[]{ 0 });
222+
testMerging(new int[]{ -10000, 10000 }, new int[]{ -5000, 5000 });
223+
testMerging(new int[]{ -5000, 5000 }, new int[]{ -10000, 10000 });
224+
testMerging(new int[]{ -5000, 10000 }, new int[]{ -10000, 5000 });
222225
testMerging(new int[]{ 10000, 0 }, new int[]{ -10000 }, new int[]{ 0 });
223226
}
224227

0 commit comments

Comments
 (0)