summaryrefslogtreecommitdiff
path: root/src/core/src/data/filters/MVDataCombiner.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/src/data/filters/MVDataCombiner.java')
-rw-r--r--src/core/src/data/filters/MVDataCombiner.java164
1 files changed, 164 insertions, 0 deletions
diff --git a/src/core/src/data/filters/MVDataCombiner.java b/src/core/src/data/filters/MVDataCombiner.java
new file mode 100644
index 0000000..1b2eb3f
--- /dev/null
+++ b/src/core/src/data/filters/MVDataCombiner.java
@@ -0,0 +1,164 @@
+/*
+ * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes
+ * Copyright (C) 2014 Ludovic Pouzenc <ludovic@pouzenc.fr>
+ *
+ * This file is part of SSSync.
+ *
+ * SSSync is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * SSSync is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with SSSync. If not, see <http://www.gnu.org/licenses/>
+ */
+
+package data.filters;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import data.MVDataEntry;
+import data.io.AbstractMVDataReader;
+import data.io.MVDataReader;
+
+/**
+ * Combines arbitrary number of MVData* sources while behaving same as AbstractMVDataReader.
+ * This could enable a sync implementation to merge multiple sources
+ * before sync'ing in a transparent manner.
+ * To prevent memory consumption, this assumes that all sources will be read
+ * with lexicographical ascending order on the "key" field.
+ *
+ * @author lpouzenc
+ */
+public class MVDataCombiner extends AbstractMVDataReader {
+
+ public enum MVDataCombineMode { PRIMARY_SOURCE, MERGE_APPEND, MERGE_REPLACE, OVERRIDE };
+
+ private final MVDataReader[] readers;
+ private final MVDataCombineMode[] mergeModes;
+
+ private transient Iterator<MVDataEntry>[] readerIterators;
+ private transient MVDataEntry[] lookAheadData;
+ private transient String lastKey;
+
+
+ public MVDataCombiner(String dataSourceName, MVDataReader[] readers, MVDataCombineMode mergeModes[]) {
+ if ( readers == null || mergeModes == null || (mergeModes.length != readers.length) ) {
+ throw new IllegalArgumentException("readers and mergeModes arrays should have same size");
+ }
+ if ( ! (mergeModes.length > 0) || mergeModes[0] != MVDataCombineMode.PRIMARY_SOURCE ) {
+ throw new IllegalArgumentException("MVDataCombiner first mergeModes should always be PRIMARY_SOURCE");
+ }
+
+ this.dataSourceName = dataSourceName;
+ this.readers = readers.clone();
+ this.mergeModes = mergeModes.clone();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings("unchecked") /* for new Iterator[...] */
+ public Iterator<MVDataEntry> iterator() {
+ // Be cautious to reset everything
+ readerIterators = new Iterator[readers.length];
+ for (int i=0; i<readers.length;i++) {
+ readerIterators[i] = readers[i].iterator();
+ }
+ lookAheadData = new MVDataEntry[readers.length];
+ lastKey = null;
+
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ for ( MVDataEntry line : lookAheadData ) {
+ if ( line != null ) { return true; }
+ }
+ for ( MVDataReader reader : readers ) {
+ if ( reader.hasNext() ) { return true; }
+ }
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public MVDataEntry next() {
+
+ final String currentKey = lookAheadAll();
+
+ // Check if there was unsorted lines in source data
+ if ( lastKey != null && (currentKey.compareTo(lastKey) < 0) ) {
+ //XXX : this is checked here and in SafeDataReader (redundant), but both are optionnal...
+ throw new UnsupportedOperationException("At least one data source is out of order. " +
+ "Data sources are excepted to be read sorted by MVDataEntry key (ascending lexicogrpahical order)");
+ }
+
+ // Merge all data sources for key currentKey
+ MVDataEntry result = null;
+ for ( int i=0; i<lookAheadData.length; i++) {
+ if ( lookAheadData[i] != null && lookAheadData[i].getKey().equals(currentKey) ) {
+ if ( result == null ) {
+ result = lookAheadData[i];
+ } else {
+ //XXX : some items in LDAP could have constrains like : "not multi-valued". Force MERGE_REPLACE mode ?
+ //FIXME : honor all Combine modes
+ result.mergeValues( (mergeModes[i] == MVDataCombineMode.MERGE_APPEND ),lookAheadData[i]);
+ }
+ lookAheadData[i]=null; // "Pop" the used entry
+ }
+ }
+
+ lastKey = currentKey;
+
+ return result;
+ }
+
+ private String lookAheadAll() {
+ String minKey=null;
+
+ // Feed the look-ahead buffer (look forward by 1 value for each reader)
+ for ( int i=0; i<lookAheadData.length; i++) {
+ if ( lookAheadData[i] == null && readerIterators[i].hasNext() ) {
+ lookAheadData[i] = readerIterators[i].next();
+ }
+ }
+
+ // Find the least RelData key from look-ahead buffers
+ for (MVDataEntry entry: lookAheadData) {
+ if ( entry != null ) {
+ final String minKeyCandidate = entry.getKey();
+ if ( minKey == null || minKey.compareTo(minKeyCandidate) > 0 ) {
+ minKey = minKeyCandidate;
+ }
+ }
+ }
+
+ // Sanity checks
+ if ( minKey == null ) {
+ // Every reader is empty and look-ahead buffer is empty (hasNext() should have said false)
+ throw new NoSuchElementException();
+ }
+
+ return minKey;
+ }
+
+ // Boring accessors
+
+ public String getLastKey() {
+ return lastKey;
+ }
+}