From d6f22a2af48f83d63b5381118d2029797458194e Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sat, 20 Sep 2014 09:17:18 +0200 Subject: Early development stages (before SCM) : WIP_1 Early development stages (before SCM) : WIP_2 Early development stages (before SCM) : WIP_3 Early development stages (before SCM) : WIP_4 Early development stages (before SCM) : WIP_6 Early development stages (before SCM) : WIP_7 Early development stages (before SCM) : WIP_8 Adds documentation folder as an Eclipse project. Adds README for github. Decent source tree by tuning Eclise project's location One forgetten file while movign everything :) Adding Copyright, licencing (GPL v3), correcting README --- src/core/.classpath | 9 + src/core/.project | 17 ++ src/core/.settings/org.eclipse.jdt.core.prefs | 11 + src/core/JUTests/data/MVDataEntryTest.java | 93 ++++++++ .../data/io/filters/MVDataCombinerTest.java | 148 +++++++++++++ src/core/build.xml | 78 +++++++ src/core/lib/guava-16.0.1.jar | Bin 0 -> 2228009 bytes src/core/src/data/MVDataEntry.java | 238 +++++++++++++++++++++ src/core/src/data/filters/MVDataCombiner.java | 164 ++++++++++++++ src/core/src/data/io/AbstractMVDataReader.java | 49 +++++ src/core/src/data/io/AbstractMVDataWriter.java | 70 ++++++ src/core/src/data/io/MVDataReader.java | 39 ++++ src/core/src/data/io/MVDataWriter.java | 45 ++++ src/core/src/data/io/stub/StubDataReader.java | 63 ++++++ src/core/src/data/io/stub/StubDataWriter.java | 104 +++++++++ src/core/src/sync/AbstractSyncTask.java | 71 ++++++ 16 files changed, 1199 insertions(+) create mode 100644 src/core/.classpath create mode 100644 src/core/.project create mode 100644 src/core/.settings/org.eclipse.jdt.core.prefs create mode 100644 src/core/JUTests/data/MVDataEntryTest.java create mode 100644 src/core/JUTests/data/io/filters/MVDataCombinerTest.java create mode 100644 src/core/build.xml create mode 100644 src/core/lib/guava-16.0.1.jar create mode 100644 src/core/src/data/MVDataEntry.java create mode 100644 src/core/src/data/filters/MVDataCombiner.java create mode 100644 src/core/src/data/io/AbstractMVDataReader.java create mode 100644 src/core/src/data/io/AbstractMVDataWriter.java create mode 100644 src/core/src/data/io/MVDataReader.java create mode 100644 src/core/src/data/io/MVDataWriter.java create mode 100644 src/core/src/data/io/stub/StubDataReader.java create mode 100644 src/core/src/data/io/stub/StubDataWriter.java create mode 100644 src/core/src/sync/AbstractSyncTask.java (limited to 'src/core') diff --git a/src/core/.classpath b/src/core/.classpath new file mode 100644 index 0000000..f7de406 --- /dev/null +++ b/src/core/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/src/core/.project b/src/core/.project new file mode 100644 index 0000000..acda864 --- /dev/null +++ b/src/core/.project @@ -0,0 +1,17 @@ + + + SSSync_Core + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/core/.settings/org.eclipse.jdt.core.prefs b/src/core/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8000cd6 --- /dev/null +++ b/src/core/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/src/core/JUTests/data/MVDataEntryTest.java b/src/core/JUTests/data/MVDataEntryTest.java new file mode 100644 index 0000000..19ccb46 --- /dev/null +++ b/src/core/JUTests/data/MVDataEntryTest.java @@ -0,0 +1,93 @@ +package data; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.google.common.collect.HashMultimap; + +public class MVDataEntryTest { + + @Test + public void testMVDataEntryStringIntInt() { + String expected = "{key=line1, attrValPairs={k4=[v4], k1=[v1b, v1a, v1c], k2=[v1c]}}"; + + MVDataEntry e1 = new MVDataEntry("line1", 1, 1); + HashMultimap e1v = e1.getAttrValPairs(); + e1v.put("k1", "v1a"); + e1v.put("k1", "v1b"); + e1v.put("k1", "v1b"); // Twice, should disappear silently + e1v.put("k1", "v1c"); + e1v.put("k2", "v1c"); + e1v.put("k4", "v4"); + + assertEquals(expected, e1.toString()); + + } + + @Test + public void testMerge() { + // Test data + MVDataEntry e1 = new MVDataEntry("10"); + HashMultimap e1v = e1.getAttrValPairs(); + e1v.put("k1", "v1a"); + e1v.put("k1", "v1b"); + e1v.put("k1", "v1c"); + e1v.put("k2", "v2"); + e1v.put("k4", "v4"); + + MVDataEntry e2 = new MVDataEntry("2"); + HashMultimap e2v = e2.getAttrValPairs(); + e2v.put("k2", "v2"); + e2v.put("k1", "v1b"); + e2v.put("k3", "v3"); + + MVDataEntry r1 = new MVDataEntry(e1); + r1.mergeValues(true, e2); + assertNotSame(r1, e1); + String expected1 = "{key=10, attrValPairs={k3=[v3], k4=[v4], k1=[v1b, v1a, v1c], k2=[v2]}}"; + assertEquals(expected1, r1.toString()); + + MVDataEntry r2 = new MVDataEntry(e2); + r2.mergeValues(true, e1); + assertNotSame(r2, e2); + String expected2 = "{key=2, attrValPairs={k3=[v3], k4=[v4], k1=[v1b, v1a, v1c], k2=[v2]}}"; + assertEquals(expected2, r2.toString()); + + MVDataEntry r3 = new MVDataEntry(e1); + r3.mergeValues(false, e2); + assertNotSame(r3, e1); + String expected3 = "{key=10, attrValPairs={k3=[v3], k4=[v4], k1=[v1b], k2=[v2]}}"; + //System.out.println(expected3); + //System.out.println(r3.toString()); + assertEquals(expected3, r3.toString()); + + MVDataEntry r4 = new MVDataEntry(e2); + r4.mergeValues(false, e1); + assertNotSame(r4, e1); + String expected4 = "{key=2, attrValPairs={k3=[v3], k4=[v4], k1=[v1b, v1a, v1c], k2=[v2]}}"; + assertEquals(expected4, r4.toString()); + + assertTrue(!r2.equals(r3)); + assertEquals(r2,r4); + } + + @Test + public void testSplitAndPut() { + MVDataEntry r1 = new MVDataEntry("10"); + r1.splitAndPut("k1", "v1a;v1b;v1c", ";"); + r1.put("k2", "v2", null); // splitAndPut does not support null regex anymore, use put() + r1.splitAndPut("k4", "v4", "^$"); + + MVDataEntry expected1 = new MVDataEntry("10"); + HashMultimap expected1v = expected1.getAttrValPairs(); + expected1v.put("k1", "v1a"); + expected1v.put("k1", "v1b"); + expected1v.put("k1", "v1c"); + expected1v.put("k2", "v2"); + expected1v.put("k4", "v4"); + + assertEquals(r1,expected1); + } + +} diff --git a/src/core/JUTests/data/io/filters/MVDataCombinerTest.java b/src/core/JUTests/data/io/filters/MVDataCombinerTest.java new file mode 100644 index 0000000..5d32dd8 --- /dev/null +++ b/src/core/JUTests/data/io/filters/MVDataCombinerTest.java @@ -0,0 +1,148 @@ +package data.io.filters; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import data.MVDataEntry; +import data.filters.MVDataCombiner; +import data.filters.MVDataCombiner.MVDataCombineMode; +import data.io.MVDataReader; +import data.io.stub.StubDataReader; + +public class MVDataCombinerTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void testOutOfOrderCase() { + // Test Data + MVDataEntry e10 = new MVDataEntry("line2"); + e10.getAttrValPairs().put("merge", "e10"); + MVDataEntry e11 = new MVDataEntry("line1"); + e11.getAttrValPairs().put("merge", "e11"); + + MVDataEntry e21 = new MVDataEntry("line2"); + e21.getAttrValPairs().put("merge", "e21"); + + MVDataEntry[][] fakeEntries = new MVDataEntry[][] { + new MVDataEntry[] { e10, e11 }, + new MVDataEntry[] { e21 }, + }; + + MVDataCombineMode mergeModes[] = new MVDataCombineMode[]{ + MVDataCombineMode.PRIMARY_SOURCE, + MVDataCombineMode.MERGE_APPEND, + }; + + // Expected results + MVDataEntry line1 = new MVDataEntry(e10); + line1.mergeValues(true, e21); + + MVDataEntry expected[] = new MVDataEntry[] { + line1, + null /* Should throw UnsupportedOperationException() before comparing */ + }; + + // Test run + exception.expect(UnsupportedOperationException.class); + doCombineTest(expected, fakeEntries, mergeModes); + } + + + @Test + public void testGeneralCase() { + + // Test Data + MVDataEntry e10 = new MVDataEntry("line3"); + e10.getAttrValPairs().put("from1", "e10"); + e10.getAttrValPairs().put("merge", "e10"); + MVDataEntry e11 = new MVDataEntry("line4"); + e11.getAttrValPairs().put("from1", "e11"); + e11.getAttrValPairs().put("merge", "e11"); + + MVDataEntry e20 = new MVDataEntry("line1"); + e20.getAttrValPairs().put("from2", "e20"); + e20.getAttrValPairs().put("merge", "e20"); + MVDataEntry e21 = new MVDataEntry("line2"); + e21.getAttrValPairs().put("from2", "e21"); + e21.getAttrValPairs().put("merge", "e21"); + MVDataEntry e22 = new MVDataEntry("line3"); + e22.getAttrValPairs().put("from2", "e22"); + e22.getAttrValPairs().put("merge", "e22"); + + MVDataEntry e30 = new MVDataEntry("line2"); + e30.getAttrValPairs().put("from3", "e30"); + e30.getAttrValPairs().put("merge", "e30"); + + + MVDataEntry[][] fakeEntries = new MVDataEntry[][] { + new MVDataEntry[] { e10, e11 }, + new MVDataEntry[] { e20, e21, e22 }, + new MVDataEntry[] { e30 }, + }; + + MVDataCombineMode mergeModes[] = new MVDataCombineMode[]{ + MVDataCombineMode.PRIMARY_SOURCE, + MVDataCombineMode.MERGE_REPLACE, + MVDataCombineMode.MERGE_APPEND, + }; + + // Expected results + MVDataEntry line1 = new MVDataEntry(e20); + + MVDataEntry line2 = new MVDataEntry(e21); + line2.mergeValues(true, e30); + + MVDataEntry line3 = new MVDataEntry(e10); + line3.mergeValues(false, e22); + + MVDataEntry line4 = new MVDataEntry(e11); + + MVDataEntry expected[] = new MVDataEntry[] { + line1,line2,line3,line4 + }; + + // Test run + doCombineTest(expected, fakeEntries, mergeModes); + } + + // TODO : test all Combine modes + + /** + * Helper function to factorise Combiner tests. + * @param expected + * @param fakeEntries + * @param mergeModes + */ + public void doCombineTest(MVDataEntry expected[], MVDataEntry[][] fakeEntries, MVDataCombineMode mergeModes[]) { + // Test init + MVDataReader readers[] = new MVDataReader[fakeEntries.length]; + for (int i = 0; i < fakeEntries.length; i++) { + readers[i] = new StubDataReader("fakeReader"+i,fakeEntries[i]); + } + + MVDataCombiner combiner = new MVDataCombiner("combiner", readers, mergeModes); + + // Test twice to check if asking a new iterator "rewinds" correctly + for (int i=0;i<2;i++) { + //System.out.println("Loop " + (i+1)); + + Iterator combinerIt = combiner.iterator(); + for (int j = 0; j < expected.length; j++) { + assertTrue(combinerIt.hasNext()); + MVDataEntry item = combinerIt.next(); + //System.out.println(expected[i]); + //System.out.println(item); + //System.out.println(); + assertEquals(expected[j], item); + } + assertFalse(combinerIt.hasNext()); + } + } +} diff --git a/src/core/build.xml b/src/core/build.xml new file mode 100644 index 0000000..e46c220 --- /dev/null +++ b/src/core/build.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/core/lib/guava-16.0.1.jar b/src/core/lib/guava-16.0.1.jar new file mode 100644 index 0000000..2c8127d Binary files /dev/null and b/src/core/lib/guava-16.0.1.jar differ diff --git a/src/core/src/data/MVDataEntry.java b/src/core/src/data/MVDataEntry.java new file mode 100644 index 0000000..f92a141 --- /dev/null +++ b/src/core/src/data/MVDataEntry.java @@ -0,0 +1,238 @@ +/* + * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes + * Copyright (C) 2014 Ludovic Pouzenc + * + * 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 + */ + +package data; + +import java.util.HashSet; +import java.util.Map.Entry; +import java.util.Set; + +import com.google.common.collect.HashMultimap; + +/** + * Generic Multi-Valued data type. Each object store a particular entry. + * Semantics are like in LDAP directories : an entry = a key + a set of multi-valued attributes. + * Relational data like in RDMS are more constrained : columns are fixed for an entire table. + * Null and empty string attribute value are silently discarded. + * + * @author lpouzenc + */ +public class MVDataEntry implements Comparable { + + /** + * The key part that identify this particular entry. + */ + private final String key; + /** + * The data part of this particular entry. + */ + private HashMultimap attrValPairs; + + // XXX : add an HashMap for meta or constraints ? + + // Constructors + + /** + * Build a fresh empty MVDataEntry. + * @param key Unique key identifying this entry + */ + public MVDataEntry(String key) { + if ( key == null ) { + throw new IllegalArgumentException("key must be non-null"); + } + this.key = key; + this.attrValPairs = HashMultimap.create(); + } + + /** + * Build a fresh empty MVDataEntry with hints about expected attr/values count. + * @param key Unique key identifying this entry + */ + public MVDataEntry(String key, int expectedAttribs, int expectedValuesPerAttrib) { + if ( key == null ) { + throw new IllegalArgumentException("key must be non-null"); + } + this.key = key; + this.attrValPairs = HashMultimap.create(expectedAttribs, expectedValuesPerAttrib); + } + + /** + * Deep copy of an existing MVDataEntry. + * @param key Unique key identifying this entry + */ + public MVDataEntry(final MVDataEntry copyFrom) { + this.key=copyFrom.key; // String is immutable, so ref copy is okay + this.attrValPairs = HashMultimap.create(copyFrom.attrValPairs); + } + + /** + * Proxy function to return all attribute/value pairs. + * One can use read a MVDataEntry without depending on non-standard HashMultimap. + * @return + */ + public Set> getAllEntries() { + return this.attrValPairs.entries(); + } + + /** + * Proxy function to add an attribute/value pair in attrValPairs. + * One can use MVDataEntry without depending on non-standard HashMultimap. + * + * @param attr + * @param value + */ + public void put(String attr, String... values) { + for (String value: values) { + if ( value != null && !value.isEmpty() ) { + this.attrValPairs.put(attr, value); + } + } + } + + /** + * Proxy function to get all values from a particular attribute. + * One can use MVDataEntry without depending on non-standard HashMultimap. + * @param attr + * @return + */ + public Set getValues(String attr) { + return this.attrValPairs.get(attr); + } + + /** + * Helper function to insert multiple values from a single string. + * + * @param attr + * @param value + * @param splitRegex + */ + public void splitAndPut(String attr, String value, String splitRegex) { + if ( value != null ) { + for (String v : value.split(splitRegex)) { + put(attr, v); + } + } + } + + /** + * Helper function to return list of changed attributes. + * Note : this don't keep track of deleted attributes. + * @param original + * @return + */ + public Set getChangedAttributes(MVDataEntry original) { + HashSet result = new HashSet(); + + for (String attr: this.attrValPairs.keySet()) { + Set thisValue = this.attrValPairs.get(attr); + Set originalValue = original.attrValPairs.get(attr); + if ( ! thisValue.equals(originalValue) ) { + result.add(attr); + } + } + + return result; + } + + /** + * Augment this entry with attr/values from other entries. + * @param appendMode Select behavior on an existing attribute : append values or replace them + * @param entries Entries to merge with current entry + */ + public void mergeValues(boolean appendMode, MVDataEntry... entries) { + for(MVDataEntry entry : entries) { + if ( ! appendMode ) { + for (String attr : entry.attrValPairs.keySet()) { + this.attrValPairs.removeAll(attr); + } + } + this.attrValPairs.putAll(entry.attrValPairs); + } + } + + /** + * Check if this entry seems contains useful data. + * @return true if this entry seems contains useful data + */ + public boolean isValid() { + boolean validKey=(this.key != null && this.key.length() > 0 ); + boolean validVal=(this.attrValPairs != null && ! this.attrValPairs.isEmpty()); + + return (validKey && validVal); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) { + // Check for self-comparison (compare object references) + if ( this == obj ) { return true; } + // Check non-nullity and type + if ( !( obj instanceof MVDataEntry) ) { return false; } + // Cast safely + MVDataEntry other = (MVDataEntry) obj; + // Check all fields (known to be always non null) + return ( this.key.equals(other.key) && this.attrValPairs.equals(other.attrValPairs) ); + } + + /** + * Compares entries. Ordering of entries is the ordering of their keys. + * (java.lang.String default ordering : lexicographical ascending order) + */ + @Override + public int compareTo(MVDataEntry other) { + return this.key.compareTo(other.key); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return "{key=" + key + ", attrValPairs=" + attrValPairs.toString() + "}"; + } + + + // Boring accessors + /** + * @return the attrValPairs + */ + public HashMultimap getAttrValPairs() { + return attrValPairs; + } + + /** + * @param attrValPairs the attrValPairs to set + */ + public void setAttrValPairs(HashMultimap attrValPairs) { + this.attrValPairs = attrValPairs; + } + + /** + * @return the key (guaranteed to be non-null) + */ + public String getKey() { + return key; + } + + + +} 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 + * + * 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 + */ + +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[] 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 iterator() { + // Be cautious to reset everything + readerIterators = new Iterator[readers.length]; + for (int i=0; i 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; + } +} diff --git a/src/core/src/data/io/AbstractMVDataReader.java b/src/core/src/data/io/AbstractMVDataReader.java new file mode 100644 index 0000000..3e63de1 --- /dev/null +++ b/src/core/src/data/io/AbstractMVDataReader.java @@ -0,0 +1,49 @@ +/* + * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes + * Copyright (C) 2014 Ludovic Pouzenc + * + * 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 + */ + +package data.io; + +/** + * Stream-oriented abstract reader from a particular data source. + * Memory footprint should not depends on readable line count nor next() call count. + * + * @author lpouzenc + */ +public abstract class AbstractMVDataReader implements MVDataReader { + + protected String dataSourceName="(unknown source)"; + + /** + * Not supported (Readers are read-only). + */ + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + /* (non-Javadoc) + * @see data.io.MVDataReader#getDataSourceName() + */ + @Override + public String getDataSourceName() { + return dataSourceName; + } + +} diff --git a/src/core/src/data/io/AbstractMVDataWriter.java b/src/core/src/data/io/AbstractMVDataWriter.java new file mode 100644 index 0000000..454e8ce --- /dev/null +++ b/src/core/src/data/io/AbstractMVDataWriter.java @@ -0,0 +1,70 @@ +/* + * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes + * Copyright (C) 2014 Ludovic Pouzenc + * + * 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 + */ + +package data.io; + +import java.util.Set; + +import data.MVDataEntry; + +/** + * Stream-oriented abstract writer from a particular data source. + * All derived writers should honor a dry-run mode. + * + * @author lpouzenc + */ +public abstract class AbstractMVDataWriter implements MVDataWriter { + //TODO : not so useful. Interface extraction was not a good idea ? + + /** + * Dry-run mode flag (disabled by default) + */ + protected boolean dryRun=false; + + /* (non-Javadoc) + * @see data.io.MVDataWriter#isDryRun() + */ + public boolean isDryRun() { + return dryRun; + } + + /* (non-Javadoc) + * @see data.io.MVDataWriter#setDryRun(boolean) + */ + public void setDryRun(boolean dryRun) { + this.dryRun = dryRun; + } + + /* (non-Javadoc) + * @see data.io.MVDataWriter#insert(data.MVDataEntry) + */ + @Override + public abstract void insert(MVDataEntry newEntry) throws Exception; + /* (non-Javadoc) + * @see data.io.MVDataWriter#update(data.MVDataEntry, data.MVDataEntry, java.util.Set) + */ + @Override + public abstract void update(MVDataEntry updatedEntry, MVDataEntry originalEntry, Set attrToUpdate) throws Exception; + /* (non-Javadoc) + * @see data.io.MVDataWriter#delete(data.MVDataEntry) + */ + @Override + public abstract void delete(MVDataEntry existingEntry) throws Exception; +} diff --git a/src/core/src/data/io/MVDataReader.java b/src/core/src/data/io/MVDataReader.java new file mode 100644 index 0000000..8a9871a --- /dev/null +++ b/src/core/src/data/io/MVDataReader.java @@ -0,0 +1,39 @@ +/* + * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes + * Copyright (C) 2014 Ludovic Pouzenc + * + * 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 + */ + +package data.io; + +import java.util.Iterator; + +import data.MVDataEntry; + +/** + * TODO javadoc + * + * @author lpouzenc + */ +public interface MVDataReader extends Iterator, Iterable{ + + /** + * @return the dataSourceName + */ + public String getDataSourceName(); + +} \ No newline at end of file diff --git a/src/core/src/data/io/MVDataWriter.java b/src/core/src/data/io/MVDataWriter.java new file mode 100644 index 0000000..2f16fbc --- /dev/null +++ b/src/core/src/data/io/MVDataWriter.java @@ -0,0 +1,45 @@ +/* + * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes + * Copyright (C) 2014 Ludovic Pouzenc + * + * 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 + */ + +package data.io; + +import java.util.Set; + +import data.MVDataEntry; + +/** + * TODO javadoc + * + * @author lpouzenc + */ +public interface MVDataWriter { + + public boolean isDryRun(); + public void setDryRun(boolean dryRun); + + public void insert(MVDataEntry newEntry) throws Exception; + + public void update(MVDataEntry updatedEntry, + MVDataEntry originalEntry, Set attrToUpdate) + throws Exception; + + public void delete(MVDataEntry existingEntry) throws Exception; + +} \ No newline at end of file diff --git a/src/core/src/data/io/stub/StubDataReader.java b/src/core/src/data/io/stub/StubDataReader.java new file mode 100644 index 0000000..ed91267 --- /dev/null +++ b/src/core/src/data/io/stub/StubDataReader.java @@ -0,0 +1,63 @@ +/* + * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes + * Copyright (C) 2014 Ludovic Pouzenc + * + * 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 + */ + +package data.io.stub; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import data.MVDataEntry; +import data.io.AbstractMVDataReader; + +/** + * Stub reader implementation for automated tests. + * + * @author lpouzenc + */ +public class StubDataReader extends AbstractMVDataReader { + + private final MVDataEntry fakeEntries[]; + private int cursorRead; + + public StubDataReader(String dataSourceName, MVDataEntry[] fakeEntries) { + this.dataSourceName = dataSourceName; + this.fakeEntries = fakeEntries.clone(); + } + + @Override + public Iterator iterator() { + this.cursorRead = 0; + return this; + } + + @Override + public boolean hasNext() { + return cursorRead < fakeEntries.length; + } + + @Override + public MVDataEntry next() { + if ( ! hasNext() ) { + throw new NoSuchElementException(); + } + return fakeEntries[cursorRead++]; + } + +} diff --git a/src/core/src/data/io/stub/StubDataWriter.java b/src/core/src/data/io/stub/StubDataWriter.java new file mode 100644 index 0000000..cd08e77 --- /dev/null +++ b/src/core/src/data/io/stub/StubDataWriter.java @@ -0,0 +1,104 @@ +/* + * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes + * Copyright (C) 2014 Ludovic Pouzenc + * + * 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 + */ + +package data.io.stub; + +import java.util.Set; + +import data.MVDataEntry; +import data.io.AbstractMVDataWriter; + +/** + * Stub writer implementation for automated tests. + * + * @author lpouzenc + */ +public class StubDataWriter extends AbstractMVDataWriter { + + enum OpKind { INSERT, UPDATE, DELETE }; + + private final int maxLogEntries; + + private OpKind opLog[]; + private MVDataEntry opData[]; + private int cursorLog; + + public StubDataWriter(int maxLogEntries) { + this.maxLogEntries = maxLogEntries; + this.opLog = new OpKind[maxLogEntries]; + this.opData = new MVDataEntry[maxLogEntries]; + } + + @Override + public void insert(MVDataEntry newline) { + if ( cursorLog >= maxLogEntries) { + throw new IllegalStateException(); + } + opLog[cursorLog]=OpKind.INSERT; + opData[cursorLog]=newline; + cursorLog++; + } + + @Override + public void update(MVDataEntry updatedLine, MVDataEntry originalLine, Set attrToUpdate) { + if ( cursorLog >= maxLogEntries) { + throw new IllegalStateException(); + } + opLog[cursorLog]=OpKind.UPDATE; + opData[cursorLog]=updatedLine; + cursorLog++; + } + + @Override + public void delete(MVDataEntry existingLine) { + if ( cursorLog >= maxLogEntries) { + throw new IllegalStateException(); + } + opLog[cursorLog]=OpKind.DELETE; + opData[cursorLog]=existingLine; + cursorLog++; + } + + @Override + public String toString() { + StringBuffer buf = new StringBuffer(); + + for (int i = 0; i < cursorLog; i++) { + buf.append(opLog[i] + ": " + opData[i] + "\n"); + } + + return buf.toString(); + } + + /** + * @return the opLog + */ + public OpKind[] getOpLog() { + return opLog.clone(); + } + + /** + * @return the opData + */ + public MVDataEntry[] getOpData() { + return opData.clone(); + } + +} diff --git a/src/core/src/sync/AbstractSyncTask.java b/src/core/src/sync/AbstractSyncTask.java new file mode 100644 index 0000000..e2ae94d --- /dev/null +++ b/src/core/src/sync/AbstractSyncTask.java @@ -0,0 +1,71 @@ +/* + * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes + * Copyright (C) 2014 Ludovic Pouzenc + * + * 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 + */ + +package sync; + +import java.util.concurrent.Callable; + +/** + * Abstract class to define a common base of all kind of synchronization algorithms. + * + * @author lpouzenc + */ +public abstract class AbstractSyncTask implements Callable { + + /** + * Pretty task name to be inserted in log lines + */ + protected String taskName="(unknown task)"; + + /** + * Dry-run mode flag (disabled by default) + */ + protected boolean dryRun=false; + + /** + * Main method that do the actual sync + */ + public abstract Boolean call(); + + + // Boring accessors + + /** + * @return the dryRun mode status (enabled/disabled) + */ + public boolean isDryRun() { + return dryRun; + } + + /** + * @param dryRun the dryRun mode to set (enabled/disabled) + */ + public void setDryRun(boolean dryRun) { + this.dryRun = dryRun; + } + + /** + * @return the taskName + */ + public String getTaskName() { + return taskName; + } + +} -- cgit v1.2.3