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

rapture.repo.postgres.PostgresDataStore Maven / Gradle / Ivy

/**
 * Copyright (C) 2011-2015 Incapture Technologies LLC
 *
 * This is an autogenerated license statement. When copyright notices appear below
 * this one that copyright supercedes this statement.
 *
 * Unless required by applicable law or agreed to in writing, software is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied.
 *
 * Unless explicit permission obtained in writing this software cannot be distributed.
 */
package rapture.repo.postgres;

import rapture.common.RaptureFolderInfo;
import rapture.common.exception.RaptNotSupportedException;
import rapture.common.impl.jackson.JacksonUtil;
import rapture.index.IndexProducer;
import rapture.index.IndexHandler;
import rapture.postgres.PostgresFactory;
import rapture.repo.AbstractKeyStore;
import rapture.repo.KeyStore;
import rapture.repo.StoreKeyVisitor;
import rapture.table.postgres.PostgresIndexHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Lists;

public class PostgresDataStore extends AbstractKeyStore implements KeyStore {
    public static final String PREFIX = "prefix";

    @Override
    public void setConfig(Map config) {
        tableName = config.get(PREFIX).replace(".", "_");
        if (tableName == null || tableName.isEmpty()) {
            tableName = "__data__" + instanceName;
        }

        docHandler = new PostgresDocHandler(instanceName, tableName);
        docHandler.setDataSource(PostgresFactory.getDataSource(instanceName));
        docHandler.initialize();

        if (needsFolderHandling) {
            folderHandler = new PostgresFolderHandler(instanceName, tableName);
            folderHandler.setDataSource(PostgresFactory.getDataSource(instanceName));
            folderHandler.initialize();
        }
    }

    @Override
    public KeyStore createRelatedKeyStore(String relation) {
        Map config = new HashMap();
        config.put(PREFIX, tableName + "_" + relation);
        PostgresDataStore related = new PostgresDataStore();
        related.resetNeedsFolderHandling();
        related.setInstanceName(instanceName);
        related.setConfig(config);
        return related;
    }

    @Override
    public void setInstanceName(String instanceName) {
        this.instanceName = instanceName;
    }

    @Override
    public void visitKeys(final String prefix, final StoreKeyVisitor iStoreKeyVisitor) {
        // Visit all keys and values
        docHandler.visitKeys(prefix, iStoreKeyVisitor);
    }

    @Override
    public IndexHandler createIndexHandler(IndexProducer indexProducer) {
        Map config = new HashMap();
        config.put(PREFIX, tableName);

        PostgresIndexHandler indexHandler = new PostgresIndexHandler();
        indexHandler.setInstanceName(instanceName);
        indexHandler.setIndexProducer(indexProducer);
        indexHandler.setConfig(config);
        indexHandler.setDataSource(PostgresFactory.getDataSource(instanceName));

        indexHandler.initialize();
        return indexHandler;
    }

    @Override
    public Boolean validate() {
        return true;
    }

    @Override
    public long getSize() {
        return docHandler.getSize();
    }

    @Override
    public boolean matches(String key, String value) {
        if (value != null) {
            String existing = get(key);
            if (existing != null) {
                existing = StringUtils.deleteWhitespace(JacksonUtil.jsonFromObject(JacksonUtil.getMapFromJson(existing)));
                //noinspection ConstantConditions
                return existing.equals(StringUtils.deleteWhitespace(value));
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public void resetFolderHandling() {

    }

    @Override
    public boolean containsKey(String ref) {
        return docHandler.exists(ref);
    }

    @Override
    public void put(String key, String value) {
        docHandler.put(key, value);
        if (needsFolderHandling) {
            folderHandler.addKey(key);
        }
    }

    @Override
    public String get(String k) {
        return docHandler.get(k);
    }

    @Override
    public long countKeys() throws RaptNotSupportedException {
        return docHandler.getCount();
    }

    @Override
    public boolean delete(String key) {
        boolean ret = docHandler.delete(key);

        if (ret && needsFolderHandling) {
            // Do something with folder handling
            folderHandler.removeKey(key);
        }
        return ret;
    }

    @Override
    public boolean dropKeyStore() {
        docHandler.drop();
        if (needsFolderHandling) {
            folderHandler.drop();
        }
        return true;
    }

    private boolean needsFolderHandling = true;

    public void resetNeedsFolderHandling() {
        needsFolderHandling = false;
    }

    @Override
    public List getBatch(final List keys) {
        List ret = new ArrayList();
        // Batch is slow at present
        for (String k : keys) {
            ret.add(get(k));
        }
        return ret;
    }

    @Override
    public String getStoreId() {
        return tableName;
    }

    @Override
    public List getSubKeys(String prefix) {
        // return dir.getChildren(prefix);
        return needsFolderHandling ? folderHandler.getChildren(prefix) : new ArrayList();
    }

    @Override
    public List removeSubKeys(String folder, Boolean force) {
        List ret = new ArrayList();
        removeEntries(ret, folder);
        return ret;
    }

    private void removeEntries(List ret, String prefix) {
        List entries = getSubKeys(prefix);
        for (RaptureFolderInfo i : entries) {
            String nextLevel = prefix + "/" + i.getName();
            if (i.isFolder()) {
                removeEntries(ret, nextLevel);
            } else {
                delete(nextLevel);
                RaptureFolderInfo nextRfi = new RaptureFolderInfo();
                nextRfi.setName(nextLevel);
                nextRfi.setFolder(false);
                ret.add(nextRfi);
            }
        }
        RaptureFolderInfo topRfi = new RaptureFolderInfo();
        topRfi.setFolder(true);
        topRfi.setName(prefix);
        ret.add(topRfi);
        folderHandler.removeKey(prefix);
    }

    @Override
    public List getAllSubKeys(String displayNamePart) {
        List result = Lists.newArrayList();
        // expandTree(base, result, null);
        return result;
    }

    private String tableName;
    private String instanceName = "default";
    private PostgresDocHandler docHandler;
    private PostgresFolderHandler folderHandler;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy