New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storm-2203 Add a getAll method to KeyValueState interface #1798
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9fcd6ab
Add a getAll method to KeyValueState interface.
aandis 5d077d1
Implement getAll for InMemoryKeyValueState
aandis a2dc559
Implement getAll for RedisKeyValueState.
aandis b5fd35a
Change interface to return iterator.
aandis 9518433
Implement iterator for InMemoryKeyValueState.
aandis 0264b7f
Add RedisKeyValueStateIterator class.
aandis 8922a60
Extract encode/decode to use the RedisEncoder class.
aandis cc31d28
Extend iterable in KeyValuestate.
aandis 2f1e80e
Iterate over pendingPrepare and pendingCommit before iterating over R…
aandis ddc8795
use hasNext within next and throw NoSuchElementException.
aandis File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...al/storm-redis/src/main/java/org/apache/storm/redis/state/RedisKeyValueStateIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* 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.storm.redis.state; | ||
|
||
import com.google.common.base.Optional; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
|
||
import org.apache.storm.redis.common.container.JedisCommandsInstanceContainer; | ||
import org.apache.storm.redis.utils.RedisEncoder; | ||
import org.apache.storm.state.DefaultStateSerializer; | ||
import org.apache.storm.state.Serializer; | ||
|
||
import redis.clients.jedis.JedisCommands; | ||
import redis.clients.jedis.ScanParams; | ||
import redis.clients.jedis.ScanResult; | ||
|
||
/** | ||
* An iterator over {@link RedisKeyValueState} | ||
*/ | ||
public class RedisKeyValueStateIterator<K, V> implements Iterator<Map.Entry<K, V>> { | ||
|
||
private final String namespace; | ||
private final Iterator<Map.Entry<String, String>> pendingPrepareIterator; | ||
private final Iterator<Map.Entry<String, String>> pendingCommitIterator; | ||
private final RedisEncoder<K, V> decoder; | ||
private final JedisCommandsInstanceContainer jedisContainer; | ||
private final ScanParams scanParams; | ||
private Iterator<Map.Entry<String, String>> pendingIterator; | ||
private String cursor; | ||
private List<Map.Entry<String, String>> cachedResult; | ||
private int readPosition; | ||
|
||
public RedisKeyValueStateIterator(String namespace, JedisCommandsInstanceContainer jedisContainer, Iterator<Map.Entry<String, String>> pendingPrepareIterator, Iterator<Map.Entry<String, String>> pendingCommitIterator, int chunkSize, Serializer<K> keySerializer, Serializer<V> valueSerializer) { | ||
this.namespace = namespace; | ||
this.pendingPrepareIterator = pendingPrepareIterator; | ||
this.pendingCommitIterator = pendingCommitIterator; | ||
this.jedisContainer = jedisContainer; | ||
this.decoder = new RedisEncoder<K, V>(keySerializer, valueSerializer); | ||
this.scanParams = new ScanParams().count(chunkSize); | ||
this.cursor = ScanParams.SCAN_POINTER_START; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (pendingPrepareIterator != null && pendingPrepareIterator.hasNext()) { | ||
pendingIterator = pendingPrepareIterator; | ||
return true; | ||
} else if (pendingCommitIterator != null && pendingCommitIterator.hasNext()) { | ||
pendingIterator = pendingCommitIterator; | ||
return true; | ||
} else { | ||
pendingIterator = null; | ||
return !cursor.equals("0"); | ||
} | ||
} | ||
|
||
@Override | ||
public Map.Entry<K, V> next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
Map.Entry<String, String> redisKeyValue = null; | ||
if (pendingIterator != null) { | ||
redisKeyValue = pendingIterator.next(); | ||
} else { | ||
if (cachedResult == null || readPosition >= cachedResult.size()) { | ||
JedisCommands commands = null; | ||
try { | ||
commands = jedisContainer.getInstance(); | ||
ScanResult<Map.Entry<String, String>> scanResult = commands.hscan(namespace, cursor, scanParams); | ||
cachedResult = scanResult.getResult(); | ||
cursor = scanResult.getStringCursor(); | ||
readPosition = 0; | ||
} finally { | ||
jedisContainer.returnInstance(commands); | ||
} | ||
} | ||
redisKeyValue = cachedResult.get(readPosition); | ||
readPosition += 1; | ||
} | ||
K key = decoder.decodeKey(redisKeyValue.getKey()); | ||
V value = decoder.decodeValue(redisKeyValue.getValue()); | ||
return new AbstractMap.SimpleEntry(key, value); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
external/storm-redis/src/main/java/org/apache/storm/redis/utils/RedisEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* 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.storm.redis.utils; | ||
|
||
import com.google.common.base.Optional; | ||
|
||
import org.apache.commons.codec.binary.Base64; | ||
import org.apache.storm.state.DefaultStateSerializer; | ||
import org.apache.storm.state.Serializer; | ||
|
||
/** | ||
* Helper class for encoding/decoding redis key values. | ||
*/ | ||
public class RedisEncoder<K, V> { | ||
|
||
public static final Serializer<Optional<byte[]>> internalValueSerializer = new DefaultStateSerializer<>(); | ||
|
||
public static final String TOMBSTONE = encode(internalValueSerializer.serialize(Optional.absent())); | ||
|
||
private final Serializer<K> keySerializer; | ||
private final Serializer<V> valueSerializer; | ||
|
||
public RedisEncoder(Serializer<K> keySerializer, Serializer<V> valueSerializer) { | ||
this.keySerializer = keySerializer; | ||
this.valueSerializer = valueSerializer; | ||
} | ||
|
||
public Serializer<K> getKeySerializer() { | ||
return keySerializer; | ||
} | ||
|
||
public Serializer<V> getValueSerializer() { | ||
return valueSerializer; | ||
} | ||
|
||
public String encodeKey(K key) { | ||
return encode(keySerializer.serialize(key)); | ||
} | ||
|
||
public String encodeValue(V value) { | ||
return encode(internalValueSerializer.serialize( | ||
Optional.of(valueSerializer.serialize(value)))); | ||
} | ||
|
||
public K decodeKey(String redisKey) { | ||
return keySerializer.deserialize(decode(redisKey)); | ||
} | ||
|
||
public V decodeValue(String redisValue) { | ||
Optional<byte[]> internalValue = internalValueSerializer.deserialize(decode(redisValue)); | ||
if (internalValue.isPresent()) { | ||
return valueSerializer.deserialize(internalValue.get()); | ||
} | ||
return null; | ||
} | ||
|
||
private static String encode(byte[] bytes) { | ||
return Base64.encodeBase64String(bytes); | ||
} | ||
|
||
private static byte[] decode(String s) { | ||
return Base64.decodeBase64(s); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
next should throw a
NoSuchElementException
if there are no more elements. https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html#next()in
next
add a check in the beginning.