Search in sources :

Example 1 with BPlusIO

use of org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO in project ignite by apache.

the class CompressionProcessorTest method doTestBTreePage.

/**
 * @param io Page IO.
 * @throws IgniteCheckedException If failed.
 */
private void doTestBTreePage(BPlusIO<byte[]> io) throws IgniteCheckedException {
    Random rnd = ThreadLocalRandom.current();
    final byte[][] rows = new byte[3][io.getItemSize()];
    for (int i = 0; i < rows.length; i++) rnd.nextBytes(rows[i]);
    ByteBuffer page = allocateDirectBuffer(pageSize);
    long pageAddr = bufferAddress(page);
    long pageId = PageIdUtils.pageId(PageIdAllocator.INDEX_PARTITION, PageIdAllocator.FLAG_IDX, 171717);
    io.initNewPage(pageAddr, pageId, pageSize, null);
    checkIo(io, page);
    Function<ByteBuffer, List<?>> getContents = (buf) -> {
        long addr = bufferAddress(buf);
        int cnt = io.getCount(addr);
        List<Object> list = new ArrayList<>(cnt);
        for (int i = 0; i < cnt; i++) {
            if (!io.isLeaf())
                list.add(((BPlusInnerIO) io).getLeft(addr, i));
            try {
                list.add(new Bytes(io.getLookupRow(null, addr, i)));
            } catch (IgniteCheckedException e) {
                throw new IllegalStateException(e);
            }
            if (!io.isLeaf())
                list.add(((BPlusInnerIO) io).getRight(addr, i));
        }
        return list;
    };
    // Empty page.
    checkCompressDecompress(page, getContents, false);
    int cnt = io.getMaxCount(pageAddr, pageSize);
    for (int i = 0; i < cnt; i++) {
        byte[] row = rows[rnd.nextInt(rows.length)];
        io.insert(pageAddr, i, row, row, 777_000 + i, false);
    }
    if (io.isLeaf())
        // Page must be full.
        assertEquals(pageSize, io.getItemsEnd(pageAddr));
    // Full page.
    checkCompressDecompress(page, getContents, io.isLeaf());
    io.setCount(pageAddr, cnt / 2);
    // Half page.
    checkCompressDecompress(page, getContents, false);
}
Also used : Arrays(java.util.Arrays) DataPagePayload(org.apache.ignite.internal.processors.cache.persistence.tree.io.DataPagePayload) LZ4_MAX_LEVEL(org.apache.ignite.internal.processors.compress.CompressionProcessor.LZ4_MAX_LEVEL) ZSTD_MAX_LEVEL(org.apache.ignite.internal.processors.compress.CompressionProcessor.ZSTD_MAX_LEVEL) Random(java.util.Random) Function(java.util.function.Function) LEAF_IO(org.apache.ignite.internal.processors.compress.CompressionProcessorTest.TestLeafIO.LEAF_IO) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) ZSTD(org.apache.ignite.configuration.DiskPageCompression.ZSTD) DiskPageCompression(org.apache.ignite.configuration.DiskPageCompression) GridUnsafe.bufferAddress(org.apache.ignite.internal.util.GridUnsafe.bufferAddress) LZ4(org.apache.ignite.configuration.DiskPageCompression.LZ4) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) PageUtils(org.apache.ignite.internal.pagemem.PageUtils) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) BPlusTree(org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree) LZ4_MIN_LEVEL(org.apache.ignite.internal.processors.compress.CompressionProcessor.LZ4_MIN_LEVEL) BPlusLeafIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO) BPlusIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO) GridIntList(org.apache.ignite.internal.util.GridIntList) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) PageIdAllocator(org.apache.ignite.internal.pagemem.PageIdAllocator) UNCOMPRESSED_PAGE(org.apache.ignite.internal.processors.compress.CompressionProcessor.UNCOMPRESSED_PAGE) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridUnsafe(org.apache.ignite.internal.util.GridUnsafe) Test(org.junit.Test) INNER_IO(org.apache.ignite.internal.processors.compress.CompressionProcessorTest.TestInnerIO.INNER_IO) BPlusInnerIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusInnerIO) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) SKIP_GARBAGE(org.apache.ignite.configuration.DiskPageCompression.SKIP_GARBAGE) List(java.util.List) PageIdUtils(org.apache.ignite.internal.pagemem.PageIdUtils) CompressionProcessorImpl.allocateDirectBuffer(org.apache.ignite.internal.processors.compress.CompressionProcessorImpl.allocateDirectBuffer) SimpleDataPageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.SimpleDataPageIO) SNAPPY(org.apache.ignite.configuration.DiskPageCompression.SNAPPY) ByteBuffer(java.nio.ByteBuffer) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ArrayList(java.util.ArrayList) GridIntList(org.apache.ignite.internal.util.GridIntList) List(java.util.List)

Example 2 with BPlusIO

use of org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO in project ignite by apache.

the class IgniteIndexReader method horizontalTreeScan.

/**
 * Traverse single index tree by each level horizontally.
 *
 * @param rootPageId Root page id.
 * @param treeName Tree name.
 * @param itemStorage Items storage.
 * @return Tree traversal info.
 */
private TreeTraversalInfo horizontalTreeScan(long rootPageId, String treeName, ItemStorage itemStorage) {
    FilePageStore store = filePageStore(partId(rootPageId));
    Map<Long, List<Throwable>> errors = new HashMap<>();
    Map<Class, Long> ioStat = new HashMap<>();
    TreeTraverseContext treeCtx = new TreeTraverseContext(treeName, store, ioStat, errors, null, null, null);
    ByteBuffer buf = allocateBuffer(pageSize);
    try {
        long addr = bufferAddress(buf);
        readPage(store, rootPageId, buf);
        PageIO pageIO = PageIO.getPageIO(addr);
        if (!(pageIO instanceof BPlusMetaIO))
            throw new IgniteException("Root page is not meta, pageId=" + rootPageId);
        BPlusMetaIO metaIO = (BPlusMetaIO) pageIO;
        ioStat.compute(metaIO.getClass(), (k, v) -> v == null ? 1 : v + 1);
        int lvlsCnt = metaIO.getLevelsCount(addr);
        long[] firstPageIds = IntStream.range(0, lvlsCnt).mapToLong(i -> metaIO.getFirstPageId(addr, i)).toArray();
        for (int i = 0; i < lvlsCnt; i++) {
            long pageId = firstPageIds[i];
            while (pageId > 0) {
                try {
                    buf.rewind();
                    readPage(store, pageId, buf);
                    pageIO = PageIO.getPageIO(addr);
                    if (i == 0 && !(pageIO instanceof BPlusLeafIO))
                        throw new IgniteException("Not-leaf page found on leaf level, pageId=" + pageId + ", level=" + i);
                    if (!(pageIO instanceof BPlusIO))
                        throw new IgniteException("Not-BPlus page found, pageId=" + pageId + ", level=" + i);
                    ioStat.compute(pageIO.getClass(), (k, v) -> v == null ? 1 : v + 1);
                    if (pageIO instanceof BPlusLeafIO) {
                        PageIOProcessor ioProcessor = getIOProcessor(pageIO);
                        PageContent pageContent = ioProcessor.getContent(pageIO, addr, pageId, treeCtx);
                        pageContent.items.forEach(itemStorage::add);
                    }
                    pageId = ((BPlusIO) pageIO).getForward(addr);
                } catch (Throwable e) {
                    errors.computeIfAbsent(pageId, k -> new LinkedList<>()).add(e);
                    pageId = 0;
                }
            }
        }
    } catch (Throwable e) {
        errors.computeIfAbsent(rootPageId, k -> new LinkedList<>()).add(e);
    } finally {
        freeBuffer(buf);
    }
    return new TreeTraversalInfo(ioStat, errors, null, rootPageId, itemStorage);
}
Also used : Arrays(java.util.Arrays) PAGE_SIZE(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.PAGE_SIZE) PART_CNT(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.PART_CNT) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) MvccInnerIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.MvccInnerIO) Collections.singletonList(java.util.Collections.singletonList) Matcher(java.util.regex.Matcher) PagePartitionMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PagePartitionMetaIO) Arrays.asList(java.util.Arrays.asList) PageIdUtils.itemId(org.apache.ignite.internal.pagemem.PageIdUtils.itemId) Map(java.util.Map) PageUtils(org.apache.ignite.internal.pagemem.PageUtils) Objects.isNull(java.util.Objects.isNull) GridStringBuilder(org.apache.ignite.internal.util.GridStringBuilder) IgniteDataIntegrityViolationException(org.apache.ignite.internal.processors.cache.persistence.wal.crc.IgniteDataIntegrityViolationException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Set(java.util.Set) CLIArgument.optionalArg(org.apache.ignite.internal.commandline.argument.parser.CLIArgument.optionalArg) StringBuilderOutputStream(org.apache.ignite.internal.commandline.StringBuilderOutputStream) Collectors.joining(java.util.stream.Collectors.joining) PageIO.getType(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO.getType) INDEX_PARTITION(org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION) GridUnsafe.allocateBuffer(org.apache.ignite.internal.util.GridUnsafe.allocateBuffer) CLIArgument.mandatoryArg(org.apache.ignite.internal.commandline.argument.parser.CLIArgument.mandatoryArg) PageIdUtils(org.apache.ignite.internal.pagemem.PageIdUtils) PageIdUtils.pageIndex(org.apache.ignite.internal.pagemem.PageIdUtils.pageIndex) Supplier(java.util.function.Supplier) PagesListNodeIO(org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListNodeIO) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PageIO.getVersion(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO.getVersion) PagesListMetaIO(org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListMetaIO) AbstractInlineInnerIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.AbstractInlineInnerIO) PageIdUtils.partId(org.apache.ignite.internal.pagemem.PageIdUtils.partId) LongStream(java.util.stream.LongStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) InnerIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.InnerIO) File(java.io.File) BPlusMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusMetaIO) AtomicLong(java.util.concurrent.atomic.AtomicLong) PageIdUtils.flag(org.apache.ignite.internal.pagemem.PageIdUtils.flag) PageIdUtils.pageId(org.apache.ignite.internal.pagemem.PageIdUtils.pageId) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException) FLAG_DATA(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_DATA) MvccLeafIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.MvccLeafIO) ByteBuffer(java.nio.ByteBuffer) GridLongList(org.apache.ignite.internal.util.GridLongList) PAGE_STORE_VER(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.PAGE_STORE_VER) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MvccDataLeafIO(org.apache.ignite.internal.processors.cache.tree.mvcc.data.MvccDataLeafIO) INDEXES(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.INDEXES) PendingRowIO(org.apache.ignite.internal.processors.cache.tree.PendingRowIO) FilePageStore(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStore) BPlusLeafIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO) Predicate(java.util.function.Predicate) IgniteException(org.apache.ignite.IgniteException) FilePageStoreManager(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) DEST_FILE(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.DEST_FILE) String.format(java.lang.String.format) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Objects(java.util.Objects) GridUnsafe.freeBuffer(org.apache.ignite.internal.util.GridUnsafe.freeBuffer) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Pattern(java.util.regex.Pattern) AbstractInlineLeafIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.AbstractInlineLeafIO) CLIArgumentParser(org.apache.ignite.internal.commandline.argument.parser.CLIArgumentParser) Objects.nonNull(java.util.Objects.nonNull) InlineIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.InlineIO) FLAG_IDX(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_IDX) IntStream(java.util.stream.IntStream) ProgressPrinter(org.apache.ignite.internal.commandline.ProgressPrinter) DataPagePayload(org.apache.ignite.internal.processors.cache.persistence.tree.io.DataPagePayload) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) PageMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageMetaIO) IndexStorageImpl(org.apache.ignite.internal.processors.cache.persistence.IndexStorageImpl) GridUnsafe.bufferAddress(org.apache.ignite.internal.util.GridUnsafe.bufferAddress) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) DIR(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.DIR) LinkedList(java.util.LinkedList) AbstractDataLeafIO(org.apache.ignite.internal.processors.cache.tree.AbstractDataLeafIO) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) RowLinkIO(org.apache.ignite.internal.processors.cache.tree.RowLinkIO) F(org.apache.ignite.internal.util.typedef.F) BPlusIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO) GridClosure3(org.apache.ignite.internal.util.lang.GridClosure3) Integer.parseInt(java.lang.Integer.parseInt) AbstractDataPageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.AbstractDataPageIO) BPlusInnerIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusInnerIO) LeafIO(org.apache.ignite.internal.cache.query.index.sorted.inline.io.LeafIO) CLIArgument(org.apache.ignite.internal.commandline.argument.parser.CLIArgument) Collectors.toList(java.util.stream.Collectors.toList) INDEX_FILE_NAME(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.INDEX_FILE_NAME) FileChannel(java.nio.channels.FileChannel) Collections(java.util.Collections) CHECK_PARTS(org.apache.ignite.internal.commandline.indexreader.IgniteIndexReader.Args.CHECK_PARTS) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) BPlusIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO) FilePageStore(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStore) BPlusLeafIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusLeafIO) ByteBuffer(java.nio.ByteBuffer) BPlusMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusMetaIO) IgniteException(org.apache.ignite.IgniteException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) GridLongList(org.apache.ignite.internal.util.GridLongList) List(java.util.List) LinkedList(java.util.LinkedList) Collectors.toList(java.util.stream.Collectors.toList) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) AbstractDataPageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.AbstractDataPageIO)

Example 3 with BPlusIO

use of org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO in project ignite by apache.

the class BPlusTreeSelfTest method testSizeForPutRmvSequential.

/**
 * Verifies that {@link BPlusTree#size} and {@link BPlusTree#size} methods behave correctly
 * on single-threaded addition and removal of elements in random order.
 *
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testSizeForPutRmvSequential() throws IgniteCheckedException {
    MAX_PER_PAGE = 5;
    boolean DEBUG_PRINT = false;
    int itemCnt = (int) Math.pow(MAX_PER_PAGE, 5) + rnd.nextInt(MAX_PER_PAGE * MAX_PER_PAGE);
    Long[] items = new Long[itemCnt];
    for (int i = 0; i < itemCnt; ++i) items[i] = (long) i;
    TestTree testTree = createTestTree(true);
    TreeMap<Long, Long> goldenMap = new TreeMap<>();
    assertEquals(0, testTree.size());
    assertEquals(0, goldenMap.size());
    final Predicate<Long> rowMatcher = new Predicate<Long>() {

        @Override
        public boolean test(Long row) {
            return row % 7 == 0;
        }
    };
    final BPlusTree.TreeRowClosure<Long, Long> rowClosure = new BPlusTree.TreeRowClosure<Long, Long>() {

        @Override
        public boolean apply(BPlusTree<Long, Long> tree, BPlusIO<Long> io, long pageAddr, int idx) throws IgniteCheckedException {
            return rowMatcher.test(io.getLookupRow(tree, pageAddr, idx));
        }
    };
    int correctMatchingRows = 0;
    Collections.shuffle(Arrays.asList(items), rnd);
    for (Long row : items) {
        if (DEBUG_PRINT) {
            X.println(" --> put(" + row + ")");
            X.print(testTree.printTree());
        }
        assertEquals(goldenMap.put(row, row), testTree.put(row));
        assertEquals(row, testTree.findOne(row));
        if (rowMatcher.test(row))
            ++correctMatchingRows;
        assertEquals(correctMatchingRows, testTree.size(rowClosure));
        long correctSize = goldenMap.size();
        assertEquals(correctSize, testTree.size());
        assertEquals(correctSize, size(testTree.find(null, null)));
        assertNoLocks();
    }
    Collections.shuffle(Arrays.asList(items), rnd);
    for (Long row : items) {
        if (DEBUG_PRINT) {
            X.println(" --> rmv(" + row + ")");
            X.print(testTree.printTree());
        }
        assertEquals(row, goldenMap.remove(row));
        assertEquals(row, testTree.remove(row));
        assertNull(testTree.findOne(row));
        if (rowMatcher.test(row))
            --correctMatchingRows;
        assertEquals(correctMatchingRows, testTree.size(rowClosure));
        long correctSize = goldenMap.size();
        assertEquals(correctSize, testTree.size());
        assertEquals(correctSize, size(testTree.find(null, null)));
        assertNoLocks();
    }
}
Also used : BPlusIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO) TreeMap(java.util.TreeMap) Predicate(java.util.function.Predicate) AtomicLong(java.util.concurrent.atomic.AtomicLong) BPlusTree(org.apache.ignite.internal.processors.cache.persistence.tree.BPlusTree) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

BPlusIO (org.apache.ignite.internal.processors.cache.persistence.tree.io.BPlusIO)3 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Predicate (java.util.function.Predicate)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 Integer.parseInt (java.lang.Integer.parseInt)1 String.format (java.lang.String.format)1 FileChannel (java.nio.channels.FileChannel)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 Collections.singletonList (java.util.Collections.singletonList)1