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

The following are top voted examples for showing how to use com.google.common.primitives.Longs. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: RedisDirectory   File: JedisPoolStream.java   View source code 7 votes vote down vote up
@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField, List<byte[]> values, long
        fileLength) {
    long blockSize = 0;
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        Pipeline pipelined = jedis.pipelined();
        //add new file length
        pipelined.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength));
        //add new file content
        blockSize = getBlockSize(fileLength);
        for (int i = 0; i < blockSize; i++) {
            pipelined.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i)));
        }
        values.clear();
        pipelined.sync();
    } finally {
        jedis.close();
        deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
    }
}
 
Example 2
Project: TakinRPC   File: Leader.java   View source code 6 votes vote down vote up
/**
 * Find the median value of the list of matchIndex, this value is the committedIndex since, by definition, half of the
 * matchIndex values are greater and half are less than this value. So, at least half of the replicas have stored the
 * median value, this is the definition of committed.
 */
private void updateCommitted() {

    List<ReplicaManager> sorted = newArrayList(managers.values());
    Collections.sort(sorted, new Comparator<ReplicaManager>() {
        @Override
        public int compare(ReplicaManager o, ReplicaManager o2) {
            return Longs.compare(o.getMatchIndex(), o2.getMatchIndex());
        }
    });

    final int middle = (int) Math.ceil(sorted.size() / 2.0);
    final long committed = sorted.get(middle).getMatchIndex();
    log.updateCommitIndex(committed);

    SortedMap<Long, SettableFuture<Boolean>> entries = requests.headMap(committed + 1);
    for (SettableFuture<Boolean> f : entries.values()) {
        f.set(true);
    }

    entries.clear();

}
 
Example 3
Project: RedisDirectory   File: RedisDirectory.java   View source code 6 votes vote down vote up
@Override
public void renameFile(String source, String dest) throws IOException {
    List<byte[]> values = new ArrayList<>();
    //在get的时候不需要加事务
    //在删除和添加的时候使用事务
    //Get the file length with old file name
    byte[] hget = inputOutputStream.hget(Constants.DIR_METADATA_BYTES, source.getBytes(), FILE_LENGTH);
    long length = Longs.fromByteArray(hget);
    long blockSize = getBlockSize(length);
    for (int i = 0; i < blockSize; i++) {
        //Get the contents with old file name
        byte[] res = inputOutputStream.hget(Constants.FILE_METADATA_BYTES, getBlockName(source, i), FILE_DATA);
        values.add(res);
    }
    inputOutputStream.rename(Constants.DIRECTORY_METADATA, Constants.FILE_METADATA, source, dest, values, length);
    log.debug("Rename file success from {} to {}", source, dest);
}
 
Example 4
Project: RedisDirectory   File: JedisPoolStream.java   View source code 6 votes vote down vote up
@Override
public void saveFile(String fileLengthKey, String fileDataKey, String fileName, List<byte[]> values, long fileLength) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        Pipeline pipelined = jedis.pipelined();
        pipelined.hset(fileLengthKey.getBytes(), fileName.getBytes(), Longs.toByteArray(fileLength));
        Long blockSize = getBlockSize(fileLength);
        for (int i = 0; i < blockSize; i++) {
            pipelined.hset(fileDataKey.getBytes(), getBlockName(fileName, i), compressFilter(values.get(i)));
            if (i % Constants.SYNC_COUNT == 0) {
                pipelined.sync();
                pipelined = jedis.pipelined();
            }
        }
        values.clear();
        pipelined.sync();
    } finally {
        jedis.close();
    }
}
 
Example 5
Project: RedisDirectory   File: ShardedJedisPoolStream.java   View source code 6 votes vote down vote up
/**
 * Use transactions to add index file and then delete the old one
 *
 * @param fileLengthKey the key using for hash file length
 * @param fileDataKey   the key using for hash file data
 * @param oldField      the old hash field
 * @param newField      the new hash field
 * @param values        the data values of the old hash field
 * @param fileLength    the data length of the old hash field
 */
@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField, List<byte[]> values, long
        fileLength) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    //add new file length
    pipelined.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength));
    //add new file content
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i)));
    }
    pipelined.sync();
    shardedJedis.close();
    values.clear();
    deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
}
 
Example 6
Project: RedisDirectory   File: ShardedJedisPoolStream.java   View source code 6 votes vote down vote up
@Override
public void saveFile(String fileLengthKey, String fileDataKey, String fileName, List<byte[]> values, long fileLength) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    pipelined.hset(fileLengthKey.getBytes(), fileName.getBytes(), Longs.toByteArray(fileLength));
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(fileName, i), compressFilter(values.get(i)));
        if (i % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            pipelined = shardedJedis.pipelined();
        }
    }
    pipelined.sync();
    shardedJedis.close();
    values.clear();
}
 
Example 7
Project: RedisDirectory   File: JedisStream.java   View source code 6 votes vote down vote up
@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField, List<byte[]> values, long
        fileLength) {
    Jedis jedis = openJedis();
    Pipeline pipelined = jedis.pipelined();
    //add new file length
    pipelined.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength));
    //add new file content
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i)));
    }
    pipelined.sync();
    jedis.close();
    values.clear();
    deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
}
 
Example 8
Project: RedisDirectory   File: JedisStream.java   View source code 6 votes vote down vote up
@Override
public void saveFile(String fileLengthKey, String fileDataKey, String fileName, List<byte[]> values, long fileLength) {
    Jedis jedis = openJedis();
    Pipeline pipelined = jedis.pipelined();
    pipelined.hset(fileLengthKey.getBytes(), fileName.getBytes(), Longs.toByteArray(fileLength));
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(fileName, i), compressFilter(values.get(i)));
        if (i % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            pipelined = jedis.pipelined();
        }
    }
    pipelined.sync();
    jedis.close();
    values.clear();
}
 
Example 9
Project: dremio-oss   File: ThreadsIterator.java   View source code 6 votes vote down vote up
public ThreadsIterator(final SabotContext dbContext, final OperatorContext context) {
  this.dbContext = dbContext;
  threadMXBean = ManagementFactory.getThreadMXBean();
  final long[] ids = threadMXBean.getAllThreadIds();

  final Iterator<Long> threadIdIterator = Longs.asList(ids).iterator();

  this.threadInfoIterator = Iterators.filter(
    Iterators.transform(threadIdIterator, new Function<Long, ThreadInfo>() {

      @Override
      public ThreadInfo apply(Long input) {
        return threadMXBean.getThreadInfo(input, 100);
      }
    }),
    Predicates.notNull());

  logger.debug("number of threads = {}, number of cores = {}", ids.length, NUMBER_OF_CORES);

  this.stats = dbContext.getWorkStatsProvider().get();
}
 
Example 10
Project: ditb   File: CellComparator.java   View source code 6 votes vote down vote up
/**
 * Compare cells.
 * TODO: Replace with dynamic rather than static comparator so can change comparator
 * implementation.
 * @param a
 * @param b
 * @param ignoreSequenceid True if we are to compare the key portion only and ignore
 * the sequenceid. Set to false to compare key and consider sequenceid.
 * @return 0 if equal, -1 if a &lt; b, and +1 if a &gt; b.
 */
public static int compare(final Cell a, final Cell b, boolean ignoreSequenceid) {
  // row
  int c = compareRows(a, b);
  if (c != 0) return c;

  c = compareWithoutRow(a, b);
  if(c != 0) return c;

  if (!ignoreSequenceid) {
    // Negate following comparisons so later edits show up first
    // mvccVersion: later sorts first
    return Longs.compare(b.getMvccVersion(), a.getMvccVersion());
  } else {
    return c;
  }
}
 
Example 11
Project: flume-release-1.7.0   File: TestHBaseSink.java   View source code 6 votes vote down vote up
@Test
public void testOneEvent() throws Exception {
  initContextForSimpleHbaseEventSerializer();
  HBaseSink sink = new HBaseSink(conf);
  Configurables.configure(sink, ctx);
  Channel channel = new MemoryChannel();
  Configurables.configure(channel, new Context());
  sink.setChannel(channel);
  sink.start();
  Transaction tx = channel.getTransaction();
  tx.begin();
  Event e = EventBuilder.withBody(
      Bytes.toBytes(valBase));
  channel.put(e);
  tx.commit();
  tx.close();

  sink.process();
  sink.stop();
  HTable table = new HTable(conf, tableName);
  byte[][] results = getResults(table, 1);
  byte[] out = results[0];
  Assert.assertArrayEquals(e.getBody(), out);
  out = results[1];
  Assert.assertArrayEquals(Longs.toByteArray(1), out);
}
 
Example 12
Project: GabrielBot   File: GabrielGatewayClient.java   View source code 6 votes vote down vote up
public GabrielGatewayClient(int shardId, Channel channel) throws IOException {
    super(shardId, channel, true);
    channel.queueDeclare("shard-" + shardId + "-getping", false, false, false, null);
    channel.queueDeclare("shard-" + shardId + "-getping-response", false, false, false, null);
    channel.basicConsume("shard-" + shardId + "-getping-response", true, new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            long now = System.currentTimeMillis();
            ping = now - Longs.fromByteArray(body);
        }
    });
    calculatePing();
    PING_CALCULATOR.scheduleAtFixedRate(this::calculatePing, 30, 30, TimeUnit.SECONDS);
}
 
Example 13
Project: guava-beta-checker   File: TestCompiler.java   View source code 6 votes vote down vote up
/**
 * Asserts that the given diagnostics contain errors with a message containing "[CheckerName]"
 * on the given lines of the given file. If there should be multiple errors on a line, the line
 * number must appear multiple times. There may not be any errors in any other file.
 */
public void assertErrorsOnLines(String file,
    List<Diagnostic<? extends JavaFileObject>> diagnostics, long... lines) {
  ListMultimap<String, Long> actualErrors = ArrayListMultimap.create();
  for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
    String message = diagnostic.getMessage(Locale.US);

    // The source may be null, e.g. for diagnostics about command-line flags
    assertNotNull(message, diagnostic.getSource());
    String sourceName = diagnostic.getSource().getName();

    assertEquals(
        "unexpected error in source file " + sourceName + ": " + message,
        file, sourceName);

    actualErrors.put(diagnostic.getSource().getName(), diagnostic.getLineNumber());

    // any errors from the compiler that are not related to this checker should fail
    assertThat(message).contains("[" + checker.getAnnotation(BugPattern.class).name() + "]");
  }

  assertEquals(
      ImmutableMultiset.copyOf(Longs.asList(lines)),
      ImmutableMultiset.copyOf(actualErrors.get(file)));
}
 
Example 14
Project: ArchUnit   File: JavaClassProcessor.java   View source code 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"}) // NOTE: We assume the component type matches the list
private Object toArray(Class<?> componentType, List<Object> values) {
    if (componentType == boolean.class) {
        return Booleans.toArray((Collection) values);
    } else if (componentType == byte.class) {
        return Bytes.toArray((Collection) values);
    } else if (componentType == short.class) {
        return Shorts.toArray((Collection) values);
    } else if (componentType == int.class) {
        return Ints.toArray((Collection) values);
    } else if (componentType == long.class) {
        return Longs.toArray((Collection) values);
    } else if (componentType == float.class) {
        return Floats.toArray((Collection) values);
    } else if (componentType == double.class) {
        return Doubles.toArray((Collection) values);
    } else if (componentType == char.class) {
        return Chars.toArray((Collection) values);
    }
    return values.toArray((Object[]) Array.newInstance(componentType, values.size()));
}
 
Example 15
Project: jpmml-tensorflow   File: TensorUtil.java   View source code 6 votes vote down vote up
static
public List<?> getValues(Tensor tensor){
	DataType dataType = tensor.dataType();

	switch(dataType){
		case FLOAT:
			return Floats.asList(TensorUtil.toFloatArray(tensor));
		case DOUBLE:
			return Doubles.asList(TensorUtil.toDoubleArray(tensor));
		case INT32:
			return Ints.asList(TensorUtil.toIntArray(tensor));
		case INT64:
			return Longs.asList(TensorUtil.toLongArray(tensor));
		case STRING:
			return Arrays.asList(TensorUtil.toStringArray(tensor));
		case BOOL:
			return Booleans.asList(TensorUtil.toBooleanArray(tensor));
		default:
			throw new IllegalArgumentException();
	}
}
 
Example 16
Project: appinventor-extensions   File: Component.java   View source code 6 votes vote down vote up
public static Component valueOf(String text) {
  String[] parts = text.split(DELIM);
  if (parts.length != 4) {
    throw new IllegalArgumentException("text should have 4 parts.");
  }

  Long id = Longs.tryParse(parts[0]);
  String authorId = parts[1];
  String fullyQualifiedName = parts[2];
  Long version = Longs.tryParse(parts[3]);
  if (id == null) {
    throw new IllegalArgumentException("id is not parsable.");
  }
  if (version == null) {
    throw new IllegalArgumentException("version is not parsable.");
  }

  return new Component(id, authorId, fullyQualifiedName, version);
}
 
Example 17
Project: tools   File: BitPatternGenerator.java   View source code 5 votes vote down vote up
private byte[] seedForThisBlock(long blockNumber) {
    final byte[] bnarr = Longs.toByteArray(blockNumber);
    final int longArrLen = 8;
    final byte[] seedSecRandom = new byte[EncConstants.SHA256_LEN + longArrLen];

    System.arraycopy(bnarr, 0, seedSecRandom, 0, longArrLen);
    System.arraycopy(secretKey, 0, seedSecRandom, longArrLen, EncConstants.SHA256_LEN);
    return bnarr;
}
 
Example 18
Project: RedisDirectory   File: RedisDirectory.java   View source code 5 votes vote down vote up
/**
 * @param name file name
 * @return Returns the length of a file in the directory.
 * @throws IOException an I/O error
 */
@Override
public final long fileLength(String name) throws IOException {
    ensureOpen();
    long current = 0;
    byte[] b = inputOutputStream.hget(Constants.DIR_METADATA_BYTES, name.getBytes(), FILE_LENGTH);
    if (b != null) {
        current = Longs.fromByteArray(b);
    }
    return current;
}
 
Example 19
Project: RedisDirectory   File: RedisDirectory.java   View source code 5 votes vote down vote up
@Override
public void deleteFile(String name) throws IOException {
    ensureOpen();
    boolean b = fileNameExists(name);
    if (b) {
        byte[] hget = inputOutputStream.hget(Constants.DIR_METADATA_BYTES, name.getBytes(), FILE_LENGTH);
        long length = Longs.fromByteArray(hget);
        long blockSize = getBlockSize(length);
        inputOutputStream.deleteFile(Constants.DIRECTORY_METADATA, Constants.FILE_METADATA, name, blockSize);
    } else {
        log.error("Delete file {} does not exists!", name);
    }
}
 
Example 20
Project: RedisDirectory   File: RedisDirectory.java   View source code 5 votes vote down vote up
private RedisFile loadRedisToFile(String fileName) {
    byte[] hget = inputOutputStream.hget(Constants.DIR_METADATA_BYTES, fileName.getBytes(), FILE_LENGTH);
    long lenght = Longs.fromByteArray(hget);
    RedisFile redisFile = new RedisFile(fileName, lenght);
    long blockSize = getBlockSize(lenght);
    List<byte[]> bytes = inputOutputStream.loadFileOnce(Constants.FILE_METADATA, fileName, blockSize);
    redisFile.setBuffers(bytes);
    return redisFile;
}
 
Example 21
Project: RedisDirectory   File: JedisClusterStream.java   View source code 5 votes vote down vote up
@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField, List<byte[]> values, long
        fileLength) {
    //add new file length
    jedisCluster.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength));
    //add new file content
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        jedisCluster.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i)));
    }
    values.clear();
    deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
}
 
Example 22
Project: dble   File: FastByteOperations.java   View source code 5 votes vote down vote up
/**
 * 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 23
Project: drift   File: ArrayField.java   View source code 5 votes vote down vote up
public Map<Short, List<Long>> getMapLongList()
{
    if (mapLongArray == null) {
        return null;
    }
    return Maps.transformValues(this.mapLongArray, Longs::asList);
}
 
Example 24
Project: mycat-src-1.6.1-RELEASE   File: FastByteOperations.java   View source code 5 votes vote down vote up
/**
 * 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 25
Project: dremio-oss   File: LBlockHashTable.java   View source code 5 votes vote down vote up
private void addDataBlocks(){

    // make sure can fit the next batch.
    listener.resized(currentOrdinal + MAX_VALUES_PER_BATCH);

    try(RollbackCloseable rollbackable = new RollbackCloseable()) {

      {
        FixedBlockVector newFixed = new FixedBlockVector(allocator, pivot.getBlockWidth());
        rollbackable.add(newFixed);
        newFixed.ensureAvailableBlocks(MAX_VALUES_PER_BATCH);
        fixedBlocks = ObjectArrays.concat(fixedBlocks, newFixed);
        tableFixedAddresses = Longs.concat(tableFixedAddresses, new long[]{newFixed.getMemoryAddress()});
      }

      {
        VariableBlockVector newVariable = new VariableBlockVector(allocator, pivot.getVariableCount());
        rollbackable.add(newVariable);
        newVariable.ensureAvailableDataSpace(pivot.getVariableCount() == 0 ? 0 : MAX_VALUES_PER_BATCH * defaultVariableLengthSize);
        variableBlocks = ObjectArrays.concat(variableBlocks, newVariable);
        initVariableAddresses = Longs.concat(initVariableAddresses, new long[]{newVariable.getMemoryAddress()});
        openVariableAddresses = Longs.concat(openVariableAddresses, new long[]{newVariable.getMemoryAddress()});
        maxVariableAddresses = Longs.concat(maxVariableAddresses, new long[]{newVariable.getMaxMemoryAddress()});
      }
      rollbackable.commit();
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }
 
Example 26
Project: guava-mock   File: Murmur3_32HashFunction.java   View source code 5 votes vote down vote up
@Override
public HashCode hashLong(long input) {
  int low = (int) input;
  int high = (int) (input >>> 32);

  int k1 = mixK1(low);
  int h1 = mixH1(seed, k1);

  k1 = mixK1(high);
  h1 = mixH1(h1, k1);

  return fmix(h1, Longs.BYTES);
}
 
Example 27
Project: guava-mock   File: LittleEndianByteArray.java   View source code 5 votes vote down vote up
@Override
public long getLongLittleEndian(byte[] source, int offset) {
  return Longs.fromBytes(
      source[offset + 7],
      source[offset + 6],
      source[offset + 5],
      source[offset + 4],
      source[offset + 3],
      source[offset + 2],
      source[offset + 1],
      source[offset]);
}
 
Example 28
Project: guava-mock   File: LittleEndianDataInputStream.java   View source code 5 votes vote down vote up
/**
 * Reads a {@code long} as specified by {@link DataInputStream#readLong()}, except using
 * little-endian byte order.
 *
 * @return the next eight bytes of the input stream, interpreted as a {@code long} in
 *     little-endian byte order
 * @throws IOException if an I/O error occurs
 */
@CanIgnoreReturnValue // to skip some bytes
@Override
public long readLong() throws IOException {
  byte b1 = readAndCheckByte();
  byte b2 = readAndCheckByte();
  byte b3 = readAndCheckByte();
  byte b4 = readAndCheckByte();
  byte b5 = readAndCheckByte();
  byte b6 = readAndCheckByte();
  byte b7 = readAndCheckByte();
  byte b8 = readAndCheckByte();

  return Longs.fromBytes(b8, b7, b6, b5, b4, b3, b2, b1);
}
 
Example 29
Project: guava-mock   File: QuantilesTest.java   View source code 5 votes vote down vote up
public void testScale_index_compute_longVarargs() {
  long[] dataset = Longs.toArray(SIXTEEN_SQUARES_LONGS);
  assertThat(Quantiles.scale(10).index(1).compute(dataset))
      .isWithin(ALLOWED_ERROR)
      .of(SIXTEEN_SQUARES_DECILE_1);
  assertThat(dataset).asList().isEqualTo(SIXTEEN_SQUARES_LONGS);
}
 
Example 30
Project: jblockchain   File: Transaction.java   View source code 5 votes vote down vote up
/**
 * Calculates the hash using relevant fields of this type
 * @return SHA256-hash as raw bytes
 */
public byte[] calculateHash() {
    byte[] hashableData = ArrayUtils.addAll(text.getBytes(), senderHash);
    hashableData = ArrayUtils.addAll(hashableData, signature);
    hashableData = ArrayUtils.addAll(hashableData, Longs.toByteArray(timestamp));
    return DigestUtils.sha256(hashableData);
}
 
Example 31
Project: jblockchain   File: Block.java   View source code 5 votes vote down vote up
/**
 * Calculates the hash using relevant fields of this type
 * @return SHA256-hash as raw bytes
 */
public byte[] calculateHash() {
    byte[] hashableData = ArrayUtils.addAll(previousBlockHash, merkleRoot);
    hashableData = ArrayUtils.addAll(hashableData, Longs.toByteArray(tries));
    hashableData = ArrayUtils.addAll(hashableData, Longs.toByteArray(timestamp));
    return DigestUtils.sha256(hashableData);
}
 
Example 32
Project: flume-release-1.7.0   File: TestAsyncHBaseSink.java   View source code 5 votes vote down vote up
@Test
public void testOneEventWithDefaults() throws Exception {
  Map<String,String> ctxMap = new HashMap<String,String>();
  ctxMap.put("table", tableName);
  ctxMap.put("columnFamily", columnFamily);
  ctxMap.put("serializer",
          "org.apache.flume.sink.hbase.SimpleAsyncHbaseEventSerializer");
  ctxMap.put("keep-alive", "0");
  ctxMap.put("timeout", "10000");
  Context tmpctx = new Context();
  tmpctx.putAll(ctxMap);

  testUtility.createTable(tableName.getBytes(), columnFamily.getBytes());
  deleteTable = true;
  AsyncHBaseSink sink = new AsyncHBaseSink(testUtility.getConfiguration());
  Configurables.configure(sink, tmpctx);
  Channel channel = new MemoryChannel();
  Configurables.configure(channel, tmpctx);
  sink.setChannel(channel);
  sink.start();
  Transaction tx = channel.getTransaction();
  tx.begin();
  Event e = EventBuilder.withBody(
          Bytes.toBytes(valBase));
  channel.put(e);
  tx.commit();
  tx.close();
  Assert.assertFalse(sink.isConfNull());
  sink.process();
  sink.stop();
  HTable table = new HTable(testUtility.getConfiguration(), tableName);
  byte[][] results = getResults(table, 1);
  byte[] out = results[0];
  Assert.assertArrayEquals(e.getBody(), out);
  out = results[1];
  Assert.assertArrayEquals(Longs.toByteArray(1), out);
}
 
Example 33
Project: flume-release-1.7.0   File: TestAsyncHBaseSink.java   View source code 5 votes vote down vote up
@Test
public void testOneEvent() throws Exception {
  testUtility.createTable(tableName.getBytes(), columnFamily.getBytes());
  deleteTable = true;
  AsyncHBaseSink sink = new AsyncHBaseSink(testUtility.getConfiguration());
  Configurables.configure(sink, ctx);
  Channel channel = new MemoryChannel();
  Configurables.configure(channel, ctx);
  sink.setChannel(channel);
  sink.start();
  Transaction tx = channel.getTransaction();
  tx.begin();
  Event e = EventBuilder.withBody(
      Bytes.toBytes(valBase));
  channel.put(e);
  tx.commit();
  tx.close();
  Assert.assertFalse(sink.isConfNull());
  sink.process();
  sink.stop();
  HTable table = new HTable(testUtility.getConfiguration(), tableName);
  byte[][] results = getResults(table, 1);
  byte[] out = results[0];
  Assert.assertArrayEquals(e.getBody(), out);
  out = results[1];
  Assert.assertArrayEquals(Longs.toByteArray(1), out);
}
 
Example 34
Project: dremio-oss   File: DremioMetaImpl.java   View source code 5 votes vote down vote up
@Override
public ExecuteBatchResult executeBatch(StatementHandle h, List<List<TypedValue>> parameterValueLists)
    throws NoSuchStatementException {
  final List<Long> updateCounts = new ArrayList<>();
  for (List<TypedValue> parameterValueList : parameterValueLists) {
    ExecuteResult executeResult = execute(h, parameterValueList, -1);
    final long updateCount =
        executeResult.resultSets.size() == 1
            ? executeResult.resultSets.get(0).updateCount
            : -1L;
    updateCounts.add(updateCount);
  }
  return new ExecuteBatchResult(Longs.toArray(updateCounts));
}
 
Example 35
Project: flume-release-1.7.0   File: TestHBaseSink.java   View source code 5 votes vote down vote up
@Test
public void testOneEventWithDefaults() throws Exception {
  //Create a context without setting increment column and payload Column
  ctx = new Context();
  ctx.put("table", tableName);
  ctx.put("columnFamily", columnFamily);
  ctx.put("serializer", SimpleHbaseEventSerializer.class.getName());

  HBaseSink sink = new HBaseSink(conf);
  Configurables.configure(sink, ctx);
  Channel channel = new MemoryChannel();
  Configurables.configure(channel, new Context());
  sink.setChannel(channel);
  sink.start();
  Transaction tx = channel.getTransaction();
  tx.begin();
  Event e = EventBuilder.withBody(Bytes.toBytes(valBase));
  channel.put(e);
  tx.commit();
  tx.close();

  sink.process();
  sink.stop();
  HTable table = new HTable(conf, tableName);
  byte[][] results = getResults(table, 1);
  byte[] out = results[0];
  Assert.assertArrayEquals(e.getBody(), out);
  out = results[1];
  Assert.assertArrayEquals(Longs.toByteArray(1), out);
}
 
Example 36
Project: flume-release-1.7.0   File: TestHBaseSink.java   View source code 5 votes vote down vote up
@Test
public void testThreeEvents() throws Exception {
  initContextForSimpleHbaseEventSerializer();
  ctx.put("batchSize", "3");
  HBaseSink sink = new HBaseSink(conf);
  Configurables.configure(sink, ctx);
  Channel channel = new MemoryChannel();
  Configurables.configure(channel, new Context());
  sink.setChannel(channel);
  sink.start();
  Transaction tx = channel.getTransaction();
  tx.begin();
  for (int i = 0; i < 3; i++) {
    Event e = EventBuilder.withBody(Bytes.toBytes(valBase + "-" + i));
    channel.put(e);
  }
  tx.commit();
  tx.close();
  sink.process();
  sink.stop();
  HTable table = new HTable(conf, tableName);
  byte[][] results = getResults(table, 3);
  byte[] out;
  int found = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (Arrays.equals(results[j], Bytes.toBytes(valBase + "-" + i))) {
        found++;
        break;
      }
    }
  }
  Assert.assertEquals(3, found);
  out = results[3];
  Assert.assertArrayEquals(Longs.toByteArray(3), out);
}
 
Example 37
Project: flume-release-1.7.0   File: TestHBaseSink.java   View source code 5 votes vote down vote up
@Test
public void testTransactionStateOnChannelException() throws Exception {
  initContextForSimpleHbaseEventSerializer();
  ctx.put("batchSize", "1");

  HBaseSink sink = new HBaseSink(conf);
  Configurables.configure(sink, ctx);
  // Reset the context to a higher batchSize
  Channel channel = spy(new MemoryChannel());
  Configurables.configure(channel, new Context());
  sink.setChannel(channel);
  sink.start();
  Transaction tx = channel.getTransaction();
  tx.begin();
  Event e = EventBuilder.withBody(Bytes.toBytes(valBase + "-" + 0));
  channel.put(e);
  tx.commit();
  tx.close();
  doThrow(new ChannelException("Mock Exception")).when(channel).take();
  try {
    sink.process();
    Assert.fail("take() method should throw exception");
  } catch (ChannelException ex) {
    Assert.assertEquals("Mock Exception", ex.getMessage());
  }
  doReturn(e).when(channel).take();
  sink.process();
  sink.stop();
  HTable table = new HTable(conf, tableName);
  byte[][] results = getResults(table, 1);
  byte[] out = results[0];
  Assert.assertArrayEquals(e.getBody(), out);
  out = results[1];
  Assert.assertArrayEquals(Longs.toByteArray(1), out);
}
 
Example 38
Project: flume-release-1.7.0   File: TestHBaseSink.java   View source code 5 votes vote down vote up
@Test
public void testTransactionStateOnSerializationException() throws Exception {
  initContextForSimpleHbaseEventSerializer();
  ctx.put("batchSize", "1");
  ctx.put(HBaseSinkConfigurationConstants.CONFIG_SERIALIZER,
          "org.apache.flume.sink.hbase.MockSimpleHbaseEventSerializer");

  HBaseSink sink = new HBaseSink(conf);
  Configurables.configure(sink, ctx);
  // Reset the context to a higher batchSize
  ctx.put("batchSize", "100");
  Channel channel = new MemoryChannel();
  Configurables.configure(channel, new Context());
  sink.setChannel(channel);
  sink.start();
  Transaction tx = channel.getTransaction();
  tx.begin();
  Event e = EventBuilder.withBody(Bytes.toBytes(valBase + "-" + 0));
  channel.put(e);
  tx.commit();
  tx.close();
  try {
    MockSimpleHbaseEventSerializer.throwException = true;
    sink.process();
    Assert.fail("FlumeException expected from serilazer");
  } catch (FlumeException ex) {
    Assert.assertEquals("Exception for testing", ex.getMessage());
  }
  MockSimpleHbaseEventSerializer.throwException = false;
  sink.process();
  sink.stop();
  HTable table = new HTable(conf, tableName);
  byte[][] results = getResults(table, 1);
  byte[] out = results[0];
  Assert.assertArrayEquals(e.getBody(), out);
  out = results[1];
  Assert.assertArrayEquals(Longs.toByteArray(1), out);
}
 
Example 39
Project: twister2   File: LMDBTest.java   View source code 5 votes vote down vote up
public static void main(String[] args) {
  //Memory Manager
  Path dataPath = new Path("/home/pulasthi/work/twister2/lmdbdatabase");
  MemoryManager memoryManager = new LMDBMemoryManager(dataPath);
  byte[] val = Longs.toByteArray(1231212121213L);
  memoryManager.put(1234L, val);

  ByteBuffer results = memoryManager.get(1234L);
  long res = results.getLong();
  if(res == 1231212121213L){
    System.out.println("Correct");
  }
}
 
Example 40
Project: NioSmtpClient   File: EhloResponse.java   View source code 5 votes vote down vote up
private void parseSize(List<String> parts) {
  if (parts.size() > 1) {
    Optional<Long> maybeSize = Optional.ofNullable(Longs.tryParse(parts.get(1)));

    if (maybeSize.isPresent() && maybeSize.get() > 0) {
      maxMessageSize = maybeSize;
    }
  }
}