Search in sources :

Example 1 with DiffTuple

use of org.sirix.diff.DiffTuple in project sirix by sirixdb.

the class DiffSunburstAxis method processFirstChild.

/**
 * Process first child.
 *
 * @param pDepth
 *          depth of diff
 * @param pNextDepth
 *          depth of next diff
 * @return {@code true} if axis has more nodes, {@code false} otherwise
 */
private boolean processFirstChild(@Nonnegative final int pDepth, @Nonnegative final int pNextDepth) {
    assert pDepth >= 0;
    assert pNextDepth >= 0;
    processLastItem(pNextDepth);
    if (mModel.getIsPruned()) {
        if (mDiff != DiffType.SAMEHASH) {
            mPrunedNodes += mDescendantCount - 1;
        }
        final boolean isOldTransaction = (mDiff == DiffType.DELETED || mDiff == DiffType.MOVEDFROM || mDiff == DiffType.REPLACEDOLD);
        final DiffDepth depthCont = mDiffCont.getDepth();
        final int depth = isOldTransaction ? depthCont.getOldDepth() : depthCont.getNewDepth();
        if (mIndex + mPrunedNodes + 1 < mSize) {
            final DiffTuple nextDiffCont = mDiffs.get(mIndex + mPrunedNodes + 1);
            final DiffType nextDiff = nextDiffCont.getDiff();
            final boolean nextIsOldTransaction = (nextDiff == DiffType.DELETED || nextDiff == DiffType.MOVEDFROM || nextDiff == DiffType.REPLACEDOLD);
            final DiffDepth nextDepthCont = nextDiffCont.getDepth();
            final int nextDepth = nextIsOldTransaction ? nextDepthCont.getOldDepth() : nextDepthCont.getNewDepth();
            if (depth == nextDepth) {
                mAngle += mExtension;
                mMoved = Moved.STARTRIGHTSIBL;
                determineIfHasNext();
                return true;
            } else {
                processStacks(depth, nextDepth);
                mMoved = Moved.ANCHESTSIBL;
                determineIfHasNext();
                return true;
            }
        }
    } else {
        mDiffStack.push(mModificationCount);
        mAngleStack.push(mAngle);
        mExtensionStack.push(mExtension);
        mParentStack.push(mIndex);
        mDescendantsStack.push(mDescendantCount);
        mDepth++;
        mMoved = Moved.CHILD;
    }
    determineIfHasNext();
    return true;
}
Also used : DiffTuple(org.sirix.diff.DiffTuple) DiffDepth(org.sirix.diff.DiffDepth) DiffType(org.sirix.diff.DiffFactory.DiffType)

Example 2 with DiffTuple

use of org.sirix.diff.DiffTuple in project sirix by sirixdb.

the class DiffSunburstAxis method hasNext.

@Override
public boolean hasNext() {
    if (getNext()) {
        return true;
    }
    resetToLastKey();
    // Fail if there is no node anymore.
    if (!mHasNext) {
        return false;
    }
    // Setup everything.
    mDiffCont = mDiffs.get(mIndex + mPrunedNodes + 1);
    mDiff = mDiffCont.getDiff();
    final boolean isOldTransaction = (mDiff == DiffType.DELETED || mDiff == DiffType.MOVEDFROM || mDiff == DiffType.REPLACEDOLD);
    final long nodeKey = isOldTransaction ? mDiffCont.getOldNodeKey() : mDiffCont.getNewNodeKey();
    final DiffDepth depthCont = mDiffCont.getDepth();
    mOrigDepth = isOldTransaction ? depthCont.getOldDepth() : depthCont.getNewDepth();
    setTransaction(isOldTransaction ? mOldRtx : mNewRtx);
    // Move to next key.
    getTransaction().moveTo(nodeKey);
    if (mDiff == DiffType.UPDATED) {
        mOldRtx.moveTo(mDiffCont.getOldNodeKey());
    }
    if (mDiff == DiffType.REPLACEDOLD) {
        mNewRtx.moveTo(mDiffCont.getNewNodeKey());
    }
    if (mIndex + mPrunedNodes + 2 < mSize) {
        final DiffTuple nextDiffCont = mDiffs.get(mIndex + mPrunedNodes + 2);
        final DiffType nextDiff = nextDiffCont.getDiff();
        final boolean nextIsOldTransaction = (nextDiff == DiffType.DELETED || nextDiff == DiffType.MOVEDFROM || nextDiff == DiffType.REPLACEDOLD);
        final DiffDepth nextDepthCont = nextDiffCont.getDepth();
        final int nextDepth = nextIsOldTransaction ? nextDepthCont.getOldDepth() : nextDepthCont.getNewDepth();
        // Always follow first child if there is one.
        if (nextDepth > mOrigDepth) {
            return processFirstChild(mOrigDepth, nextDepth);
        }
        // Then follow right sibling if there is one.
        if (mOrigDepth == nextDepth) {
            return processRightSibling(nextDepth);
        }
        // Then follow right sibling on stack.
        if (nextDepth < mOrigDepth) {
            return processNextFollowing(mOrigDepth, nextDepth);
        }
    }
    // Then end.
    processLastItem(1);
    mHasNext = false;
    return true;
}
Also used : DiffTuple(org.sirix.diff.DiffTuple) DiffDepth(org.sirix.diff.DiffDepth) DiffType(org.sirix.diff.DiffFactory.DiffType)

Example 3 with DiffTuple

use of org.sirix.diff.DiffTuple in project sirix by sirixdb.

the class TraverseCompareTree method diffListener.

@Override
public void diffListener(final DiffType diffType, @Nonnull final long newNodeKey, @Nonnull final long oldNodeKey, @Nonnull final DiffDepth depth) {
    LOGWRAPPER.debug("kind of diff: " + diffType);
    if (mPrune != Pruning.DIFF_WITHOUT_SAMEHASHES || (mPrune == Pruning.DIFF_WITHOUT_SAMEHASHES && diffType != DiffType.SAMEHASH) || mEntries == 0) {
        final DiffTuple diffCont = new DiffTuple(diffType, newNodeKey, oldNodeKey, depth);
        final DiffType diff = diffCont.getDiff();
        if (!mHasUpdatedNodes && mLastNodeUpdated) {
            // Has at least one diff, thus it's safe.
            final DiffTuple oldCont = mDiffs.get(mEntries - 1);
            final int oldDepth = getDepth(oldCont);
            final int newDepth = getDepth(diffCont);
            if (newDepth > oldDepth) {
                mHasUpdatedNodes = true;
            }
        }
        try {
            mDiffQueue.put(diffCont);
        } catch (final InterruptedException e) {
            LOGWRAPPER.error(e.getMessage(), e);
        }
        mDiffs.put(mEntries, diffCont);
        switch(diff) {
            case INSERTED:
                mNewKeys.put(newNodeKey, mEntries);
                mModifications++;
                break;
            case DELETED:
                mOldKeys.put(oldNodeKey, mEntries);
                mModifications++;
                break;
            case UPDATED:
                mLastNodeUpdated = true;
                mModifications++;
                break;
            case REPLACEDNEW:
            case REPLACEDOLD:
                mModifications++;
                break;
            default:
        }
        mEntries++;
        if (mEntries % DIFF_THRESHOLD == 0) {
            try {
                // Works even if transactions are not enabled.
                mRunner.run(new PopulateDatabase(mDiffDatabase, mDiffs, mDiffDatabase.getMap().size()));
            } catch (final Exception e) {
                LOGWRAPPER.error(e.getMessage(), e);
            }
        }
    }
}
Also used : DiffTuple(org.sirix.diff.DiffTuple) DiffType(org.sirix.diff.DiffFactory.DiffType) DatabaseException(com.sleepycat.je.DatabaseException) SirixException(org.sirix.exception.SirixException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with DiffTuple

use of org.sirix.diff.DiffTuple in project sirix by sirixdb.

the class TraverseCompareTree method getDepthMax.

/**
 * Get the maximum depth in the tree of the nodes which haven't changed.
 *
 * @return {@code maximum depth} of unchanged nodes
 */
private int getDepthMax() {
    int depthMax = 0;
    for (boolean isNotEmpty = !mDiffQueue.isEmpty(); ((isNotEmpty = !mDiffQueue.isEmpty()) == true) || !mDone; ) {
        if (isNotEmpty) {
            final DiffTuple tuple = mDiffQueue.peek();
            final DiffType diff = tuple.getDiff();
            if (diff == DiffType.SAME || diff == DiffType.SAMEHASH) {
                // Set depth max.
                depthMax = Math.max(mDiffQueue.poll().getDepth().getOldDepth() - mDepth, depthMax);
            } else {
                mDiffQueue.poll();
            }
        }
    }
    mStart.countDown();
    return depthMax;
}
Also used : DiffTuple(org.sirix.diff.DiffTuple) DiffType(org.sirix.diff.DiffFactory.DiffType)

Example 5 with DiffTuple

use of org.sirix.diff.DiffTuple in project sirix by sirixdb.

the class TraverseCompareTree method createSunburstItem.

@Override
public float createSunburstItem(final Item pItem, @Nonnegative final int pDepth, @Nonnegative final int pIndex) {
    checkNotNull(pItem);
    checkArgument(pDepth >= 0, "pDepth must be positive!");
    checkArgument(pIndex >= 0, "pIndex must be >= 0!");
    // Initialize variables.
    final float angle = pItem.mAngle;
    final float parExtension = pItem.mExtension;
    final int indexToParent = pItem.mIndexToParent;
    final int descendantCount = pItem.mDescendantCount;
    final int parentDescCount = pItem.mParentDescendantCount;
    final int modificationCount = pItem.mModificationCount;
    long parentModificationCount = pItem.mParentModificationCount;
    final boolean subtract = pItem.mSubtract;
    final DiffTuple diffCont = pItem.mDiff;
    final int origDepth = pItem.mOrigDepth;
    final int nextDepth = pItem.mNextDepth;
    final int depth = pDepth;
    // Calculate extension.
    float extension = 2 * PConstants.PI;
    if (indexToParent > -1) {
        if (mItems.get(indexToParent).getSubtract()) {
            parentModificationCount -= FACTOR;
        }
        extension = (1f - mModWeight) * (parExtension * (float) descendantCount / ((float) parentDescCount - 1f)) + mModWeight * // modificationCount/parentModificationCount.
        (parExtension * (float) modificationCount / ((float) parentModificationCount - 1f));
    }
    LOGWRAPPER.debug("ITEM: " + pIndex);
    LOGWRAPPER.debug("modificationCount: " + modificationCount);
    LOGWRAPPER.debug("parentModificationCount: " + parentModificationCount);
    LOGWRAPPER.debug("descendantCount: " + descendantCount);
    LOGWRAPPER.debug("parentDescCount: " + parentDescCount);
    LOGWRAPPER.debug("indexToParent: " + indexToParent);
    LOGWRAPPER.debug("extension: " + extension);
    LOGWRAPPER.debug("depth: " + depth);
    LOGWRAPPER.debug("next Depth: " + nextDepth);
    LOGWRAPPER.debug("angle: " + angle);
    if (mPrune == Pruning.ITEMSIZE && extension < TraverseModel.ANGLE_TO_PRUNE && modificationCount <= descendantCount) {
        nodePruned();
    } else {
        // Add a sunburst item.
        if (mPrune == Pruning.DIFF && diffCont.getDiff() == DiffType.SAMEHASH) {
            mIsPruned = true;
        } else {
            mIsPruned = false;
        }
        final NodeReadTrx rtx = (diffCont.getDiff() == DiffType.DELETED || diffCont.getDiff() == DiffType.MOVEDFROM || diffCont.getDiff() == DiffType.REPLACEDOLD) ? mOldRtx : mNewRtx;
        final EStructType structKind = nextDepth > depth ? EStructType.ISINNERNODE : EStructType.ISLEAFNODE;
        // Set node relations.
        String text = "";
        NodeRelations relations = null;
        final DiffType currDiff = diffCont.getDiff();
        if (rtx.getKind() == Kind.TEXT) {
            if (currDiff == DiffType.DELETED || currDiff == DiffType.MOVEDFROM || currDiff == DiffType.REPLACEDOLD) {
                text = mOldRtx.getValue();
            } else {
                text = mNewRtx.getValue();
            }
            if (currDiff == DiffType.UPDATED || ((currDiff == DiffType.REPLACEDNEW || currDiff == DiffType.REPLACEDOLD) && mOldRtx.getKind() == mNewRtx.getKind())) {
                final String oldValue = mOldRtx.getValue();
                final String newValue = mNewRtx.getValue();
                float similarity = 1;
                // try {
                // Integer.parseInt(oldValue);
                // Integer.parseInt(newValue);
                // 
                // // TODO: Implement similarity measure on numerical data (easy!).
                // } catch (final NumberFormatException e) {
                similarity = mLevenshtein.getSimilarity(oldValue, newValue);
                // }
                relations = new NodeRelations(origDepth, depth, structKind, similarity, 0, 1, indexToParent).setSubtract(subtract);
            } else if (currDiff == DiffType.SAME || currDiff == DiffType.SAMEHASH) {
                relations = new NodeRelations(origDepth, depth, structKind, 1, 0, 1, indexToParent).setSubtract(subtract);
            } else {
                relations = new NodeRelations(origDepth, depth, structKind, 0, 0, 1, indexToParent).setSubtract(subtract);
            }
        } else {
            if (mMaxDescendantCount == 0) {
                if (mPrune == Pruning.NO) {
                    try {
                        mMaxDescendantCount = mMaxDescendantCountFuture.get().getDescendants();
                    } catch (final InterruptedException | ExecutionException e) {
                        LOGWRAPPER.error(e.getMessage(), e);
                    }
                } else {
                    mMaxDescendantCount = mAxis.getDescendantCount();
                }
            }
            relations = new NodeRelations(origDepth, depth, structKind, descendantCount, 1, mMaxDescendantCount, indexToParent).setSubtract(subtract);
        }
        // Build item.
        final SunburstItem.Builder builder = new SunburstItem.Builder(mParent, angle, extension, relations, mDb, mGUI).setNodeKey(rtx.getNodeKey()).setKind(rtx.getKind()).setDiff(diffCont.getDiff());
        if (modificationCount > descendantCount) {
            final int diffCounts = (modificationCount - descendantCount) / TraverseModel.FACTOR;
            LOGWRAPPER.debug("modCount: " + diffCounts);
            builder.setModifications(diffCounts);
        }
        if (text.isEmpty()) {
            QNm name = null;
            if (diffCont.getDiff() == DiffType.DELETED || diffCont.getDiff() == DiffType.MOVEDFROM || diffCont.getDiff() == DiffType.REPLACEDOLD) {
                name = mOldRtx.getName();
                builder.setAttributes(fillAttributes(mOldRtx));
                builder.setNamespaces(fillNamespaces(mOldRtx));
                builder.setOldKey(mOldRtx.getNodeKey());
                LOGWRAPPER.debug("name: " + name.getLocalName());
                builder.setOldQName(name);
            } else {
                name = mNewRtx.getName();
                builder.setAttributes(fillAttributes(mNewRtx));
                builder.setNamespaces(fillNamespaces(mNewRtx));
                LOGWRAPPER.debug("name: " + name.getLocalName());
                builder.setQName(name);
            }
        } else {
            LOGWRAPPER.debug("text: " + text);
            if (currDiff == DiffType.DELETED || currDiff == DiffType.MOVEDFROM || currDiff == DiffType.REPLACEDOLD) {
                builder.setOldText(text);
                builder.setOldKey(mOldRtx.getNodeKey());
            } else {
                builder.setText(text);
            }
        }
        updated(diffCont.getDiff(), builder);
        final SunburstItem item = builder.build();
        if (item.getDiff() == DiffType.MOVEDFROM) {
            LOGWRAPPER.debug("movedToIndex: " + diffCont.getIndex());
            item.setIndexMovedTo(diffCont.getIndex() - mAxis.getPrunedNodes());
        }
        mItems.add(item);
        // Set depth max.
        mNewDepthMax = Math.max(depth, mNewDepthMax);
    }
    return extension;
}
Also used : DiffTuple(org.sirix.diff.DiffTuple) NodeRelations(org.sirix.gui.view.sunburst.NodeRelations) DiffType(org.sirix.diff.DiffFactory.DiffType) QNm(org.brackit.xquery.atomic.QNm) NodeReadTrx(org.sirix.api.NodeReadTrx) SunburstItem(org.sirix.gui.view.sunburst.SunburstItem) EStructType(org.sirix.gui.view.sunburst.SunburstItem.EStructType) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

DiffTuple (org.sirix.diff.DiffTuple)9 DiffType (org.sirix.diff.DiffFactory.DiffType)7 DiffDepth (org.sirix.diff.DiffDepth)4 ExecutionException (java.util.concurrent.ExecutionException)3 SirixException (org.sirix.exception.SirixException)2 DatabaseException (com.sleepycat.je.DatabaseException)1 ExecutorService (java.util.concurrent.ExecutorService)1 QNm (org.brackit.xquery.atomic.QNm)1 NodeReadTrx (org.sirix.api.NodeReadTrx)1 DiffOptimized (org.sirix.diff.DiffFactory.DiffOptimized)1 NodeRelations (org.sirix.gui.view.sunburst.NodeRelations)1 SunburstItem (org.sirix.gui.view.sunburst.SunburstItem)1 EStructType (org.sirix.gui.view.sunburst.SunburstItem.EStructType)1 DiffSunburstAxis (org.sirix.gui.view.sunburst.axis.DiffSunburstAxis)1