org.opensearch.common.util.BigIntArray Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opensearch Show documentation
Show all versions of opensearch Show documentation
OpenSearch subproject :server
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.common.util;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.RamUsageEstimator;
import java.util.Arrays;
import static org.opensearch.common.util.PageCacheRecycler.INT_PAGE_SIZE;
/**
* Int array abstraction able to support more than 2B values. This implementation slices data into fixed-sized blocks of
* configurable length.
*
* @opensearch.internal
*/
final class BigIntArray extends AbstractBigArray implements IntArray {
private static final BigIntArray ESTIMATOR = new BigIntArray(0, BigArrays.NON_RECYCLING_INSTANCE, false);
private int[][] pages;
/** Constructor. */
BigIntArray(long size, BigArrays bigArrays, boolean clearOnResize) {
super(INT_PAGE_SIZE, bigArrays, clearOnResize);
this.size = size;
pages = new int[numPages(size)][];
for (int i = 0; i < pages.length; ++i) {
pages[i] = newIntPage(i);
}
}
@Override
public int get(long index) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
return pages[pageIndex][indexInPage];
}
@Override
public int set(long index, int value) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
final int[] page = pages[pageIndex];
final int ret = page[indexInPage];
page[indexInPage] = value;
return ret;
}
@Override
public int increment(long index, int inc) {
final int pageIndex = pageIndex(index);
final int indexInPage = indexInPage(index);
return pages[pageIndex][indexInPage] += inc;
}
@Override
public void fill(long fromIndex, long toIndex, int value) {
if (fromIndex > toIndex) {
throw new IllegalArgumentException();
}
final int fromPage = pageIndex(fromIndex);
final int toPage = pageIndex(toIndex - 1);
if (fromPage == toPage) {
Arrays.fill(pages[fromPage], indexInPage(fromIndex), indexInPage(toIndex - 1) + 1, value);
} else {
Arrays.fill(pages[fromPage], indexInPage(fromIndex), pages[fromPage].length, value);
for (int i = fromPage + 1; i < toPage; ++i) {
Arrays.fill(pages[i], value);
}
Arrays.fill(pages[toPage], 0, indexInPage(toIndex - 1) + 1, value);
}
}
@Override
protected int numBytesPerElement() {
return Integer.BYTES;
}
/** Change the size of this array. Content between indexes 0
and min(size(), newSize)
will be preserved. */
@Override
public void resize(long newSize) {
final int numPages = numPages(newSize);
if (numPages > pages.length) {
pages = Arrays.copyOf(pages, ArrayUtil.oversize(numPages, RamUsageEstimator.NUM_BYTES_OBJECT_REF));
}
for (int i = numPages - 1; i >= 0 && pages[i] == null; --i) {
pages[i] = newIntPage(i);
}
for (int i = numPages; i < pages.length && pages[i] != null; ++i) {
pages[i] = null;
releasePage(i);
}
this.size = newSize;
}
/** Estimates the number of bytes that would be consumed by an array of the given size. */
public static long estimateRamBytes(final long size) {
return ESTIMATOR.ramBytesEstimated(size);
}
}