-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chen Dai <[email protected]>
- Loading branch information
Showing
5 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...n/java/org/opensearch/sql/planner/streaming/windowing/assigner/SlidingWindowAssigner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.planner.streaming.windowing.assigner; | ||
|
||
import com.google.common.base.Preconditions; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.opensearch.sql.planner.streaming.windowing.Window; | ||
|
||
/** | ||
* A sliding window assigner assigns multiple overlapped window per event timestamp. | ||
* The overlap size is determined by the given slide interval. | ||
*/ | ||
public class SlidingWindowAssigner implements WindowAssigner { | ||
|
||
/** Window size in millisecond. */ | ||
private final long windowSize; | ||
|
||
/** Slide size in millisecond. */ | ||
private final long slideSize; | ||
|
||
/** | ||
* Create sliding window assigner with the given window and slide size. | ||
*/ | ||
public SlidingWindowAssigner(long windowSize, long slideSize) { | ||
Preconditions.checkArgument(windowSize > 0, | ||
"Window size [%s] must be positive number", windowSize); | ||
Preconditions.checkArgument(slideSize > 0, | ||
"Slide size [%s] must be positive number", slideSize); | ||
this.windowSize = windowSize; | ||
this.slideSize = slideSize; | ||
} | ||
|
||
@Override | ||
public List<Window> assign(long timestamp) { | ||
List<Window> windows = new ArrayList<>(); | ||
|
||
// Assign window from the last start time to first until given timestamp outside current window | ||
long startTime = timestamp - timestamp % slideSize; | ||
for (Window win = window(startTime); win.maxTimestamp() >= timestamp; win = window(startTime)) { | ||
windows.add(win); | ||
startTime -= slideSize; | ||
} | ||
|
||
// Reverse the window list for easy read and test | ||
Collections.reverse(windows); | ||
return windows; | ||
} | ||
|
||
private Window window(long startTime) { | ||
return new Window(startTime, startTime + windowSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...va/org/opensearch/sql/planner/streaming/windowing/assigner/SlidingWindowAssignerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.planner.streaming.windowing.assigner; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.sql.planner.streaming.windowing.Window; | ||
|
||
class SlidingWindowAssignerTest { | ||
|
||
@Test | ||
void testAssignWindows() { | ||
long windowSize = 1000; | ||
long slideSize = 500; | ||
SlidingWindowAssigner assigner = new SlidingWindowAssigner(windowSize, slideSize); | ||
|
||
assertEquals( | ||
List.of( | ||
new Window(0, 1000), | ||
new Window(500, 1500)), | ||
assigner.assign(500)); | ||
|
||
assertEquals( | ||
List.of( | ||
new Window(0, 1000), | ||
new Window(500, 1500)), | ||
assigner.assign(999)); | ||
|
||
assertEquals( | ||
List.of( | ||
new Window(500, 1500), | ||
new Window(1000, 2000)), | ||
assigner.assign(1000)); | ||
} | ||
|
||
@Test | ||
void testConstructWithIllegalArguments() { | ||
IllegalArgumentException error1 = assertThrows(IllegalArgumentException.class, | ||
() -> new SlidingWindowAssigner(-1, 100)); | ||
assertEquals("Window size [-1] must be positive number", error1.getMessage()); | ||
|
||
IllegalArgumentException error2 = assertThrows(IllegalArgumentException.class, | ||
() -> new SlidingWindowAssigner(1000, 0)); | ||
assertEquals("Slide size [0] must be positive number", error2.getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters