APIClient to support EstimatedHistogram
This patch modify the CacheEntry to support both String and EstimatedHistogram. It is possible to add more supported types in the future when needed. In the APIClient, the cache will now support both String and EstimatedHistogram in a similiar way. Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com> apiclient need to merge to cache
This commit is contained in:
parent
931571c3ed
commit
a7c30493cd
@ -26,6 +26,7 @@ import javax.ws.rs.core.MediaType;
|
|||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
import javax.ws.rs.core.UriBuilder;
|
import javax.ws.rs.core.UriBuilder;
|
||||||
|
|
||||||
|
import com.cloudius.urchin.utils.EstimatedHistogram;
|
||||||
import com.cloudius.urchin.utils.SnapshotDetailsTabularData;
|
import com.cloudius.urchin.utils.SnapshotDetailsTabularData;
|
||||||
import com.sun.jersey.api.client.Client;
|
import com.sun.jersey.api.client.Client;
|
||||||
import com.sun.jersey.api.client.WebResource;
|
import com.sun.jersey.api.client.WebResource;
|
||||||
@ -50,13 +51,23 @@ public class APIClient {
|
|||||||
}
|
}
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
String getFromCache(String key, long duration) {
|
|
||||||
|
String getStringFromCache(String key, long duration) {
|
||||||
if (key == null) {
|
if (key == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
CacheEntry value = cache.get(key);
|
CacheEntry value = cache.get(key);
|
||||||
return (value!= null && value.valid(duration))? value.value : null;
|
return (value!= null && value.valid(duration))? value.stringValue() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EstimatedHistogram getEstimatedHistogramFromCache(String key, long duration) {
|
||||||
|
if (key == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
CacheEntry value = cache.get(key);
|
||||||
|
return (value!= null && value.valid(duration))? value.getEstimatedHistogram() : null;
|
||||||
|
}
|
||||||
|
|
||||||
JsonReaderFactory factory = Json.createReaderFactory(null);
|
JsonReaderFactory factory = Json.createReaderFactory(null);
|
||||||
private static final java.util.logging.Logger logger = java.util.logging.Logger
|
private static final java.util.logging.Logger logger = java.util.logging.Logger
|
||||||
.getLogger(APIClient.class.getName());
|
.getLogger(APIClient.class.getName());
|
||||||
@ -131,7 +142,7 @@ public class APIClient {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
String key = getCacheKey(string, queryParams, duration);
|
String key = getCacheKey(string, queryParams, duration);
|
||||||
String res = getFromCache(key, duration);
|
String res = getStringFromCache(key, duration);
|
||||||
if (res != null) {
|
if (res != null) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -611,6 +622,20 @@ public class APIClient {
|
|||||||
return getHistogramValue(url, null);
|
return getHistogramValue(url, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EstimatedHistogram getEstimatedHistogram(String string,
|
||||||
|
MultivaluedMap<String, String> queryParams, long duration) {
|
||||||
|
String key = getCacheKey(string, queryParams, duration);
|
||||||
|
EstimatedHistogram res = getEstimatedHistogramFromCache(key, duration);
|
||||||
|
if (res != null) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
res = new EstimatedHistogram(getEstimatedHistogramAsLongArrValue(string, queryParams));
|
||||||
|
if (duration > 0) {
|
||||||
|
cache.put(key, new CacheEntry(res));
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
|
||||||
|
}
|
||||||
public long[] getEstimatedHistogramAsLongArrValue(String string,
|
public long[] getEstimatedHistogramAsLongArrValue(String string,
|
||||||
MultivaluedMap<String, String> queryParams) {
|
MultivaluedMap<String, String> queryParams) {
|
||||||
JsonObject obj = getJsonObj(string, queryParams);
|
JsonObject obj = getJsonObj(string, queryParams);
|
||||||
|
@ -1,20 +1,46 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2015 Cloudius Systems
|
* Copyright (C) 2015 ScyllaDB
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Scylla.
|
||||||
|
*
|
||||||
|
* Scylla is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Scylla is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.cloudius.urchin.api;
|
package com.cloudius.urchin.api;
|
||||||
|
|
||||||
|
import com.cloudius.urchin.utils.EstimatedHistogram;
|
||||||
|
|
||||||
public class CacheEntry {
|
public class CacheEntry {
|
||||||
long time;
|
long time;
|
||||||
public String value;
|
Object value;
|
||||||
|
|
||||||
CacheEntry(String value) {
|
CacheEntry(Object res) {
|
||||||
time = System.currentTimeMillis();
|
time = System.currentTimeMillis();
|
||||||
this.value = value;
|
this.value = res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean valid(long duration) {
|
public boolean valid(long duration) {
|
||||||
return (System.currentTimeMillis() - time) < duration;
|
return (System.currentTimeMillis() - time) < duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String stringValue() {
|
||||||
|
return (String) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EstimatedHistogram getEstimatedHistogram() {
|
||||||
|
return (EstimatedHistogram)value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user