Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ public int compare(TimeWindow o1, TimeWindow o2) {
* @return window start
*/
public static long getWindowStartWithOffset(long timestamp, long offset, long windowSize) {
return timestamp - (timestamp - offset + windowSize) % windowSize;
final long remainder = (timestamp - offset) % windowSize;
// handle both positive and negative cases
if (remainder < 0) {
return timestamp - (remainder + windowSize);
} else {
return timestamp - remainder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,38 @@
public class TimeWindowTest {
@Test
public void testGetWindowStartWithOffset() {
// [0, 7), [7, 14), [14, 21)...
// [-21, -14), [-14, -7), [-7, 0), [0, 7), [7, 14), [14, 21)...
long offset = 0;
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-8, offset, 7), -14);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-7, offset, 7), -7);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-6, offset, 7), -7);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-1, offset, 7), -7);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(1, offset, 7), 0);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(6, offset, 7), 0);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(7, offset, 7), 7);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(8, offset, 7), 7);

// [-4, 3), [3, 10), [10, 17)...
// [-11, -4), [-4, 3), [3, 10), [10, 17)...
offset = 3;
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-10, offset, 7), -11);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-9, offset, 7), -11);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-3, offset, 7), -4);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-2, offset, 7), -4);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-1, offset, 7), -4);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(1, offset, 7), -4);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(2, offset, 7), -4);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(3, offset, 7), 3);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(9, offset, 7), 3);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(10, offset, 7), 10);

// [-2, 5), [5, 12), [12, 19)...
// [-16, -9), [-9, -2), [-2, 5), [5, 12), [12, 19)...
offset = -2;
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-12, offset, 7), -16);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-7, offset, 7), -9);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-4, offset, 7), -9);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-3, offset, 7), -9);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(2, offset, 7), -2);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-1, offset, 7), -2);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(1, offset, 7), -2);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-2, offset, 7), -2);
Assert.assertEquals(TimeWindow.getWindowStartWithOffset(3, offset, 7), -2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ public TimeWindowSerializerSnapshot() {
* @return window start
*/
public static long getWindowStartWithOffset(long timestamp, long offset, long windowSize) {
return timestamp - (timestamp - offset + windowSize) % windowSize;
final long remainder = (timestamp - offset) % windowSize;
// handle both positive and negative cases
if (remainder < 0) {
return timestamp - (remainder + windowSize);
} else {
return timestamp - remainder;
}
}

public static TimeWindow of(long start, long end) {
Expand Down