io.sirix.diff.export.EditScript Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sirix-core Show documentation
Show all versions of sirix-core Show documentation
SirixDB is a hybrid on-disk and in-memory document oriented, versioned database system. It has a lightweight buffer manager, stores everything in a huge persistent and durable tree and allows efficient reconstruction of every revision. Furthermore, SirixDB implements change tracking, diffing and supports time travel queries.
/**
* Copyright (c) 2011, University of Konstanz, Distributed Systems Group All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met: * Redistributions of source code must retain the
* above copyright notice, this list of conditions and the following disclaimer. * Redistributions
* in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the name of the University of Konstanz nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package io.sirix.diff.export;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import io.sirix.diff.DiffFactory.DiffType;
import io.sirix.diff.DiffTuple;
/**
* Builds an edit script.
*
* @author Johannes Lichtenberger, University of Konstanz
*
*/
public final class EditScript implements Iterator, Iterable {
/** Preserves the order of changes and is used to iterate over all changes. */
private final List mChanges;
/** To do a lookup; we use node/object identities. */
private final IdentityHashMap mChangeByNode;
/** Index in the {@link List} of {@link DiffTuple}s. */
private transient int mIndex;
/**
* Constructor.
*/
public EditScript() {
mChanges = new ArrayList();
mChangeByNode = new IdentityHashMap();
mIndex = 0;
}
/**
* Calculates the size of the edit script. This can be used to estimate the amicability of an
* algorithm.
*
* @return number of changes
*/
public int size() {
return mChanges.size();
}
/**
* Checks if the edit script is empty.
*
* @return true if empty
*/
public boolean isEmpty() {
return mChanges.isEmpty();
}
/**
* Checks if a node has been added(changed).
*
* @param key key of node
* @return true if the changes {@link List} already contains the nodeKey, false otherwise
*/
public boolean containsNode(final long key) {
if (key < 0) {
throw new IllegalArgumentException("paramKey may not be < 0!");
}
return mChangeByNode.containsKey(key);
}
/**
* Clears the edit script.
*/
public void clear() {
mChanges.clear();
mChangeByNode.clear();
}
/**
* Look up a change for the given nodeKey.
*
* @param paramKey (not) changed key of node
* @return the change assigned to the node or null
*/
public DiffTuple get(final long paramKey) {
return mChangeByNode.get(paramKey);
}
/**
* Adds a change to the edit script.
*
* @param change {@link DiffTuple} reference
* @return the change
*/
public DiffTuple add(final DiffTuple change) {
assert change != null;
final long nodeKey =
change.getDiff() == DiffType.DELETED ? change.getOldNodeKey() : change.getNewNodeKey();
if (mChangeByNode.containsKey(nodeKey)) {
return change;
}
mChanges.add(change);
return mChangeByNode.put(nodeKey, change);
}
@Override
public Iterator iterator() {
return mChanges.iterator();
}
@Override
public boolean hasNext() {
if (mIndex < mChanges.size() - 1) {
return true;
}
return false;
}
@Override
public DiffTuple next() {
if (mIndex < mChanges.size()) {
return mChanges.get(mIndex++);
} else {
throw new NoSuchElementException("No more elements in the change list!");
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove is not supported!");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy