Search in sources :

Example 6 with PropertyType

use of org.umlg.sqlg.structure.PropertyType in project sqlg by pietermartin.

the class PostgresDialect method temporaryTableCopyCommandSqlVertex.

@Override
public String temporaryTableCopyCommandSqlVertex(SqlgGraph sqlgGraph, SchemaTable schemaTable, Set<String> keys) {
    StringBuilder sql = new StringBuilder();
    sql.append("COPY ");
    // Temp tables only
    sql.append(maybeWrapInQoutes(VERTEX_PREFIX + schemaTable.getTable()));
    sql.append(" (");
    if (keys.isEmpty()) {
        // copy command needs at least one field.
        // check if the dummy field exist, if not createVertexLabel it
        Map<String, PropertyType> columns = new HashMap<>();
        columns.put(COPY_DUMMY, PropertyType.from(0));
        sqlgGraph.getTopology().ensureVertexLabelPropertiesExist(schemaTable.getSchema(), schemaTable.getTable(), columns);
        sql.append(maybeWrapInQoutes(COPY_DUMMY));
    } else {
        int count = 1;
        for (String key : keys) {
            if (count > 1 && count <= keys.size()) {
                sql.append(", ");
            }
            count++;
            sql.append(maybeWrapInQoutes(key));
        }
    }
    sql.append(")");
    sql.append(" FROM stdin CSV DELIMITER '");
    sql.append(COPY_COMMAND_DELIMITER);
    sql.append("' ");
    sql.append("QUOTE ");
    sql.append(COPY_COMMAND_QUOTE);
    sql.append(" ESCAPE '");
    sql.append(ESCAPE);
    sql.append("';");
    if (logger.isDebugEnabled()) {
        logger.debug(sql.toString());
    }
    return sql.toString();
}
Also used : PropertyType(org.umlg.sqlg.structure.PropertyType) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint)

Example 7 with PropertyType

use of org.umlg.sqlg.structure.PropertyType in project sqlg by pietermartin.

the class PostgresDialect method flushElementGlobalUniqueIndexPropertyCache.

private <T extends SqlgElement> void flushElementGlobalUniqueIndexPropertyCache(SqlgGraph sqlgGraph, boolean forVertices, Map<SchemaTable, Pair<SortedSet<String>, Map<T, Map<String, Object>>>> schemaVertexPropertyCache) {
    Connection conn = sqlgGraph.tx().getConnection();
    for (SchemaTable schemaTable : schemaVertexPropertyCache.keySet()) {
        Pair<SortedSet<String>, Map<T, Map<String, Object>>> vertexPropertyCache = schemaVertexPropertyCache.get(schemaTable);
        Map<String, PropertyColumn> globalUniqueIndexPropertyMap = sqlgGraph.getTopology().getPropertiesWithGlobalUniqueIndexFor(schemaTable.withPrefix(VERTEX_PREFIX));
        for (Map.Entry<String, PropertyColumn> propertyColumnEntry : globalUniqueIndexPropertyMap.entrySet()) {
            PropertyColumn propertyColumn = propertyColumnEntry.getValue();
            for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
                SortedSet<String> keys = new TreeSet<>();
                keys.add("value");
                StringBuilder sql = new StringBuilder();
                sql.append("UPDATE ");
                sql.append(maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
                sql.append(".");
                sql.append(maybeWrapInQoutes((forVertices ? VERTEX_PREFIX : EDGE_PREFIX) + globalUniqueIndex.getName()));
                sql.append(" a \nSET\n\t(");
                appendKeyForBatchUpdate(PropertyType.STRING, sql, "recordId", false);
                sql.append(", ");
                appendKeyForBatchUpdate(PropertyType.STRING, sql, "property", false);
                sql.append(", ");
                appendKeyForBatchUpdate(propertyColumn.getPropertyType(), sql, "value", false);
                sql.append(") = \n\t(");
                sql.append("v.");
                appendKeyForBatchUpdate(PropertyType.STRING, sql, "recordId", true);
                sql.append(", v.");
                appendKeyForBatchUpdate(PropertyType.STRING, sql, "property", true);
                sql.append(", ");
                int count = 1;
                for (String key : keys) {
                    sql.append("v.");
                    PropertyType propertyType = propertyColumn.getPropertyType();
                    appendKeyForBatchUpdate(propertyType, sql, key, true);
                    sqlCastArray(sql, propertyType);
                    if (count++ < keys.size()) {
                        sql.append(", ");
                    }
                }
                sql.append(")\nFROM (\nVALUES\n\t");
                count = 1;
                boolean foundSomething = false;
                for (SqlgElement sqlgElement : vertexPropertyCache.getRight().keySet()) {
                    Map<String, Object> properties = vertexPropertyCache.getRight().get(sqlgElement);
                    if (!foundSomething && properties.containsKey(propertyColumn.getName())) {
                        foundSomething = true;
                    }
                    sql.append("($token$");
                    sql.append(sqlgElement.id().toString());
                    sql.append("$token$, $token$");
                    sql.append(propertyColumn.getName());
                    sql.append("$token$, ");
                    int countProperties = 1;
                    Object value = properties.get(propertyColumn.getName());
                    if (value == null) {
                        if (sqlgElement.property(propertyColumn.getName()).isPresent()) {
                            value = sqlgElement.value(propertyColumn.getName());
                        } else {
                            value = null;
                        }
                    }
                    PropertyType propertyType = propertyColumn.getPropertyType();
                    appendSqlValue(sql, value, propertyType);
                    sql.append(")");
                    if (count++ < vertexPropertyCache.getRight().size()) {
                        sql.append(",\n\t");
                    }
                }
                if (!foundSomething) {
                    continue;
                }
                sql.append("\n) AS v(\"recordId\", property, ");
                count = 1;
                for (String key : keys) {
                    PropertyType propertyType = propertyColumn.getPropertyType();
                    appendKeyForBatchUpdate(propertyType, sql, key, false);
                    if (count++ < keys.size()) {
                        sql.append(", ");
                    }
                }
                sql.append(")");
                sql.append("\nWHERE a.\"recordId\" = v.\"recordId\" and a.property = v.property");
                if (logger.isDebugEnabled()) {
                    logger.debug(sql.toString());
                }
                try (Statement statement = conn.createStatement()) {
                    statement.execute(sql.toString());
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : PGConnection(org.postgresql.PGConnection) PropertyType(org.umlg.sqlg.structure.PropertyType) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) MultiMap(org.apache.tinkerpop.gremlin.util.tools.MultiMap)

Example 8 with PropertyType

use of org.umlg.sqlg.structure.PropertyType in project sqlg by pietermartin.

the class PostgresDialect method writeStreamingVertex.

@Override
public void writeStreamingVertex(Writer writer, Map<String, Object> keyValueMap) {
    try {
        int countKeys = 1;
        if (keyValueMap.isEmpty()) {
            writer.write(Integer.toString(1));
        } else {
            for (Map.Entry<String, Object> entry : keyValueMap.entrySet()) {
                if (countKeys > 1 && countKeys <= keyValueMap.size()) {
                    writer.write(COPY_COMMAND_DELIMITER);
                }
                countKeys++;
                Object value = entry.getValue();
                PropertyType propertyType;
                if (value == null) {
                    propertyType = PropertyType.STRING;
                } else {
                    propertyType = PropertyType.from(value);
                }
                valueToStreamBytes(writer, propertyType, value);
            }
        }
        writer.write("\n");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : PropertyType(org.umlg.sqlg.structure.PropertyType) MultiMap(org.apache.tinkerpop.gremlin.util.tools.MultiMap) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint)

Example 9 with PropertyType

use of org.umlg.sqlg.structure.PropertyType in project sqlg by pietermartin.

the class SQLServerBaseCacheBulkRecord method addMetaData.

protected int addMetaData(SQLServerBulkCopy bulkCopy, SqlgGraph sqlgGraph) throws SQLServerException {
    int i = 1;
    for (String column : this.columns) {
        PropertyType propertyType;
        if (this.propertyColumns != null) {
            PropertyColumn propertyColumn = propertyColumns.get(column);
            propertyType = propertyColumn.getPropertyType();
        } else {
            propertyType = this.properties.get(column);
        }
        switch(propertyType) {
            case BOOLEAN:
                // Add the column mappings, skipping the first identity column.
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case BYTE:
            case SHORT:
            case INTEGER:
            case LONG:
            case FLOAT:
            case DOUBLE:
            case JSON:
            case STRING:
                // Add the column mappings, skipping the first identity column.
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case LOCALDATE:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case LOCALDATETIME:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case LOCALTIME:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 0, 0, null, propertyType));
                break;
            case ZONEDDATETIME:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.ZONEDDATETIME)[0], 0, 0, null, PropertyType.LOCALDATETIME));
                bulkCopy.addColumnMapping(i, column + PropertyType.ZONEDDATETIME.getPostFixes()[0]);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.ZONEDDATETIME)[0], 0, 0, null, PropertyType.STRING));
                break;
            case PERIOD:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.PERIOD)[0], 0, 0, null, PropertyType.INTEGER));
                bulkCopy.addColumnMapping(i, column + PropertyType.PERIOD.getPostFixes()[0]);
                this.columnMetadata.put(i++, new ColumnMetadata(column + PropertyType.PERIOD.getPostFixes()[0], sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.PERIOD)[1], 0, 0, null, PropertyType.INTEGER));
                bulkCopy.addColumnMapping(i, column + PropertyType.PERIOD.getPostFixes()[1]);
                this.columnMetadata.put(i++, new ColumnMetadata(column + PropertyType.PERIOD.getPostFixes()[1], sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.PERIOD)[2], 0, 0, null, PropertyType.INTEGER));
                break;
            case DURATION:
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.DURATION)[0], 0, 0, null, PropertyType.LONG));
                bulkCopy.addColumnMapping(i, column + PropertyType.DURATION.getPostFixes()[0]);
                this.columnMetadata.put(i++, new ColumnMetadata(column + PropertyType.DURATION.getPostFixes()[0], sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(PropertyType.DURATION)[1], 0, 0, null, PropertyType.INTEGER));
                break;
            case byte_ARRAY:
                // Add the column mappings, skipping the first identity column.
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 8000, 0, null, propertyType));
                break;
            case BYTE_ARRAY:
                // Add the column mappings, skipping the first identity column.
                bulkCopy.addColumnMapping(i, column);
                this.columnMetadata.put(i++, new ColumnMetadata(column, sqlgGraph.getSqlDialect().propertyTypeToJavaSqlType(propertyType)[0], 8000, 0, null, propertyType));
                break;
            default:
                throw SqlgExceptions.invalidPropertyType(propertyType);
        }
    }
    return i;
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) PropertyType(org.umlg.sqlg.structure.PropertyType)

Example 10 with PropertyType

use of org.umlg.sqlg.structure.PropertyType in project sqlg by pietermartin.

the class TestTopologyUpgrade method testUpgradeIndex.

@SuppressWarnings("serial")
@Test
public void testUpgradeIndex() throws Exception {
    // with topology
    VertexLabel personVertexLabel = this.sqlgGraph.getTopology().ensureVertexLabelExist("Person", new HashMap<String, PropertyType>() {

        {
            put("firstName", PropertyType.STRING);
            put("lastName", PropertyType.STRING);
        }
    });
    personVertexLabel.ensureIndexExists(IndexType.UNIQUE, new ArrayList<>(personVertexLabel.getProperties().values()));
    Map<String, PropertyType> properties = new HashMap<String, PropertyType>() {

        {
            put("name", PropertyType.STRING);
        }
    };
    VertexLabel dogVertexLabel = this.sqlgGraph.getTopology().ensureVertexLabelExist("Dog", properties);
    dogVertexLabel.ensureIndexExists(IndexType.NON_UNIQUE, new ArrayList<>(dogVertexLabel.getProperties().values()));
    Map<String, PropertyType> eproperties = new HashMap<String, PropertyType>() {

        {
            put("since", PropertyType.LOCALDATE);
        }
    };
    EdgeLabel eLabel = this.sqlgGraph.getTopology().ensureEdgeLabelExist("Owns", personVertexLabel, dogVertexLabel, eproperties);
    eLabel.ensureIndexExists(IndexType.UNIQUE, new ArrayList<>(eLabel.getProperties().values()));
    // test performance
    /*for (int a=0;a<1000;a++){
        	VertexLabel testVertex = this.sqlgGraph.getTopology().ensureVertexLabelExist("Person"+a, new HashMap<String, PropertyType>() {{
                put("firstName", PropertyType.STRING);
                put("lastName", PropertyType.STRING);
            }});
        	testVertex.ensureIndexExists(IndexType.UNIQUE, new ArrayList<>(testVertex.getProperties().values()));

        }*/
    // not supported yet
    /*this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(new HashSet<PropertyColumn>() {{
            add(personVertexLabel.getProperty("firstName").get());
            add(dogVertexLabel.getProperty("name").get());
        }});*/
    this.sqlgGraph.tx().commit();
    // sanity
    topologyCheck(this.sqlgGraph);
    // Delete the topology
    dropSqlgSchema(this.sqlgGraph);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.close();
    // topology will be recreated
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        topologyCheck(sqlgGraph1);
    }
    // from topology
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        topologyCheck(sqlgGraph1);
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) PropertyType(org.umlg.sqlg.structure.PropertyType) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

PropertyType (org.umlg.sqlg.structure.PropertyType)28 GeographyPoint (org.umlg.sqlg.gis.GeographyPoint)9 MultiMap (org.apache.tinkerpop.gremlin.util.tools.MultiMap)7 BaseTest (org.umlg.sqlg.test.BaseTest)7 Test (org.junit.Test)6 PGConnection (org.postgresql.PGConnection)5 SqlgGraph (org.umlg.sqlg.structure.SqlgGraph)4 VertexLabel (org.umlg.sqlg.structure.topology.VertexLabel)4 SecureRandom (java.security.SecureRandom)3 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Array (java.lang.reflect.Array)2 java.sql (java.sql)2 HashMap (java.util.HashMap)2 Triple (org.apache.commons.lang3.tuple.Triple)2 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)2 JdbcArray (org.h2.jdbc.JdbcArray)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Preconditions (com.google.common.base.Preconditions)1