public boolean resolveConflict(IIndex writeSet, ITuple txTuple, ITuple currentTuple)
        throws Exception {

      // The key must be the same for both tuples.
      assertEquals(txTuple.getKey(), currentTuple.getKey());

      // the key for the conflicting writes.
      final byte[] key = txTuple.getKey();

      assertEquals("key", expectedKey, key);

      writeSet.insert(key, resolvedValue);

      return true;
    }
Esempio n. 2
0
  /**
   * Set the direction of iterator progress. Clears {@link #sourceTuple} iff the current direction
   * is different from the new direction and is otherwise a NOP.
   *
   * <p>Note: Care is required for sequences such as
   *
   * <pre>
   * ITuple t1 = next();
   *
   * ITuple t2 = prior();
   * </pre>
   *
   * <p>to visit the same tuple for {@link #next()} and {@link #prior()}.
   *
   * @param forward <code>true</code> iff the new direction of iterator progress is forward using
   *     {@link #hasNext()} and {@link #next()}.
   */
  private void setForwardDirection(boolean forward) {

    if (this.forward != forward) {

      if (INFO) log.info("Changing direction: forward=" + forward);

      /*
       * This is the last key visited -or- null iff nothing has been
       * visited.
       */
      final byte[] lastKeyVisited;
      if (lastVisited == -1) {

        lastKeyVisited = null;

      } else {

        //                lastKeyVisited = ((ITupleCursor2<E>) sourceIterator[lastVisited])
        //                        .tuple().getKey();
        lastKeyVisited = lastKeyBuffer.getKey();

        if (INFO) log.info("key for last tuple visited=" + BytesUtil.toString(lastKeyVisited));
      }

      for (int i = 0; i < n; i++) {

        /*
         * Recover the _current_ tuple for each source iterator.
         */

        // current tuple for the source iterator.
        ITuple<E> tuple = ((ITupleCursor2<E>) sourceIterator[i]).tuple();

        if (INFO) log.info("sourceIterator[" + i + "]=" + tuple);

        if (lastKeyVisited != null) {

          /*
           * When we are changing to [forward == true] (visiting the
           * next tuples in the index order), then we advance the
           * source iterator zero or more tuples until it is
           * positioned GT the lastVisitedKey.
           *
           * When we are changing to [forward == false] (visiting the
           * prior tuples in the index order), then we backup the
           * source iterator zero or more tuples until it is
           * positioned LT the lastVisitedKey.
           */

          while (tuple != null) {

            final int ret =
                BytesUtil.compareBytes( //
                    tuple.getKey(), //
                    lastKeyVisited //
                    );

            final boolean ok = forward ? ret > 0 : ret < 0;

            if (ok) break;

            /*
             * If the source iterator is currently positioned on the
             * same key as the last tuple that we visited then we
             * need to move it off of that key - either to the
             * previous or the next visitable tuple depending on the
             * new direction for the iterator.
             */

            if (forward) {

              if (sourceIterator[i].hasNext()) {

                // next tuple
                tuple = sourceIterator[i].next();

              } else {

                // exhausted in this direction.
                tuple = null;
              }

            } else {

              if (sourceIterator[i].hasPrior()) {

                // prior tuple
                tuple = sourceIterator[i].prior();

              } else {

                // exhausted in this direction.
                tuple = null;
              }
            }

            if (INFO)
              log.info(
                  "skipping tuple: source="
                      + i
                      + ", direction="
                      + (forward ? "next" : "prior")
                      + ", newTuple="
                      + tuple);
          }
        }

        sourceTuple[i] = tuple;

        // as assigned to source[i].
        if (INFO) log.info("sourceTuple   [" + i + "]=" + sourceTuple[i]);
      }

      // set the new iterator direction.
      this.forward = forward;

      // clear current since the old lookahead choice is no longer valid.
      this.current = -1;
    }
  }