Github user aandis commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1798#discussion_r93878165
  
    --- Diff: 
external/storm-redis/src/main/java/org/apache/storm/redis/state/RedisKeyValueStateIterator.java
 ---
    @@ -0,0 +1,104 @@
    +/**
    + * 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 
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 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()) {
    +            return true;
    +        } else if (pendingCommitIterator != null && 
pendingCommitIterator.hasNext()) {
    +            return true;
    +        } else {
    +            return !cursor.equals("0");
    +        }
    +    }
    +
    +    @Override
    +    public Map.Entry<K, V> next() {
    +        Map.Entry<String, String> redisKeyValue = null;
    +        if (pendingPrepareIterator != null && 
pendingPrepareIterator.hasNext()) {
    --- End diff --
    
    > Instead of repeating the checks may be assign the current pending 
iterator to a variable in hasNext() and reuse it here.
    
    This assumes `hasNext` is always called before calling `next` which may not 
be true isn't it?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to