You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apex.apache.org by da...@apache.org on 2016/08/15 19:55:01 UTC
[1/3] apex-malhar git commit: APEXMALHAR-2048 Added implementations
of SpillableList, SpillableMap, and SpillableArrayListMultimap
Repository: apex-malhar
Updated Branches:
refs/heads/master d1fb2b604 -> 9b6e11d85
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemorySpillableStateStoreTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemorySpillableStateStoreTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemorySpillableStateStoreTest.java
new file mode 100644
index 0000000..769bc0d
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemorySpillableStateStoreTest.java
@@ -0,0 +1,60 @@
+/**
+ * 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.state.spillable.inmem;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.datatorrent.lib.util.TestUtils;
+
+public class InMemorySpillableStateStoreTest
+{
+ @Test
+ public void simpleStoreTest()
+ {
+ InMemSpillableStateStore store = new InMemSpillableStateStore();
+
+ store.setup(null);
+
+ long windowId = 0L;
+ store.beginWindow(windowId);
+ windowId++;
+
+ Assert.assertEquals(null, store.getSync(0L, TestUtils.getSlice(1)));
+
+ store.put(0L, TestUtils.getSlice(1), TestUtils.getSlice(2));
+ store.put(0L, TestUtils.getSlice(3), TestUtils.getSlice(10));
+ store.put(1L, TestUtils.getSlice(2), TestUtils.getSlice(3));
+ store.put(1L, TestUtils.getSlice(4), TestUtils.getSlice(11));
+
+ Assert.assertEquals(null, store.getSync(0L, TestUtils.getSlice(2)));
+ Assert.assertEquals(null, store.getSync(0L, TestUtils.getSlice(4)));
+ Assert.assertEquals(TestUtils.getSlice(2), store.getSync(0L, TestUtils.getSlice(1)));
+ Assert.assertEquals(TestUtils.getSlice(10), store.getSync(0L, TestUtils.getSlice(3)));
+
+ Assert.assertEquals(null, store.getSync(1L, TestUtils.getSlice(1)));
+ Assert.assertEquals(null, store.getSync(1L, TestUtils.getSlice(3)));
+ Assert.assertEquals(TestUtils.getSlice(3), store.getSync(1L, TestUtils.getSlice(2)));
+ Assert.assertEquals(TestUtils.getSlice(11), store.getSync(1L, TestUtils.getSlice(4)));
+
+ store.endWindow();
+
+ store.teardown();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/utils/serde/SerdeListSliceTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/utils/serde/SerdeListSliceTest.java b/library/src/test/java/org/apache/apex/malhar/lib/utils/serde/SerdeListSliceTest.java
new file mode 100644
index 0000000..f7753d2
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/utils/serde/SerdeListSliceTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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.utils.serde;
+
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+
+import com.datatorrent.netlet.util.Slice;
+
+public class SerdeListSliceTest
+{
+ @Test
+ public void simpleSerdeTest()
+ {
+ SerdeListSlice<String> serdeList = new SerdeListSlice<String>(new SerdeStringSlice());
+
+ List<String> stringList = Lists.newArrayList("a", "b", "c");
+
+ Slice slice = serdeList.serialize(stringList);
+
+ List<String> deserializedList = serdeList.deserialize(slice);
+
+ Assert.assertEquals(stringList, deserializedList);
+ }
+}
[3/3] apex-malhar git commit: APEXMALHAR-2048 Added implementations
of SpillableList, SpillableMap, and SpillableArrayListMultimap
Posted by da...@apache.org.
APEXMALHAR-2048 Added implementations of SpillableList, SpillableMap, and SpillableArrayListMultimap
Project: http://git-wip-us.apache.org/repos/asf/apex-malhar/repo
Commit: http://git-wip-us.apache.org/repos/asf/apex-malhar/commit/9b6e11d8
Tree: http://git-wip-us.apache.org/repos/asf/apex-malhar/tree/9b6e11d8
Diff: http://git-wip-us.apache.org/repos/asf/apex-malhar/diff/9b6e11d8
Branch: refs/heads/master
Commit: 9b6e11d85accc88faa08d7b4a8daeb9b069fc878
Parents: d1fb2b6
Author: Timothy Farkas <ti...@datatorrent.com>
Authored: Sun Jul 17 14:32:34 2016 -0700
Committer: David Yan <da...@datatorrent.com>
Committed: Mon Aug 15 12:15:21 2016 -0700
----------------------------------------------------------------------
.../SequentialSpillableIdentifierGenerator.java | 84 +++
.../malhar/lib/state/spillable/Spillable.java | 14 +-
.../state/spillable/SpillableArrayListImpl.java | 326 ++++++++++
.../SpillableByteArrayListMultimapImpl.java | 291 +++++++++
.../state/spillable/SpillableByteMapImpl.java | 235 ++++++++
.../spillable/SpillableComplexComponent.java | 4 +-
.../SpillableComplexComponentImpl.java | 193 ++++++
.../spillable/SpillableIdentifierGenerator.java | 41 ++
.../state/spillable/SpillableStateStore.java | 35 ++
.../state/spillable/TimeBasedPriorityQueue.java | 154 +++++
.../state/spillable/WindowBoundedMapCache.java | 129 ++++
.../lib/state/spillable/WindowListener.java | 42 ++
.../state/spillable/inmem/InMemMultiset.java | 161 -----
.../inmem/InMemSpillableArrayList.java | 175 ------
.../InMemSpillableByteArrayListMultimap.java | 154 -----
.../inmem/InMemSpillableComplexComponent.java | 117 ----
.../inmem/InMemSpillableStateStore.java | 118 ++++
.../ManagedStateSpillableStateStore.java | 34 ++
.../lib/utils/serde/PassThruByteArraySerde.java | 2 +
.../serde/PassThruByteArraySliceSerde.java | 59 ++
.../lib/utils/serde/PassThruSliceSerde.java | 50 ++
.../malhar/lib/utils/serde/SerdeIntSlice.java | 52 ++
.../malhar/lib/utils/serde/SerdeListSlice.java | 109 ++++
.../lib/utils/serde/SerdeStringSlice.java | 53 ++
.../apex/malhar/lib/utils/serde/SliceUtils.java | 101 ++++
.../com/datatorrent/lib/util/TestUtils.java | 25 +
.../state/managed/ManagedStateTestUtils.java | 13 +-
.../spillable/inmem/InMemMultisetTest.java | 44 --
.../inmem/InMemSpillableArrayListTest.java | 44 --
...InMemSpillableByteArrayListMultimapTest.java | 45 --
...uentialSpillableIdentifierGeneratorTest.java | 125 ++++
.../spillable/SpillableArrayListImplTest.java | 594 +++++++++++++++++++
.../SpillableByteArrayListMultimapImplTest.java | 341 +++++++++++
.../spillable/SpillableByteMapImplTest.java | 484 +++++++++++++++
.../SpillableComplexComponentImplTest.java | 63 ++
.../lib/state/spillable/SpillableTestUtils.java | 134 +++++
.../spillable/TimeBasedPriorityQueueTest.java | 134 +++++
.../spillable/WindowBoundedMapCacheTest.java | 116 ++++
.../inmem/InMemorySpillableStateStoreTest.java | 60 ++
.../lib/utils/serde/SerdeListSliceTest.java | 45 ++
40 files changed, 4240 insertions(+), 760 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SequentialSpillableIdentifierGenerator.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SequentialSpillableIdentifierGenerator.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SequentialSpillableIdentifierGenerator.java
new file mode 100644
index 0000000..600fa98
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SequentialSpillableIdentifierGenerator.java
@@ -0,0 +1,84 @@
+/**
+ * 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.state.spillable;
+
+import java.util.Set;
+
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
+
+/**
+ * This is an id generator that generates single byte ids for Spillable datastructures.
+ */
+@InterfaceStability.Evolving
+public class SequentialSpillableIdentifierGenerator implements SpillableIdentifierGenerator
+{
+ private boolean nextCalled = false;
+ private boolean done = false;
+ private byte currentIdentifier = 0;
+
+ private Set<Byte> registeredIdentifier = Sets.newHashSet();
+
+ @Override
+ public byte[] next()
+ {
+ Preconditions.checkState(!done);
+
+ nextCalled = true;
+
+ byte nextIndentifier = currentIdentifier;
+ seek();
+
+ return new byte[]{nextIndentifier};
+ }
+
+ @Override
+ public void register(byte[] identifierArray)
+ {
+ Preconditions.checkState(!nextCalled);
+ Preconditions.checkState(!done);
+ Preconditions.checkArgument(identifierArray.length == 1);
+
+ byte identifier = identifierArray[0];
+
+ Preconditions.checkState(identifier >= currentIdentifier &&
+ !registeredIdentifier.contains(identifier));
+
+ registeredIdentifier.add(identifier);
+
+ if (currentIdentifier == identifier) {
+ seek();
+ }
+ }
+
+ private void seek()
+ {
+ if (currentIdentifier == Byte.MAX_VALUE) {
+ done = true;
+ } else {
+ do {
+ currentIdentifier++;
+ } while (registeredIdentifier.contains(currentIdentifier) && currentIdentifier < Byte.MAX_VALUE);
+
+ done = currentIdentifier == Byte.MAX_VALUE && registeredIdentifier.contains(currentIdentifier);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/Spillable.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/Spillable.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/Spillable.java
index 41a0efc..4c9b997 100644
--- a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/Spillable.java
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/Spillable.java
@@ -98,19 +98,7 @@ public interface Spillable
* should implement this interface. A user working with an implementation of this interface needs
* to make sure that the {@link com.datatorrent.api.Operator} call-backs are propagated to it.
*/
- interface SpillableComponent extends Component<OperatorContext>, Spillable
+ interface SpillableComponent extends Component<OperatorContext>, Spillable, WindowListener
{
- /**
- * This signals that the parent {@link com.datatorrent.api.Operator}'s
- * {@link com.datatorrent.api.Operator#beginWindow(long)} method has been called.
- * @param windowId The next windowId of the parent operator.
- */
- void beginWindow(long windowId);
-
- /**
- * This signals that the parent {@link com.datatorrent.api.Operator}'s
- * {@link com.datatorrent.api.Operator#endWindow()} method has been called.
- */
- void endWindow();
}
}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableArrayListImpl.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableArrayListImpl.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableArrayListImpl.java
new file mode 100644
index 0000000..5d46906
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableArrayListImpl.java
@@ -0,0 +1,326 @@
+/**
+ * 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.state.spillable;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+import javax.validation.constraints.NotNull;
+
+import org.apache.apex.malhar.lib.utils.serde.Serde;
+import org.apache.apex.malhar.lib.utils.serde.SerdeIntSlice;
+import org.apache.apex.malhar.lib.utils.serde.SerdeListSlice;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.esotericsoftware.kryo.DefaultSerializer;
+import com.esotericsoftware.kryo.serializers.FieldSerializer;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+
+import com.datatorrent.api.Context;
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * A Spillable implementation of {@link List} backed by a {@link SpillableStateStore}.
+ * @param <T> The type of object stored in the {@link SpillableArrayListImpl}.
+ */
+@DefaultSerializer(FieldSerializer.class)
+@InterfaceStability.Evolving
+public class SpillableArrayListImpl<T> implements Spillable.SpillableArrayList<T>, Spillable.SpillableComponent
+{
+ public static final int DEFAULT_BATCH_SIZE = 1000;
+
+ private int batchSize = DEFAULT_BATCH_SIZE;
+ private long bucketId;
+ private byte[] prefix;
+
+ @NotNull
+ private SpillableStateStore store;
+ @NotNull
+ private Serde<T, Slice> serde;
+ @NotNull
+ private SpillableByteMapImpl<Integer, List<T>> map;
+
+ private boolean sizeCached = false;
+ private int size;
+ private int numBatches;
+
+ private SpillableArrayListImpl()
+ {
+ //for kryo
+ }
+
+ public SpillableStateStore getStore()
+ {
+ return store;
+ }
+
+ /**
+ * Creates a {@link SpillableArrayListImpl}.
+ * @param bucketId The Id of the bucket used to store this
+ * {@link SpillableArrayListImpl} in the provided {@link SpillableStateStore}.
+ * @param prefix The Id of this {@link SpillableArrayListImpl}.
+ * @param store The {@link SpillableStateStore} in which to spill to.
+ * @param serde The {@link Serde} to use when serializing and deserializing data.
+ */
+ public SpillableArrayListImpl(long bucketId, @NotNull byte[] prefix,
+ @NotNull SpillableStateStore store,
+ @NotNull Serde<T, Slice> serde)
+ {
+ this.bucketId = bucketId;
+ this.prefix = Preconditions.checkNotNull(prefix);
+ this.store = Preconditions.checkNotNull(store);
+ this.serde = Preconditions.checkNotNull(serde);
+
+ map = new SpillableByteMapImpl<>(store, prefix, bucketId, new SerdeIntSlice(), new SerdeListSlice(serde));
+ }
+
+ /**
+ * Creates a {@link SpillableArrayListImpl}.
+ * @param bucketId The Id of the bucket used to store this
+ * {@link SpillableArrayListImpl} in the provided {@link SpillableStateStore}.
+ * @param prefix The Id of this {@link SpillableArrayListImpl}.
+ * @param store The {@link SpillableStateStore} in which to spill to.
+ * @param serde The {@link Serde} to use when serializing and deserializing data.
+ * @param batchSize When spilled to a {@link SpillableStateStore} data is stored in a batch. This determines the
+ * number of elements a batch will contain when it's spilled. Having small batches will increase
+ * the number of keys stored by your {@link SpillableStateStore} but will improve random reads and
+ * writes. Increasing the batch size will improve sequential read and write speed.
+ */
+ public SpillableArrayListImpl(long bucketId, @NotNull byte[] prefix,
+ @NotNull SpillableStateStore store,
+ @NotNull Serde<T, Slice> serde,
+ int batchSize)
+ {
+ this(bucketId, prefix, store, serde);
+
+ Preconditions.checkArgument(this.batchSize > 0);
+ this.batchSize = batchSize;
+ }
+
+ public void setSize(int size)
+ {
+ Preconditions.checkArgument(size >= 0);
+ this.size = size;
+ }
+
+ @Override
+ public int size()
+ {
+ return size;
+ }
+
+ @Override
+ public boolean isEmpty()
+ {
+ return size == 0;
+ }
+
+ @Override
+ public boolean contains(Object o)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Iterator<T> iterator()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Object[] toArray()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <T1> T1[] toArray(T1[] t1s)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean add(T t)
+ {
+ Preconditions.checkArgument((size() + 1) > 0);
+
+ int batchIndex = (size / batchSize);
+
+ List<T> batch = null;
+
+ if (batchIndex == numBatches) {
+ batch = Lists.newArrayListWithCapacity(batchSize);
+ numBatches++;
+ } else {
+ batch = map.get(batchIndex);
+ }
+
+ batch.add(t);
+
+ size++;
+ map.put(batchIndex, batch);
+ return true;
+ }
+
+ @Override
+ public boolean remove(Object o)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> collection)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends T> collection)
+ {
+ for (T element: collection) {
+ add(element);
+ }
+
+ return true;
+ }
+
+ @Override
+ public boolean addAll(int i, Collection<? extends T> collection)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> collection)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> collection)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void clear()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public T get(int i)
+ {
+ if (!(i < size)) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ int batchIndex = i / batchSize;
+ int batchOffset = i % batchSize;
+
+ List<T> batch = map.get(batchIndex);
+ return batch.get(batchOffset);
+ }
+
+ @Override
+ public T set(int i, T t)
+ {
+ if (!(i < size)) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ int batchIndex = i / batchSize;
+ int batchOffset = i % batchSize;
+
+ List<T> batch = map.get(batchIndex);
+ T old = batch.get(batchOffset);
+ batch.set(batchOffset, t);
+ map.put(batchIndex, batch);
+ return old;
+ }
+
+ @Override
+ public void add(int i, T t)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public T remove(int i)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int indexOf(Object o)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int lastIndexOf(Object o)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public ListIterator<T> listIterator()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public ListIterator<T> listIterator(int i)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<T> subList(int i, int i1)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setup(Context.OperatorContext context)
+ {
+ map.setup(context);
+ }
+
+ @Override
+ public void beginWindow(long windowId)
+ {
+ map.beginWindow(windowId);
+ }
+
+ @Override
+ public void endWindow()
+ {
+ map.endWindow();
+ }
+
+ @Override
+ public void teardown()
+ {
+ map.teardown();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteArrayListMultimapImpl.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteArrayListMultimapImpl.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteArrayListMultimapImpl.java
new file mode 100644
index 0000000..ba0bb77
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteArrayListMultimapImpl.java
@@ -0,0 +1,291 @@
+/**
+ * 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.state.spillable;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.Nullable;
+import javax.validation.constraints.NotNull;
+
+import org.apache.apex.malhar.lib.utils.serde.PassThruByteArraySliceSerde;
+import org.apache.apex.malhar.lib.utils.serde.Serde;
+import org.apache.apex.malhar.lib.utils.serde.SerdeIntSlice;
+import org.apache.apex.malhar.lib.utils.serde.SliceUtils;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.esotericsoftware.kryo.DefaultSerializer;
+import com.esotericsoftware.kryo.serializers.FieldSerializer;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multiset;
+
+import com.datatorrent.api.Context;
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * This is an implementation of Guava's ListMultimap which spills data to a {@link SpillableStateStore}.
+ */
+@DefaultSerializer(FieldSerializer.class)
+@InterfaceStability.Evolving
+public class SpillableByteArrayListMultimapImpl<K, V> implements Spillable.SpillableByteArrayListMultimap<K, V>,
+ Spillable.SpillableComponent
+{
+ public static final int DEFAULT_BATCH_SIZE = 1000;
+ public static final byte[] SIZE_KEY_SUFFIX = new byte[]{(byte)0, (byte)0, (byte)0};
+
+ private transient WindowBoundedMapCache<K, SpillableArrayListImpl<V>> cache = new WindowBoundedMapCache<>();
+ private transient boolean isRunning = false;
+ private transient boolean isInWindow = false;
+
+ private int batchSize = DEFAULT_BATCH_SIZE;
+ @NotNull
+ private SpillableByteMapImpl<byte[], Integer> map;
+ private SpillableStateStore store;
+ private byte[] identifier;
+ private long bucket;
+ private Serde<K, Slice> serdeKey;
+ private Serde<V, Slice> serdeValue;
+
+ private SpillableByteArrayListMultimapImpl()
+ {
+ // for kryo
+ }
+
+ /**
+ * Creates a {@link SpillableByteArrayListMultimapImpl}.
+ * @param store The {@link SpillableStateStore} in which to spill to.
+ * @param identifier The Id of this {@link SpillableByteArrayListMultimapImpl}.
+ * @param bucket The Id of the bucket used to store this
+ * {@link SpillableByteArrayListMultimapImpl} in the provided {@link SpillableStateStore}.
+ * @param serdeKey The {@link Serde} to use when serializing and deserializing keys.
+ * @param serdeKey The {@link Serde} to use when serializing and deserializing values.
+ */
+ public SpillableByteArrayListMultimapImpl(SpillableStateStore store, byte[] identifier, long bucket,
+ Serde<K, Slice> serdeKey,
+ Serde<V, Slice> serdeValue)
+ {
+ this.store = Preconditions.checkNotNull(store);
+ this.identifier = Preconditions.checkNotNull(identifier);
+ this.bucket = bucket;
+ this.serdeKey = Preconditions.checkNotNull(serdeKey);
+ this.serdeValue = Preconditions.checkNotNull(serdeValue);
+
+ map = new SpillableByteMapImpl(store, identifier, bucket, new PassThruByteArraySliceSerde(), new SerdeIntSlice());
+ }
+
+ public SpillableStateStore getStore()
+ {
+ return store;
+ }
+
+ @Override
+ public List<V> get(@Nullable K key)
+ {
+ return getHelper(key);
+ }
+
+ private SpillableArrayListImpl<V> getHelper(@Nullable K key)
+ {
+ SpillableArrayListImpl<V> spillableArrayList = cache.get(key);
+
+ if (spillableArrayList == null) {
+ Slice keySlice = serdeKey.serialize(key);
+ Integer size = map.get(SliceUtils.concatenate(keySlice, SIZE_KEY_SUFFIX).toByteArray());
+
+ if (size == null) {
+ return null;
+ }
+
+ Slice keyPrefix = SliceUtils.concatenate(identifier, keySlice);
+ spillableArrayList = new SpillableArrayListImpl<V>(bucket, keyPrefix.toByteArray(), store, serdeValue);
+ spillableArrayList.setSize(size);
+ }
+
+ cache.put(key, spillableArrayList);
+
+ return spillableArrayList;
+ }
+
+ @Override
+ public Set<K> keySet()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Multiset<K> keys()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Collection<V> values()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Collection<Map.Entry<K, V>> entries()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<V> removeAll(@Nullable Object key)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void clear()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int size()
+ {
+ return map.size();
+ }
+
+ @Override
+ public boolean isEmpty()
+ {
+ return map.isEmpty();
+ }
+
+ @Override
+ public boolean containsKey(@Nullable Object key)
+ {
+ return cache.contains((K)key) || map.containsKey(SliceUtils.concatenate(serdeKey.serialize((K)key),
+ SIZE_KEY_SUFFIX).toByteArray());
+ }
+
+ @Override
+ public boolean containsValue(@Nullable Object value)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean containsEntry(@Nullable Object key, @Nullable Object value)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean put(@Nullable K key, @Nullable V value)
+ {
+ SpillableArrayListImpl<V> spillableArrayList = getHelper(key);
+
+ if (spillableArrayList == null) {
+ Slice keyPrefix = SliceUtils.concatenate(identifier, serdeKey.serialize(key));
+ spillableArrayList = new SpillableArrayListImpl<V>(bucket, keyPrefix.toByteArray(), store, serdeValue);
+
+ cache.put(key, spillableArrayList);
+ }
+
+ spillableArrayList.add(value);
+ return true;
+ }
+
+ @Override
+ public boolean remove(@Nullable Object key, @Nullable Object value)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean putAll(@Nullable K key, Iterable<? extends V> values)
+ {
+ boolean changed = false;
+
+ for (V value: values) {
+ changed |= put(key, value);
+ }
+
+ return changed;
+ }
+
+ @Override
+ public boolean putAll(Multimap<? extends K, ? extends V> multimap)
+ {
+ boolean changed = false;
+
+ for (Map.Entry<? extends K, ? extends V> entry: multimap.entries()) {
+ changed |= put(entry.getKey(), entry.getValue());
+ }
+
+ return changed;
+ }
+
+ @Override
+ public List<V> replaceValues(K key, Iterable<? extends V> values)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Map<K, Collection<V>> asMap()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setup(Context.OperatorContext context)
+ {
+ map.setup(context);
+ isRunning = true;
+ }
+
+ @Override
+ public void beginWindow(long windowId)
+ {
+ map.beginWindow(windowId);
+ isInWindow = true;
+ }
+
+ @Override
+ public void endWindow()
+ {
+ isInWindow = false;
+ for (K key: cache.getChangedKeys()) {
+
+ SpillableArrayListImpl<V> spillableArrayList = cache.get(key);
+ spillableArrayList.endWindow();
+
+ Integer size = map.put(SliceUtils.concatenate(serdeKey.serialize(key), SIZE_KEY_SUFFIX).toByteArray(),
+ spillableArrayList.size());
+ }
+
+ Preconditions.checkState(cache.getRemovedKeys().isEmpty());
+ cache.endWindow();
+ map.endWindow();
+ }
+
+ @Override
+ public void teardown()
+ {
+ isRunning = false;
+ map.teardown();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteMapImpl.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteMapImpl.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteMapImpl.java
new file mode 100644
index 0000000..da313ee
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteMapImpl.java
@@ -0,0 +1,235 @@
+/**
+ * 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.state.spillable;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+import javax.validation.constraints.NotNull;
+
+import org.apache.apex.malhar.lib.state.BucketedState;
+import org.apache.apex.malhar.lib.utils.serde.Serde;
+import org.apache.apex.malhar.lib.utils.serde.SliceUtils;
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.esotericsoftware.kryo.DefaultSerializer;
+import com.esotericsoftware.kryo.serializers.FieldSerializer;
+import com.google.common.base.Preconditions;
+
+import com.datatorrent.api.Context;
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * A Spillable implementation of {@link Map}
+ * @param <K> The types of keys.
+ * @param <V> The types of values.
+ */
+@DefaultSerializer(FieldSerializer.class)
+@InterfaceStability.Evolving
+public class SpillableByteMapImpl<K, V> implements Spillable.SpillableByteMap<K, V>, Spillable.SpillableComponent,
+ Serializable
+{
+ private transient WindowBoundedMapCache<K, V> cache = new WindowBoundedMapCache<>();
+ private transient MutableInt tempOffset = new MutableInt();
+
+ @NotNull
+ private SpillableStateStore store;
+ @NotNull
+ private byte[] identifier;
+ private long bucket;
+ @NotNull
+ private Serde<K, Slice> serdeKey;
+ @NotNull
+ private Serde<V, Slice> serdeValue;
+
+ private int size = 0;
+
+ private SpillableByteMapImpl()
+ {
+ //for kryo
+ }
+
+ /**
+ * Creats a {@link SpillableByteMapImpl}.
+ * @param store The {@link SpillableStateStore} in which to spill to.
+ * @param identifier The Id of this {@link SpillableByteMapImpl}.
+ * @param bucket The Id of the bucket used to store this
+ * {@link SpillableByteMapImpl} in the provided {@link SpillableStateStore}.
+ * @param serdeKey The {@link Serde} to use when serializing and deserializing keys.
+ * @param serdeKey The {@link Serde} to use when serializing and deserializing values.
+ */
+ public SpillableByteMapImpl(SpillableStateStore store, byte[] identifier, long bucket, Serde<K, Slice> serdeKey,
+ Serde<V, Slice> serdeValue)
+ {
+ this.store = Preconditions.checkNotNull(store);
+ this.identifier = Preconditions.checkNotNull(identifier);
+ this.bucket = bucket;
+ this.serdeKey = Preconditions.checkNotNull(serdeKey);
+ this.serdeValue = Preconditions.checkNotNull(serdeValue);
+ }
+
+ public SpillableStateStore getStore()
+ {
+ return this.store;
+ }
+
+ @Override
+ public int size()
+ {
+ return size;
+ }
+
+ @Override
+ public boolean isEmpty()
+ {
+ return size == 0;
+ }
+
+ @Override
+ public boolean containsKey(Object o)
+ {
+ return get(o) != null;
+ }
+
+ @Override
+ public boolean containsValue(Object o)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public V get(Object o)
+ {
+ K key = (K)o;
+
+ if (cache.getRemovedKeys().contains(key)) {
+ return null;
+ }
+
+ V val = cache.get(key);
+
+ if (val != null) {
+ return val;
+ }
+
+ Slice valSlice = store.getSync(bucket, SliceUtils.concatenate(identifier, serdeKey.serialize(key)));
+
+ if (valSlice == null || valSlice == BucketedState.EXPIRED || valSlice.length == 0) {
+ return null;
+ }
+
+ tempOffset.setValue(0);
+ return serdeValue.deserialize(valSlice, tempOffset);
+ }
+
+ @Override
+ public V put(K k, V v)
+ {
+ V value = get(k);
+
+ if (value == null) {
+ size++;
+ }
+
+ cache.put(k, v);
+
+ return value;
+ }
+
+ @Override
+ public V remove(Object o)
+ {
+ V value = get(o);
+
+ if (value != null) {
+ size--;
+ }
+
+ cache.remove((K)o);
+
+ return value;
+ }
+
+ @Override
+ public void putAll(Map<? extends K, ? extends V> map)
+ {
+ for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
+ put(entry.getKey(), entry.getValue());
+ }
+ }
+
+ @Override
+ public void clear()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Set<K> keySet()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Collection<V> values()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Set<Entry<K, V>> entrySet()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setup(Context.OperatorContext context)
+ {
+ }
+
+ @Override
+ public void beginWindow(long windowId)
+ {
+ }
+
+ @Override
+ public void endWindow()
+ {
+ for (K key: cache.getChangedKeys()) {
+ store.put(this.bucket, SliceUtils.concatenate(identifier, serdeKey.serialize(key)),
+ serdeValue.serialize(cache.get(key)));
+ }
+
+ for (K key: cache.getRemovedKeys()) {
+ store.put(this.bucket, SliceUtils.concatenate(identifier, serdeKey.serialize(key)),
+ new Slice(ArrayUtils.EMPTY_BYTE_ARRAY));
+ }
+
+ cache.endWindow();
+ }
+
+ @Override
+ public void teardown()
+ {
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponent.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponent.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponent.java
index 29da3f5..c63c7ef 100644
--- a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponent.java
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponent.java
@@ -23,6 +23,7 @@ import org.apache.apex.malhar.lib.utils.serde.Serde;
import com.datatorrent.api.Component;
import com.datatorrent.api.Context.OperatorContext;
+import com.datatorrent.api.Operator;
import com.datatorrent.netlet.util.Slice;
/**
@@ -31,7 +32,8 @@ import com.datatorrent.netlet.util.Slice;
*
* @since 3.4.0
*/
-public interface SpillableComplexComponent extends Component<OperatorContext>, SpillableComponent
+public interface SpillableComplexComponent extends Component<OperatorContext>, SpillableComponent,
+ Operator.CheckpointNotificationListener
{
/**
* This is a method for creating a {@link SpillableArrayList}. This method
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponentImpl.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponentImpl.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponentImpl.java
new file mode 100644
index 0000000..b31adfd
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponentImpl.java
@@ -0,0 +1,193 @@
+/**
+ * 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.state.spillable;
+
+import java.util.List;
+
+import javax.validation.constraints.NotNull;
+
+import org.apache.apex.malhar.lib.utils.serde.Serde;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+
+import com.datatorrent.api.Context;
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * This is a factory that is used for Spillable datastructures. This component is used by nesting it inside of an
+ * operator and forwarding the appropriate operator callbacks are called on the {@link SpillableComplexComponentImpl}.
+ * Spillable datastructures are created by called the appropriate factory methods on the
+ * {@link SpillableComplexComponentImpl} in the setup method of an operator.
+ */
+@InterfaceStability.Evolving
+public class SpillableComplexComponentImpl implements SpillableComplexComponent
+{
+ private List<SpillableComponent> componentList = Lists.newArrayList();
+
+ @NotNull
+ private SpillableStateStore store;
+
+ @NotNull
+ private SpillableIdentifierGenerator identifierGenerator;
+
+ private SpillableComplexComponentImpl()
+ {
+ // for kryo
+ }
+
+ public SpillableComplexComponentImpl(SpillableStateStore store)
+ {
+ this(store, new SequentialSpillableIdentifierGenerator());
+ }
+
+ public SpillableComplexComponentImpl(SpillableStateStore store, SpillableIdentifierGenerator identifierGenerator)
+ {
+ this.store = Preconditions.checkNotNull(store);
+ this.identifierGenerator = Preconditions.checkNotNull(identifierGenerator);
+ }
+
+ public <T> SpillableArrayList<T> newSpillableArrayList(long bucket, Serde<T, Slice> serde)
+ {
+ SpillableArrayListImpl<T> list = new SpillableArrayListImpl<T>(bucket, identifierGenerator.next(), store, serde);
+ componentList.add(list);
+ return list;
+ }
+
+ public <T> SpillableArrayList<T> newSpillableArrayList(byte[] identifier, long bucket, Serde<T, Slice> serde)
+ {
+ identifierGenerator.register(identifier);
+ SpillableArrayListImpl<T> list = new SpillableArrayListImpl<T>(bucket, identifier, store, serde);
+ componentList.add(list);
+ return list;
+ }
+
+ public <K, V> SpillableByteMap<K, V> newSpillableByteMap(long bucket, Serde<K, Slice> serdeKey,
+ Serde<V, Slice> serdeValue)
+ {
+ SpillableByteMapImpl<K, V> map = new SpillableByteMapImpl<K, V>(store, identifierGenerator.next(),
+ bucket, serdeKey, serdeValue);
+ componentList.add(map);
+ return map;
+ }
+
+ public <K, V> SpillableByteMap<K, V> newSpillableByteMap(byte[] identifier, long bucket, Serde<K, Slice> serdeKey,
+ Serde<V, Slice> serdeValue)
+ {
+ identifierGenerator.register(identifier);
+ SpillableByteMapImpl<K, V> map = new SpillableByteMapImpl<K, V>(store, identifier, bucket, serdeKey, serdeValue);
+ componentList.add(map);
+ return map;
+ }
+
+ public <K, V> SpillableByteArrayListMultimap<K, V> newSpillableByteArrayListMultimap(long bucket, Serde<K,
+ Slice> serdeKey, Serde<V, Slice> serdeValue)
+ {
+ SpillableByteArrayListMultimapImpl<K, V> map = new SpillableByteArrayListMultimapImpl<K, V>(store,
+ identifierGenerator.next(), bucket, serdeKey, serdeValue);
+ componentList.add(map);
+ return map;
+ }
+
+ public <K, V> SpillableByteArrayListMultimap<K, V> newSpillableByteArrayListMultimap(byte[] identifier, long bucket,
+ Serde<K, Slice> serdeKey,
+ Serde<V, Slice> serdeValue)
+ {
+ identifierGenerator.register(identifier);
+ SpillableByteArrayListMultimapImpl<K, V> map = new SpillableByteArrayListMultimapImpl<K, V>(store,
+ identifier, bucket, serdeKey, serdeValue);
+ componentList.add(map);
+ return map;
+ }
+
+ public <T> SpillableByteMultiset<T> newSpillableByteMultiset(long bucket, Serde<T, Slice> serde)
+ {
+ throw new UnsupportedOperationException("Unsupported Operation");
+ }
+
+ public <T> SpillableByteMultiset<T> newSpillableByteMultiset(byte[] identifier, long bucket, Serde<T, Slice> serde)
+ {
+ throw new UnsupportedOperationException("Unsupported Operation");
+ }
+
+ public <T> SpillableQueue<T> newSpillableQueue(long bucket, Serde<T, Slice> serde)
+ {
+ throw new UnsupportedOperationException("Unsupported Operation");
+ }
+
+ public <T> SpillableQueue<T> newSpillableQueue(byte[] identifier, long bucket, Serde<T, Slice> serde)
+ {
+ throw new UnsupportedOperationException("Unsupported Operation");
+ }
+
+ @Override
+ public void setup(Context.OperatorContext context)
+ {
+ store.setup(context);
+ for (SpillableComponent spillableComponent: componentList) {
+ spillableComponent.setup(context);
+ }
+ }
+
+ @Override
+ public void beginWindow(long windowId)
+ {
+ store.beginWindow(windowId);
+ for (SpillableComponent spillableComponent: componentList) {
+ spillableComponent.beginWindow(windowId);
+ }
+ }
+
+ @Override
+ public void endWindow()
+ {
+ for (SpillableComponent spillableComponent: componentList) {
+ spillableComponent.endWindow();
+ }
+ store.endWindow();
+ }
+
+ @Override
+ public void teardown()
+ {
+ for (SpillableComponent spillableComponent: componentList) {
+ spillableComponent.teardown();
+ }
+ store.teardown();
+ }
+
+ @Override
+ public void beforeCheckpoint(long l)
+ {
+ store.beforeCheckpoint(l);
+ }
+
+ @Override
+ public void checkpointed(long l)
+ {
+ store.checkpointed(l);
+ }
+
+ @Override
+ public void committed(long l)
+ {
+ store.committed(l);
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableIdentifierGenerator.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableIdentifierGenerator.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableIdentifierGenerator.java
new file mode 100644
index 0000000..17a52f0
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableIdentifierGenerator.java
@@ -0,0 +1,41 @@
+/**
+ * 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.state.spillable;
+
+import org.apache.hadoop.classification.InterfaceStability;
+
+/**
+ * Classes implementing this interface can be used as generators for identifiers for Spillable data structures. This is
+ * mainly used in implementations of {@link SpillableComplexComponent}.
+ */
+@InterfaceStability.Evolving
+public interface SpillableIdentifierGenerator
+{
+ /**
+ * Generators the next valid identifier for a Spillable data structure.
+ * @return A byte array which represents the next valid identifier for a Spillable data structure.
+ */
+ byte[] next();
+
+ /**
+ * Registers the given identifier with this {@link SpillableIdentifierGenerator}.
+ * @param identifier The identifier to register with this {@link SpillableIdentifierGenerator}.
+ */
+ void register(byte[] identifier);
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableStateStore.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableStateStore.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableStateStore.java
new file mode 100644
index 0000000..1db0eeb
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/SpillableStateStore.java
@@ -0,0 +1,35 @@
+/**
+ * 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.state.spillable;
+
+import org.apache.apex.malhar.lib.state.BucketedState;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.datatorrent.api.Component;
+import com.datatorrent.api.Context;
+import com.datatorrent.api.Operator;
+
+/**
+ * Implementations of this interface are used by Spillable datastructures to spill data to disk.
+ */
+@InterfaceStability.Evolving
+public interface SpillableStateStore extends BucketedState, Component<Context.OperatorContext>,
+ Operator.CheckpointNotificationListener, WindowListener
+{
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/TimeBasedPriorityQueue.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/TimeBasedPriorityQueue.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/TimeBasedPriorityQueue.java
new file mode 100644
index 0000000..025c501
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/TimeBasedPriorityQueue.java
@@ -0,0 +1,154 @@
+/**
+ * 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.state.spillable;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+/**
+ * A simple priority queue where the priority of an object is determined by the time at which it is inserted into the
+ * queue. The object in the queue with the smallest time stamp is the first to be dequeued.
+ * @param <T> The type of the objects inserted into the queue.
+ */
+@InterfaceStability.Evolving
+public class TimeBasedPriorityQueue<T>
+{
+ private Map<T, TimeWrapper<T>> timeWrappperMap = Maps.newHashMap();
+ private Set<TimeWrapper<T>> sortedTimestamp = Sets.newTreeSet();
+
+ public void upSert(T value)
+ {
+ TimeWrapper<T> timeWrapper = timeWrappperMap.get(value);
+
+ if (timeWrapper != null) {
+ sortedTimestamp.remove(timeWrapper);
+ timeWrapper.setTimestamp(System.currentTimeMillis());
+ } else {
+ timeWrapper = new TimeWrapper<>(value, System.currentTimeMillis());
+ timeWrappperMap.put(value, timeWrapper);
+ }
+
+ sortedTimestamp.add(timeWrapper);
+ }
+
+ public void remove(T value)
+ {
+ TimeWrapper<T> timeWrapper = timeWrappperMap.get(value);
+ sortedTimestamp.remove(timeWrapper);
+ timeWrappperMap.remove(value);
+ }
+
+ public Set<T> removeLRU(int count)
+ {
+ Preconditions.checkArgument(count > 0 && count <= timeWrappperMap.size());
+
+ Iterator<TimeWrapper<T>> iterator = sortedTimestamp.iterator();
+ Set<T> valueSet = Sets.newHashSet();
+
+ for (int counter = 0; counter < count; counter++) {
+ T value = iterator.next().getKey();
+ valueSet.add(value);
+ timeWrappperMap.remove(value);
+ iterator.remove();
+ }
+
+ return valueSet;
+ }
+
+ protected static class TimeWrapper<T> implements Comparable<TimeWrapper<T>>
+ {
+ private T key;
+ private long timestamp;
+
+ public TimeWrapper(T key, long timestamp)
+ {
+ this.key = Preconditions.checkNotNull(key);
+ this.timestamp = timestamp;
+ }
+
+ public T getKey()
+ {
+ return key;
+ }
+
+ public long getTimestamp()
+ {
+ return timestamp;
+ }
+
+ public void setTimestamp(long timestamp)
+ {
+ this.timestamp = timestamp;
+ }
+
+ @Override
+ public int compareTo(TimeWrapper<T> timeWrapper)
+ {
+ if (this.timestamp < timeWrapper.getTimestamp()) {
+ return -1;
+ } else if (this.timestamp > timeWrapper.getTimestamp()) {
+ return 1;
+ }
+
+ return 0;
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ TimeWrapper<?> that = (TimeWrapper<?>)o;
+
+ return key.equals(that.key);
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return key.hashCode();
+ }
+
+ @Override
+ public String toString()
+ {
+ return "TimeWrapper{" +
+ "key=" + key +
+ ", timestamp=" + timestamp +
+ '}';
+ }
+ }
+
+ private static final Logger LOG = LoggerFactory.getLogger(TimeBasedPriorityQueue.class);
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/WindowBoundedMapCache.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/WindowBoundedMapCache.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/WindowBoundedMapCache.java
new file mode 100644
index 0000000..fcf219d
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/WindowBoundedMapCache.java
@@ -0,0 +1,129 @@
+/**
+ * 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.state.spillable;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+/**
+ * This is an LRU cache with a maximum size. When the cache size is exceeded, the excess elements are kept in the
+ * cache until the end of the window. When the end of the window is reached, the least recently used entries are
+ * evicted from the cache.
+ * @param <K> The type of the keys.
+ * @param <V> The type of the values.
+ */
+@InterfaceStability.Evolving
+public class WindowBoundedMapCache<K, V>
+{
+ public static final int DEFAULT_MAX_SIZE = 50000;
+
+ private int maxSize = DEFAULT_MAX_SIZE;
+
+ private Map<K, V> cache = Maps.newHashMap();
+
+ private Set<K> changedKeys = Sets.newHashSet();
+ private Set<K> removedKeys = Sets.newHashSet();
+ private TimeBasedPriorityQueue<K> priorityQueue = new TimeBasedPriorityQueue<>();
+
+ public WindowBoundedMapCache()
+ {
+ }
+
+ public WindowBoundedMapCache(int maxSize)
+ {
+ Preconditions.checkArgument(maxSize > 0);
+
+ this.maxSize = maxSize;
+ }
+
+ public void put(K key, V value)
+ {
+ Preconditions.checkNotNull(key);
+ Preconditions.checkNotNull(value);
+
+ removedKeys.remove(key);
+ changedKeys.add(key);
+ priorityQueue.upSert(key);
+
+ cache.put(key, value);
+ }
+
+ public V get(K key)
+ {
+ Preconditions.checkNotNull(key);
+
+ return cache.get(key);
+ }
+
+ public boolean contains(K key)
+ {
+ return cache.containsKey(key);
+ }
+
+ public void remove(K key)
+ {
+ Preconditions.checkNotNull(key);
+
+ if (!cache.containsKey(key)) {
+ return;
+ }
+
+ cache.remove(key);
+ changedKeys.remove(key);
+ removedKeys.add(key);
+ priorityQueue.remove(key);
+ }
+
+ public Set<K> getChangedKeys()
+ {
+ return changedKeys;
+ }
+
+ public Set<K> getRemovedKeys()
+ {
+ return removedKeys;
+ }
+
+ /*
+ Note: beginWindow is intentionally not implemented because many users need a cache that does not require
+ beginWindow to be called.
+ */
+
+ public void endWindow()
+ {
+ int count = cache.size() - maxSize;
+
+ if (count > 0) {
+ Set<K> expiredKeys = priorityQueue.removeLRU(count);
+
+ for (K expiredKey: expiredKeys) {
+ cache.remove(expiredKey);
+ }
+ }
+
+ changedKeys = Sets.newHashSet();
+ removedKeys = Sets.newHashSet();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/WindowListener.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/WindowListener.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/WindowListener.java
new file mode 100644
index 0000000..fa8cd9f
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/WindowListener.java
@@ -0,0 +1,42 @@
+/**
+ * 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.state.spillable;
+
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.datatorrent.api.Operator;
+
+/**
+ * This interface represents components which need to listen to the operator {@link Operator#beginWindow(long)} and
+ * {@link Operator#endWindow()} callbacks.
+ */
+@InterfaceStability.Evolving
+public interface WindowListener
+{
+ /**
+ * This is called when the parent {@link Operator}'s {@link Operator#beginWindow(long)} callback is called.
+ * @param windowId The id of the current application window.
+ */
+ void beginWindow(long windowId);
+
+ /**
+ * This is called when the parent {@link Operator}'s {@link Operator#endWindow()} callback is called.
+ */
+ void endWindow();
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemMultiset.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemMultiset.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemMultiset.java
deleted file mode 100644
index fa7bf08..0000000
--- a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemMultiset.java
+++ /dev/null
@@ -1,161 +0,0 @@
-/**
- * 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.state.spillable.inmem;
-
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Set;
-
-import javax.annotation.Nullable;
-
-import org.apache.apex.malhar.lib.state.spillable.Spillable;
-
-import com.esotericsoftware.kryo.serializers.FieldSerializer;
-import com.esotericsoftware.kryo.serializers.JavaSerializer;
-import com.google.common.collect.HashMultiset;
-
-/**
- * An in memory implementation of the {@link Spillable.SpillableByteMultiset} interface.
- * @param <T> The type of the data stored in the {@link InMemMultiset}
- */
-public class InMemMultiset<T> implements Spillable.SpillableByteMultiset<T>
-{
- @FieldSerializer.Bind(JavaSerializer.class)
- private HashMultiset<T> multiset = HashMultiset.create();
-
- @Override
- public int count(@Nullable Object element)
- {
- return multiset.count(element);
- }
-
- @Override
- public int add(@Nullable T element, int occurrences)
- {
- return multiset.add(element, occurrences);
- }
-
- @Override
- public int remove(@Nullable Object element, int occurrences)
- {
- return multiset.remove(element, occurrences);
- }
-
- @Override
- public int setCount(T element, int count)
- {
- return multiset.setCount(element, count);
- }
-
- @Override
- public boolean setCount(T element, int oldCount, int newCount)
- {
- return multiset.setCount(element, oldCount, newCount);
- }
-
- @Override
- public Set<T> elementSet()
- {
- return multiset.elementSet();
- }
-
- @Override
- public Set<Entry<T>> entrySet()
- {
- return multiset.entrySet();
- }
-
- @Override
- public Iterator<T> iterator()
- {
- return multiset.iterator();
- }
-
- @Override
- public Object[] toArray()
- {
- return multiset.toArray();
- }
-
- @Override
- public <T1> T1[] toArray(T1[] t1s)
- {
- return multiset.toArray(t1s);
- }
-
- @Override
- public int size()
- {
- return multiset.size();
- }
-
- @Override
- public boolean isEmpty()
- {
- return multiset.isEmpty();
- }
-
- @Override
- public boolean contains(@Nullable Object element)
- {
- return multiset.contains(element);
- }
-
- @Override
- public boolean containsAll(Collection<?> es)
- {
- return multiset.containsAll(es);
- }
-
- @Override
- public boolean addAll(Collection<? extends T> collection)
- {
- return multiset.addAll(collection);
- }
-
- @Override
- public boolean add(T element)
- {
- return multiset.add(element);
- }
-
- @Override
- public boolean remove(@Nullable Object element)
- {
- return multiset.remove(element);
- }
-
- @Override
- public boolean removeAll(Collection<?> c)
- {
- return multiset.removeAll(c);
- }
-
- @Override
- public boolean retainAll(Collection<?> c)
- {
- return multiset.retainAll(c);
- }
-
- @Override
- public void clear()
- {
- multiset.clear();
- }
-}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableArrayList.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableArrayList.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableArrayList.java
deleted file mode 100644
index 9742537..0000000
--- a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableArrayList.java
+++ /dev/null
@@ -1,175 +0,0 @@
-/**
- * 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.state.spillable.inmem;
-
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ListIterator;
-
-import org.apache.apex.malhar.lib.state.spillable.Spillable;
-
-import com.google.common.collect.Lists;
-
-/**
- * An in memory implementation of the {@link Spillable.SpillableArrayList} interface.
- * @param <T> The type of the data stored in the {@link InMemSpillableArrayList}
- */
-public class InMemSpillableArrayList<T> implements Spillable.SpillableArrayList<T>
-{
- private List<T> list = Lists.newArrayList();
-
- @Override
- public int size()
- {
- return list.size();
- }
-
- @Override
- public boolean isEmpty()
- {
- return list.isEmpty();
- }
-
- @Override
- public boolean contains(Object o)
- {
- return list.contains(o);
- }
-
- @Override
- public Iterator<T> iterator()
- {
- return list.iterator();
- }
-
- @Override
- public Object[] toArray()
- {
- return list.toArray();
- }
-
- @Override
- public <T1> T1[] toArray(T1[] t1s)
- {
- return list.toArray(t1s);
- }
-
- @Override
- public boolean add(T t)
- {
- return list.add(t);
- }
-
- @Override
- public boolean remove(Object o)
- {
- return list.remove(o);
- }
-
- @Override
- public boolean containsAll(Collection<?> collection)
- {
- return list.containsAll(collection);
- }
-
- @Override
- public boolean addAll(Collection<? extends T> collection)
- {
- return list.addAll(collection);
- }
-
- @Override
- public boolean addAll(int i, Collection<? extends T> collection)
- {
- return list.addAll(i, collection);
- }
-
- @Override
- public boolean removeAll(Collection<?> collection)
- {
- return list.removeAll(collection);
- }
-
- @Override
- public boolean retainAll(Collection<?> collection)
- {
- return list.retainAll(collection);
- }
-
- @Override
- public void clear()
- {
- list.clear();
- }
-
- @Override
- public T get(int i)
- {
- return list.get(i);
- }
-
- @Override
- public T set(int i, T t)
- {
- return list.set(i, t);
- }
-
- @Override
- public void add(int i, T t)
- {
- list.add(i, t);
- }
-
- @Override
- public T remove(int i)
- {
- return list.remove(i);
- }
-
- @Override
- public int indexOf(Object o)
- {
- return list.indexOf(o);
- }
-
- @Override
- public int lastIndexOf(Object o)
- {
- return list.lastIndexOf(o);
- }
-
- @Override
- public ListIterator<T> listIterator()
- {
- return list.listIterator();
- }
-
- @Override
- public ListIterator<T> listIterator(int i)
- {
- return list.listIterator(i);
- }
-
- @Override
- public List<T> subList(int i, int i1)
- {
- return list.subList(i, i1);
- }
-}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableByteArrayListMultimap.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableByteArrayListMultimap.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableByteArrayListMultimap.java
deleted file mode 100644
index 8376bd5..0000000
--- a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableByteArrayListMultimap.java
+++ /dev/null
@@ -1,154 +0,0 @@
-/**
- * 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.state.spillable.inmem;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.annotation.Nullable;
-
-import org.apache.apex.malhar.lib.state.spillable.Spillable;
-
-import com.esotericsoftware.kryo.serializers.FieldSerializer;
-import com.esotericsoftware.kryo.serializers.JavaSerializer;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.ListMultimap;
-import com.google.common.collect.Multimap;
-import com.google.common.collect.Multiset;
-
-/**
- * An in memory implementation of the {@link Spillable.SpillableByteArrayListMultimap} interface.
- * @param <K> The type of the keys stored in the {@link InMemSpillableByteArrayListMultimap}
- * @param <V> The type of the values stored in the {@link InMemSpillableByteArrayListMultimap}
- */
-public class InMemSpillableByteArrayListMultimap<K, V> implements Spillable.SpillableByteArrayListMultimap<K, V>
-{
- @FieldSerializer.Bind(JavaSerializer.class)
- private ListMultimap<K, V> multimap = ArrayListMultimap.create();
-
- @Override
- public List<V> get(@Nullable K key)
- {
- return multimap.get(key);
- }
-
- @Override
- public Set<K> keySet()
- {
- return multimap.keySet();
- }
-
- @Override
- public Multiset<K> keys()
- {
- return multimap.keys();
- }
-
- @Override
- public Collection<V> values()
- {
- return multimap.values();
- }
-
- @Override
- public Collection<Map.Entry<K, V>> entries()
- {
- return multimap.entries();
- }
-
- @Override
- public List<V> removeAll(@Nullable Object key)
- {
- return multimap.removeAll(key);
- }
-
- @Override
- public void clear()
- {
- multimap.clear();
- }
-
- @Override
- public int size()
- {
- return multimap.size();
- }
-
- @Override
- public boolean isEmpty()
- {
- return multimap.isEmpty();
- }
-
- @Override
- public boolean containsKey(@Nullable Object key)
- {
- return multimap.containsKey(key);
- }
-
- @Override
- public boolean containsValue(@Nullable Object value)
- {
- return multimap.containsValue(value);
- }
-
- @Override
- public boolean containsEntry(@Nullable Object key, @Nullable Object value)
- {
- return multimap.containsEntry(key, value);
- }
-
- @Override
- public boolean put(@Nullable K key, @Nullable V value)
- {
- return multimap.put(key, value);
- }
-
- @Override
- public boolean remove(@Nullable Object key, @Nullable Object value)
- {
- return multimap.remove(key, value);
- }
-
- @Override
- public boolean putAll(@Nullable K key, Iterable<? extends V> values)
- {
- return multimap.putAll(key, values);
- }
-
- @Override
- public boolean putAll(Multimap<? extends K, ? extends V> m)
- {
- return multimap.putAll(m);
- }
-
- @Override
- public List<V> replaceValues(K key, Iterable<? extends V> values)
- {
- return multimap.replaceValues(key, values);
- }
-
- @Override
- public Map<K, Collection<V>> asMap()
- {
- return multimap.asMap();
- }
-}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableComplexComponent.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableComplexComponent.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableComplexComponent.java
deleted file mode 100644
index 25e8b2c..0000000
--- a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableComplexComponent.java
+++ /dev/null
@@ -1,117 +0,0 @@
-/**
- * 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.state.spillable.inmem;
-
-import org.apache.apex.malhar.lib.state.spillable.SpillableComplexComponent;
-import org.apache.apex.malhar.lib.utils.serde.Serde;
-
-import com.datatorrent.api.Context;
-import com.datatorrent.netlet.util.Slice;
-
-/**
- * An in memory implementation {@link SpillableComplexComponent}
- */
-public class InMemSpillableComplexComponent implements SpillableComplexComponent
-{
- @Override
- public <T> SpillableArrayList<T> newSpillableArrayList(long bucket, Serde<T, Slice> serde)
- {
- return new InMemSpillableArrayList<>();
- }
-
- @Override
- public <T> SpillableArrayList<T> newSpillableArrayList(byte[] identifier, long bucket,
- Serde<T, Slice> serde)
- {
- return new InMemSpillableArrayList<>();
- }
-
- @Override
- public <K, V> SpillableByteMap<K, V> newSpillableByteMap(long bucket, Serde<K, Slice> serdeKey,
- Serde<V, Slice> serdeValue)
- {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public <K, V> SpillableByteMap<K, V> newSpillableByteMap(byte[] identifier, long bucket,
- Serde<K, Slice> serdeKey, Serde<V, Slice> serdeValue)
- {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public <K, V> SpillableByteArrayListMultimap<K, V> newSpillableByteArrayListMultimap(long bucket,
- Serde<K, Slice> serdeKey, Serde<V, Slice> serdeValue)
- {
- return new InMemSpillableByteArrayListMultimap<>();
- }
-
- @Override
- public <K, V> SpillableByteArrayListMultimap<K, V> newSpillableByteArrayListMultimap(byte[] identifier,
- long bucket, Serde<K, Slice> serdeKey, Serde<V, Slice> serdeValue)
- {
- return new InMemSpillableByteArrayListMultimap<>();
- }
-
- @Override
- public <T> SpillableByteMultiset<T> newSpillableByteMultiset(long bucket, Serde<T, Slice> serde)
- {
- return new InMemMultiset<>();
- }
-
- @Override
- public <T> SpillableByteMultiset<T> newSpillableByteMultiset(byte[] identifier, long bucket,
- Serde<T, Slice> serde)
- {
- return new InMemMultiset<>();
- }
-
- @Override
- public <T> SpillableQueue<T> newSpillableQueue(long bucket, Serde<T, Slice> serde)
- {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public <T> SpillableQueue<T> newSpillableQueue(byte[] identifier, long bucket, Serde<T, Slice> serde)
- {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void setup(Context.OperatorContext context)
- {
- }
-
- @Override
- public void beginWindow(long windowId)
- {
- }
-
- @Override
- public void endWindow()
- {
- }
-
- @Override
- public void teardown()
- {
- }
-}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableStateStore.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableStateStore.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableStateStore.java
new file mode 100644
index 0000000..0e65344
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/inmem/InMemSpillableStateStore.java
@@ -0,0 +1,118 @@
+/**
+ * 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.state.spillable.inmem;
+
+import java.util.Map;
+import java.util.concurrent.Future;
+
+import javax.validation.constraints.NotNull;
+
+import org.apache.apex.malhar.lib.state.spillable.SpillableStateStore;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.google.common.collect.Maps;
+
+import com.datatorrent.api.Context;
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * A simple in memory implementation of a {@link SpillableStateStore} backed by a {@link Map}.
+ */
+@InterfaceStability.Evolving
+public class InMemSpillableStateStore implements SpillableStateStore
+{
+ private Map<Long, Map<Slice, Slice>> store = Maps.newHashMap();
+
+ @Override
+ public void setup(Context.OperatorContext context)
+ {
+
+ }
+
+ @Override
+ public void beginWindow(long windowId)
+ {
+
+ }
+
+ @Override
+ public void endWindow()
+ {
+
+ }
+
+ @Override
+ public void teardown()
+ {
+
+ }
+
+ @Override
+ public void put(long bucketId, @NotNull Slice key, @NotNull Slice value)
+ {
+ Map<Slice, Slice> bucket = store.get(bucketId);
+
+ if (bucket == null) {
+ bucket = Maps.newHashMap();
+ store.put(bucketId, bucket);
+ }
+
+ bucket.put(key, value);
+ }
+
+ @Override
+ public Slice getSync(long bucketId, @NotNull Slice key)
+ {
+ Map<Slice, Slice> bucket = store.get(bucketId);
+
+ if (bucket == null) {
+ bucket = Maps.newHashMap();
+ store.put(bucketId, bucket);
+ }
+
+ return bucket.get(key);
+ }
+
+ @Override
+ public Future<Slice> getAsync(long bucketId, @NotNull Slice key)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void beforeCheckpoint(long l)
+ {
+ }
+
+ @Override
+ public void checkpointed(long l)
+ {
+ }
+
+ @Override
+ public void committed(long l)
+ {
+ }
+
+ @Override
+ public String toString()
+ {
+ return store.toString();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/managed/ManagedStateSpillableStateStore.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/managed/ManagedStateSpillableStateStore.java b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/managed/ManagedStateSpillableStateStore.java
new file mode 100644
index 0000000..6d68acc
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/state/spillable/managed/ManagedStateSpillableStateStore.java
@@ -0,0 +1,34 @@
+/**
+ * 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.state.spillable.managed;
+
+import org.apache.apex.malhar.lib.state.managed.ManagedStateImpl;
+import org.apache.apex.malhar.lib.state.spillable.SpillableStateStore;
+
+import com.esotericsoftware.kryo.DefaultSerializer;
+import com.esotericsoftware.kryo.serializers.FieldSerializer;
+
+@DefaultSerializer(FieldSerializer.class)
+public class ManagedStateSpillableStateStore extends ManagedStateImpl implements SpillableStateStore
+{
+ public ManagedStateSpillableStateStore()
+ {
+ super();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruByteArraySerde.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruByteArraySerde.java b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruByteArraySerde.java
index 85c34d9..9669981 100644
--- a/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruByteArraySerde.java
+++ b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruByteArraySerde.java
@@ -19,6 +19,7 @@
package org.apache.apex.malhar.lib.utils.serde;
import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.hadoop.classification.InterfaceStability;
/**
* This is a simple pass through {@link Serde}. When serialization is performed the input byte array is returned.
@@ -26,6 +27,7 @@ import org.apache.commons.lang3.mutable.MutableInt;
*
* @since 3.4.0
*/
+@InterfaceStability.Evolving
public class PassThruByteArraySerde implements Serde<byte[], byte[]>
{
@Override
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruByteArraySliceSerde.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruByteArraySliceSerde.java b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruByteArraySliceSerde.java
new file mode 100644
index 0000000..436e7f8
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruByteArraySliceSerde.java
@@ -0,0 +1,59 @@
+/**
+ * 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.utils.serde;
+
+import org.apache.commons.lang3.mutable.MutableInt;
+
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * This is a simple {@link Serde} which serializes and deserializes byte arrays to {@link Slice}s. A byte array is
+ * serialized by simply wrapping it in a {@link Slice} object and deserialized by simply reading the byte array
+ * out of the {@link Slice} object.
+ *
+ * <b>Note:</b> The deserialized method doesn't use the offset argument in this implementation.
+ */
+public class PassThruByteArraySliceSerde implements Serde<byte[], Slice>
+{
+ @Override
+ public Slice serialize(byte[] object)
+ {
+ return new Slice(object);
+ }
+
+ @Override
+ public byte[] deserialize(Slice object, MutableInt offset)
+ {
+ offset.add(object.length);
+
+ if (object.offset == 0) {
+ return object.buffer;
+ }
+
+ byte[] bytes = new byte[object.length];
+ System.arraycopy(object.buffer, object.offset, bytes, 0, object.length);
+ return bytes;
+ }
+
+ @Override
+ public byte[] deserialize(Slice object)
+ {
+ return deserialize(object, new MutableInt(0));
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruSliceSerde.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruSliceSerde.java b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruSliceSerde.java
new file mode 100644
index 0000000..f9d93b3
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/PassThruSliceSerde.java
@@ -0,0 +1,50 @@
+/**
+ * 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.utils.serde;
+
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * This is a {@link Serde} implementation which simply allows an input slice to pass through. No serialization or
+ * deserialization transformation is performed on the input {@link Slice}s.
+ */
+@InterfaceStability.Evolving
+public class PassThruSliceSerde implements Serde<Slice, Slice>
+{
+ @Override
+ public Slice serialize(Slice object)
+ {
+ return object;
+ }
+
+ @Override
+ public Slice deserialize(Slice object, MutableInt offset)
+ {
+ return object;
+ }
+
+ @Override
+ public Slice deserialize(Slice object)
+ {
+ return object;
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeIntSlice.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeIntSlice.java b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeIntSlice.java
new file mode 100644
index 0000000..c18af33
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeIntSlice.java
@@ -0,0 +1,52 @@
+/**
+ * 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.utils.serde;
+
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.datatorrent.lib.appdata.gpo.GPOUtils;
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * This is an implementation of {@link Serde} which deserializes and serializes integers.
+ */
+@InterfaceStability.Evolving
+public class SerdeIntSlice implements Serde<Integer, Slice>
+{
+ @Override
+ public Slice serialize(Integer object)
+ {
+ return new Slice(GPOUtils.serializeInt(object));
+ }
+
+ @Override
+ public Integer deserialize(Slice slice, MutableInt offset)
+ {
+ int val = GPOUtils.deserializeInt(slice.buffer, new MutableInt(slice.offset + offset.intValue()));
+ offset.add(4);
+ return val;
+ }
+
+ @Override
+ public Integer deserialize(Slice object)
+ {
+ return deserialize(object, new MutableInt(0));
+ }
+}
[2/3] apex-malhar git commit: APEXMALHAR-2048 Added implementations
of SpillableList, SpillableMap, and SpillableArrayListMultimap
Posted by da...@apache.org.
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeListSlice.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeListSlice.java b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeListSlice.java
new file mode 100644
index 0000000..2a7947d
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeListSlice.java
@@ -0,0 +1,109 @@
+/**
+ * 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.utils.serde;
+
+import java.util.List;
+
+import javax.validation.constraints.NotNull;
+
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+
+import com.datatorrent.lib.appdata.gpo.GPOUtils;
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * This is an implementation of {@link Serde} which serializes and deserializes lists.
+ */
+@InterfaceStability.Evolving
+public class SerdeListSlice<T> implements Serde<List<T>, Slice>
+{
+ @NotNull
+ private Serde<T, Slice> serde;
+
+ private SerdeListSlice()
+ {
+ // for Kryo
+ }
+
+ /**
+ * Creates a {@link SerdeListSlice}.
+ * @param serde The {@link Serde} that is used to serialize and deserialize each element of a list.
+ */
+ public SerdeListSlice(@NotNull Serde<T, Slice> serde)
+ {
+ this.serde = Preconditions.checkNotNull(serde);
+ }
+
+ @Override
+ public Slice serialize(List<T> objects)
+ {
+ Slice[] slices = new Slice[objects.size()];
+
+ int size = 4;
+
+ for (int index = 0; index < objects.size(); index++) {
+ Slice slice = serde.serialize(objects.get(index));
+ slices[index] = slice;
+ size += slice.length;
+ }
+
+ byte[] bytes = new byte[size];
+ int offset = 0;
+
+ byte[] sizeBytes = GPOUtils.serializeInt(objects.size());
+ System.arraycopy(sizeBytes, 0, bytes, offset, 4);
+ offset += 4;
+
+ for (int index = 0; index < slices.length; index++) {
+ Slice slice = slices[index];
+ System.arraycopy(slice.buffer, slice.offset, bytes, offset, slice.length);
+ offset += slice.length;
+ }
+
+ return new Slice(bytes);
+ }
+
+ @Override
+ public List<T> deserialize(Slice slice, MutableInt offset)
+ {
+ MutableInt sliceOffset = new MutableInt(slice.offset + offset.intValue());
+
+ int numElements = GPOUtils.deserializeInt(slice.buffer, sliceOffset);
+ List<T> list = Lists.newArrayListWithCapacity(numElements);
+ sliceOffset.subtract(slice.offset);
+
+ for (int index = 0; index < numElements; index++) {
+ T object = serde.deserialize(slice, sliceOffset);
+ list.add(object);
+ }
+
+ offset.setValue(sliceOffset.intValue());
+ return list;
+ }
+
+ @Override
+ public List<T> deserialize(Slice slice)
+ {
+ return deserialize(slice, new MutableInt(0));
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeStringSlice.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeStringSlice.java b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeStringSlice.java
new file mode 100644
index 0000000..80ee597
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SerdeStringSlice.java
@@ -0,0 +1,53 @@
+/**
+ * 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.utils.serde;
+
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.datatorrent.lib.appdata.gpo.GPOUtils;
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * An implementation of {@link Serde} which serializes and deserializes {@link String}s.
+ */
+@InterfaceStability.Evolving
+public class SerdeStringSlice implements Serde<String, Slice>
+{
+ @Override
+ public Slice serialize(String object)
+ {
+ return new Slice(GPOUtils.serializeString(object));
+ }
+
+ @Override
+ public String deserialize(Slice object, MutableInt offset)
+ {
+ offset.add(object.offset);
+ String string = GPOUtils.deserializeString(object.buffer, offset);
+ offset.subtract(object.offset);
+ return string;
+ }
+
+ @Override
+ public String deserialize(Slice object)
+ {
+ return deserialize(object, new MutableInt(0));
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SliceUtils.java
----------------------------------------------------------------------
diff --git a/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SliceUtils.java b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SliceUtils.java
new file mode 100644
index 0000000..b6a61f4
--- /dev/null
+++ b/library/src/main/java/org/apache/apex/malhar/lib/utils/serde/SliceUtils.java
@@ -0,0 +1,101 @@
+/**
+ * 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.utils.serde;
+
+import org.apache.hadoop.classification.InterfaceStability;
+
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * A utility class which contains static methods for manipulating byte arrays and {@link Slice}s
+ */
+@InterfaceStability.Evolving
+public class SliceUtils
+{
+ private SliceUtils()
+ {
+ }
+
+ /**
+ * Concatenates two byte arrays.
+ * @param a The first byte array to concatenate.
+ * @param b The second byte array to concatenate.
+ * @return The concatenated byte arrays.
+ */
+ public static byte[] concatenate(byte[] a, byte[] b)
+ {
+ byte[] output = new byte[a.length + b.length];
+
+ System.arraycopy(a, 0, output, 0, a.length);
+ System.arraycopy(b, 0, output, a.length, b.length);
+ return output;
+ }
+
+ /**
+ * Concatenates two {@link Slice}s
+ * @param a The first {@link Slice} to concatenate.
+ * @param b The second {@link Slice} to concatenate.
+ * @return The concatenated {@link Slice}.
+ */
+ public static Slice concatenate(Slice a, Slice b)
+ {
+ int size = a.length + b.length;
+ byte[] bytes = new byte[size];
+
+ System.arraycopy(a.buffer, a.offset, bytes, 0, a.length);
+ System.arraycopy(b.buffer, b.offset, bytes, a.length, b.length);
+
+ return new Slice(bytes);
+ }
+
+ /**
+ * Concatenates a byte array with the contents of a {@link Slice}.
+ * @param a The byte array to concatenate. The contents of the byte array appear first in the concatenation.
+ * @param b The {@link Slice} to concatenate a byte array with.
+ * @return A {@link Slice} whose contents are the concatenation of the input byte array and {@link Slice}.
+ */
+ public static Slice concatenate(byte[] a, Slice b)
+ {
+ int size = a.length + b.length;
+ byte[] bytes = new byte[size];
+
+ System.arraycopy(a, 0, bytes, 0, a.length);
+ System.arraycopy(b.buffer, b.offset, bytes, a.length, b.length);
+
+ return new Slice(bytes);
+ }
+
+ /**
+ * Concatenates a byte array with the contents of a {@link Slice}.
+ * @param a The byte array to concatenate.
+ * @param b The {@link Slice} to concatenate a byte array with. The contents of the {@link Slice} appear first in the
+ * concatenation.
+ * @return A {@link Slice} whose contents are the concatenation of the input byte array and {@link Slice}.
+ */
+ public static Slice concatenate(Slice a, byte[] b)
+ {
+ int size = a.length + b.length;
+ byte[] bytes = new byte[size];
+
+ System.arraycopy(a.buffer, a.offset, bytes, 0, a.length);
+ System.arraycopy(b, 0, bytes, a.length, b.length);
+
+ return new Slice(bytes);
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/com/datatorrent/lib/util/TestUtils.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/com/datatorrent/lib/util/TestUtils.java b/library/src/test/java/com/datatorrent/lib/util/TestUtils.java
index f3b2140..673054b 100644
--- a/library/src/test/java/com/datatorrent/lib/util/TestUtils.java
+++ b/library/src/test/java/com/datatorrent/lib/util/TestUtils.java
@@ -26,15 +26,40 @@ import org.junit.runner.Description;
import org.apache.commons.io.FileUtils;
+import com.google.common.base.Preconditions;
+
import com.datatorrent.api.DefaultPartition;
import com.datatorrent.api.Operator;
import com.datatorrent.api.Operator.OutputPort;
import com.datatorrent.api.Sink;
import com.datatorrent.api.Stats;
import com.datatorrent.api.StatsListener;
+import com.datatorrent.netlet.util.Slice;
public class TestUtils
{
+ public static byte[] getByte(int val)
+ {
+ Preconditions.checkArgument(val <= Byte.MAX_VALUE);
+ return new byte[]{(byte)val};
+ }
+
+ public static byte[] getBytes(int val)
+ {
+ byte[] bytes = new byte[4];
+ bytes[0] = (byte)(val & 0xFF);
+ bytes[1] = (byte)((val >> 8) & 0xFF);
+ bytes[2] = (byte)((val >> 16) & 0xFF);
+ bytes[3] = (byte)((val >> 24) & 0xFF);
+
+ return bytes;
+ }
+
+ public static Slice getSlice(int val)
+ {
+ return new Slice(getBytes(val));
+ }
+
public static class TestInfo extends TestWatcher
{
public org.junit.runner.Description desc;
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/managed/ManagedStateTestUtils.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/managed/ManagedStateTestUtils.java b/library/src/test/java/org/apache/apex/malhar/lib/state/managed/ManagedStateTestUtils.java
index d3564ba..c57e0ca 100644
--- a/library/src/test/java/org/apache/apex/malhar/lib/state/managed/ManagedStateTestUtils.java
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/managed/ManagedStateTestUtils.java
@@ -44,7 +44,8 @@ import com.datatorrent.netlet.util.Slice;
public class ManagedStateTestUtils
{
- static void transferBucketHelper(FileAccess fileAccess, long bucketId, Map<Slice, Bucket.BucketedValue> unsavedBucket,
+ public static void transferBucketHelper(FileAccess fileAccess, long bucketId, Map<Slice, Bucket.BucketedValue>
+ unsavedBucket,
int keysPerTimeBucket) throws IOException
{
RemoteIterator<LocatedFileStatus> iterator = fileAccess.listFiles(bucketId);
@@ -82,7 +83,7 @@ public class ManagedStateTestUtils
Assert.assertEquals("data of bucket" + bucketId, testBucket, fromDisk);
}
- static Map<Long, Map<Slice, Bucket.BucketedValue>> getTestData(int startBucket, int endBucket, int keyStart)
+ public static Map<Long, Map<Slice, Bucket.BucketedValue>> getTestData(int startBucket, int endBucket, int keyStart)
{
Map<Long, Map<Slice, Bucket.BucketedValue>> data = Maps.newHashMap();
for (int i = startBucket; i < endBucket; i++) {
@@ -92,7 +93,7 @@ public class ManagedStateTestUtils
return data;
}
- static Map<Slice, Bucket.BucketedValue> getTestBucketData(int keyStart, long timeBucketStart)
+ public static Map<Slice, Bucket.BucketedValue> getTestBucketData(int keyStart, long timeBucketStart)
{
Map<Slice, Bucket.BucketedValue> bucketData = Maps.newHashMap();
for (int j = 0; j < 5; j++) {
@@ -103,14 +104,14 @@ public class ManagedStateTestUtils
return bucketData;
}
- static Context.OperatorContext getOperatorContext(int operatorId, String applicationPath)
+ public static Context.OperatorContext getOperatorContext(int operatorId, String applicationPath)
{
Attribute.AttributeMap.DefaultAttributeMap attributes = new Attribute.AttributeMap.DefaultAttributeMap();
attributes.put(DAG.APPLICATION_PATH, applicationPath);
return new OperatorContextTestHelper.TestIdOperatorContext(operatorId, attributes);
}
- static Context.OperatorContext getOperatorContext(int operatorId)
+ public static Context.OperatorContext getOperatorContext(int operatorId)
{
Attribute.AttributeMap.DefaultAttributeMap attributes = new Attribute.AttributeMap.DefaultAttributeMap();
return new OperatorContextTestHelper.TestIdOperatorContext(operatorId, attributes);
@@ -118,7 +119,7 @@ public class ManagedStateTestUtils
private static final transient Logger LOG = LoggerFactory.getLogger(ManagedStateTestUtils.class);
- static Slice getSliceFor(String x)
+ public static Slice getSliceFor(String x)
{
return new Slice(x.getBytes());
}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemMultisetTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemMultisetTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemMultisetTest.java
deleted file mode 100644
index d73bd95..0000000
--- a/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemMultisetTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- * 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.state.managed.spillable.inmem;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-import org.apache.apex.malhar.lib.state.spillable.inmem.InMemMultiset;
-
-import com.esotericsoftware.kryo.Kryo;
-
-import com.datatorrent.lib.util.KryoCloneUtils;
-
-public class InMemMultisetTest
-{
- @Test
- public void serializationTest()
- {
- InMemMultiset<String> set = new InMemMultiset<>();
-
- set.add("a");
- set.add("a");
-
- InMemMultiset<String> cloned = KryoCloneUtils.cloneObject(new Kryo(), set);
-
- Assert.assertEquals(2, cloned.count("a"));
- }
-}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemSpillableArrayListTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemSpillableArrayListTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemSpillableArrayListTest.java
deleted file mode 100644
index 573982a..0000000
--- a/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemSpillableArrayListTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- * 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.state.managed.spillable.inmem;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-import org.apache.apex.malhar.lib.state.spillable.inmem.InMemSpillableArrayList;
-
-import com.esotericsoftware.kryo.Kryo;
-
-import com.datatorrent.lib.util.KryoCloneUtils;
-
-public class InMemSpillableArrayListTest
-{
- @Test
- public void serializationTest()
- {
- InMemSpillableArrayList<String> list = new InMemSpillableArrayList<>();
-
- list.add("a");
- list.add("a");
-
- InMemSpillableArrayList<String> cloned = KryoCloneUtils.cloneObject(new Kryo(), list);
-
- Assert.assertEquals(2, list.size());
- }
-}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemSpillableByteArrayListMultimapTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemSpillableByteArrayListMultimapTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemSpillableByteArrayListMultimapTest.java
deleted file mode 100644
index a6bf811..0000000
--- a/library/src/test/java/org/apache/apex/malhar/lib/state/managed/spillable/inmem/InMemSpillableByteArrayListMultimapTest.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/**
- * 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.state.managed.spillable.inmem;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-import org.apache.apex.malhar.lib.state.spillable.inmem.InMemSpillableByteArrayListMultimap;
-
-import com.esotericsoftware.kryo.Kryo;
-
-import com.datatorrent.lib.util.KryoCloneUtils;
-
-public class InMemSpillableByteArrayListMultimapTest
-{
- @Test
- public void serializationTest()
- {
- InMemSpillableByteArrayListMultimap<String, String> multimap =
- new InMemSpillableByteArrayListMultimap<String, String>();
-
- multimap.put("a", "b");
- multimap.put("a", "c");
-
- InMemSpillableByteArrayListMultimap<String, String> cloned = KryoCloneUtils.cloneObject(new Kryo(), multimap);
-
- Assert.assertEquals(2, cloned.get("a").size());
- }
-}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SequentialSpillableIdentifierGeneratorTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SequentialSpillableIdentifierGeneratorTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SequentialSpillableIdentifierGeneratorTest.java
new file mode 100644
index 0000000..f40ae87
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SequentialSpillableIdentifierGeneratorTest.java
@@ -0,0 +1,125 @@
+/**
+ * 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.state.spillable;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.datatorrent.lib.util.TestUtils;
+
+public class SequentialSpillableIdentifierGeneratorTest
+{
+ @Test
+ public void dontAllowRegistrationAfterNextCallTest()
+ {
+ SequentialSpillableIdentifierGenerator gen = new SequentialSpillableIdentifierGenerator();
+
+ gen.next();
+
+ boolean exception = false;
+
+ try {
+ gen.register(TestUtils.getByte(1));
+ } catch (Exception e) {
+ exception = true;
+ }
+
+ Assert.assertTrue(exception);
+ }
+
+ @Test
+ public void simpleSequentialIdGenerationTest()
+ {
+ SequentialSpillableIdentifierGenerator gen = new SequentialSpillableIdentifierGenerator();
+
+ for (int index = 0; index < (((int)Byte.MAX_VALUE) + 1); index++) {
+ byte[] id = gen.next();
+
+ checkId(index, id);
+ }
+
+ boolean threwException = false;
+
+ try {
+ gen.next();
+ } catch (Exception e) {
+ threwException = true;
+ }
+
+ Assert.assertTrue(threwException);
+ }
+
+ @Test
+ public void registerFirst()
+ {
+ SequentialSpillableIdentifierGenerator gen = new SequentialSpillableIdentifierGenerator();
+ gen.register(TestUtils.getByte(0));
+
+ byte[] id = gen.next();
+
+ Assert.assertArrayEquals(TestUtils.getByte(1), id);
+ }
+
+ @Test
+ public void registerLast()
+ {
+ SequentialSpillableIdentifierGenerator gen = new SequentialSpillableIdentifierGenerator();
+ gen.register(TestUtils.getByte(Byte.MAX_VALUE));
+
+ for (int index = 0; index <= (((int)Byte.MAX_VALUE) - 1); index++) {
+ byte[] id = gen.next();
+
+ checkId(index, id);
+ }
+
+ boolean threwException = false;
+
+ try {
+ gen.next();
+ } catch (Exception e) {
+ threwException = true;
+ }
+
+ Assert.assertTrue(threwException);
+ }
+
+ @Test
+ public void intermingledRegistered()
+ {
+ SequentialSpillableIdentifierGenerator gen = new SequentialSpillableIdentifierGenerator();
+
+ gen.register(TestUtils.getByte(1));
+ gen.register(TestUtils.getByte(2));
+ gen.register(TestUtils.getByte(5));
+ gen.register(TestUtils.getByte(7));
+
+ checkId(0, gen.next());
+ checkId(3, gen.next());
+ checkId(4, gen.next());
+ checkId(6, gen.next());
+ checkId(8, gen.next());
+ checkId(9, gen.next());
+ }
+
+ private void checkId(int val, byte[] id)
+ {
+ Assert.assertEquals(1, id.length);
+ Assert.assertEquals(val, id[0]);
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableArrayListImplTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableArrayListImplTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableArrayListImplTest.java
new file mode 100644
index 0000000..af05c88
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableArrayListImplTest.java
@@ -0,0 +1,594 @@
+/**
+ * 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.state.spillable;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.apache.apex.malhar.lib.state.spillable.inmem.InMemSpillableStateStore;
+import org.apache.apex.malhar.lib.utils.serde.SerdeStringSlice;
+
+import com.google.common.collect.Lists;
+
+import com.datatorrent.api.Attribute;
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DAG;
+import com.datatorrent.lib.helper.OperatorContextTestHelper;
+import com.datatorrent.lib.util.KryoCloneUtils;
+
+public class SpillableArrayListImplTest
+{
+ public static final byte[] ID1 = new byte[]{(byte)0};
+ public static final byte[] ID2 = new byte[]{(byte)1};
+
+ @Rule
+ public SpillableTestUtils.TestMeta testMeta = new SpillableTestUtils.TestMeta();
+
+ @Test
+ public void simpleAddGetAndSetTest1()
+ {
+ InMemSpillableStateStore store = new InMemSpillableStateStore();
+
+ simpleAddGetAndSetTest1Helper(store);
+ }
+
+ @Test
+ public void simpleAddGetAndSetManagedStateTest1()
+ {
+ simpleAddGetAndSetTest1Helper(testMeta.store);
+ }
+
+ public void simpleAddGetAndSetTest1Helper(SpillableStateStore store)
+ {
+ SpillableArrayListImpl<String> list = new SpillableArrayListImpl<>(0L, ID1, store,
+ new SerdeStringSlice(), 1);
+
+ store.setup(testMeta.operatorContext);
+ list.setup(testMeta.operatorContext);
+
+ long windowId = 0L;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ SpillableTestUtils.checkOutOfBounds(list, 0);
+ Assert.assertEquals(0, list.size());
+
+ list.add("a");
+
+ SpillableTestUtils.checkOutOfBounds(list, 1);
+ Assert.assertEquals(1, list.size());
+
+ Assert.assertEquals("a", list.get(0));
+
+ list.addAll(Lists.newArrayList("a", "b", "c"));
+
+ Assert.assertEquals(4, list.size());
+
+ Assert.assertEquals("a", list.get(0));
+ Assert.assertEquals("a", list.get(1));
+ Assert.assertEquals("b", list.get(2));
+ Assert.assertEquals("c", list.get(3));
+
+ SpillableTestUtils.checkOutOfBounds(list, 4);
+
+ list.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, ID1, 0, Lists.newArrayList("a"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 1, Lists.newArrayList("a"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 2, Lists.newArrayList("b"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 3, Lists.newArrayList("c"));
+
+ Assert.assertEquals(4, list.size());
+
+ Assert.assertEquals("a", list.get(0));
+ Assert.assertEquals("a", list.get(1));
+ Assert.assertEquals("b", list.get(2));
+ Assert.assertEquals("c", list.get(3));
+
+ list.add("tt");
+ list.add("ab");
+ list.add("99");
+ list.add("oo");
+
+ Assert.assertEquals("tt", list.get(4));
+ Assert.assertEquals("ab", list.get(5));
+ Assert.assertEquals("99", list.get(6));
+ Assert.assertEquals("oo", list.get(7));
+
+ list.set(1, "111");
+
+ Assert.assertEquals("a", list.get(0));
+ Assert.assertEquals("111", list.get(1));
+ Assert.assertEquals("b", list.get(2));
+ Assert.assertEquals("c", list.get(3));
+ Assert.assertEquals("tt", list.get(4));
+ Assert.assertEquals("ab", list.get(5));
+ Assert.assertEquals("99", list.get(6));
+ Assert.assertEquals("oo", list.get(7));
+
+ list.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, ID1, 0, Lists.newArrayList("a"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 1, Lists.newArrayList("111"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 2, Lists.newArrayList("b"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 3, Lists.newArrayList("c"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 4, Lists.newArrayList("tt"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 5, Lists.newArrayList("ab"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 6, Lists.newArrayList("99"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 7, Lists.newArrayList("oo"));
+
+ list.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ list.teardown();
+ store.teardown();
+ }
+
+ @Test
+ public void simpleAddGetAndSetTest3()
+ {
+ InMemSpillableStateStore store = new InMemSpillableStateStore();
+
+ simpleAddGetAndSetTest3Helper(store);
+ }
+
+ @Test
+ public void simpleAddGetAndSetManagedStateTest3()
+ {
+ simpleAddGetAndSetTest3Helper(testMeta.store);
+ }
+
+ private void simpleAddGetAndSetTest3Helper(SpillableStateStore store)
+ {
+ SpillableArrayListImpl<String> list = new SpillableArrayListImpl<>(0L, ID1, store,
+ new SerdeStringSlice(), 3);
+
+ store.setup(testMeta.operatorContext);
+ list.setup(testMeta.operatorContext);
+
+ long windowId = 0L;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ SpillableTestUtils.checkOutOfBounds(list, 0);
+ Assert.assertEquals(0, list.size());
+
+ list.add("a");
+
+ SpillableTestUtils.checkOutOfBounds(list, 1);
+ Assert.assertEquals(1, list.size());
+
+ Assert.assertEquals("a", list.get(0));
+
+ list.addAll(Lists.newArrayList("a", "b", "c", "d", "e", "f", "g"));
+
+ Assert.assertEquals(8, list.size());
+
+ Assert.assertEquals("a", list.get(0));
+ Assert.assertEquals("a", list.get(1));
+ Assert.assertEquals("b", list.get(2));
+ Assert.assertEquals("c", list.get(3));
+ Assert.assertEquals("d", list.get(4));
+ Assert.assertEquals("e", list.get(5));
+ Assert.assertEquals("f", list.get(6));
+ Assert.assertEquals("g", list.get(7));
+
+ SpillableTestUtils.checkOutOfBounds(list, 20);
+
+ list.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, ID1, 0, Lists.newArrayList("a", "a", "b"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 1, Lists.newArrayList("c", "d", "e"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 2, Lists.newArrayList("f", "g"));
+
+ Assert.assertEquals(8, list.size());
+
+ Assert.assertEquals("a", list.get(0));
+ Assert.assertEquals("a", list.get(1));
+ Assert.assertEquals("b", list.get(2));
+ Assert.assertEquals("c", list.get(3));
+ Assert.assertEquals("d", list.get(4));
+ Assert.assertEquals("e", list.get(5));
+ Assert.assertEquals("f", list.get(6));
+ Assert.assertEquals("g", list.get(7));
+
+ list.add("tt");
+ list.add("ab");
+ list.add("99");
+ list.add("oo");
+
+ Assert.assertEquals("tt", list.get(8));
+ Assert.assertEquals("ab", list.get(9));
+ Assert.assertEquals("99", list.get(10));
+ Assert.assertEquals("oo", list.get(11));
+
+ list.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, ID1, 0, Lists.newArrayList("a", "a", "b"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 1, Lists.newArrayList("c", "d", "e"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 2, Lists.newArrayList("f", "g", "tt"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 3, Lists.newArrayList("ab", "99", "oo"));
+
+ list.set(1, "111");
+ list.set(3, "222");
+ list.set(5, "333");
+ list.set(11, "444");
+
+ Assert.assertEquals("a", list.get(0));
+ Assert.assertEquals("111", list.get(1));
+ Assert.assertEquals("b", list.get(2));
+ Assert.assertEquals("222", list.get(3));
+ Assert.assertEquals("d", list.get(4));
+ Assert.assertEquals("333", list.get(5));
+ Assert.assertEquals("f", list.get(6));
+ Assert.assertEquals("g", list.get(7));
+ Assert.assertEquals("tt", list.get(8));
+ Assert.assertEquals("ab", list.get(9));
+ Assert.assertEquals("99", list.get(10));
+ Assert.assertEquals("444", list.get(11));
+
+ list.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, ID1, 0, Lists.newArrayList("a", "111", "b"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 1, Lists.newArrayList("222", "d", "333"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 2, Lists.newArrayList("f", "g", "tt"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 3, Lists.newArrayList("ab", "99", "444"));
+
+ list.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ list.teardown();
+ store.teardown();
+ }
+
+ @Test
+ public void simpleMultiListTest()
+ {
+ InMemSpillableStateStore store = new InMemSpillableStateStore();
+
+ simpleMultiListTestHelper(store);
+ }
+
+ @Test
+ public void simpleMultiListManagedStateTest()
+ {
+ simpleMultiListTestHelper(testMeta.store);
+ }
+
+ public void simpleMultiListTestHelper(SpillableStateStore store)
+ {
+ SpillableArrayListImpl<String> list1 = new SpillableArrayListImpl<>(0L, ID1, store,
+ new SerdeStringSlice(), 1);
+
+ SpillableArrayListImpl<String> list2 = new SpillableArrayListImpl<>(0L, ID2, store,
+ new SerdeStringSlice(), 1);
+
+ store.setup(testMeta.operatorContext);
+ list1.setup(testMeta.operatorContext);
+ list2.setup(testMeta.operatorContext);
+
+ long windowId = 0L;
+ store.beginWindow(windowId);
+ list1.beginWindow(windowId);
+ list2.beginWindow(windowId);
+
+ SpillableTestUtils.checkOutOfBounds(list1, 0);
+ Assert.assertEquals(0, list1.size());
+
+ list1.add("a");
+
+ SpillableTestUtils.checkOutOfBounds(list2, 0);
+
+ list2.add("2a");
+
+ SpillableTestUtils.checkOutOfBounds(list1, 1);
+ SpillableTestUtils.checkOutOfBounds(list2, 1);
+
+ Assert.assertEquals(1, list1.size());
+ Assert.assertEquals(1, list2.size());
+
+ Assert.assertEquals("a", list1.get(0));
+ Assert.assertEquals("2a", list2.get(0));
+
+ list1.addAll(Lists.newArrayList("a", "b", "c"));
+ list2.addAll(Lists.newArrayList("2a", "2b"));
+
+ Assert.assertEquals(4, list1.size());
+ Assert.assertEquals(3, list2.size());
+
+ Assert.assertEquals("a", list1.get(0));
+ Assert.assertEquals("a", list1.get(1));
+ Assert.assertEquals("b", list1.get(2));
+ Assert.assertEquals("c", list1.get(3));
+
+ Assert.assertEquals("2a", list2.get(0));
+ Assert.assertEquals("2a", list2.get(1));
+ Assert.assertEquals("2b", list2.get(2));
+
+ SpillableTestUtils.checkOutOfBounds(list1, 4);
+ SpillableTestUtils.checkOutOfBounds(list2, 3);
+
+ list1.endWindow();
+ list2.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, ID1, 0, Lists.newArrayList("a"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 1, Lists.newArrayList("a"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 2, Lists.newArrayList("b"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 3, Lists.newArrayList("c"));
+
+ SpillableTestUtils.checkValue(store, 0L, ID2, 0, Lists.newArrayList("2a"));
+ SpillableTestUtils.checkValue(store, 0L, ID2, 1, Lists.newArrayList("2a"));
+ SpillableTestUtils.checkValue(store, 0L, ID2, 2, Lists.newArrayList("2b"));
+
+ windowId++;
+ store.beginWindow(windowId);
+ list1.beginWindow(windowId);
+ list2.beginWindow(windowId);
+
+ Assert.assertEquals(4, list1.size());
+ Assert.assertEquals(3, list2.size());
+
+ Assert.assertEquals("a", list1.get(0));
+ Assert.assertEquals("a", list1.get(1));
+ Assert.assertEquals("b", list1.get(2));
+ Assert.assertEquals("c", list1.get(3));
+
+ Assert.assertEquals("2a", list2.get(0));
+ Assert.assertEquals("2a", list2.get(1));
+ Assert.assertEquals("2b", list2.get(2));
+
+ list1.add("tt");
+ list1.add("ab");
+ list1.add("99");
+ list1.add("oo");
+
+ list2.add("2tt");
+ list2.add("2ab");
+
+ Assert.assertEquals("tt", list1.get(4));
+ Assert.assertEquals("ab", list1.get(5));
+ Assert.assertEquals("99", list1.get(6));
+ Assert.assertEquals("oo", list1.get(7));
+
+ Assert.assertEquals("2tt", list2.get(3));
+ Assert.assertEquals("2ab", list2.get(4));
+
+ list1.set(1, "111");
+ list2.set(1, "2111");
+
+ Assert.assertEquals("a", list1.get(0));
+ Assert.assertEquals("111", list1.get(1));
+ Assert.assertEquals("b", list1.get(2));
+ Assert.assertEquals("c", list1.get(3));
+ Assert.assertEquals("tt", list1.get(4));
+ Assert.assertEquals("ab", list1.get(5));
+ Assert.assertEquals("99", list1.get(6));
+ Assert.assertEquals("oo", list1.get(7));
+
+ Assert.assertEquals("2a", list2.get(0));
+ Assert.assertEquals("2111", list2.get(1));
+ Assert.assertEquals("2b", list2.get(2));
+ Assert.assertEquals("2tt", list2.get(3));
+ Assert.assertEquals("2ab", list2.get(4));
+
+ list1.endWindow();
+ list2.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ list1.beginWindow(windowId);
+ list2.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, ID1, 0, Lists.newArrayList("a"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 1, Lists.newArrayList("111"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 2, Lists.newArrayList("b"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 3, Lists.newArrayList("c"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 4, Lists.newArrayList("tt"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 5, Lists.newArrayList("ab"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 6, Lists.newArrayList("99"));
+ SpillableTestUtils.checkValue(store, 0L, ID1, 7, Lists.newArrayList("oo"));
+
+ SpillableTestUtils.checkValue(store, 0L, ID2, 0, Lists.newArrayList("2a"));
+ SpillableTestUtils.checkValue(store, 0L, ID2, 1, Lists.newArrayList("2111"));
+ SpillableTestUtils.checkValue(store, 0L, ID2, 2, Lists.newArrayList("2b"));
+ SpillableTestUtils.checkValue(store, 0L, ID2, 3, Lists.newArrayList("2tt"));
+ SpillableTestUtils.checkValue(store, 0L, ID2, 4, Lists.newArrayList("2ab"));
+
+ list1.endWindow();
+ list2.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ list1.teardown();
+ list2.teardown();
+ store.teardown();
+ }
+
+ @Test
+ public void recoveryManagedStateTest()
+ {
+ SpillableStateStore store = testMeta.store;
+
+ SpillableArrayListImpl<String> list = new SpillableArrayListImpl<>(0L, ID1, store,
+ new SerdeStringSlice(), 3);
+
+ store.setup(testMeta.operatorContext);
+ list.setup(testMeta.operatorContext);
+
+ long windowId = 0L;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ SpillableTestUtils.checkOutOfBounds(list, 0);
+
+ list.add("a");
+ list.addAll(Lists.newArrayList("a", "b", "c", "d", "e", "f", "g"));
+
+ Assert.assertEquals(8, list.size());
+
+ list.endWindow();
+ store.endWindow();
+
+ windowId++;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ list.add("tt");
+ list.add("ab");
+ list.add("99");
+ list.add("oo");
+
+ list.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ list.set(1, "111");
+ list.set(3, "222");
+ list.set(5, "333");
+ list.set(11, "444");
+
+ list.endWindow();
+ store.endWindow();
+
+ windowId++;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ list.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ long activationWindow = windowId;
+ SpillableArrayListImpl<String> clonedList = KryoCloneUtils.cloneObject(list);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ list.set(1, "111111");
+ list.set(3, "222222");
+ list.add("xyz");
+
+ list.endWindow();
+ store.endWindow();
+
+ list.teardown();
+ store.teardown();
+
+ Attribute.AttributeMap.DefaultAttributeMap attributes = new Attribute.AttributeMap.DefaultAttributeMap();
+ attributes.put(DAG.APPLICATION_PATH, testMeta.applicationPath);
+ attributes.put(Context.OperatorContext.ACTIVATION_WINDOW_ID, activationWindow);
+ Context.OperatorContext context =
+ new OperatorContextTestHelper.TestIdOperatorContext(testMeta.operatorContext.getId(), attributes);
+
+ list = clonedList;
+ store = clonedList.getStore();
+
+ store.setup(context);
+ list.setup(context);
+
+ windowId = activationWindow + 1L;
+ store.beginWindow(windowId);
+ list.beginWindow(windowId);
+
+ Assert.assertEquals("a", list.get(0));
+ Assert.assertEquals("111", list.get(1));
+ Assert.assertEquals("b", list.get(2));
+ Assert.assertEquals("222", list.get(3));
+ Assert.assertEquals("d", list.get(4));
+ Assert.assertEquals("333", list.get(5));
+ Assert.assertEquals("f", list.get(6));
+ Assert.assertEquals("g", list.get(7));
+ Assert.assertEquals("tt", list.get(8));
+ Assert.assertEquals("ab", list.get(9));
+ Assert.assertEquals("99", list.get(10));
+ Assert.assertEquals("444", list.get(11));
+ Assert.assertEquals(12, list.size());
+
+ list.endWindow();
+ store.endWindow();
+
+ list.teardown();
+ store.teardown();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteArrayListMultimapImplTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteArrayListMultimapImplTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteArrayListMultimapImplTest.java
new file mode 100644
index 0000000..42d7d20
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteArrayListMultimapImplTest.java
@@ -0,0 +1,341 @@
+/**
+ * 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.state.spillable;
+
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.apache.apex.malhar.lib.state.spillable.inmem.InMemSpillableStateStore;
+import org.apache.apex.malhar.lib.utils.serde.SerdeIntSlice;
+import org.apache.apex.malhar.lib.utils.serde.SerdeStringSlice;
+import org.apache.apex.malhar.lib.utils.serde.SliceUtils;
+
+import com.google.common.collect.Lists;
+
+import com.datatorrent.api.Attribute;
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DAG;
+import com.datatorrent.lib.helper.OperatorContextTestHelper;
+import com.datatorrent.lib.util.KryoCloneUtils;
+import com.datatorrent.netlet.util.Slice;
+
+public class SpillableByteArrayListMultimapImplTest
+{
+ public static final byte[] ID1 = new byte[]{(byte)0};
+
+ @Rule
+ public SpillableTestUtils.TestMeta testMeta = new SpillableTestUtils.TestMeta();
+
+ @Test
+ public void simpleMultiKeyTest()
+ {
+ InMemSpillableStateStore store = new InMemSpillableStateStore();
+
+ simpleMultiKeyTestHelper(store);
+ }
+
+ @Test
+ public void simpleMultiKeyManagedStateTest()
+ {
+ simpleMultiKeyTestHelper(testMeta.store);
+ }
+
+ public void simpleMultiKeyTestHelper(SpillableStateStore store)
+ {
+ SpillableByteArrayListMultimapImpl<String, String> map =
+ new SpillableByteArrayListMultimapImpl<String, String>(store, ID1, 0L, new SerdeStringSlice(),
+ new SerdeStringSlice());
+
+ store.setup(testMeta.operatorContext);
+ map.setup(testMeta.operatorContext);
+
+ long nextWindowId = 0L;
+ nextWindowId = simpleMultiKeyTestHelper(store, map, "a", nextWindowId);
+ nextWindowId++;
+
+ store.beginWindow(nextWindowId);
+ map.beginWindow(nextWindowId);
+
+ Assert.assertEquals(1, map.size());
+
+ map.endWindow();
+ store.endWindow();
+
+ nextWindowId++;
+ nextWindowId = simpleMultiKeyTestHelper(store, map, "b", nextWindowId);
+ nextWindowId++;
+
+ store.beginWindow(nextWindowId);
+ map.beginWindow(nextWindowId);
+
+ Assert.assertEquals(2, map.size());
+
+ map.endWindow();
+ store.endWindow();
+
+ nextWindowId++;
+ simpleMultiKeyTestHelper(store, map, "c", nextWindowId);
+
+ nextWindowId++;
+ store.beginWindow(nextWindowId);
+ map.beginWindow(nextWindowId);
+
+ Assert.assertEquals(3, map.size());
+
+ map.endWindow();
+ store.endWindow();
+
+ map.teardown();
+ store.teardown();
+ }
+
+ public long simpleMultiKeyTestHelper(SpillableStateStore store,
+ SpillableByteArrayListMultimapImpl<String, String> map, String key, long nextWindowId)
+ {
+ SerdeStringSlice serdeString = new SerdeStringSlice();
+ SerdeIntSlice serdeInt = new SerdeIntSlice();
+
+ Slice keySlice = serdeString.serialize(key);
+
+ byte[] keyBytes = SliceUtils.concatenate(ID1, keySlice.toByteArray());
+
+ nextWindowId++;
+ store.beginWindow(nextWindowId);
+ map.beginWindow(nextWindowId);
+
+ Assert.assertNull(map.get(key));
+
+ Assert.assertFalse(map.containsKey(key));
+
+ map.put(key, "a");
+
+ Assert.assertTrue(map.containsKey(key));
+
+ List<String> list1 = map.get(key);
+ Assert.assertEquals(1, list1.size());
+
+ Assert.assertEquals("a", list1.get(0));
+
+ list1.addAll(Lists.newArrayList("a", "b", "c", "d", "e", "f", "g"));
+
+ Assert.assertEquals(8, list1.size());
+
+ Assert.assertEquals("a", list1.get(0));
+ Assert.assertEquals("a", list1.get(1));
+ Assert.assertEquals("b", list1.get(2));
+ Assert.assertEquals("c", list1.get(3));
+ Assert.assertEquals("d", list1.get(4));
+ Assert.assertEquals("e", list1.get(5));
+ Assert.assertEquals("f", list1.get(6));
+ Assert.assertEquals("g", list1.get(7));
+
+ map.endWindow();
+ store.endWindow();
+
+ nextWindowId++;
+ store.beginWindow(nextWindowId);
+ map.beginWindow(nextWindowId);
+
+ SpillableTestUtils.checkValue(store, 0L,
+ SliceUtils.concatenate(keyBytes, SpillableByteArrayListMultimapImpl.SIZE_KEY_SUFFIX), 8, 0, serdeInt);
+
+ SpillableTestUtils.checkValue(store, 0L, keyBytes, 0, Lists.<String>newArrayList("a", "a", "b", "c", "d", "e",
+ "f", "g"));
+
+ List<String> list2 = map.get(key);
+
+ Assert.assertEquals(8, list2.size());
+
+ Assert.assertEquals("a", list2.get(0));
+ Assert.assertEquals("a", list2.get(1));
+ Assert.assertEquals("b", list2.get(2));
+ Assert.assertEquals("c", list2.get(3));
+ Assert.assertEquals("d", list2.get(4));
+ Assert.assertEquals("e", list2.get(5));
+ Assert.assertEquals("f", list2.get(6));
+ Assert.assertEquals("g", list2.get(7));
+
+ list2.add("tt");
+ list2.add("ab");
+ list2.add("99");
+ list2.add("oo");
+
+ Assert.assertEquals("tt", list2.get(8));
+ Assert.assertEquals("ab", list2.get(9));
+ Assert.assertEquals("99", list2.get(10));
+ Assert.assertEquals("oo", list2.get(11));
+
+ Assert.assertEquals(12, list2.size());
+
+ map.endWindow();
+ store.endWindow();
+
+ nextWindowId++;
+ store.beginWindow(nextWindowId);
+ map.beginWindow(nextWindowId);
+
+ Assert.assertEquals(12, list2.size());
+
+ SpillableTestUtils.checkValue(store, 0L,
+ SliceUtils.concatenate(keyBytes, SpillableByteArrayListMultimapImpl.SIZE_KEY_SUFFIX), 12, 0, serdeInt);
+
+ SpillableTestUtils.checkValue(store, 0L, keyBytes, 0, Lists.<String>newArrayList("a", "a", "b", "c", "d", "e",
+ "f", "g", "tt", "ab", "99", "oo"));
+
+ List<String> list3 = map.get(key);
+
+ list3.set(1, "111");
+ list3.set(3, "222");
+ list3.set(5, "333");
+ list3.set(11, "444");
+
+ Assert.assertEquals("a", list3.get(0));
+ Assert.assertEquals("111", list3.get(1));
+ Assert.assertEquals("b", list3.get(2));
+ Assert.assertEquals("222", list3.get(3));
+ Assert.assertEquals("d", list3.get(4));
+ Assert.assertEquals("333", list3.get(5));
+ Assert.assertEquals("f", list3.get(6));
+ Assert.assertEquals("g", list3.get(7));
+ Assert.assertEquals("tt", list3.get(8));
+ Assert.assertEquals("ab", list3.get(9));
+ Assert.assertEquals("99", list3.get(10));
+ Assert.assertEquals("444", list3.get(11));
+
+ Assert.assertEquals(12, list2.size());
+
+ map.endWindow();
+ store.endWindow();
+
+ nextWindowId++;
+ store.beginWindow(nextWindowId);
+ map.beginWindow(nextWindowId);
+
+ SpillableTestUtils.checkValue(store, 0L,
+ SliceUtils.concatenate(keyBytes, SpillableByteArrayListMultimapImpl.SIZE_KEY_SUFFIX), 12, 0, serdeInt);
+
+ SpillableTestUtils.checkValue(store, 0L, keyBytes, 0, Lists.<String>newArrayList("a", "111", "b", "222", "d", "333",
+ "f", "g", "tt", "ab", "99", "444"));
+
+ map.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(nextWindowId);
+
+ return nextWindowId;
+ }
+
+ @Test
+ public void recoveryTestWithManagedState()
+ {
+ SpillableStateStore store = testMeta.store;
+
+ SpillableByteArrayListMultimapImpl<String, String> map =
+ new SpillableByteArrayListMultimapImpl<>(store, ID1, 0L, new SerdeStringSlice(), new SerdeStringSlice());
+
+ store.setup(testMeta.operatorContext);
+ map.setup(testMeta.operatorContext);
+
+ long nextWindowId = 0L;
+ nextWindowId = simpleMultiKeyTestHelper(store, map, "a", nextWindowId);
+ long activationWindow = nextWindowId;
+ nextWindowId++;
+
+ SpillableByteArrayListMultimapImpl<String, String> clonedMap = KryoCloneUtils.cloneObject(map);
+ store.checkpointed(nextWindowId);
+ store.committed(nextWindowId);
+
+ store.beginWindow(nextWindowId);
+ map.beginWindow(nextWindowId);
+
+ List<String> list1 = map.get("a");
+
+ Assert.assertEquals(12, list1.size());
+
+ Assert.assertEquals("a", list1.get(0));
+ Assert.assertEquals("111", list1.get(1));
+ Assert.assertEquals("b", list1.get(2));
+ Assert.assertEquals("222", list1.get(3));
+ Assert.assertEquals("d", list1.get(4));
+ Assert.assertEquals("333", list1.get(5));
+ Assert.assertEquals("f", list1.get(6));
+ Assert.assertEquals("g", list1.get(7));
+ Assert.assertEquals("tt", list1.get(8));
+ Assert.assertEquals("ab", list1.get(9));
+ Assert.assertEquals("99", list1.get(10));
+ Assert.assertEquals("444", list1.get(11));
+
+ list1.add("111");
+
+ Assert.assertEquals("a", list1.get(0));
+ Assert.assertEquals("111", list1.get(1));
+ Assert.assertEquals("b", list1.get(2));
+ Assert.assertEquals("222", list1.get(3));
+ Assert.assertEquals("d", list1.get(4));
+ Assert.assertEquals("333", list1.get(5));
+ Assert.assertEquals("f", list1.get(6));
+ Assert.assertEquals("g", list1.get(7));
+ Assert.assertEquals("tt", list1.get(8));
+ Assert.assertEquals("ab", list1.get(9));
+ Assert.assertEquals("99", list1.get(10));
+ Assert.assertEquals("444", list1.get(11));
+ Assert.assertEquals("111", list1.get(12));
+
+ Assert.assertEquals(13, list1.size());
+
+ map.endWindow();
+ store.endWindow();
+
+ map.teardown();
+ store.teardown();
+
+ map = clonedMap;
+ store = map.getStore();
+
+ Attribute.AttributeMap.DefaultAttributeMap attributes = new Attribute.AttributeMap.DefaultAttributeMap();
+ attributes.put(DAG.APPLICATION_PATH, testMeta.applicationPath);
+ attributes.put(Context.OperatorContext.ACTIVATION_WINDOW_ID, activationWindow);
+ Context.OperatorContext context =
+ new OperatorContextTestHelper.TestIdOperatorContext(testMeta.operatorContext.getId(), attributes);
+
+ store.setup(context);
+ map.setup(context);
+
+ store.beginWindow(nextWindowId);
+ map.beginWindow(nextWindowId);
+
+ SerdeStringSlice serdeString = new SerdeStringSlice();
+ Slice keySlice = serdeString.serialize("a");
+ byte[] keyBytes = SliceUtils.concatenate(ID1, keySlice.toByteArray());
+
+ SpillableTestUtils.checkValue(store, 0L, keyBytes, 0, Lists.<String>newArrayList("a", "111", "b", "222", "d",
+ "333", "f", "g", "tt", "ab", "99", "444"));
+
+ Assert.assertEquals(1, map.size());
+ Assert.assertEquals(12, map.get("a").size());
+
+ map.endWindow();
+ store.endWindow();
+
+ map.teardown();
+ store.teardown();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteMapImplTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteMapImplTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteMapImplTest.java
new file mode 100644
index 0000000..63f7b79
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableByteMapImplTest.java
@@ -0,0 +1,484 @@
+/**
+ * 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.state.spillable;
+
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.apache.apex.malhar.lib.state.spillable.inmem.InMemSpillableStateStore;
+import org.apache.apex.malhar.lib.utils.serde.SerdeStringSlice;
+
+import com.datatorrent.api.Attribute;
+import com.datatorrent.api.Context;
+import com.datatorrent.api.DAG;
+import com.datatorrent.lib.helper.OperatorContextTestHelper;
+import com.datatorrent.lib.util.KryoCloneUtils;
+
+public class SpillableByteMapImplTest
+{
+ public static final byte[] ID1 = new byte[]{(byte)0};
+ public static final byte[] ID2 = new byte[]{(byte)1};
+
+ @Rule
+ public SpillableTestUtils.TestMeta testMeta = new SpillableTestUtils.TestMeta();
+
+ @Test
+ public void simpleGetAndPutTest()
+ {
+ InMemSpillableStateStore store = new InMemSpillableStateStore();
+
+ simpleGetAndPutTestHelper(store);
+ }
+
+ @Test
+ public void simpleGetAndPutManagedStateTest()
+ {
+ simpleGetAndPutTestHelper(testMeta.store);
+ }
+
+ private void simpleGetAndPutTestHelper(SpillableStateStore store)
+ {
+ SerdeStringSlice sss = new SerdeStringSlice();
+
+ SpillableByteMapImpl<String, String> map = new SpillableByteMapImpl<>(store, ID1, 0L,
+ new SerdeStringSlice(),
+ new SerdeStringSlice());
+
+ store.setup(testMeta.operatorContext);
+ map.setup(testMeta.operatorContext);
+
+ long windowId = 0L;
+ store.beginWindow(windowId);
+ map.beginWindow(windowId);
+
+ Assert.assertEquals(0, map.size());
+
+ map.put("a", "1");
+ map.put("b", "2");
+ map.put("c", "3");
+
+ Assert.assertEquals(3, map.size());
+
+ Assert.assertEquals("1", map.get("a"));
+ Assert.assertEquals("2", map.get("b"));
+ Assert.assertEquals("3", map.get("c"));
+ Assert.assertEquals(null, map.get("d"));
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, null);
+
+ map.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ map.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, "1");
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, "2");
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, "3");
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, null);
+
+ Assert.assertEquals(3, map.size());
+
+ Assert.assertEquals("1", map.get("a"));
+ Assert.assertEquals("2", map.get("b"));
+ Assert.assertEquals("3", map.get("c"));
+ Assert.assertEquals(null, map.get("d"));
+
+ map.put("d", "4");
+ map.put("e", "5");
+ map.put("f", "6");
+
+ Assert.assertEquals(6, map.size());
+
+ Assert.assertEquals("4", map.get("d"));
+ Assert.assertEquals("5", map.get("e"));
+ Assert.assertEquals("6", map.get("f"));
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, "1");
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, "2");
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, "3");
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "e", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "f", ID1, null);
+
+ map.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ map.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, "1");
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, "2");
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, "3");
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, "4");
+ SpillableTestUtils.checkValue(store, 0L, "e", ID1, "5");
+ SpillableTestUtils.checkValue(store, 0L, "f", ID1, "6");
+ SpillableTestUtils.checkValue(store, 0L, "g", ID1, null);
+
+ map.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ map.teardown();
+ store.teardown();
+ }
+
+ @Test
+ public void simpleRemoveTest()
+ {
+ InMemSpillableStateStore store = new InMemSpillableStateStore();
+
+ simpleRemoveTestHelper(store);
+ }
+
+ @Test
+ public void simpleRemoveManagedStateTest()
+ {
+ simpleRemoveTestHelper(testMeta.store);
+ }
+
+ private void simpleRemoveTestHelper(SpillableStateStore store)
+ {
+ SerdeStringSlice sss = new SerdeStringSlice();
+
+ SpillableByteMapImpl<String, String> map = new SpillableByteMapImpl<>(store, ID1, 0L,
+ new SerdeStringSlice(),
+ new SerdeStringSlice());
+
+ store.setup(testMeta.operatorContext);
+ map.setup(testMeta.operatorContext);
+
+ long windowId = 0L;
+ store.beginWindow(windowId);
+ map.beginWindow(windowId);
+
+ Assert.assertEquals(0, map.size());
+
+ map.put("a", "1");
+ map.put("b", "2");
+ map.put("c", "3");
+
+ Assert.assertEquals(3, map.size());
+
+ map.remove("b");
+ map.remove("c");
+
+ Assert.assertEquals("1", map.get("a"));
+ Assert.assertEquals(null, map.get("b"));
+ Assert.assertEquals(null, map.get("c"));
+ Assert.assertEquals(null, map.get("d"));
+
+ Assert.assertEquals(1, map.size());
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, null);
+
+ map.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, "1");
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, null);
+
+ windowId++;
+ store.beginWindow(windowId);
+ map.beginWindow(windowId);
+
+ Assert.assertEquals(1, map.size());
+
+ Assert.assertEquals("1", map.get("a"));
+ Assert.assertEquals(null, map.get("b"));
+ Assert.assertEquals(null, map.get("c"));
+ Assert.assertEquals(null, map.get("d"));
+
+ map.put("d", "4");
+ map.put("e", "5");
+ map.put("f", "6");
+
+ Assert.assertEquals(4, map.size());
+
+ Assert.assertEquals("4", map.get("d"));
+ Assert.assertEquals("5", map.get("e"));
+ Assert.assertEquals("6", map.get("f"));
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, "1");
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "e", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "f", ID1, null);
+
+ map.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ map.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, "1");
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, "4");
+ SpillableTestUtils.checkValue(store, 0L, "e", ID1, "5");
+ SpillableTestUtils.checkValue(store, 0L, "f", ID1, "6");
+ SpillableTestUtils.checkValue(store, 0L, "g", ID1, null);
+
+ map.remove("a");
+ map.remove("d");
+ Assert.assertEquals(null, map.get("a"));
+ Assert.assertEquals(null, map.get("b"));
+ Assert.assertEquals(null, map.get("c"));
+ Assert.assertEquals(null, map.get("d"));
+ Assert.assertEquals("5", map.get("e"));
+ Assert.assertEquals("6", map.get("f"));
+ Assert.assertEquals(null, map.get("g"));
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, "1");
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, "4");
+ SpillableTestUtils.checkValue(store, 0L, "e", ID1, "5");
+ SpillableTestUtils.checkValue(store, 0L, "f", ID1, "6");
+ SpillableTestUtils.checkValue(store, 0L, "g", ID1, null);
+
+ map.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ map.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "c", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "d", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "e", ID1, "5");
+ SpillableTestUtils.checkValue(store, 0L, "f", ID1, "6");
+ SpillableTestUtils.checkValue(store, 0L, "g", ID1, null);
+
+ map.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+ store.committed(windowId);
+
+ map.teardown();
+ store.teardown();
+ }
+
+ @Test
+ public void multiMapPerBucketTest()
+ {
+ InMemSpillableStateStore store = new InMemSpillableStateStore();
+
+ multiMapPerBucketTestHelper(store);
+ }
+
+ @Test
+ public void multiMapPerBucketManagedStateTest()
+ {
+ multiMapPerBucketTestHelper(testMeta.store);
+ }
+
+ public void multiMapPerBucketTestHelper(SpillableStateStore store)
+ {
+ SerdeStringSlice sss = new SerdeStringSlice();
+
+ SpillableByteMapImpl<String, String> map1 = new SpillableByteMapImpl<>(store, ID1, 0L,
+ new SerdeStringSlice(),
+ new SerdeStringSlice());
+ SpillableByteMapImpl<String, String> map2 = new SpillableByteMapImpl<>(store, ID2, 0L,
+ new SerdeStringSlice(),
+ new SerdeStringSlice());
+
+ store.setup(testMeta.operatorContext);
+ map1.setup(testMeta.operatorContext);
+ map2.setup(testMeta.operatorContext);
+
+ long windowId = 0L;
+ store.beginWindow(windowId);
+ map1.beginWindow(windowId);
+ map2.beginWindow(windowId);
+
+ map1.put("a", "1");
+
+ Assert.assertEquals("1", map1.get("a"));
+ Assert.assertEquals(null, map2.get("a"));
+
+ map2.put("a", "a1");
+
+ Assert.assertEquals("1", map1.get("a"));
+ Assert.assertEquals("a1", map2.get("a"));
+
+ map1.put("b", "2");
+ map2.put("c", "3");
+
+ Assert.assertEquals("1", map1.get("a"));
+ Assert.assertEquals("2", map1.get("b"));
+
+ Assert.assertEquals("a1", map2.get("a"));
+ Assert.assertEquals(null, map2.get("b"));
+ Assert.assertEquals("3", map2.get("c"));
+
+ map1.endWindow();
+ map2.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ map1.beginWindow(windowId);
+ map2.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, "1");
+ SpillableTestUtils.checkValue(store, 0L, "b", ID1, "2");
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID2, "a1");
+ SpillableTestUtils.checkValue(store, 0L, "b", ID2, null);
+ SpillableTestUtils.checkValue(store, 0L, "c", ID2, "3");
+
+ map1.remove("a");
+
+ Assert.assertEquals(null, map1.get("a"));
+ Assert.assertEquals("a1", map2.get("a"));
+
+ map1.endWindow();
+ map2.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+
+ windowId++;
+ store.beginWindow(windowId);
+ map1.beginWindow(windowId);
+ map2.beginWindow(windowId);
+
+ SpillableTestUtils.checkValue(store, 0L, "a", ID1, null);
+ SpillableTestUtils.checkValue(store, 0L, "a", ID2, "a1");
+
+ map1.endWindow();
+ map2.endWindow();
+ store.endWindow();
+ store.beforeCheckpoint(windowId);
+ store.checkpointed(windowId);
+
+ map1.teardown();
+ map2.teardown();
+ store.teardown();
+ }
+
+ @Test
+ public void recoveryWithManagedStateTest() throws Exception
+ {
+ SerdeStringSlice sss = new SerdeStringSlice();
+
+ SpillableByteMapImpl<String, String> map1 = new SpillableByteMapImpl<>(testMeta.store, ID1, 0L,
+ new SerdeStringSlice(),
+ new SerdeStringSlice());
+
+ testMeta.store.setup(testMeta.operatorContext);
+ map1.setup(testMeta.operatorContext);
+
+ testMeta.store.beginWindow(0);
+ map1.beginWindow(0);
+ map1.put("x", "1");
+ map1.put("y", "2");
+ map1.put("z", "3");
+ map1.put("zz", "33");
+ Assert.assertEquals(4, map1.size());
+ map1.endWindow();
+ testMeta.store.endWindow();
+
+ testMeta.store.beginWindow(1);
+ map1.beginWindow(1);
+ Assert.assertEquals(4, map1.size());
+ map1.put("x", "4");
+ map1.put("y", "5");
+ map1.remove("zz");
+ Assert.assertEquals(3, map1.size());
+ map1.endWindow();
+ testMeta.store.endWindow();
+ testMeta.store.beforeCheckpoint(1);
+ testMeta.store.checkpointed(1);
+
+ SpillableByteMapImpl<String, String> clonedMap1 = KryoCloneUtils.cloneObject(map1);
+
+ testMeta.store.beginWindow(2);
+ map1.beginWindow(2);
+ Assert.assertEquals(3, map1.size());
+ map1.put("x", "6");
+ map1.put("y", "7");
+ map1.put("w", "8");
+ Assert.assertEquals(4, map1.size());
+ map1.endWindow();
+ testMeta.store.endWindow();
+
+ // simulating crash here
+ map1.teardown();
+ testMeta.store.teardown();
+
+ Attribute.AttributeMap.DefaultAttributeMap attributes = new Attribute.AttributeMap.DefaultAttributeMap();
+ attributes.put(DAG.APPLICATION_PATH, testMeta.applicationPath);
+ attributes.put(Context.OperatorContext.ACTIVATION_WINDOW_ID, 1L);
+ Context.OperatorContext context =
+ new OperatorContextTestHelper.TestIdOperatorContext(testMeta.operatorContext.getId(), attributes);
+
+ map1 = clonedMap1;
+ map1.getStore().setup(context);
+ map1.setup(testMeta.operatorContext);
+
+ map1.getStore().beginWindow(2);
+ map1.beginWindow(2);
+ Assert.assertEquals(3, map1.size());
+ Assert.assertEquals("4", map1.get("x"));
+ Assert.assertEquals("5", map1.get("y"));
+ Assert.assertEquals("3", map1.get("z"));
+ map1.endWindow();
+ map1.getStore().endWindow();
+
+ map1.teardown();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponentImplTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponentImplTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponentImplTest.java
new file mode 100644
index 0000000..67db6ba
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableComplexComponentImplTest.java
@@ -0,0 +1,63 @@
+/**
+ * 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.state.spillable;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.apache.apex.malhar.lib.state.spillable.inmem.InMemSpillableStateStore;
+import org.apache.apex.malhar.lib.utils.serde.SerdeStringSlice;
+
+public class SpillableComplexComponentImplTest
+{
+ @Rule
+ public SpillableTestUtils.TestMeta testMeta = new SpillableTestUtils.TestMeta();
+
+ @Test
+ public void simpleIntegrationTest()
+ {
+ InMemSpillableStateStore store = new InMemSpillableStateStore();
+
+ simpleIntegrationTestHelper(store);
+ }
+
+ @Test
+ public void simpleIntegrationManagedStateTest()
+ {
+ simpleIntegrationTestHelper(testMeta.store);
+ }
+
+ public void simpleIntegrationTestHelper(SpillableStateStore store)
+ {
+ SpillableComplexComponentImpl sccImpl = new SpillableComplexComponentImpl(store);
+
+ Spillable.SpillableComponent scList =
+ (Spillable.SpillableComponent)sccImpl.newSpillableArrayList(0L, new SerdeStringSlice());
+ Spillable.SpillableComponent scMap =
+ (Spillable.SpillableComponent)sccImpl.newSpillableByteMap(0L, new SerdeStringSlice(), new SerdeStringSlice());
+
+ sccImpl.setup(testMeta.operatorContext);
+
+ sccImpl.beginWindow(0L);
+
+ sccImpl.endWindow();
+
+ sccImpl.teardown();
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableTestUtils.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableTestUtils.java b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableTestUtils.java
new file mode 100644
index 0000000..00ea58d
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/SpillableTestUtils.java
@@ -0,0 +1,134 @@
+/**
+ * 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.state.spillable;
+
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+import org.apache.apex.malhar.lib.state.managed.ManagedStateTestUtils;
+import org.apache.apex.malhar.lib.state.spillable.managed.ManagedStateSpillableStateStore;
+import org.apache.apex.malhar.lib.utils.serde.Serde;
+import org.apache.apex.malhar.lib.utils.serde.SerdeListSlice;
+import org.apache.apex.malhar.lib.utils.serde.SerdeStringSlice;
+import org.apache.apex.malhar.lib.utils.serde.SliceUtils;
+import org.apache.commons.lang3.mutable.MutableInt;
+
+import com.datatorrent.api.Context;
+import com.datatorrent.lib.appdata.gpo.GPOUtils;
+import com.datatorrent.lib.fileaccess.FileAccessFSImpl;
+import com.datatorrent.lib.util.TestUtils;
+import com.datatorrent.netlet.util.Slice;
+
+/**
+ * This class contains utility methods that can be used by Spillable data structure unit tests.
+ */
+public class SpillableTestUtils
+{
+ public static SerdeStringSlice SERDE_STRING_SLICE = new SerdeStringSlice();
+ public static SerdeListSlice<String> SERDE_STRING_LIST_SLICE = new SerdeListSlice(new SerdeStringSlice());
+
+ private SpillableTestUtils()
+ {
+ //Shouldn't instantiate this
+ }
+
+ static class TestMeta extends TestWatcher
+ {
+ ManagedStateSpillableStateStore store;
+ Context.OperatorContext operatorContext;
+ String applicationPath;
+
+ @Override
+ protected void starting(Description description)
+ {
+ TestUtils.deleteTargetTestClassFolder(description);
+ store = new ManagedStateSpillableStateStore();
+ applicationPath = "target/" + description.getClassName() + "/" + description.getMethodName();
+ ((FileAccessFSImpl)store.getFileAccess()).setBasePath(applicationPath + "/" + "bucket_data");
+
+ operatorContext = ManagedStateTestUtils.getOperatorContext(1, applicationPath);
+ }
+
+ @Override
+ protected void finished(Description description)
+ {
+ TestUtils.deleteTargetTestClassFolder(description);
+ }
+ }
+
+ public static Slice getKeySlice(byte[] id, String key)
+ {
+ return SliceUtils.concatenate(id, SERDE_STRING_SLICE.serialize(key));
+ }
+
+ public static Slice getKeySlice(byte[] id, int index, String key)
+ {
+ return SliceUtils.concatenate(id,
+ SliceUtils.concatenate(GPOUtils.serializeInt(index),
+ SERDE_STRING_SLICE.serialize(key)));
+ }
+
+ public static void checkValue(SpillableStateStore store, long bucketId, String key,
+ byte[] prefix, String expectedValue)
+ {
+ checkValue(store, bucketId, SliceUtils.concatenate(prefix, SERDE_STRING_SLICE.serialize(key)).buffer,
+ expectedValue, 0, SERDE_STRING_SLICE);
+ }
+
+ public static void checkValue(SpillableStateStore store, long bucketId,
+ byte[] prefix, int index, List<String> expectedValue)
+ {
+ checkValue(store, bucketId, SliceUtils.concatenate(prefix, GPOUtils.serializeInt(index)), expectedValue, 0,
+ SERDE_STRING_LIST_SLICE);
+ }
+
+ public static <T> void checkValue(SpillableStateStore store, long bucketId, byte[] bytes,
+ T expectedValue, int offset, Serde<T, Slice> serde)
+ {
+ Slice slice = store.getSync(bucketId, new Slice(bytes));
+
+ if (slice == null || slice.length == 0) {
+ if (expectedValue != null) {
+ Assert.assertEquals(expectedValue, slice);
+ } else {
+ return;
+ }
+ }
+
+ T string = serde.deserialize(slice, new MutableInt(offset));
+
+ Assert.assertEquals(expectedValue, string);
+ }
+
+ public static void checkOutOfBounds(SpillableArrayListImpl<String> list, int index)
+ {
+ boolean exceptionThrown = false;
+
+ try {
+ list.get(index);
+ } catch (IndexOutOfBoundsException ex) {
+ exceptionThrown = true;
+ }
+
+ Assert.assertTrue(exceptionThrown);
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/TimeBasedPriorityQueueTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/TimeBasedPriorityQueueTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/TimeBasedPriorityQueueTest.java
new file mode 100644
index 0000000..8033a7d
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/TimeBasedPriorityQueueTest.java
@@ -0,0 +1,134 @@
+/**
+ * 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.state.spillable;
+
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.google.common.collect.Sets;
+
+/**
+ * Created by tfarkas on 6/4/16.
+ */
+public class TimeBasedPriorityQueueTest
+{
+ @Test
+ public void simpleInsertAndRemoveTest()
+ {
+ TimeBasedPriorityQueue<String> queue = new TimeBasedPriorityQueue<String>();
+ queue.upSert("a");
+ queue.remove("a");
+
+ overRemoveTest(queue, 1);
+ }
+
+ @Test
+ public void simpleInsertAndLRURemoveTest()
+ {
+ TimeBasedPriorityQueue<String> queue = new TimeBasedPriorityQueue<String>();
+ queue.upSert("a");
+
+ Set<String> set = queue.removeLRU(1);
+
+ Assert.assertEquals(Sets.newHashSet("a"), set);
+ }
+
+ @Test
+ public void simpleLRUTest() throws Exception
+ {
+ TimeBasedPriorityQueue<String> queue = new TimeBasedPriorityQueue<String>();
+
+ queue.upSert("a");
+ Thread.sleep(1L);
+
+ queue.upSert("b");
+ Thread.sleep(1L);
+
+ queue.upSert("a");
+
+ Set<String> set = queue.removeLRU(1);
+
+ Assert.assertEquals(Sets.newHashSet("b"), set);
+ }
+
+ @Test
+ public void complexLRUTest() throws Exception
+ {
+ //0, 3, 6, 9
+ //1, 4, 7
+ //2, 5, 8
+
+ TimeBasedPriorityQueue<String> queue = new TimeBasedPriorityQueue<String>();
+
+ for (int counter = 0; counter < 10; counter++) {
+ String val = "" + counter;
+
+ queue.upSert(val);
+ Thread.sleep(1L);
+ }
+
+ for (int counter = 0; counter < 10; counter++) {
+ if (counter % 3 != 1) {
+ continue;
+ }
+
+ String val = "" + counter;
+ queue.remove(val);
+ }
+
+ for (int counter = 0; counter < 10; counter++) {
+ if (counter % 3 != 0) {
+ continue;
+ }
+
+ String val = "" + counter;
+ queue.upSert(val);
+ Thread.sleep(1L);
+ }
+
+ //2, 5, 8, 0, 3, 6, 9
+
+ overRemoveTest(queue, 8);
+
+ Set<String> expiredValues = queue.removeLRU(3);
+
+ Assert.assertEquals(Sets.newHashSet("2", "5", "8"), expiredValues);
+
+ overRemoveTest(queue, 6);
+
+ expiredValues = queue.removeLRU(4);
+
+ Assert.assertEquals(Sets.newHashSet("0", "3", "6", "9"), expiredValues);
+ }
+
+ private void overRemoveTest(TimeBasedPriorityQueue<String> queue, int removeCount)
+ {
+ boolean exceptionThrown = false;
+
+ try {
+ queue.removeLRU(removeCount);
+ } catch (IllegalArgumentException e) {
+ exceptionThrown = true;
+ }
+
+ Assert.assertTrue(exceptionThrown);
+ }
+}
http://git-wip-us.apache.org/repos/asf/apex-malhar/blob/9b6e11d8/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/WindowBoundedMapCacheTest.java
----------------------------------------------------------------------
diff --git a/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/WindowBoundedMapCacheTest.java b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/WindowBoundedMapCacheTest.java
new file mode 100644
index 0000000..b1710dd
--- /dev/null
+++ b/library/src/test/java/org/apache/apex/malhar/lib/state/spillable/WindowBoundedMapCacheTest.java
@@ -0,0 +1,116 @@
+/**
+ * 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.state.spillable;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.google.common.collect.Sets;
+
+public class WindowBoundedMapCacheTest
+{
+ @Test
+ public void simplePutGetTest()
+ {
+ WindowBoundedMapCache<String, String> cache = new WindowBoundedMapCache<>();
+
+ long windowId = 0L;
+
+ windowId++;
+
+ cache.put("1", "a");
+ Assert.assertEquals("a", cache.get("1"));
+
+ cache.endWindow();
+
+ windowId++;
+
+ Assert.assertEquals("a", cache.get("1"));
+
+ cache.endWindow();
+ }
+
+ @Test
+ public void getChangedGetRemovedTest()
+ {
+ WindowBoundedMapCache<String, String> cache = new WindowBoundedMapCache<>();
+
+ long windowId = 0L;
+
+ windowId++;
+
+ cache.put("1", "a");
+ cache.put("2", "b");
+
+ Assert.assertEquals(Sets.newHashSet("1", "2"), cache.getChangedKeys());
+ Assert.assertEquals(Sets.newHashSet(), cache.getRemovedKeys());
+
+ cache.endWindow();
+
+ windowId++;
+
+ cache.remove("1");
+
+ Assert.assertEquals(Sets.newHashSet(), cache.getChangedKeys());
+ Assert.assertEquals(Sets.newHashSet("1"), cache.getRemovedKeys());
+
+ Assert.assertEquals(null, cache.get("1"));
+ Assert.assertEquals("b", cache.get("2"));
+
+ cache.endWindow();
+
+ windowId++;
+
+ Assert.assertEquals(Sets.newHashSet(), cache.getChangedKeys());
+ Assert.assertEquals(Sets.newHashSet(), cache.getRemovedKeys());
+
+ cache.endWindow();
+ }
+
+ @Test
+ public void expirationTest() throws Exception
+ {
+ WindowBoundedMapCache<String, String> cache = new WindowBoundedMapCache<>(2);
+
+ long windowId = 0L;
+
+ windowId++;
+
+ cache.put("1", "a");
+ Thread.sleep(1L);
+ cache.put("2", "b");
+ Thread.sleep(1L);
+ cache.put("3", "c");
+
+ Assert.assertEquals(Sets.newHashSet("1", "2", "3"), cache.getChangedKeys());
+
+ cache.endWindow();
+
+ windowId++;
+
+ Assert.assertEquals(null, cache.get("1"));
+ Assert.assertEquals("b", cache.get("2"));
+ Assert.assertEquals("c", cache.get("3"));
+
+ Assert.assertEquals(Sets.newHashSet(), cache.getChangedKeys());
+ Assert.assertEquals(Sets.newHashSet(), cache.getRemovedKeys());
+
+ cache.endWindow();
+ }
+}