Java Code Examples for org.apache.pig.data.Tuple#compareTo()

The following examples show how to use org.apache.pig.data.Tuple#compareTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestHelper.java    From spork with Apache License 2.0 6 votes vote down vote up
public static boolean compareBags(DataBag db1, DataBag db2) {
    if (db1.size() != db2.size())
        return false;
    
    boolean equal = true;
    for (Tuple tuple : db2) {
        boolean contains = false;
        for (Tuple tuple2 : db1) {
            if (tuple.compareTo(tuple2) == 0) {
                contains = true;
                break;
            }
        }
        if (!contains) {
            equal = false;
            break;
        }
    }
    return equal;
}
 
Example 2
Source File: IndexedStorage.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * Scans the index looking for a given key.
 * @return the matching index tuple OR the last index tuple
 * greater than the requested key if no match is found.
 */
public Tuple ScanIndex(Tuple keys) throws IOException {
    if (lastIndexKeyTuple != null && keys.compareTo(this.lastIndexKeyTuple) <= 0) {
        return indexTuple;
    }

    /* Scan the index looking for given key */
    while ((indexTuple = this.ReadIndex()) != null) {
        if (keys.compareTo(this.lastIndexKeyTuple) > 0)
            continue;
        else
            break;
    }

    return indexTuple;
}
 
Example 3
Source File: FindQuantiles.java    From spork with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public int compare(Tuple t1, Tuple t2) {
    switch (mState) {
    case ALL_ASC:
        return t1.compareTo(t2);

    case ALL_DESC:
        return t2.compareTo(t1);

    case MIXED:
        // Have to break the tuple down and compare it field to field.
        int sz1 = t1.size();
        int sz2 = t2.size();
        if (sz2 < sz1) {
            return 1;
        } else if (sz2 > sz1) {
            return -1;
        } else {
            for (int i = 0; i < sz1; i++) {
                try {
                    int c = DataType.compare(t1.get(i), t2.get(i));
                    if (c != 0) {
                        if (!mAsc[i]) c *= -1;
                        return c;
                    }
                } catch (ExecException e) {
                    throw new RuntimeException("Unable to compare tuples", e);
                }
            }
            return 0;
        }
    }
    return -1; // keep the compiler happy
}
 
Example 4
Source File: TestHelper.java    From spork with Apache License 2.0 5 votes vote down vote up
public static boolean bagContains(DataBag db, Tuple t) {
    Iterator<Tuple> iter = db.iterator();
    for (Tuple tuple : db) {
        if (tuple.compareTo(t) == 0 || tupleEquals(tuple, t))
            return true;
    }
    return false;
}
 
Example 5
Source File: IndexedStorage.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * Build index tuple
 *
 * @throws IOException
 */
private void BuildIndex(Tuple t, long offset) throws IOException {
    /* Build index key tuple */
    Tuple indexKeyTuple = tupleFactory.newTuple(this.offsetsToIndexKeys.length);
    for (int i = 0; i < this.offsetsToIndexKeys.length; ++i) {
        indexKeyTuple.set(i, t.get(this.offsetsToIndexKeys[i]));
    }

    /* Check if we have already seen Tuple(s) with same index keys */
    if (indexKeyTuple.compareTo(this.lastIndexKeyTuple) == 0) {
        /* We have seen Tuple(s) with given index keys, update the tuple count */
        this.numberOfTuples += 1;
    }
    else {
        if (this.lastIndexKeyTuple != null)
            this.WriteIndex();

        this.lastIndexKeyTuple = indexKeyTuple;
        this.minIndexKeyTuple = ((this.minIndexKeyTuple == null) || (indexKeyTuple.compareTo(this.minIndexKeyTuple) < 0)) ? indexKeyTuple : this.minIndexKeyTuple;
        this.maxIndexKeyTuple = ((this.maxIndexKeyTuple == null) || (indexKeyTuple.compareTo(this.maxIndexKeyTuple) > 0)) ? indexKeyTuple : this.maxIndexKeyTuple;

        /* New index tuple for newly seen index key */
        this.indexTuple = tupleFactory.newTuple(3);

        /* Add index keys to index Tuple */
        this.indexTuple.set(0, indexKeyTuple);

        /* Reset Tuple count for index key */
        this.numberOfTuples = 1;

        /* Remember offset to Tuple with new index keys */
        this.indexTuple.set(2, offset);
    }
}
 
Example 6
Source File: IndexedStorage.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * Scan the index for given key and seek to appropriate offset in the data
 * @param keys to look for
 * @return true if the given key was found, false otherwise
 * @throws IOException
 */
public boolean seekNear(Tuple keys) throws IOException {
    boolean ret = false;
    Tuple indexTuple = this.indexManager.ScanIndex(keys);
    if (indexTuple != null) {
        long offset = this.indexManager.getOffset(indexTuple) ;
        in.seek(offset);

        if (keys.compareTo(this.indexManager.getIndexKeyTuple(indexTuple)) == 0) {
            ret = true;
        }
    }

    return ret;
}
 
Example 7
Source File: CondEntropy.java    From datafu with Apache License 2.0 5 votes vote down vote up
@Override
public void accumulate(Tuple input) throws IOException
{
  for (Tuple t : (DataBag) input.get(0)) {

    if (this.xy != null)
    {
        int cmp = t.compareTo(this.xy);
        
        //check if the comparison result is different from previous compare result
        if ((cmp < 0 && this.lastCmp > 0)
            || (cmp > 0 && this.lastCmp < 0)) {
            throw new ExecException("Out of order! previous tuple: " + this.xy + ", present tuple: " + t
                                    + ", comparsion: " + cmp + ", previous comparsion: " + this.lastCmp);
        }
        if (cmp != 0) {
           //different <x,y>
           this.combEstimator.accumulate(this.cxy);
           this.cxy = 0;
           this.lastCmp = cmp;
           if(DataType.compare(this.xy.get(0), t.get(0)) != 0) {
              //different x
               this.condXEstimator.accumulate(this.cx);
               this.cx = 0;
           }
        } 
    }

    //set tuple t as the next tuple for comparison
    this.xy = t;

    //accumulate cx
    this.cx++;
    
    //accumulate cxy
    this.cxy++;
  }
}
 
Example 8
Source File: Entropy.java    From datafu with Apache License 2.0 5 votes vote down vote up
@Override
public void accumulate(Tuple input) throws IOException
{
  for (Tuple t : (DataBag) input.get(0)) {

    if (this.x != null)
    {
        int cmp = t.compareTo(this.x);
        
        //check if the comparison result is different from previous compare result
        if ((cmp < 0 && this.lastCmp > 0)
            || (cmp > 0 && this.lastCmp < 0)) {
            throw new ExecException("Out of order! previous tuple: " + this.x + ", present tuple: " + t
                                    + ", comparsion: " + cmp + ", previous comparsion: " + this.lastCmp);
        }

        if (cmp != 0) {
           //different tuple
           this.estimator.accumulate(this.cx);
           this.cx = 0;
           this.lastCmp = cmp;
        } 
    }

    //set tuple t as the next tuple for comparison
    this.x = t;

    //accumulate cx
    this.cx++;
  }
}
 
Example 9
Source File: OrdDesc.java    From spork with Apache License 2.0 4 votes vote down vote up
public int compare(Tuple t1, Tuple t2) {
    return t2.compareTo(t1);
}
 
Example 10
Source File: TestEvalPipelineLocal.java    From spork with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(Tuple t1, Tuple t2) {
    return t1.compareTo(t2);
}
 
Example 11
Source File: OrdAsc.java    From spork with Apache License 2.0 4 votes vote down vote up
public int compare(Tuple t1, Tuple t2) {
    return t1.compareTo(t2);
}
 
Example 12
Source File: OrdDesc.java    From spork with Apache License 2.0 4 votes vote down vote up
public int compare(Tuple t1, Tuple t2) {
    return t2.compareTo(t1);
}
 
Example 13
Source File: IndexedStorage.java    From spork with Apache License 2.0 4 votes vote down vote up
@Override
/* The list of readers is always sorted before and after this call. */
public void seekNear(Tuple keys) throws IOException {

    /* Keeps track of the last (if any) reader where seekNear was called */
    int lastIndexModified = -1;

    int idx = currentReaderIndexStart;
    while (idx < this.readers.length) {
        IndexedStorageRecordReader r = this.readers[idx];

        /* The key falls within the range of the reader index */
        if (keys.compareTo(r.indexManager.maxIndexKeyTuple) <= 0 && keys.compareTo(r.indexManager.minIndexKeyTuple) >= 0) {
            r.seekNear(keys);
            lastIndexModified = idx;

        /* The key is greater than the current range of the reader index */
        } else if (keys.compareTo(r.indexManager.maxIndexKeyTuple) > 0) {
            currentReaderIndexStart++;
        /* DO NOTHING - The key is less than the current range of the reader index */
        } else {
            break;
        }
        idx++;
    }

    /*
     * There is something to sort.
     * We can rely on the following invariants that make the following check accurate:
      *  - currentReaderIndexStart is always >= 0.
     *  - lastIndexModified is only positive if seekNear was called.
     *  - lastIndexModified >= currentReaderIndexStart if lastIndexModifed >= 0.  This is true because the list
      * is already sorted.
     */
    if (lastIndexModified - currentReaderIndexStart >= 0) {

        /*
         * The following logic is optimized for the (common) case where there are a tiny number of readers that
             * need to be repositioned relative to the other readers in the much larger sorted list.
         */

        /* First, just sort the readers that were updated relative to one another. */
        Arrays.sort(this.readers, currentReaderIndexStart, lastIndexModified+1, this.readerComparator);

        /* In descending order, push the updated readers back in the the sorted list. */
        for (idx = lastIndexModified; idx >= currentReaderIndexStart; idx--) {
            sortReader(idx);
        }
    }
}
 
Example 14
Source File: IndexedStorage.java    From spork with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(IndexedStorageRecordReader o1, IndexedStorageRecordReader o2) {
    Tuple t1 = (o1.indexManager.lastIndexKeyTuple == null) ?  o1.indexManager.minIndexKeyTuple : o1.indexManager.lastIndexKeyTuple;
    Tuple t2 = (o2.indexManager.lastIndexKeyTuple == null) ?  o2.indexManager.minIndexKeyTuple : o2.indexManager.lastIndexKeyTuple;
    return t1.compareTo(t2);
}