public static int toInt(byte[] bytes) { return Ints.fromByteArray(bytes); }
@Override public int compare(byte[] o1, byte[] o2) { return Integer.compare(Ints.fromByteArray(o1), Ints.fromByteArray(o2)); } };
@Override public Integer deserializeInventory(byte[] bytes) { return Ints.fromByteArray(bytes); }
private int readInt(InputStream in) throws IOException { byte[] intBytes = new byte[Integer.BYTES]; ByteStreams.readFully(in, intBytes); return Ints.fromByteArray(intBytes); }
public static int getVersionFromDir(File inDir) throws IOException { File versionFile = new File(inDir, "version.bin"); if (versionFile.exists()) { return Ints.fromByteArray(Files.toByteArray(versionFile)); } final File indexFile = new File(inDir, "index.drd"); int version; if (indexFile.exists()) { try (InputStream in = new FileInputStream(indexFile)) { version = in.read(); } return version; } throw new IOE("Invalid segment dir [%s]. Can't find either of version.bin or index.drd.", inDir); } }
public void testFromByteArrayFails() { try { Ints.fromByteArray(new byte[Ints.BYTES - 1]); fail(); } catch (IllegalArgumentException expected) { } }
public void testFromByteArray() { assertEquals(0x12131415, Ints.fromByteArray(new byte[] {0x12, 0x13, 0x14, 0x15, 0x33})); assertEquals( 0xFFEEDDCC, Ints.fromByteArray(new byte[] {(byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC})); }
public void testByteArrayRoundTrips() { Random r = new Random(5); byte[] b = new byte[Ints.BYTES]; // total overkill, but, it takes 0.1 sec so why not... for (int i = 0; i < 10000; i++) { int num = r.nextInt(); assertEquals(num, Ints.fromByteArray(Ints.toByteArray(num))); r.nextBytes(b); assertTrue(Arrays.equals(b, Ints.toByteArray(Ints.fromByteArray(b)))); } }
@Override public ListenableFuture<PrestoThriftPageResult> getRows(PrestoThriftId splitId, List<String> columns, long maxBytes, PrestoThriftNullableToken nextToken) { int key = Ints.fromByteArray(splitId.getId()); signals.get(key).countDown(); return futures.get(key); } };
static void verifyHashFunction(HashFn hashFunction, int hashbits, int expected) { int hashBytes = hashbits / 8; byte[] key = new byte[256]; byte[] hashes = new byte[hashBytes * 256]; // Hash keys of the form {}, {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as the seed for (int i = 0; i < 256; i++) { key[i] = (byte) i; int seed = 256 - i; byte[] hash = hashFunction.hash(Arrays.copyOf(key, i), seed); System.arraycopy(hash, 0, hashes, i * hashBytes, hash.length); } // Then hash the result array byte[] result = hashFunction.hash(hashes, 0); // interpreted in little-endian order. int verification = Integer.reverseBytes(Ints.fromByteArray(result)); if (expected != verification) { throw new AssertionError( "Expected: " + Integer.toHexString(expected) + " got: " + Integer.toHexString(verification)); } }
public int get(Cache cache, Cache.NamedKey key) { return Ints.fromByteArray(cache.get(key)); }
private TransferSettings createTransferSettings(byte[] plaintextPluginSettingsBytes) throws StorageException, IOException { // Find plugin ID and settings XML int pluginIdentifierLength = Ints.fromByteArray(Arrays.copyOfRange(plaintextPluginSettingsBytes, 0, INTEGER_BYTES)); String pluginId = new String(Arrays.copyOfRange(plaintextPluginSettingsBytes, INTEGER_BYTES, INTEGER_BYTES + pluginIdentifierLength)); byte[] gzippedPluginSettingsByteArray = Arrays.copyOfRange(plaintextPluginSettingsBytes, INTEGER_BYTES + pluginIdentifierLength, plaintextPluginSettingsBytes.length); String pluginSettings = IOUtils.toString(new GZIPInputStream(new ByteArrayInputStream(gzippedPluginSettingsByteArray))); // Create transfer settings object try { TransferPlugin plugin = Plugins.get(pluginId, TransferPlugin.class); if (plugin == null) { throw new StorageException("Link contains unknown connection type '" + pluginId + "'. Corresponding plugin not found."); } Class<? extends TransferSettings> pluginTransferSettingsClass = TransferPluginUtil.getTransferSettingsClass(plugin.getClass()); TransferSettings transferSettings = new Persister().read(pluginTransferSettingsClass, pluginSettings); logger.log(Level.INFO, "(Decrypted) link contains: " + pluginId + " -- " + pluginSettings); return transferSettings; } catch (Exception e) { throw new StorageException(e); } }
public int get(Cache cache, String namespace, byte[] key) { return Ints.fromByteArray(cache.get(new Cache.NamedKey(namespace, key))); } }
public int get(Cache cache, String namespace, byte[] key) { return Ints.fromByteArray(cache.get(new Cache.NamedKey(namespace, key))); } }
@Override public ListenableFuture<PrestoThriftPageResult> getRows(PrestoThriftId splitId, List<String> columns, long maxBytes, PrestoThriftNullableToken nextToken) { if (rowsPerSplit == 0) { return immediateFuture(new PrestoThriftPageResult(ImmutableList.of(), 0, null)); } int key = Ints.fromByteArray(splitId.getId()); int offset = nextToken.getToken() != null ? Ints.fromByteArray(nextToken.getToken().getId()) : 0; PrestoThriftId newNextToken = offset + 1 < rowsPerSplit ? new PrestoThriftId(Ints.toByteArray(offset + 1)) : null; return immediateFuture(pageResult(key * 10 + offset, newNextToken)); }
private static PrestoThriftPageResult getRowsInternal(ConnectorPageSource pageSource, String tableName, List<String> columnNames, @Nullable PrestoThriftId nextToken) { // very inefficient implementation as it needs to re-generate all previous results to get the next page int skipPages = nextToken != null ? Ints.fromByteArray(nextToken.getId()) : 0; skipPages(pageSource, skipPages); Page page = null; while (!pageSource.isFinished() && page == null) { page = pageSource.getNextPage(); skipPages++; } PrestoThriftId newNextToken = pageSource.isFinished() ? null : new PrestoThriftId(Ints.toByteArray(skipPages)); return toThriftPage(page, types(tableName, columnNames), newNextToken); }
private static PrestoThriftSplitBatch getSplitsSync( PrestoThriftSchemaTableName schemaTableName, int maxSplitCount, PrestoThriftNullableToken nextToken) { int totalParts = DEFAULT_NUMBER_OF_SPLITS; // last sent part int partNumber = nextToken.getToken() == null ? 0 : Ints.fromByteArray(nextToken.getToken().getId()); int numberOfSplits = min(maxSplitCount, totalParts - partNumber); List<PrestoThriftSplit> splits = new ArrayList<>(numberOfSplits); for (int i = 0; i < numberOfSplits; i++) { SplitInfo splitInfo = normalSplit( schemaTableName.getSchemaName(), schemaTableName.getTableName(), partNumber + 1, totalParts); splits.add(new PrestoThriftSplit(new PrestoThriftId(SPLIT_INFO_CODEC.toJsonBytes(splitInfo)), ImmutableList.of())); partNumber++; } PrestoThriftId newNextToken = partNumber < totalParts ? new PrestoThriftId(Ints.toByteArray(partNumber)) : null; return new PrestoThriftSplitBatch(splits, newNextToken); }
@Test public void testGetBulk() { Assert.assertNull(cache.get(new Cache.NamedKey("the", HI))); put(cache, "the", HI, 2); put(cache, "the", HO, 10); Cache.NamedKey key1 = new Cache.NamedKey("the", HI); Cache.NamedKey key2 = new Cache.NamedKey("the", HO); Map<Cache.NamedKey, byte[]> result = cache.getBulk( Lists.newArrayList( key1, key2 ) ); Assert.assertEquals(2, Ints.fromByteArray(result.get(key1))); Assert.assertEquals(10, Ints.fromByteArray(result.get(key2))); }
@Test public void testGetBulk() { Assert.assertNull(cache.get(new Cache.NamedKey("the", HI))); Cache.NamedKey key1 = new Cache.NamedKey("the", HI); put(cache, key1, 2); Cache.NamedKey key2 = new Cache.NamedKey("the", HO); put(cache, key2, 10); Map<Cache.NamedKey, byte[]> result = cache.getBulk( Lists.newArrayList( key1, key2 ) ); Assert.assertEquals(2, Ints.fromByteArray(result.get(key1))); Assert.assertEquals(10, Ints.fromByteArray(result.get(key2))); Cache.NamedKey missingKey = new Cache.NamedKey("missing", HI); result = cache.getBulk(Collections.singletonList(missingKey)); Assert.assertEquals(result.size(), 0); result = cache.getBulk(new ArrayList<>()); Assert.assertEquals(result.size(), 0); }
@Test public void testLastPushWinsForConcurrentPushes() throws IOException { File replicatedDataSegmentFiles = temporaryFolder.newFolder(); Files.asByteSink(new File(replicatedDataSegmentFiles, "version.bin")).write(Ints.toByteArray(0x8)); DataSegment returnSegment1 = localDataSegmentPusher.push(dataSegmentFiles, dataSegment, false); DataSegment returnSegment2 = localDataSegmentPusher.push(replicatedDataSegmentFiles, dataSegment2, false); Assert.assertEquals(dataSegment.getDimensions(), returnSegment1.getDimensions()); Assert.assertEquals(dataSegment2.getDimensions(), returnSegment2.getDimensions()); File unzipDir = new File(config.storageDirectory, "unzip"); FileUtils.forceMkdir(unzipDir); CompressionUtils.unzip( new File(config.storageDirectory, "/ds/1970-01-01T00:00:00.000Z_1970-01-01T00:00:00.001Z/v1/0/index.zip"), unzipDir ); Assert.assertEquals(0x8, Ints.fromByteArray(Files.toByteArray(new File(unzipDir, "version.bin")))); }