Search in sources :

Example 31 with RecordReader

use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.

the class CopyRecords method run.

@Override
public void run() {
    try {
        final Query query = this.sourceRecordStore.newQuery(this.typePath);
        query.setOrderBy(this.orderBy);
        try (final RecordReader reader = this.sourceRecordStore.getRecords(query);
            final RecordWriter targetWriter = this.targetRecordStore.newRecordWriter()) {
            final RecordDefinition targetRecordDefinition = this.targetRecordStore.getRecordDefinition(this.typePath);
            if (targetRecordDefinition == null) {
                Logs.error(this, "Cannot find target table: " + this.typePath);
            } else {
                if (this.hasSequence) {
                    final String idFieldName = targetRecordDefinition.getIdFieldName();
                    Identifier maxId = this.targetRecordStore.newPrimaryIdentifier(this.typePath);
                    for (final Record sourceRecord : reader) {
                        final Record targetRecord = this.targetRecordStore.newRecord(this.typePath, sourceRecord);
                        final Identifier sourceId = sourceRecord.getIdentifier(idFieldName);
                        while (CompareUtil.compare(maxId, sourceId) < 0) {
                            maxId = this.targetRecordStore.newPrimaryIdentifier(this.typePath);
                        }
                        targetWriter.write(targetRecord);
                    }
                } else {
                    for (final Record sourceRecord : reader) {
                        final Record targetRecord = this.targetRecordStore.newRecord(this.typePath, sourceRecord);
                        targetWriter.write(targetRecord);
                    }
                }
            }
        }
    } catch (final Throwable e) {
        throw new RuntimeException("Unable to copy records for " + this.typePath, e);
    }
}
Also used : RecordWriter(com.revolsys.record.io.RecordWriter) Identifier(com.revolsys.identifier.Identifier) Query(com.revolsys.record.query.Query) RecordReader(com.revolsys.record.io.RecordReader) Record(com.revolsys.record.Record) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 32 with RecordReader

use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.

the class EsriCoordinateSystemsLoader method projected.

private void projected() {
    final Map<ByteArray, Map<Integer, ProjectedCoordinateSystem>> csBymd5 = new LinkedHashMap<>();
    try (RecordReader reader = RecordReader.newRecordReader(this.mainPath + "data/esri/esriProjectedCs.tsv");
        final ChannelWriter writer = ChannelWriter.newChannelWriter(this.mainPath + "resources/CoordinateSystems/esri/Projected.cs")) {
        for (final Record record : reader) {
            final int id = record.getInteger("ID");
            final String wkt = record.getString("WKT");
            final ProjectedCoordinateSystem coordinateSystem = WktCsParser.read(wkt);
            final byte[] digest = coordinateSystem.md5Digest();
            Maps.addToMap(Maps::newTree, csBymd5, new ByteArray(digest), id, coordinateSystem);
            final String csName = coordinateSystem.getCoordinateSystemName();
            final GeographicCoordinateSystem geographicCoordinateSystem = coordinateSystem.getGeographicCoordinateSystem();
            final String geographicCoordinateSystemName = geographicCoordinateSystem.getCoordinateSystemName();
            final int geographicCoordinateSystemId = this.geographicIdByName.getOrDefault(geographicCoordinateSystemName, 0);
            if (geographicCoordinateSystemId == 0) {
                System.out.println(wkt);
            }
            final String projectionName = coordinateSystem.getCoordinateOperationMethod().getName();
            final Map<ParameterName, ParameterValue> parameterValues = coordinateSystem.getParameterValues();
            final LinearUnit linearUnit = coordinateSystem.getLinearUnit();
            final String unitName = linearUnit.getName();
            final double conversionFactor = linearUnit.getConversionFactor();
            writer.putInt(id);
            writer.putStringUtf8ByteCount(csName);
            writer.putInt(geographicCoordinateSystemId);
            writer.putStringUtf8ByteCount(projectionName);
            writeParameters(writer, parameterValues);
            writer.putStringUtf8ByteCount(unitName);
            writer.putDouble(conversionFactor);
        }
    }
    writeDigestFile(csBymd5, "Projected");
}
Also used : LinearUnit(com.revolsys.geometry.cs.unit.LinearUnit) ParameterValue(com.revolsys.geometry.cs.ParameterValue) RecordReader(com.revolsys.record.io.RecordReader) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) ParameterName(com.revolsys.geometry.cs.ParameterName) LinkedHashMap(java.util.LinkedHashMap) ChannelWriter(com.revolsys.io.channels.ChannelWriter) Maps(com.revolsys.collection.map.Maps) Record(com.revolsys.record.Record) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 33 with RecordReader

use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.

the class AbstractRecordLayer method copyRecordsToClipboard.

public void copyRecordsToClipboard(final List<LayerRecord> records) {
    if (!records.isEmpty()) {
        final RecordDefinition recordDefinition = getRecordDefinition();
        final List<Record> copies = new ArrayList<>();
        for (final LayerRecord record : records) {
            final ArrayRecord recordCopy = new ArrayRecord(recordDefinition, record);
            copies.add(recordCopy);
        }
        final RecordReader reader = new ListRecordReader(recordDefinition, copies);
        final RecordReaderTransferable transferable = new RecordReaderTransferable(reader);
        ClipboardUtil.setContents(transferable);
    }
}
Also used : ListRecordReader(com.revolsys.record.io.ListRecordReader) RecordReaderTransferable(com.revolsys.swing.dnd.transferable.RecordReaderTransferable) ArrayRecord(com.revolsys.record.ArrayRecord) RecordReader(com.revolsys.record.io.RecordReader) ListRecordReader(com.revolsys.record.io.ListRecordReader) ArrayList(java.util.ArrayList) ArrayRecord(com.revolsys.record.ArrayRecord) Record(com.revolsys.record.Record) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 34 with RecordReader

use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.

the class AbstractRecordLayer method pasteRecords.

public void pasteRecords() {
    final List<LayerRecord> newRecords = new ArrayList<>();
    try (BaseCloseable eventsEnabled = eventsDisabled()) {
        RecordReader reader = ClipboardUtil.getContents(RecordReaderTransferable.DATA_OBJECT_READER_FLAVOR);
        if (reader == null) {
            final String string = ClipboardUtil.getContents(DataFlavor.stringFlavor);
            if (Property.hasValue(string)) {
                if (string.contains("\t")) {
                    final Resource tsvResource = new ByteArrayResource("t.tsv", string);
                    reader = RecordReader.newRecordReader(tsvResource);
                } else {
                    final Resource resource = new ByteArrayResource("t.csv", string);
                    reader = RecordReader.newRecordReader(resource);
                }
            }
        }
        if (reader != null) {
            for (final Record sourceRecord : reader) {
                final Map<String, Object> newValues = getPasteNewValues(sourceRecord);
                if (!newValues.isEmpty()) {
                    final LayerRecord newRecord = newLayerRecord(newValues);
                    if (newRecord != null) {
                        newRecords.add(newRecord);
                    }
                }
            }
        }
    } catch (final Throwable e) {
        LoggingEventPanel.showDialog(getMapPanel(), "Unexpected error pasting records", e);
        return;
    }
    RecordValidationDialog.validateRecords("Pasting Records", this, newRecords, (validator) -> {
        // Success
        // Save the valid records
        final List<LayerRecord> validRecords = validator.getValidRecords();
        if (!validRecords.isEmpty()) {
            saveChanges(validRecords);
            addSelectedRecords(validRecords);
            zoomToRecords(validRecords);
            showRecordsTable(RecordLayerTableModel.MODE_RECORDS_SELECTED);
            firePropertyChange(RECORDS_INSERTED, null, validRecords);
        }
        // Delete any invalid records
        final List<LayerRecord> invalidRecords = validator.getInvalidRecords();
        if (!invalidRecords.isEmpty()) {
            deleteRecords(invalidRecords);
        }
    }, (validator) -> {
        // Cancel, delete all the records
        deleteRecords(newRecords);
    });
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) RecordReader(com.revolsys.record.io.RecordReader) ListRecordReader(com.revolsys.record.io.ListRecordReader) ArrayList(java.util.ArrayList) ByteArrayResource(com.revolsys.spring.resource.ByteArrayResource) Resource(com.revolsys.spring.resource.Resource) ArrayRecord(com.revolsys.record.ArrayRecord) Record(com.revolsys.record.Record) LineString(com.revolsys.geometry.model.LineString) ByteArrayResource(com.revolsys.spring.resource.ByteArrayResource)

Example 35 with RecordReader

use of com.revolsys.record.io.RecordReader in project com.revolsys.open by revolsys.

the class RecordStoreLayer method forEachRecordInternal.

@Override
protected void forEachRecordInternal(Query query, final Consumer<? super LayerRecord> consumer) {
    if (isExists()) {
        try {
            final RecordStore recordStore = getRecordStore();
            if (recordStore != null && query != null) {
                final Predicate<Record> filter = query.getWhereCondition();
                final Map<? extends CharSequence, Boolean> orderBy = query.getOrderBy();
                final List<LayerRecord> changedRecords = new ArrayList<>();
                changedRecords.addAll(getRecordsNew());
                changedRecords.addAll(getRecordsModified());
                Records.filterAndSort(changedRecords, filter, orderBy);
                final Iterator<LayerRecord> changedIterator = changedRecords.iterator();
                LayerRecord currentChangedRecord = Iterators.next(changedIterator);
                final RecordDefinition internalRecordDefinition = getInternalRecordDefinition();
                query = query.newQuery(internalRecordDefinition);
                final Comparator<Record> comparator = Records.newComparatorOrderBy(orderBy);
                try (final BaseCloseable booleanValueCloseable = eventsDisabled();
                    Transaction transaction = recordStore.newTransaction(Propagation.REQUIRES_NEW);
                    final RecordReader reader = newRecordStoreRecordReader(query)) {
                    for (LayerRecord record : reader.<LayerRecord>i()) {
                        boolean write = true;
                        final Identifier identifier = getId(record);
                        if (identifier == null) {
                            record = newProxyLayerRecordNoId(record);
                        } else {
                            final LayerRecord cachedRecord = this.recordsByIdentifier.get(identifier);
                            if (cachedRecord != null) {
                                record = cachedRecord;
                                if (record.isChanged() || isDeleted(record)) {
                                    write = false;
                                }
                            }
                        }
                        if (!isDeleted(record) && write) {
                            while (currentChangedRecord != null && comparator.compare(currentChangedRecord, record) < 0) {
                                consumer.accept(currentChangedRecord);
                                currentChangedRecord = Iterators.next(changedIterator);
                            }
                            consumer.accept(record);
                        }
                    }
                    while (currentChangedRecord != null) {
                        consumer.accept(currentChangedRecord);
                        currentChangedRecord = Iterators.next(changedIterator);
                    }
                }
            }
        } catch (final CancellationException e) {
        } catch (final RuntimeException e) {
            Logs.error(this, "Error executing query: " + query, e);
            throw e;
        }
    }
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) RecordReader(com.revolsys.record.io.RecordReader) ArrayList(java.util.ArrayList) RecordDefinition(com.revolsys.record.schema.RecordDefinition) Identifier(com.revolsys.identifier.Identifier) Transaction(com.revolsys.transaction.Transaction) CancellationException(java.util.concurrent.CancellationException) RecordStore(com.revolsys.record.schema.RecordStore) Record(com.revolsys.record.Record)

Aggregations

RecordReader (com.revolsys.record.io.RecordReader)43 Record (com.revolsys.record.Record)34 ChannelWriter (com.revolsys.io.channels.ChannelWriter)19 RecordDefinition (com.revolsys.record.schema.RecordDefinition)11 Identifier (com.revolsys.identifier.Identifier)5 ArrayRecord (com.revolsys.record.ArrayRecord)5 ListRecordReader (com.revolsys.record.io.ListRecordReader)5 Query (com.revolsys.record.query.Query)5 HashMap (java.util.HashMap)5 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)4 BaseCloseable (com.revolsys.io.BaseCloseable)4 Transaction (com.revolsys.transaction.Transaction)4 ArrayList (java.util.ArrayList)4 Maps (com.revolsys.collection.map.Maps)3 Ellipsoid (com.revolsys.geometry.cs.Ellipsoid)3 Geometry (com.revolsys.geometry.model.Geometry)3 RecordWriter (com.revolsys.record.io.RecordWriter)3 RecordStore (com.revolsys.record.schema.RecordStore)3 Resource (com.revolsys.spring.resource.Resource)3 LinkedHashMap (java.util.LinkedHashMap)3