Java Code Examples for java.util.OptionalLong#getAsLong()

The following examples show how to use java.util.OptionalLong#getAsLong() . 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: WALEntryStream.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether the file is opened for writing.
 */
private boolean readNextEntryAndRecordReaderPosition() throws IOException {
  Entry readEntry = reader.next();
  long readerPos = reader.getPosition();
  OptionalLong fileLength = walFileLengthProvider.getLogFileSizeIfBeingWritten(currentPath);
  if (fileLength.isPresent() && readerPos > fileLength.getAsLong()) {
    // see HBASE-14004, for AsyncFSWAL which uses fan-out, it is possible that we read uncommitted
    // data, so we need to make sure that we do not read beyond the committed file length.
    if (LOG.isDebugEnabled()) {
      LOG.debug("The provider tells us the valid length for " + currentPath + " is " +
          fileLength.getAsLong() + ", but we have advanced to " + readerPos);
    }
    resetReader();
    return true;
  }
  if (readEntry != null) {
    LOG.trace("reading entry: {} ", readEntry);
    metrics.incrLogEditsRead();
    metrics.incrLogReadInBytes(readerPos - currentPositionOfEntry);
  }
  currentEntry = readEntry; // could be null
  this.currentPositionOfReader = readerPos;
  return fileLength.isPresent();
}
 
Example 2
Source File: IDiffSortedMap.java    From bifurcan with MIT License 6 votes vote down vote up
@Override
default OptionalLong floorIndex(K key) {
  ISortedMap<K, ISortedMap<K, V>> segments = segments();
  ISortedSet<Long> segmentOffsets = segmentOffsets();

  OptionalLong oSegmentIdx = segments.floorIndex(key);
  if (!oSegmentIdx.isPresent()) {
    return OptionalLong.empty();
  }
  long segmentIdx = oSegmentIdx.getAsLong();

  ISortedMap<K, V> segment = segments.nth(segmentIdx).value();
  OptionalLong oIdx = segment.floorIndex(key);

  if (oIdx.isPresent()) {
    return OptionalLong.of(segmentOffsets.nth(segmentIdx) + oIdx.getAsLong());
  } else if (segmentIdx > 0) {
    return OptionalLong.of(segmentOffsets.nth(segmentIdx - 1) + segments.nth(segmentIdx - 1).value().size() - 1);
  } else {
    return OptionalLong.empty();
  }
}
 
Example 3
Source File: ThriftMetastoreUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
public static OptionalLong getTotalSizeInBytes(OptionalDouble averageColumnLength, OptionalLong rowCount, OptionalLong nullsCount)
{
    if (averageColumnLength.isPresent() && rowCount.isPresent() && nullsCount.isPresent()) {
        long nonNullsCount = rowCount.getAsLong() - nullsCount.getAsLong();
        if (nonNullsCount < 0) {
            return OptionalLong.empty();
        }
        return OptionalLong.of(round(averageColumnLength.getAsDouble() * nonNullsCount));
    }
    return OptionalLong.empty();
}
 
Example 4
Source File: MetadataAccumulator.java    From timely with Apache License 2.0 5 votes vote down vote up
public Entry(Text tablet) {
    this.tablet = tablet;
    Optional<String> prefixOptional = TabletRowAdapter.decodeRowPrefix(tablet);
    OptionalLong offsetOptional = TabletRowAdapter.decodeRowOffset(tablet);
    tabletPrefix = prefixOptional.orElseGet(tablet::toString);
    if (offsetOptional.isPresent()) {
        hasTabletOffset = true;
        tabletOffset = offsetOptional.getAsLong();
    } else {
        hasTabletOffset = false;
        tabletOffset = -1;
    }
}
 
Example 5
Source File: MemoryPagesStore.java    From presto with Apache License 2.0 5 votes vote down vote up
public synchronized List<Page> getPages(
        Long tableId,
        int partNumber,
        int totalParts,
        List<Integer> columnIndexes,
        long expectedRows,
        OptionalLong limit,
        OptionalDouble sampleRatio)
{
    if (!contains(tableId)) {
        throw new PrestoException(MISSING_DATA, "Failed to find table on a worker.");
    }
    TableData tableData = tables.get(tableId);
    if (tableData.getRows() < expectedRows) {
        throw new PrestoException(MISSING_DATA,
                format("Expected to find [%s] rows on a worker, but found [%s].", expectedRows, tableData.getRows()));
    }

    ImmutableList.Builder<Page> partitionedPages = ImmutableList.builder();

    boolean done = false;
    long totalRows = 0;
    for (int i = partNumber; i < tableData.getPages().size() && !done; i += totalParts) {
        if (sampleRatio.isPresent() && ThreadLocalRandom.current().nextDouble() >= sampleRatio.getAsDouble()) {
            continue;
        }

        Page page = tableData.getPages().get(i);
        totalRows += page.getPositionCount();
        if (limit.isPresent() && totalRows > limit.getAsLong()) {
            page = page.getRegion(0, (int) (page.getPositionCount() - (totalRows - limit.getAsLong())));
            done = true;
        }
        partitionedPages.add(getColumns(page, columnIndexes));
    }

    return partitionedPages.build();
}
 
Example 6
Source File: PrestoSystemRequirements.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void verifyFileDescriptor()
{
    OptionalLong maxFileDescriptorCount = getMaxFileDescriptorCount();
    if (maxFileDescriptorCount.isEmpty()) {
        // This should never happen since we have verified the OS and JVM above
        failRequirement("Cannot read OS file descriptor limit");
    }
    if (maxFileDescriptorCount.getAsLong() < MIN_FILE_DESCRIPTORS) {
        failRequirement("Presto requires at least %s file descriptors (found %s)", MIN_FILE_DESCRIPTORS, maxFileDescriptorCount.getAsLong());
    }
    if (maxFileDescriptorCount.getAsLong() < RECOMMENDED_FILE_DESCRIPTORS) {
        warnRequirement("Current OS file descriptor limit is %s. Presto recommends at least %s", maxFileDescriptorCount.getAsLong(), RECOMMENDED_FILE_DESCRIPTORS);
    }
}
 
Example 7
Source File: JsonGenesisConfigOptions.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public OptionalLong getDaoForkBlock() {
  final OptionalLong block = getOptionalLong("daoforkblock");
  if (block.isPresent() && block.getAsLong() <= 0) {
    return OptionalLong.empty();
  }
  return block;
}
 
Example 8
Source File: TableMetadataSystemTable.java    From presto with Apache License 2.0 4 votes vote down vote up
private static List<Page> buildPages(MetadataDao dao, ConnectorTableMetadata tableMetadata, TupleDomain<Integer> tupleDomain)
{
    Map<Integer, NullableValue> domainValues = extractFixedValues(tupleDomain).orElse(ImmutableMap.of());
    String schemaName = getStringValue(domainValues.get(getColumnIndex(tableMetadata, SCHEMA_NAME)));
    String tableName = getStringValue(domainValues.get(getColumnIndex(tableMetadata, TABLE_NAME)));

    PageListBuilder pageBuilder = new PageListBuilder(tableMetadata.getColumns().stream()
            .map(ColumnMetadata::getType)
            .collect(toList()));

    List<TableMetadataRow> tableRows = dao.getTableMetadataRows(schemaName, tableName);
    PeekingIterator<ColumnMetadataRow> columnRowIterator = peekingIterator(dao.getColumnMetadataRows(schemaName, tableName).iterator());

    for (TableMetadataRow tableRow : tableRows) {
        while (columnRowIterator.hasNext() && columnRowIterator.peek().getTableId() < tableRow.getTableId()) {
            columnRowIterator.next();
        }

        String temporalColumnName = null;
        SortedMap<Integer, String> sortColumnNames = new TreeMap<>();
        SortedMap<Integer, String> bucketColumnNames = new TreeMap<>();
        OptionalLong temporalColumnId = tableRow.getTemporalColumnId();
        while (columnRowIterator.hasNext() && columnRowIterator.peek().getTableId() == tableRow.getTableId()) {
            ColumnMetadataRow columnRow = columnRowIterator.next();
            if (temporalColumnId.isPresent() && columnRow.getColumnId() == temporalColumnId.getAsLong()) {
                temporalColumnName = columnRow.getColumnName();
            }
            OptionalInt sortOrdinalPosition = columnRow.getSortOrdinalPosition();
            if (sortOrdinalPosition.isPresent()) {
                sortColumnNames.put(sortOrdinalPosition.getAsInt(), columnRow.getColumnName());
            }
            OptionalInt bucketOrdinalPosition = columnRow.getBucketOrdinalPosition();
            if (bucketOrdinalPosition.isPresent()) {
                bucketColumnNames.put(bucketOrdinalPosition.getAsInt(), columnRow.getColumnName());
            }
        }

        pageBuilder.beginRow();

        // schema_name, table_name
        VARCHAR.writeSlice(pageBuilder.nextBlockBuilder(), utf8Slice(tableRow.getSchemaName()));
        VARCHAR.writeSlice(pageBuilder.nextBlockBuilder(), utf8Slice(tableRow.getTableName()));

        // temporal_column
        if (temporalColumnId.isPresent()) {
            if (temporalColumnName == null) {
                throw new PrestoException(RAPTOR_CORRUPT_METADATA, format("Table ID %s has corrupt metadata (invalid temporal column ID)", tableRow.getTableId()));
            }
            VARCHAR.writeSlice(pageBuilder.nextBlockBuilder(), utf8Slice(temporalColumnName));
        }
        else {
            pageBuilder.nextBlockBuilder().appendNull();
        }

        // ordering_columns
        writeArray(pageBuilder.nextBlockBuilder(), sortColumnNames.values());

        // distribution_name
        Optional<String> distributionName = tableRow.getDistributionName();
        if (distributionName.isPresent()) {
            VARCHAR.writeSlice(pageBuilder.nextBlockBuilder(), utf8Slice(distributionName.get()));
        }
        else {
            pageBuilder.nextBlockBuilder().appendNull();
        }

        // bucket_count
        OptionalInt bucketCount = tableRow.getBucketCount();
        if (bucketCount.isPresent()) {
            BIGINT.writeLong(pageBuilder.nextBlockBuilder(), bucketCount.getAsInt());
        }
        else {
            pageBuilder.nextBlockBuilder().appendNull();
        }

        // bucketing_columns
        writeArray(pageBuilder.nextBlockBuilder(), bucketColumnNames.values());

        // organized
        BOOLEAN.writeBoolean(pageBuilder.nextBlockBuilder(), tableRow.isOrganized());
    }

    return pageBuilder.build();
}
 
Example 9
Source File: BasicLong.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NoSuchElementException.class)
public void testEmptyGet() {
    OptionalLong empty = OptionalLong.empty();

    long got = empty.getAsLong();
}
 
Example 10
Source File: DateTieredCompactionPolicy.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldPerformMajorCompaction(Collection<HStoreFile> filesToCompact)
    throws IOException {
  long mcTime = getNextMajorCompactTime(filesToCompact);
  if (filesToCompact == null || mcTime == 0) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("filesToCompact: " + filesToCompact + " mcTime: " + mcTime);
    }
    return false;
  }

  // TODO: Use better method for determining stamp of last major (HBASE-2990)
  long lowTimestamp = StoreUtils.getLowestTimestamp(filesToCompact);
  long now = EnvironmentEdgeManager.currentTime();
  if (lowTimestamp <= 0L || lowTimestamp >= (now - mcTime)) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("lowTimestamp: " + lowTimestamp + " lowTimestamp: " + lowTimestamp + " now: " +
          now + " mcTime: " + mcTime); 
    }
    return false;
  }

  long cfTTL = this.storeConfigInfo.getStoreFileTtl();
  HDFSBlocksDistribution hdfsBlocksDistribution = new HDFSBlocksDistribution();
  List<Long> boundaries = getCompactBoundariesForMajor(filesToCompact, now);
  boolean[] filesInWindow = new boolean[boundaries.size()];

  for (HStoreFile file: filesToCompact) {
    OptionalLong minTimestamp = file.getMinimumTimestamp();
    long oldest = minTimestamp.isPresent() ? now - minTimestamp.getAsLong() : Long.MIN_VALUE;
    if (cfTTL != Long.MAX_VALUE && oldest >= cfTTL) {
      LOG.debug("Major compaction triggered on store " + this
        + "; for TTL maintenance");
      return true;
    }
    if (!file.isMajorCompactionResult() || file.isBulkLoadResult()) {
      LOG.debug("Major compaction triggered on store " + this
        + ", because there are new files and time since last major compaction "
        + (now - lowTimestamp) + "ms");
      return true;
    }

    int lowerWindowIndex =
        Collections.binarySearch(boundaries, minTimestamp.orElse(Long.MAX_VALUE));
    int upperWindowIndex =
        Collections.binarySearch(boundaries, file.getMaximumTimestamp().orElse(Long.MAX_VALUE));
    // Handle boundary conditions and negative values of binarySearch
    lowerWindowIndex = (lowerWindowIndex < 0) ? Math.abs(lowerWindowIndex + 2) : lowerWindowIndex;
    upperWindowIndex = (upperWindowIndex < 0) ? Math.abs(upperWindowIndex + 2) : upperWindowIndex;
    if (lowerWindowIndex != upperWindowIndex) {
      LOG.debug("Major compaction triggered on store " + this + "; because file "
        + file.getPath() + " has data with timestamps cross window boundaries");
      return true;
    } else if (filesInWindow[upperWindowIndex]) {
      LOG.debug("Major compaction triggered on store " + this +
        "; because there are more than one file in some windows");
      return true;
    } else {
      filesInWindow[upperWindowIndex] = true;
    }
    hdfsBlocksDistribution.add(file.getHDFSBlockDistribution());
  }

  float blockLocalityIndex = hdfsBlocksDistribution
      .getBlockLocalityIndex(DNS.getHostname(comConf.conf, DNS.ServerType.REGIONSERVER));
  if (blockLocalityIndex < comConf.getMinLocalityToForceCompact()) {
    LOG.debug("Major compaction triggered on store " + this
      + "; to make hdfs blocks local, current blockLocalityIndex is "
      + blockLocalityIndex + " (min " + comConf.getMinLocalityToForceCompact() + ")");
    return true;
  }

  LOG.debug("Skipping major compaction of " + this +
    ", because the files are already major compacted");
  return false;
}
 
Example 11
Source File: OptionalUtils.java    From dalesbred with MIT License 4 votes vote down vote up
private static @Nullable Object unwrap(@NotNull OptionalLong o) {
    return o.isPresent() ? o.getAsLong() : null;
}
 
Example 12
Source File: XceiverClientRatis.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Override
public long getReplicatedMinCommitIndex() {
  OptionalLong minIndex =
      commitInfoMap.values().parallelStream().mapToLong(v -> v).min();
  return minIndex.isPresent() ? minIndex.getAsLong() : 0;
}
 
Example 13
Source File: BasicLong.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NoSuchElementException.class)
public void testEmptyGet() {
    OptionalLong empty = OptionalLong.empty();

    long got = empty.getAsLong();
}
 
Example 14
Source File: BasicLong.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NoSuchElementException.class)
public void testEmptyGet() {
    OptionalLong empty = OptionalLong.empty();

    long got = empty.getAsLong();
}
 
Example 15
Source File: InternalTable.java    From presto with Apache License 2.0 4 votes vote down vote up
public boolean atLimit(OptionalLong limit)
{
    return limit.isPresent() && recordCount >= limit.getAsLong();
}
 
Example 16
Source File: SiteToSiteBulletinReportingTask.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ReportingContext context) {

    final boolean isClustered = context.isClustered();
    final String nodeId = context.getClusterNodeIdentifier();
    if (nodeId == null && isClustered) {
        getLogger().debug("This instance of NiFi is configured for clustering, but the Cluster Node Identifier is not yet available. "
            + "Will wait for Node Identifier to be established.");
        return;
    }

    final BulletinQuery bulletinQuery = new BulletinQuery.Builder().after(lastSentBulletinId).build();
    final List<Bulletin> bulletins = context.getBulletinRepository().findBulletins(bulletinQuery);

    if(bulletins == null || bulletins.isEmpty()) {
        getLogger().debug("No events to send because no events are stored in the repository.");
        return;
    }

    final OptionalLong opMaxId = bulletins.stream().mapToLong(t -> t.getId()).max();
    final Long currMaxId = opMaxId.isPresent() ? opMaxId.getAsLong() : -1;

    if(currMaxId < lastSentBulletinId){
        getLogger().warn("Current bulletin max id is {} which is less than what was stored in state as the last queried event, which was {}. "
                + "This means the bulletins repository restarted its ids. Restarting querying from the beginning.", new Object[]{currMaxId, lastSentBulletinId});
        lastSentBulletinId = -1;
    }

    if (currMaxId == lastSentBulletinId) {
        getLogger().debug("No events to send due to the current max id being equal to the last id that was sent.");
        return;
    }

    final String platform = context.getProperty(SiteToSiteUtils.PLATFORM).evaluateAttributeExpressions().getValue();
    final Boolean allowNullValues = context.getProperty(ALLOW_NULL_VALUES).asBoolean();

    final Map<String, ?> config = Collections.emptyMap();
    final JsonBuilderFactory factory = Json.createBuilderFactory(config);
    final JsonObjectBuilder builder = factory.createObjectBuilder();

    final DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMAT);
    df.setTimeZone(TimeZone.getTimeZone("Z"));

    final long start = System.nanoTime();

    // Create a JSON array of all the events in the current batch
    final JsonArrayBuilder arrayBuilder = factory.createArrayBuilder();
    for (final Bulletin bulletin : bulletins) {
        if(bulletin.getId() > lastSentBulletinId) {
            arrayBuilder.add(serialize(factory, builder, bulletin, df, platform, nodeId, allowNullValues));
        }
    }
    final JsonArray jsonArray = arrayBuilder.build();

    // Send the JSON document for the current batch
    Transaction transaction = null;
    try {
        // Lazily create SiteToSiteClient to provide a StateManager
        setup(context);

        transaction = getClient().createTransaction(TransferDirection.SEND);
        if (transaction == null) {
            getLogger().info("All destination nodes are penalized; will attempt to send data later");
            return;
        }

        final Map<String, String> attributes = new HashMap<>();
        final String transactionId = UUID.randomUUID().toString();
        attributes.put("reporting.task.transaction.id", transactionId);
        attributes.put("reporting.task.name", getName());
        attributes.put("reporting.task.uuid", getIdentifier());
        attributes.put("reporting.task.type", this.getClass().getSimpleName());
        attributes.put("mime.type", "application/json");

        sendData(context, transaction, attributes, jsonArray);
        transaction.confirm();
        transaction.complete();

        final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
        getLogger().info("Successfully sent {} Bulletins to destination in {} ms; Transaction ID = {}; First Event ID = {}",
                new Object[]{bulletins.size(), transferMillis, transactionId, bulletins.get(0).getId()});
    } catch (final Exception e) {
        if (transaction != null) {
            transaction.error();
        }
        if (e instanceof ProcessException) {
            throw (ProcessException) e;
        } else {
            throw new ProcessException("Failed to send Bulletins to destination due to IOException:" + e.getMessage(), e);
        }
    }

    lastSentBulletinId = currMaxId;
}
 
Example 17
Source File: BasicLong.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NoSuchElementException.class)
public void testEmptyGet() {
    OptionalLong empty = OptionalLong.empty();

    long got = empty.getAsLong();
}
 
Example 18
Source File: BasicLong.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NoSuchElementException.class)
public void testEmptyGet() {
    OptionalLong empty = OptionalLong.empty();

    long got = empty.getAsLong();
}
 
Example 19
Source File: BasicLong.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NoSuchElementException.class)
public void testEmptyGet() {
    OptionalLong empty = OptionalLong.empty();

    long got = empty.getAsLong();
}
 
Example 20
Source File: PluginDefaultGroovyMethods.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * If a value is present in the {@code OptionalLong}, returns the value,
 * otherwise throws {@code NoSuchElementException}.
 * <pre class="groovyTestCase">
 * assert OptionalLong.of(1234L).get() == 1234L
 * </pre>
 *
 * @since 3.0.0
 */
public static long get(final OptionalLong self) {
    return self.getAsLong();
}