Java Code Examples for com.google.common.primitives.Longs.BYTES

The following are Jave code examples for showing how to use BYTES of the com.google.common.primitives.Longs class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: mycat-src-1.6.1-RELEASE   File: FastByteOperations.java   View Source Code Vote up 5 votes
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand: a byte[] or null
 * @param buffer2 right operand: a byte[] or null
 * @param memoryOffset1 Where to start comparing in the left buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param memoryOffset2 Where to start comparing in the right buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param length1 How much to compare from the left buffer
 * @param length2 How much to compare from the right buffer
 * @return 0 if equal, < 0 if left is less than right, etc.
 */

public static int compareTo(Object buffer1, long memoryOffset1, int length1,
                     Object buffer2, long memoryOffset2, int length2)
{
    int minLength = Math.min(length1, length2);

    /*
     * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
     * time is no slower than comparing 4 bytes at a time even on 32-bit.
     * On the other hand, it is substantially faster on 64-bit.
     */
    int wordComparisons = minLength & ~7;
    for (int i = 0; i < wordComparisons ; i += Longs.BYTES)
    {
        long lw = theUnsafe.getLong(buffer1, memoryOffset1 + (long) i);
        long rw = theUnsafe.getLong(buffer2, memoryOffset2 + (long) i);

        if (lw != rw)
        {
            if (BIG_ENDIAN) {
                return UnsignedLongs.compare(lw, rw);
            }

            return UnsignedLongs.compare(Long.reverseBytes(lw), Long.reverseBytes(rw));
        }
    }

    for (int i = wordComparisons ; i < minLength ; i++)
    {
        int b1 = theUnsafe.getByte(buffer1, memoryOffset1 + i) & 0xFF;
        int b2 = theUnsafe.getByte(buffer2, memoryOffset2 + i) & 0xFF;
        if (b1 != b2) {
            return b1 - b2;
        }
    }

    return length1 - length2;
}
 
Example 2
Project: hadoop   File: BestEffortLongFile.java   View Source Code Vote up 5 votes
private void lazyOpen() throws IOException {
  if (ch != null) {
    return;
  }

  // Load current value.
  byte[] data = null;
  try {
    data = Files.toByteArray(file);
  } catch (FileNotFoundException fnfe) {
    // Expected - this will use default value.
  }

  if (data != null && data.length != 0) {
    if (data.length != Longs.BYTES) {
      throw new IOException("File " + file + " had invalid length: " +
          data.length);
    }
    value = Longs.fromByteArray(data);
  } else {
    value = defaultVal;
  }
  
  // Now open file for future writes.
  RandomAccessFile raf = new RandomAccessFile(file, "rw");
  try {
    ch = raf.getChannel();
  } finally {
    if (ch == null) {
      IOUtils.closeStream(raf);
    }
  }
}
 
Example 3
Project: dble   File: FastByteOperations.java   View Source Code Vote up 5 votes
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1       left operand: a byte[] or null
 * @param buffer2       right operand: a byte[] or null
 * @param memoryOffset1 Where to start comparing in the left buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param memoryOffset2 Where to start comparing in the right buffer (pure memory address if buffer1 is null, or relative otherwise)
 * @param length1       How much to compare from the left buffer
 * @param length2       How much to compare from the right buffer
 * @return 0 if equal, < 0 if left is less than right, etc.
 */

public static int compareTo(Object buffer1, long memoryOffset1, int length1,
                            Object buffer2, long memoryOffset2, int length2) {
    int minLength = Math.min(length1, length2);

    /*
     * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
     * time is no slower than comparing 4 bytes at a time even on 32-bit.
     * On the other hand, it is substantially faster on 64-bit.
     */
    int wordComparisons = minLength & ~7;
    for (int i = 0; i < wordComparisons; i += Longs.BYTES) {
        long lw = THE_UNSAFE.getLong(buffer1, memoryOffset1 + (long) i);
        long rw = THE_UNSAFE.getLong(buffer2, memoryOffset2 + (long) i);

        if (lw != rw) {
            if (BIG_ENDIAN) {
                return UnsignedLongs.compare(lw, rw);
            }

            return UnsignedLongs.compare(Long.reverseBytes(lw), Long.reverseBytes(rw));
        }
    }

    for (int i = wordComparisons; i < minLength; i++) {
        int b1 = THE_UNSAFE.getByte(buffer1, memoryOffset1 + i) & 0xFF;
        int b2 = THE_UNSAFE.getByte(buffer2, memoryOffset2 + i) & 0xFF;
        if (b1 != b2) {
            return b1 - b2;
        }
    }

    return length1 - length2;
}
 
Example 4
Project: hadoop-oss   File: FastByteComparisons.java   View Source Code Vote up 4 votes
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @param buffer2 right operand
 * @param offset1 Where to start comparing in the left buffer
 * @param offset2 Where to start comparing in the right buffer
 * @param length1 How much to compare from the left buffer
 * @param length2 How much to compare from the right buffer
 * @return 0 if equal, < 0 if left is less than right, etc.
 */
@Override
public int compareTo(byte[] buffer1, int offset1, int length1,
    byte[] buffer2, int offset2, int length2) {
  // Short circuit equal case
  if (buffer1 == buffer2 &&
      offset1 == offset2 &&
      length1 == length2) {
    return 0;
  }
  int minLength = Math.min(length1, length2);
  int minWords = minLength / Longs.BYTES;
  int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET;
  int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET;

  /*
   * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
   * time is no slower than comparing 4 bytes at a time even on 32-bit.
   * On the other hand, it is substantially faster on 64-bit.
   */
  for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) {
    long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i);
    long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i);
    long diff = lw ^ rw;

    if (diff != 0) {
      if (!littleEndian) {
        return lessThanUnsigned(lw, rw) ? -1 : 1;
      }

      // Use binary search
      int n = 0;
      int y;
      int x = (int) diff;
      if (x == 0) {
        x = (int) (diff >>> 32);
        n = 32;
      }

      y = x << 16;
      if (y == 0) {
        n += 16;
      } else {
        x = y;
      }

      y = x << 8;
      if (y == 0) {
        n += 8;
      }
      return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL));
    }
  }

  // The epilogue to cover the last (minLength % 8) elements.
  for (int i = minWords * Longs.BYTES; i < minLength; i++) {
    int result = UnsignedBytes.compare(
        buffer1[offset1 + i],
        buffer2[offset2 + i]);
    if (result != 0) {
      return result;
    }
  }
  return length1 - length2;
}
 
Example 5
Project: tikv-client-lib-java   File: FastByteComparisons.java   View Source Code Vote up 4 votes
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @param buffer2 right operand
 * @param offset1 Where to start comparing in the left buffer
 * @param offset2 Where to start comparing in the right buffer
 * @param length1 How much to compare from the left buffer
 * @param length2 How much to compare from the right buffer
 * @return 0 if equal, < 0 if left is less than right, etc.
 */
@Override
public int compareTo(byte[] buffer1, int offset1, int length1,
    byte[] buffer2, int offset2, int length2) {
  // Short circuit equal case
  if (buffer1 == buffer2 &&
      offset1 == offset2 &&
      length1 == length2) {
    return 0;
  }
  int minLength = Math.min(length1, length2);
  int minWords = minLength / Longs.BYTES;
  int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET;
  int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET;

  /*
   * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
   * time is no slower than comparing 4 bytes at a time even on 32-bit.
   * On the other hand, it is substantially faster on 64-bit.
   */
  for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) {
    long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i);
    long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i);
    long diff = lw ^ rw;

    if (diff != 0) {
      if (!littleEndian) {
        return lessThanUnsigned(lw, rw) ? -1 : 1;
      }

      // Use binary search
      int n = 0;
      int y;
      int x = (int) diff;
      if (x == 0) {
        x = (int) (diff >>> 32);
        n = 32;
      }

      y = x << 16;
      if (y == 0) {
        n += 16;
      } else {
        x = y;
      }

      y = x << 8;
      if (y == 0) {
        n += 8;
      }
      return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL));
    }
  }

  // The epilogue to cover the last (minLength % 8) elements.
  for (int i = minWords * Longs.BYTES; i < minLength; i++) {
    int result = UnsignedBytes.compare(
        buffer1[offset1 + i],
        buffer2[offset2 + i]);
    if (result != 0) {
      return result;
    }
  }
  return length1 - length2;
}
 
Example 6
Project: hadoop   File: FastByteComparisons.java   View Source Code Vote up 4 votes
/**
 * Lexicographically compare two arrays.
 *
 * @param buffer1 left operand
 * @param buffer2 right operand
 * @param offset1 Where to start comparing in the left buffer
 * @param offset2 Where to start comparing in the right buffer
 * @param length1 How much to compare from the left buffer
 * @param length2 How much to compare from the right buffer
 * @return 0 if equal, < 0 if left is less than right, etc.
 */
@Override
public int compareTo(byte[] buffer1, int offset1, int length1,
    byte[] buffer2, int offset2, int length2) {
  // Short circuit equal case
  if (buffer1 == buffer2 &&
      offset1 == offset2 &&
      length1 == length2) {
    return 0;
  }
  int minLength = Math.min(length1, length2);
  int minWords = minLength / Longs.BYTES;
  int offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET;
  int offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET;

  /*
   * Compare 8 bytes at a time. Benchmarking shows comparing 8 bytes at a
   * time is no slower than comparing 4 bytes at a time even on 32-bit.
   * On the other hand, it is substantially faster on 64-bit.
   */
  for (int i = 0; i < minWords * Longs.BYTES; i += Longs.BYTES) {
    long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i);
    long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i);
    long diff = lw ^ rw;

    if (diff != 0) {
      if (!littleEndian) {
        return lessThanUnsigned(lw, rw) ? -1 : 1;
      }

      // Use binary search
      int n = 0;
      int y;
      int x = (int) diff;
      if (x == 0) {
        x = (int) (diff >>> 32);
        n = 32;
      }

      y = x << 16;
      if (y == 0) {
        n += 16;
      } else {
        x = y;
      }

      y = x << 8;
      if (y == 0) {
        n += 8;
      }
      return (int) (((lw >>> n) & 0xFFL) - ((rw >>> n) & 0xFFL));
    }
  }

  // The epilogue to cover the last (minLength % 8) elements.
  for (int i = minWords * Longs.BYTES; i < minLength; i++) {
    int result = UnsignedBytes.compare(
        buffer1[offset1 + i],
        buffer2[offset2 + i]);
    if (result != 0) {
      return result;
    }
  }
  return length1 - length2;
}