Search in sources :

Example 41 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class RecordStoreLayer method saveChangesDo.

@Override
protected boolean saveChangesDo(final RecordLayerErrors errors, final LayerRecord record) {
    boolean deleted = super.isDeleted(record);
    if (isExists()) {
        final RecordStore recordStore = getRecordStore();
        if (recordStore == null) {
            return true;
        } else {
            try (Transaction transaction = recordStore.newTransaction(Propagation.REQUIRES_NEW)) {
                try {
                    Identifier identifier = record.getIdentifier();
                    try (final Writer<Record> writer = recordStore.newRecordWriter()) {
                        if (isRecordCached(getCacheIdDeleted(), record) || super.isDeleted(record)) {
                            preDeleteRecord(record);
                            record.setState(RecordState.DELETED);
                            writeDelete(writer, record);
                            deleted = true;
                        } else {
                            final RecordDefinition recordDefinition = getRecordDefinition();
                            if (super.isNew(record)) {
                                final List<String> idFieldNames = recordDefinition.getIdFieldNames();
                                if (identifier == null && !idFieldNames.isEmpty()) {
                                    identifier = recordStore.newPrimaryIdentifier(this.typePath);
                                    if (identifier != null) {
                                        identifier.setIdentifier(record, idFieldNames);
                                    }
                                }
                            }
                            final int fieldCount = recordDefinition.getFieldCount();
                            for (int fieldIndex = 0; fieldIndex < fieldCount; fieldIndex++) {
                                record.validateField(fieldIndex);
                            }
                            if (super.isModified(record)) {
                                writeUpdate(writer, record);
                            } else if (super.isNew(record)) {
                                writer.write(record);
                            }
                        }
                    }
                    if (!deleted) {
                        record.setState(RecordState.PERSISTED);
                    }
                    removeFromRecordIdToRecordMap(identifier);
                    return true;
                } catch (final Throwable e) {
                    throw transaction.setRollbackOnly(e);
                }
            }
        }
    } else {
        return true;
    }
}
Also used : Identifier(com.revolsys.identifier.Identifier) Transaction(com.revolsys.transaction.Transaction) RecordStore(com.revolsys.record.schema.RecordStore) Record(com.revolsys.record.Record) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 42 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class RecordStoreLayer method removeRecordFromCache.

@Override
protected boolean removeRecordFromCache(final LayerRecord record) {
    boolean removed = false;
    if (isLayerRecord(record)) {
        synchronized (getSync()) {
            final Identifier identifier = record.getIdentifier();
            if (identifier != null) {
                for (final Iterator<Set<Identifier>> iterator = this.recordIdentifiersByCacheId.values().iterator(); iterator.hasNext(); ) {
                    final Set<Identifier> identifiers = iterator.next();
                    identifiers.remove(identifier);
                    if (identifiers.isEmpty()) {
                        iterator.remove();
                    }
                }
            }
            removed |= super.removeRecordFromCache(record);
        }
    }
    return removed;
}
Also used : Identifier(com.revolsys.identifier.Identifier) Set(java.util.Set) HashSet(java.util.HashSet)

Example 43 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class RecordStoreLayer method forEachRecordsPersisted.

@SuppressWarnings("unchecked")
public <R extends LayerRecord> void forEachRecordsPersisted(final Query query, final Consumer<? super R> consumer) {
    if (query != null && isExists()) {
        final RecordStore recordStore = getRecordStore();
        if (recordStore != null) {
            try (final BaseCloseable booleanValueCloseable = eventsDisabled();
                Transaction transaction = recordStore.newTransaction(Propagation.REQUIRES_NEW);
                final RecordReader reader = newRecordStoreRecordReader(query)) {
                final LabelCountMap labelCountMap = query.getProperty("statistics");
                for (final LayerRecord record : reader.<LayerRecord>i()) {
                    final Identifier identifier = getId(record);
                    R proxyRecord = null;
                    if (identifier == null) {
                        proxyRecord = (R) newProxyLayerRecordNoId(record);
                    } else {
                        synchronized (getSync()) {
                            final LayerRecord cachedRecord = getCachedRecord(identifier, record, true);
                            if (!cachedRecord.isDeleted()) {
                                proxyRecord = newProxyLayerRecord(identifier);
                            }
                        }
                    }
                    if (proxyRecord != null) {
                        consumer.accept(proxyRecord);
                        if (labelCountMap != null) {
                            labelCountMap.addCount(record);
                        }
                    }
                }
            }
        }
    }
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) Identifier(com.revolsys.identifier.Identifier) Transaction(com.revolsys.transaction.Transaction) LabelCountMap(com.revolsys.util.count.LabelCountMap) RecordStore(com.revolsys.record.schema.RecordStore) RecordReader(com.revolsys.record.io.RecordReader)

Example 44 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class RecordStoreLayer method deleteRecordDo.

@Override
protected boolean deleteRecordDo(final LayerRecord record) {
    final Identifier identifier = record.getIdentifier();
    final boolean result = super.deleteRecordDo(record);
    removeFromRecordIdToRecordMap(identifier);
    return result;
}
Also used : Identifier(com.revolsys.identifier.Identifier)

Example 45 with Identifier

use of com.revolsys.identifier.Identifier in project com.revolsys.open by revolsys.

the class RecordStoreLayer method getCacheIdsDo.

@Override
protected Set<Label> getCacheIdsDo(final LayerRecord record) {
    final Set<Label> cacheIds = super.getCacheIdsDo(record);
    final Identifier identifier = record.getIdentifier();
    if (identifier != null) {
        for (final Entry<Label, Set<Identifier>> entry : this.recordIdentifiersByCacheId.entrySet()) {
            final Label cacheId = entry.getKey();
            if (!cacheIds.contains(cacheId)) {
                final Collection<Identifier> identifiers = entry.getValue();
                if (identifiers.contains(identifier)) {
                    cacheIds.add(cacheId);
                }
            }
        }
    }
    return cacheIds;
}
Also used : Identifier(com.revolsys.identifier.Identifier) Set(java.util.Set) HashSet(java.util.HashSet) Label(com.revolsys.util.Label)

Aggregations

Identifier (com.revolsys.identifier.Identifier)56 Record (com.revolsys.record.Record)17 ArrayList (java.util.ArrayList)12 RecordDefinition (com.revolsys.record.schema.RecordDefinition)11 SingleIdentifier (com.revolsys.identifier.SingleIdentifier)8 ListIdentifier (com.revolsys.identifier.ListIdentifier)7 TypedIdentifier (com.revolsys.identifier.TypedIdentifier)6 CodeTable (com.revolsys.record.code.CodeTable)6 RecordReader (com.revolsys.record.io.RecordReader)5 RecordStore (com.revolsys.record.schema.RecordStore)5 Transaction (com.revolsys.transaction.Transaction)5 List (java.util.List)5 Geometry (com.revolsys.geometry.model.Geometry)4 BaseCloseable (com.revolsys.io.BaseCloseable)4 PathName (com.revolsys.io.PathName)4 ArrayRecord (com.revolsys.record.ArrayRecord)4 Query (com.revolsys.record.query.Query)4 OsmElement (com.revolsys.record.io.format.openstreetmap.model.OsmElement)3 FieldDefinition (com.revolsys.record.schema.FieldDefinition)3 CompoundCoordinateSystem (com.revolsys.geometry.cs.CompoundCoordinateSystem)2