com.netflix.dyno.jedis.CursorBasedResultImpl Maven / Gradle / Ivy
/*******************************************************************************
* Copyright 2015 Netflix
*
* Licensed 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 com.netflix.dyno.jedis;
import com.netflix.dyno.connectionpool.CursorBasedResult;
import com.netflix.dyno.connectionpool.TokenRackMapper;
import redis.clients.jedis.ScanResult;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Encapsulates the results of performing a distributed SCAN operation.
*
* Example usage
*
* CursorBasedResult cbi = null;
* do {
* cbi = client.dyno_scan(cbi, "regex_pattern");
* .
* .
* .
* } while (!cbi.isComplete());
*
*/
public class CursorBasedResultImpl implements CursorBasedResult, TokenRackMapper {
private final Map> result;
private Map tokenRackMap;
/* package private */ CursorBasedResultImpl(Map> result) {
this.result = result;
this.tokenRackMap = new LinkedHashMap<>();
}
/* package private */ CursorBasedResultImpl(Map> result, Map tokenRackMap) {
this.result = result;
this.tokenRackMap = tokenRackMap;
}
@Override
public List getResult() {
final List aggregated = new ArrayList<>();
for (ScanResult sr : result.values()) {
aggregated.addAll(sr.getResult());
}
return aggregated;
}
@Override
public List getStringResult() {
final List aggregated = new ArrayList<>();
for (Map.Entry> entry : result.entrySet()) {
aggregated.add(String.format("%s -> %s", entry.getKey(), entry.getValue().getCursor()));
}
return aggregated;
}
@Override
public String getCursorForHost(String host) {
ScanResult sr = result.get(host);
if (sr != null) {
return sr.getCursor();
}
return "0";
}
@Override
public boolean isComplete() {
for (ScanResult r : result.values()) {
if (!r.getCursor().equals("0")) {
return false;
}
}
return true;
}
@Override
public String getRackForToken(Long token) {
return tokenRackMap.get(token);
}
public void setRackForToken(Long token, String rack) {
tokenRackMap.put(token, rack);
}
@Override
public Map getTokenRackMap() {
return this.tokenRackMap;
}
}