This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
InMemoryWindowedStorage.java
102 lines (89 loc) · 2.63 KB
/
InMemoryWindowedStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.apex.malhar.lib.window.impl;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import org.apache.apex.malhar.lib.state.spillable.Spillable;
import org.apache.apex.malhar.lib.window.Window;
import org.apache.apex.malhar.lib.window.WindowedStorage;
import org.apache.hadoop.classification.InterfaceStability;
import com.datatorrent.api.Context;
/**
* This is the in-memory implementation of {@link WindowedPlainStorage}. Do not use this class if you have a large state that
* can't be fit in memory. Also, this class may go away soon as there are plans to incorporate {@link Spillable} data
* structures in the near future.
*
* @since 3.5.0
*/
@InterfaceStability.Unstable
public class InMemoryWindowedStorage<T> implements WindowedStorage.WindowedPlainStorage<T>
{
protected final TreeMap<Window, T> map = new TreeMap<>();
@Override
public long size()
{
return map.size();
}
@Override
public void put(Window window, T value)
{
map.put(window, value);
}
@Override
public boolean containsWindow(Window window)
{
return map.containsKey(window);
}
@Override
public T get(Window window)
{
return map.get(window);
}
@Override
public void remove(Window window)
{
map.remove(window);
}
@Override
public Iterable<Map.Entry<Window, T>> entries()
{
return map.entrySet();
}
@Override
public void setup(Context.OperatorContext context)
{
}
@Override
public void teardown()
{
}
@Override
public void purge(long horizonMillis)
{
for (Iterator<Map.Entry<Window, T>> iterator = map.entrySet().iterator(); iterator.hasNext(); ) {
Window window = iterator.next().getKey();
if (window.getBeginTimestamp() + window.getDurationMillis() < horizonMillis) {
iterator.remove();
} else {
break;
}
}
}
}