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

org.vertexium.sql.SqlGraphConfiguration Maven / Gradle / Ivy

There is a newer version: 3.2.3
Show newest version
package org.vertexium.sql;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.vertexium.VertexiumSerializer;
import org.vertexium.inmemory.*;
import org.vertexium.sql.collections.SqlMap;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class SqlGraphConfiguration extends InMemoryGraphConfiguration {
    protected static final String KEY_COLUMN_NAME = "id";
    protected static final String VALUE_COLUMN_NAME = "object";
    protected static final String VERTEX_TABLE_NAME = "vertex";
    protected static final String EDGE_TABLE_NAME = "edge";
    protected static final String EXTENDED_DATA_TABLE_NAME = "extendeddata";
    protected static final String METADATA_TABLE_NAME = "metadata";
    protected static final String STREAMING_PROPERTIES_TABLE_NAME = "streaming_properties";
    protected static final String IN_VERTEX_ID_COLUMN = "in_vertex_id";
    protected static final String OUT_VERTEX_ID_COLUMN = "out_vertex_id";
    private static final String CONFIG_PREFIX = "sql.";

    private final DataSource dataSource;
    private final VertexiumSerializer serializer;

    public SqlGraphConfiguration(Map config) {
        super(config);
        dataSource = createDataSource(config);
        serializer = createSerializer();
    }

    private DataSource createDataSource(Map config) {
        Properties properties = new Properties();
        for (Map.Entry configEntry : config.entrySet()) {
            String key = configEntry.getKey();
            if (key.startsWith(CONFIG_PREFIX)) {
                key = key.substring(CONFIG_PREFIX.length());
                properties.put(key, configEntry.getValue());
            }
        }
        HikariConfig hikariConfig = new HikariConfig(properties);
        return new HikariDataSource(hikariConfig);
    }

    protected DataSource getDataSource() {
        return dataSource;
    }

    protected String tableNameWithPrefix(String tableName) {
        return getTableNamePrefix() + "_" + tableName;
    }

    protected SqlMap> newEdgeMap() {
        return new SqlMap>(
                tableNameWithPrefix(EDGE_TABLE_NAME), KEY_COLUMN_NAME, VALUE_COLUMN_NAME, dataSource, serializer) {

            @Override
            protected Map additionalColumns(String key, InMemoryTableElement value) {
                SqlTableEdge edge = (SqlTableEdge) value;
                Map columns = new HashMap<>();
                columns.put(IN_VERTEX_ID_COLUMN, edge.inVertexId());
                columns.put(OUT_VERTEX_ID_COLUMN, edge.outVertexId());
                return columns;
            }
        };
    }

    public SqlExtendedDataTable newExtendedDataTable() {
        return new SqlExtendedDataTable(tableNameWithPrefix(EXTENDED_DATA_TABLE_NAME), dataSource, serializer);
    }

    protected SqlMap> newVertexMap() {
        return new SqlMap<>(
                tableNameWithPrefix(VERTEX_TABLE_NAME), KEY_COLUMN_NAME, VALUE_COLUMN_NAME, dataSource, serializer);
    }

    protected SqlMap newMetadataMap() {
        return new SqlMap<>(
                tableNameWithPrefix(METADATA_TABLE_NAME), KEY_COLUMN_NAME, VALUE_COLUMN_NAME, dataSource, serializer);
    }

    protected SqlStreamingPropertyTable newStreamingPropertyTable() {
        return new SqlStreamingPropertyTable(tableNameWithPrefix(STREAMING_PROPERTIES_TABLE_NAME), dataSource);
    }
}