All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rapture.series.mem.MemorySeriesStore Maven / Gradle / Ivy

There is a newer version: 3.0.4
Show newest version
/**
 * 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
 */
public class MemorySeriesStore implements SeriesStore {

    private Map> seriesStore = Maps.newHashMap();
    private final ChildrenRepo childrenRepo;

    public MemorySeriesStore() {
        this.childrenRepo = new ChildrenRepo() {

            @Override
            public List getPoints(String key) {
                return MemorySeriesStore.this.getPoints(key);
            }

            @Override
            public boolean dropPoints(String key, List points) {
                return MemorySeriesStore.this.deletePointsFromSeriesByPointKey(key, points);
            }

            @Override
            public boolean addPoint(String key, SeriesValue value) {
                MemorySeriesStore.this.addPointToSeries(key, value);
                return true;
            }

            @Override
            public void dropRow(String key) {
                MemorySeriesStore.this.deletePointsFromSeries(key);
            }
        };
    }

    @Override
    public void drop() {
        seriesStore = Maps.newHashMap();
    }

    private SortedMap getOrMakeSeries(String key) {
        if (!seriesStore.containsKey(key)) {
            SortedMap data = Maps.newTreeMap();
            seriesStore.put(key, data);
            childrenRepo.registerParentage(key);
            return data;
        }
        return seriesStore.get(key);
    }

    private SortedMap getSeries(String key) {
        return seriesStore.get(key);
    }

    @Override
    public void addPointToSeries(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);
    }

    @Override
    public void addDoubleToSeries(String key, String column, double value) {
        addPointToSeries(key, new DecimalSeriesValue(value, column));
    }

    @Override
    public void addDoublesToSeries(String key, List columns, List values) {
        addPointsToSeries(key, DecimalSeriesValue.zip(columns, values));
    }

    @Override
    public void addLongToSeries(String key, String column, long value) {
        addPointToSeries(key, new LongSeriesValue(value, column));
    }

    @Override
    public void addLongsToSeries(String key, List columns, List values) {
        addPointsToSeries(key, LongSeriesValue.zip(columns, values));
    }

    @Override
    public void addStringToSeries(String key, String column, String value) {
        addPointToSeries(key, new StringSeriesValue(value, column));
    }

    @Override
    public void addStringsToSeries(String key, List columns, List values) {
        addPointsToSeries(key, StringSeriesValue.zip(columns, values));
    }

    @Override
    public void addStructureToSeries(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);
        }
    }

    @Override
    public void addStructuresToSeries(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);
        }
    }

    @Override
    public void addPointsToSeries(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");
    }

    @Override
    public Boolean deletePointsFromSeriesByPointKey(String key, List pointKeys) {
        SortedMap series = getSeries(key);
        if (series == null) return false;
        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.
        return true;
    }

    private void dropSeries(String key) {
        seriesStore.remove(key);
        childrenRepo.dropFileEntry(key);
    }

    @Override
    public void deletePointsFromSeries(String key) {
        dropSeries(key);
    }

    @Override
    public List getPoints(String key) {
        SortedMap map = getSeries(key);
        if (map == null) return Lists.newArrayList();
        return Lists.newArrayList(map.values());
    }

    @Override
    public List getPointsAfter(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;
    }

    @Override
    public List getPointsAfterReverse(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>() {
            @Override
            public boolean apply(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()) {
            return new ArrayList<>(vals);
        }
        return new ArrayList<>(vals.subList(0, maxNumber));
    }

    @Override
    public void setInstanceName(String instanceName) {
    }

    @Override
    public void setConfig(Map config) {
        drop();
    }

    @Override
    public List getSeriesLike(String keyPrefix) {
        List ret = Lists.newArrayList();
        for (String key : seriesStore.keySet()) {
            if (key.startsWith(keyPrefix)) {
                ret.add(key);
            }
        }
        return ret;
    }

    @Override
    public List getPointsAfter(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;
    }

    @Override
    public Iterable getRangeAsIteration(String key, String startCol, String endCol, int pageSize) {
        return new SeriesPaginator(key, startCol, endCol, pageSize, this);
    }

    @Override
    public List getRangeAsList(String key, String startCol, String endCol) {
        return getPointsAfter(key, startCol, endCol, Integer.MAX_VALUE);
    }

    @Override
    public List listSeriesByUriPrefix(String string) {
        return childrenRepo.getChildren(string);
    }

    @Override
    public void unregisterKey(String key) {
        childrenRepo.dropFileEntry(key);
    }

    @Override
    public SeriesValue getLastPoint(String key) {
        SortedMap map = getSeries(key);
        String lastKey = map.lastKey();
        return map.get(lastKey);
    }

    @Override
    public void unregisterKey(String key, boolean isFolder) {
        if (isFolder) childrenRepo.dropFolderEntry(key);
        childrenRepo.dropFileEntry(key);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy