use of com.revolsys.record.schema.RecordStore in project com.revolsys.open by revolsys.
the class RecordStoreConnectionManager method getRecordStore.
/**
* Get an initialized record store.
* @param connectionProperties
* @return
*/
@SuppressWarnings("unchecked")
public static <T extends RecordStore> T getRecordStore(final Map<String, ? extends Object> config) {
@SuppressWarnings("rawtypes") final Map<String, Object> configClone = (Map) JavaBeanUtil.clone(config);
synchronized (recordStoreByConfig) {
RecordStore recordStore = recordStoreByConfig.get(configClone);
if (recordStore != null && recordStore.isClosed()) {
recordStoreByConfig.remove(configClone);
recordStoreCounts.remove(configClone);
recordStore = null;
}
if (recordStore == null) {
final Map<String, ? extends Object> connectionProperties = (Map<String, ? extends Object>) configClone.get("connection");
final String name = (String) connectionProperties.get("name");
if (Property.hasValue(name)) {
recordStore = getRecordStore(name);
if (recordStore == null) {
// TODO give option to add
return null;
}
} else {
recordStore = RecordStore.newRecordStore(connectionProperties);
if (recordStore == null) {
return null;
} else {
recordStore.setProperties(config);
recordStore.initialize();
}
}
recordStoreByConfig.put(configClone, recordStore);
recordStoreCounts.put(configClone, new AtomicInteger(1));
} else {
final AtomicInteger count = recordStoreCounts.get(configClone);
count.incrementAndGet();
}
return (T) recordStore;
}
}
use of com.revolsys.record.schema.RecordStore 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;
}
}
}
use of com.revolsys.record.schema.RecordStore 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;
}
}
use of com.revolsys.record.schema.RecordStore 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);
}
}
}
}
}
}
}
use of com.revolsys.record.schema.RecordStore in project com.revolsys.open by revolsys.
the class RecordStoreLayer method newRecordStoreRecordReader.
protected RecordReader newRecordStoreRecordReader(final Query query) {
final RecordStore recordStore = getRecordStore();
if (recordStore == null) {
return RecordReader.empty();
} else {
final RecordFactory<LayerRecord> recordFactory = getRecordFactory();
query.setRecordFactory(recordFactory);
return recordStore.getRecords(query);
}
}
Aggregations