Search in sources :

Example 6 with Record

use of com.revolsys.record.Record in project com.revolsys.open by revolsys.

the class AbstractJdbcRecordStore method writeAll.

protected int writeAll(final Iterable<? extends Record> records, final RecordState state) {
    int count = 0;
    try (Transaction transaction = newTransaction(Propagation.REQUIRED)) {
        // rolled back.
        try (final RecordWriter writer = newRecordWriter(true)) {
            for (final Record record : records) {
                write(writer, record, state);
                count++;
            }
        } catch (final RuntimeException e) {
            transaction.setRollbackOnly();
            throw e;
        } catch (final Error e) {
            transaction.setRollbackOnly();
            throw e;
        }
    }
    return count;
}
Also used : RecordWriter(com.revolsys.record.io.RecordWriter) Transaction(com.revolsys.transaction.Transaction) ArrayRecord(com.revolsys.record.ArrayRecord) Record(com.revolsys.record.Record)

Example 7 with Record

use of com.revolsys.record.Record in project com.revolsys.open by revolsys.

the class JdbcRecordWriter method processCurrentBatch.

private void processCurrentBatch(final JdbcRecordDefinition recordDefinition, final Map<JdbcRecordDefinition, String> sqlMap, final PreparedStatement statement, final Map<JdbcRecordDefinition, Integer> batchCountMap, final Map<JdbcRecordDefinition, List<Record>> recordsByType, final boolean hasGeneratedKeys) {
    Integer batchCount = batchCountMap.get(recordDefinition);
    if (batchCount == null) {
        batchCount = 0;
    }
    try {
        Integer typeCount = this.typeCountMap.get(recordDefinition);
        if (typeCount == null) {
            typeCount = batchCount;
        } else {
            typeCount += batchCount;
        }
        this.typeCountMap.put(recordDefinition, typeCount);
        statement.executeBatch();
        if (hasGeneratedKeys) {
            final List<Record> records = recordsByType.remove(recordDefinition);
            if (records != null) {
                final ResultSet generatedKeyResultSet = statement.getGeneratedKeys();
                int recordIndex = 0;
                while (generatedKeyResultSet.next()) {
                    final Record record = records.get(recordIndex++);
                    int columnIndex = 1;
                    for (final FieldDefinition idField : recordDefinition.getIdFields()) {
                        ((JdbcFieldDefinition) idField).setFieldValueFromResultSet(generatedKeyResultSet, columnIndex++, record, false);
                    }
                }
            }
        }
    } catch (final SQLException e) {
        final String sql = sqlMap.get(recordDefinition);
        throw this.connection.getException("Process Batch", sql, e);
    } catch (final RuntimeException e) {
        LOG.error(sqlMap, e);
        throw e;
    } finally {
        batchCountMap.put(recordDefinition, 0);
    }
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) SQLException(java.sql.SQLException) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) ResultSet(java.sql.ResultSet) Record(com.revolsys.record.Record)

Example 8 with Record

use of com.revolsys.record.Record in project com.revolsys.open by revolsys.

the class DirectoryRecordWriter method getWriter.

private Writer<Record> getWriter(final Record record) {
    final RecordDefinition recordDefinition = record.getRecordDefinition();
    final String path = recordDefinition.getPath();
    Writer<Record> writer = this.writers.get(path);
    if (writer == null) {
        final File directory = getDirectory(recordDefinition);
        directory.mkdirs();
        final String fileName = getFileName(recordDefinition);
        final File file = new File(directory, fileName + this.nameSuffix + "." + this.fileExtension);
        final PathResource resource = new PathResource(file);
        writer = RecordWriter.newRecordWriter(recordDefinition, resource);
        if (writer == null) {
            throw new IllegalArgumentException("Unable to create writer for " + resource);
        } else {
            final Map<String, Object> properties = getProperties();
            writer.setProperties(properties);
            final Geometry geometry = record.getGeometry();
            if (geometry != null) {
                final GeometryFactory geometryFactory = geometry.getGeometryFactory();
                setProperty(IoConstants.GEOMETRY_FACTORY, geometryFactory);
            }
            this.writers.put(path, writer);
            RecordDefinition writerRecordDefinition = recordDefinition;
            if (writer instanceof AbstractRecordWriter) {
                final AbstractRecordWriter recordWriter = (AbstractRecordWriter) writer;
                writerRecordDefinition = recordWriter.getRecordDefinition();
                if (writerRecordDefinition == null) {
                    writerRecordDefinition = recordDefinition;
                }
            }
            this.recordDefinitionMap.put(path, writerRecordDefinition);
        }
    }
    return writer;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) AbstractRecordWriter(com.revolsys.io.AbstractRecordWriter) PathResource(com.revolsys.spring.resource.PathResource) Record(com.revolsys.record.Record) File(java.io.File) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 9 with Record

use of com.revolsys.record.Record in project com.revolsys.open by revolsys.

the class CodeTableProperty method loadId.

@Override
protected synchronized Identifier loadId(final List<Object> values, final boolean createId) {
    if (this.loadAll && !this.loadMissingCodes && !isEmpty()) {
        return null;
    }
    Identifier id = null;
    if (createId && this.loadAll && !isLoaded()) {
        loadAll();
        id = getIdentifier(values, false);
    } else {
        final Query query = new Query(this.typePath);
        final And and = new And();
        if (!values.isEmpty()) {
            int i = 0;
            for (final String fieldName : this.valueFieldNames) {
                final Object value = values.get(i);
                if (value == null) {
                    and.and(Q.isNull(fieldName));
                } else {
                    final FieldDefinition fieldDefinition = this.recordDefinition.getField(fieldName);
                    and.and(Q.equal(fieldDefinition, value));
                }
                i++;
            }
        }
        query.setWhereCondition(and);
        final Reader<Record> reader = this.recordStore.getRecords(query);
        try {
            final List<Record> codes = reader.toList();
            if (codes.size() > 0) {
                final CategoryLabelCountMap statistics = this.recordStore.getStatistics();
                if (statistics != null) {
                    statistics.getLabelCountMap("query").addCount(this.typePath, -codes.size());
                }
                addValues(codes);
            }
            id = getIdByValue(values);
            Property.firePropertyChange(this, "valuesChanged", false, true);
        } finally {
            reader.close();
        }
    }
    if (createId && id == null) {
        return newIdentifier(values);
    } else {
        return id;
    }
}
Also used : Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) ListIdentifier(com.revolsys.identifier.ListIdentifier) Query(com.revolsys.record.query.Query) CategoryLabelCountMap(com.revolsys.util.count.CategoryLabelCountMap) And(com.revolsys.record.query.And) FieldDefinition(com.revolsys.record.schema.FieldDefinition) Record(com.revolsys.record.Record)

Example 10 with Record

use of com.revolsys.record.Record in project com.revolsys.open by revolsys.

the class CodeTableProperty method loadValues.

@Override
protected List<Object> loadValues(final Object id) {
    if (this.loadAll && !isLoaded()) {
        loadAll();
    } else {
        try {
            final Record code;
            if (id instanceof Identifier) {
                final Identifier identifier = (Identifier) id;
                code = this.recordStore.getRecord(this.typePath, identifier);
            } else {
                code = this.recordStore.getRecord(this.typePath, id);
            }
            if (code != null) {
                addValue(code);
            }
        } catch (final Throwable e) {
            return null;
        }
    }
    return getValueById(id);
}
Also used : Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) ListIdentifier(com.revolsys.identifier.ListIdentifier) Record(com.revolsys.record.Record)

Aggregations

Record (com.revolsys.record.Record)198 ArrayRecord (com.revolsys.record.ArrayRecord)43 RecordReader (com.revolsys.record.io.RecordReader)34 RecordDefinition (com.revolsys.record.schema.RecordDefinition)34 Geometry (com.revolsys.geometry.model.Geometry)29 LineString (com.revolsys.geometry.model.LineString)21 Point (com.revolsys.geometry.model.Point)20 ChannelWriter (com.revolsys.io.channels.ChannelWriter)19 Identifier (com.revolsys.identifier.Identifier)17 ArrayList (java.util.ArrayList)16 FieldDefinition (com.revolsys.record.schema.FieldDefinition)15 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)14 LayerRecord (com.revolsys.swing.map.layer.record.LayerRecord)13 NoSuchElementException (java.util.NoSuchElementException)13 DataType (com.revolsys.datatype.DataType)10 Query (com.revolsys.record.query.Query)9 HashMap (java.util.HashMap)9 List (java.util.List)8 LinkedHashMap (java.util.LinkedHashMap)7 Edge (com.revolsys.geometry.graph.Edge)6