Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* The MIT License (MIT)
*
* Copyright (c) 2011-2016 Incapture Technologies LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/package rapture.series.mem;
import rapture.common.RaptureFolderInfo;
import rapture.common.SeriesValue;
import rapture.common.exception.RaptureExceptionFactory;
import rapture.dsl.serfun.DecimalSeriesValue;
import rapture.dsl.serfun.LongSeriesValue;
import rapture.dsl.serfun.StringSeriesValue;
import rapture.dsl.serfun.StructureSeriesValueImpl;
import rapture.series.SeriesPaginator;
import rapture.series.SeriesStore;
import rapture.series.children.ChildrenRepo;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
/**
* An in memory version of a series repo, for testing
*
* @author amkimian
*/publicclassMemorySeriesStoreimplementsSeriesStore{
private Map> seriesStore = Maps.newHashMap();
privatefinal ChildrenRepo childrenRepo;
publicMemorySeriesStore(){
this.childrenRepo = new ChildrenRepo() {
@Overridepublic ListgetPoints(String key){
return MemorySeriesStore.this.getPoints(key);
}
@OverridepublicbooleandropPoints(String key, List points){
return MemorySeriesStore.this.deletePointsFromSeriesByPointKey(key, points);
}
@OverridepublicbooleanaddPoint(String key, SeriesValue value){
MemorySeriesStore.this.addPointToSeries(key, value);
returntrue;
}
@OverridepublicvoiddropRow(String key){
MemorySeriesStore.this.deletePointsFromSeries(key);
}
};
}
@Overridepublicvoiddrop(){
seriesStore = Maps.newHashMap();
}
private SortedMapgetOrMakeSeries(String key){
if (!seriesStore.containsKey(key)) {
SortedMap data = Maps.newTreeMap();
seriesStore.put(key, data);
childrenRepo.registerParentage(key);
return data;
}
return seriesStore.get(key);
}
private SortedMapgetSeries(String key){
return seriesStore.get(key);
}
@OverridepublicvoidaddPointToSeries(String key, SeriesValue value){
if (value.getColumn() == null) throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, "Null column not allowed");
SortedMap series = getOrMakeSeries(key);
series.put(value.getColumn(), value);
}
@OverridepublicvoidaddDoubleToSeries(String key, String column, double value){
addPointToSeries(key, new DecimalSeriesValue(value, column));
}
@OverridepublicvoidaddDoublesToSeries(String key, List columns, List values){
addPointsToSeries(key, DecimalSeriesValue.zip(columns, values));
}
@OverridepublicvoidaddLongToSeries(String key, String column, long value){
addPointToSeries(key, new LongSeriesValue(value, column));
}
@OverridepublicvoidaddLongsToSeries(String key, List columns, List values){
addPointsToSeries(key, LongSeriesValue.zip(columns, values));
}
@OverridepublicvoidaddStringToSeries(String key, String column, String value){
addPointToSeries(key, new StringSeriesValue(value, column));
}
@OverridepublicvoidaddStringsToSeries(String key, List columns, List values){
addPointsToSeries(key, StringSeriesValue.zip(columns, values));
}
@OverridepublicvoidaddStructureToSeries(String key, String column, String jsonValue){
try {
addPointToSeries(key, StructureSeriesValueImpl.unmarshal(jsonValue, column));
} catch (IOException e) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, "Error parsing json value " + jsonValue, e);
}
}
@OverridepublicvoidaddStructuresToSeries(String key, List columns, List jsonValues){
try {
addPointsToSeries(key, StructureSeriesValueImpl.zip(columns, jsonValues));
} catch (IOException e) {
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, "Error parsing json values", e);
}
}
@OverridepublicvoidaddPointsToSeries(String key, List values){
boolean nullKey = false;
for (SeriesValue value : values) {
if (value.getColumn() == null) nullKey = true;
else addPointToSeries(key, value);
}
if (nullKey) throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, "Column Key may not be null, other values added");
}
@Overridepublic Boolean deletePointsFromSeriesByPointKey(String key, List pointKeys){
SortedMap series = getSeries(key);
if (series == null) returnfalse;
for (String column : pointKeys) {
series.remove(column);
}
// Do not drop an empty series here. It's OK to have a series without any points in it.returntrue;
}
privatevoiddropSeries(String key){
seriesStore.remove(key);
childrenRepo.dropFileEntry(key);
}
@OverridepublicvoiddeletePointsFromSeries(String key){
dropSeries(key);
}
@Overridepublic ListgetPoints(String key){
SortedMap map = getSeries(key);
if (map == null) return Lists.newArrayList();
return Lists.newArrayList(map.values());
}
@Overridepublic ListgetPointsAfter(String key, String startColumn, int maxNumber){
SortedMap map = getSeries(key);
if (map == null) return Lists.newArrayList();
Iterator iter = map.tailMap(startColumn).values().iterator();
List result = Lists.newLinkedList();
for (int i = 0; i < maxNumber; i++) {
if (!iter.hasNext()) break;
result.add(iter.next());
}
return result;
}
@Overridepublic ListgetPointsAfterReverse(String key, final String startColumn, int maxNumber){
SortedMap map = getSeries(key);
if (map == null) return Lists.newArrayList();
Iterator> entryIterator = Iterables.filter(map.entrySet(), new Predicate>() {
@Overridepublicbooleanapply(Map.Entry input){
return startColumn.compareTo(input.getKey()) >= 0;
}
}).iterator();
List vals = Lists.newArrayList();
while (entryIterator.hasNext()) {
vals.add(entryIterator.next().getValue());
}
Collections.reverse(vals);
if (maxNumber > vals.size()) {
returnnew ArrayList<>(vals);
}
returnnew ArrayList<>(vals.subList(0, maxNumber));
}
@OverridepublicvoidsetInstanceName(String instanceName){
}
@OverridepublicvoidsetConfig(Map config){
drop();
}
@Overridepublic ListgetSeriesLike(String keyPrefix){
List ret = Lists.newArrayList();
for (String key : seriesStore.keySet()) {
if (key.startsWith(keyPrefix)) {
ret.add(key);
}
}
return ret;
}
@Overridepublic ListgetPointsAfter(String key, String startColumn, String endColumn, int maxNumber){
SortedMap map = getSeries(key);
if (map == null) return Lists.newArrayList();
Iterator iter = map.subMap(startColumn, endColumn.concat("\0")).values().iterator();
List result = Lists.newLinkedList();
for (int i = 0; i < maxNumber; i++) {
if (!iter.hasNext()) break;
result.add(iter.next());
}
return result;
}
@Overridepublic IterablegetRangeAsIteration(String key, String startCol, String endCol, int pageSize){
returnnew SeriesPaginator(key, startCol, endCol, pageSize, this);
}
@Overridepublic ListgetRangeAsList(String key, String startCol, String endCol){
return getPointsAfter(key, startCol, endCol, Integer.MAX_VALUE);
}
@Overridepublic ListlistSeriesByUriPrefix(String string){
return childrenRepo.getChildren(string);
}
@OverridepublicvoidunregisterKey(String key){
childrenRepo.dropFileEntry(key);
}
@Overridepublic SeriesValue getLastPoint(String key){
SortedMap map = getSeries(key);
String lastKey = map.lastKey();
return map.get(lastKey);
}
@OverridepublicvoidunregisterKey(String key, boolean isFolder){
if (isFolder) childrenRepo.dropFolderEntry(key);
childrenRepo.dropFileEntry(key);
}
}