Search in sources :

Example 1 with MultiInputSelector

use of com.revolsys.parallel.channel.MultiInputSelector in project com.revolsys.open by revolsys.

the class OrderedEqualCompareProcessor method run.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void run(final Channel<Record> in) {
    this.running = true;
    final Channel<Record>[] channels = new Channel[] { in, this.otherIn };
    Record previousEqualObject = null;
    final Record[] objects = new Record[2];
    final boolean[] guard = new boolean[] { true, true };
    final MultiInputSelector alt = new MultiInputSelector();
    while (this.running) {
        final int index = alt.select(channels, guard);
        if (index == -1) {
            if (in.isClosed()) {
                logNoMatch(objects, this.otherIn, true);
                return;
            } else if (this.otherIn.isClosed()) {
                logNoMatch(objects, in, false);
                return;
            } else {
            }
        } else {
            final Channel<Record> channel = channels[index];
            final Record readObject = readObject(channel);
            if (index == 0 && this.recordDefinition1 == null) {
                setRecordDefinition1(readObject.getRecordDefinition());
            } else if (index == 1 && this.recordDefinition2 == null) {
                setRecordDefinition2(readObject.getRecordDefinition());
            }
            if (readObject != null) {
                if (previousEqualObject != null && DataType.equal(previousEqualObject, readObject)) {
                    if (index == 0) {
                        RecordLog.error(getClass(), "Duplicate in " + this.sourceName, readObject);
                    } else {
                        RecordLog.error(getClass(), "Duplicate in " + this.otherName, readObject);
                    }
                } else {
                    Record sourceObject;
                    Record otherObject;
                    final int oppositeIndex = (index + 1) % 2;
                    if (index == 0) {
                        sourceObject = readObject;
                        otherObject = objects[oppositeIndex];
                    } else {
                        sourceObject = objects[oppositeIndex];
                        otherObject = readObject;
                    }
                    final Object value = readObject.getValue(this.fieldName);
                    if (value == null) {
                        RecordLog.error(getClass(), "Missing key value for " + this.fieldName, readObject);
                    } else if (objects[oppositeIndex] == null) {
                        objects[index] = readObject;
                        guard[index] = false;
                        guard[oppositeIndex] = true;
                    } else {
                        final Object sourceValue = sourceObject.getValue(this.fieldName);
                        final Comparable<Object> sourceComparator;
                        if (sourceValue instanceof Number) {
                            final Number number = (Number) sourceValue;
                            final Double doubleValue = number.doubleValue();
                            sourceComparator = (Comparable) doubleValue;
                        } else {
                            sourceComparator = (Comparable<Object>) sourceValue;
                        }
                        Object otherValue = otherObject.getValue(this.fieldName);
                        if (otherValue instanceof Number) {
                            final Number number = (Number) otherValue;
                            otherValue = number.doubleValue();
                        }
                        // TODO duplicates
                        final int compare = sourceComparator.compareTo(otherValue);
                        if (compare == 0) {
                            final Set<String> notEqualFieldNames = getNotEqualFieldNames(sourceObject, otherObject);
                            final boolean geometryEquals = geometryEquals(sourceObject, otherObject);
                            if (!geometryEquals) {
                                final String geometryFieldName = sourceObject.getRecordDefinition().getGeometryFieldName();
                                notEqualFieldNames.add(geometryFieldName);
                            }
                            if (!notEqualFieldNames.isEmpty()) {
                                logNotEqual(sourceObject, otherObject, notEqualFieldNames, geometryEquals);
                            }
                            objects[0] = null;
                            objects[1] = null;
                            guard[0] = true;
                            guard[1] = true;
                            previousEqualObject = sourceObject;
                        } else if (compare < 0) {
                            // other object is bigger, keep other
                            // object
                            logNoMatch(sourceObject, false);
                            objects[0] = null;
                            objects[1] = otherObject;
                            guard[0] = true;
                            guard[1] = false;
                        } else {
                            // source is bigger, keep source object
                            logNoMatch(otherObject, true);
                            objects[0] = sourceObject;
                            objects[1] = null;
                            guard[0] = false;
                            guard[1] = true;
                        }
                    }
                }
            }
        }
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Channel(com.revolsys.parallel.channel.Channel) MultiInputSelector(com.revolsys.parallel.channel.MultiInputSelector) Record(com.revolsys.record.Record)

Example 2 with MultiInputSelector

use of com.revolsys.parallel.channel.MultiInputSelector in project com.revolsys.open by revolsys.

the class AbstractMergeProcess method run.

@Override
@SuppressWarnings("unchecked")
protected void run(final Channel<Record> in, final Channel<Record> out) {
    setUp();
    try {
        RecordDefinition currentType = null;
        String currentTypeName = null;
        final Channel<Record>[] channels = ArrayUtil.newArray(in, this.otherIn);
        final boolean[] guard = new boolean[] { true, true };
        final Record[] objects = new Record[2];
        final String[] typePaths = new String[2];
        for (int i = 0; i < 2; i++) {
            try {
                final Channel<Record> channel = channels[i];
                if (channel == null) {
                    guard[i] = false;
                } else {
                    Record object = null;
                    boolean test = false;
                    do {
                        object = channel.read();
                        test = testObject(object);
                    } while (!test);
                    if (test) {
                        objects[i] = object;
                        typePaths[i] = objects[i].getRecordDefinition().getPath();
                    }
                }
            } catch (final ClosedException e) {
                guard[i] = false;
            }
        }
        final Record otherObject = objects[OTHER_INDEX];
        if (typePaths[SOURCE_INDEX] != null) {
            final Record sourceObject = objects[SOURCE_INDEX];
            if (typePaths[OTHER_INDEX] != null) {
                final int nameCompare = typePaths[SOURCE_INDEX].toString().compareTo(typePaths[OTHER_INDEX].toString());
                if (nameCompare <= 0) {
                    currentType = sourceObject.getRecordDefinition();
                    currentTypeName = typePaths[SOURCE_INDEX];
                    addSourceObject(sourceObject);
                    objects[SOURCE_INDEX] = null;
                    if (nameCompare != 0) {
                        guard[OTHER_INDEX] = false;
                    }
                }
                if (nameCompare >= 0) {
                    currentType = otherObject.getRecordDefinition();
                    currentTypeName = typePaths[OTHER_INDEX];
                    addOtherObject(otherObject);
                    objects[OTHER_INDEX] = null;
                    if (nameCompare != 0) {
                        guard[SOURCE_INDEX] = false;
                    }
                }
            } else {
                currentType = sourceObject.getRecordDefinition();
                currentTypeName = typePaths[SOURCE_INDEX];
                if (otherObject != null) {
                    addSourceObject(otherObject);
                }
            }
        } else {
            currentType = otherObject.getRecordDefinition();
            currentTypeName = typePaths[OTHER_INDEX];
            if (otherObject != null) {
                addOtherObject(otherObject);
            }
            objects[OTHER_INDEX] = null;
        }
        try {
            final MultiInputSelector alt = new MultiInputSelector();
            final boolean running = true;
            while (running) {
                final int channelIndex = alt.select(channels, guard, 1000);
                if (channelIndex >= 0) {
                    final Record object = channels[channelIndex].read();
                    if (testObject(object)) {
                        final RecordDefinition recordDefinition = object.getRecordDefinition();
                        final String typePath = recordDefinition.getPath();
                        if (currentTypeName == null) {
                            currentTypeName = typePath;
                            currentType = recordDefinition;
                            init(recordDefinition);
                        }
                        if (typePath.equals(currentTypeName)) {
                            currentTypeName = typePath;
                            currentType = recordDefinition;
                            if (channelIndex == SOURCE_INDEX) {
                                addSourceObject(object);
                            } else {
                                addOtherObject(object);
                            }
                        } else {
                            objects[channelIndex] = object;
                            addObjectFromOtherChannel(channels, guard, objects, channelIndex);
                            currentType = addSavedObjects(currentType, currentTypeName, out, guard, objects);
                            if (currentType != null) {
                                currentTypeName = currentType.getPath();
                            }
                        }
                    }
                } else {
                    if (channels[0].isClosed()) {
                        guard[1] = true;
                    } else if (channels[1].isClosed()) {
                        guard[0] = true;
                    }
                }
            }
        } finally {
            try {
                while (addSavedObjects(currentType, currentTypeName, out, guard, objects) != null) {
                }
                processObjects(currentType, out);
            } finally {
            }
        }
    } finally {
        this.otherIn.readDisconnect();
        tearDown();
    }
}
Also used : ClosedException(com.revolsys.parallel.channel.ClosedException) Channel(com.revolsys.parallel.channel.Channel) RecordDefinition(com.revolsys.record.schema.RecordDefinition) MultiInputSelector(com.revolsys.parallel.channel.MultiInputSelector) Record(com.revolsys.record.Record)

Example 3 with MultiInputSelector

use of com.revolsys.parallel.channel.MultiInputSelector in project com.revolsys.open by revolsys.

the class RunnableChannelExecutor method run.

@Override
public void run() {
    preRun();
    try {
        final MultiInputSelector selector = new MultiInputSelector();
        while (!isShutdown()) {
            final List<Channel<Runnable>> channels = this.channels;
            try {
                if (!isShutdown()) {
                    final Channel<Runnable> channel = selector.selectChannelInput(channels);
                    if (channel != null) {
                        final Runnable runnable = channel.read();
                        execute(runnable);
                    }
                }
            } catch (final ClosedException e) {
                final Throwable cause = e.getCause();
                if (cause instanceof ThreadInterruptedException) {
                    throw (ThreadInterruptedException) cause;
                }
                synchronized (this.monitor) {
                    for (final Iterator<Channel<Runnable>> iterator = channels.iterator(); iterator.hasNext(); ) {
                        final Channel<Runnable> channel = iterator.next();
                        if (channel.isClosed()) {
                            iterator.remove();
                        }
                    }
                    if (channels.isEmpty()) {
                        return;
                    }
                }
            }
        }
    } catch (final ThreadInterruptedException e) {
        throw e;
    } catch (final Throwable t) {
        if (!isShutdown()) {
            Logs.error(this, "Unexexpected error ", t);
        }
    } finally {
        postRun();
    }
}
Also used : MultiInputSelector(com.revolsys.parallel.channel.MultiInputSelector) ClosedException(com.revolsys.parallel.channel.ClosedException) Channel(com.revolsys.parallel.channel.Channel) Iterator(java.util.Iterator) ThreadInterruptedException(com.revolsys.parallel.ThreadInterruptedException)

Aggregations

Channel (com.revolsys.parallel.channel.Channel)3 MultiInputSelector (com.revolsys.parallel.channel.MultiInputSelector)3 ClosedException (com.revolsys.parallel.channel.ClosedException)2 Record (com.revolsys.record.Record)2 ThreadInterruptedException (com.revolsys.parallel.ThreadInterruptedException)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1