<commit_before>/*
 *  Licensed 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.
 *
 *  See the NOTICE file distributed with this work for additional
 *  information regarding copyright ownership.
 */

package org.seaborne.dboe.index.bplustree;

import static org.seaborne.dboe.base.block.BlockType.BPTREE_BRANCH ;
import static org.seaborne.dboe.base.block.BlockType.BPTREE_LEAF ;
import static org.seaborne.dboe.base.block.BlockType.RECORD_BLOCK ;

import java.nio.ByteBuffer ;

import org.seaborne.dboe.base.block.Block ;
import org.seaborne.dboe.base.block.BlockConverter ;
import org.seaborne.dboe.base.block.BlockMgr ;
import org.seaborne.dboe.base.block.BlockType ;
import org.seaborne.dboe.base.buffer.PtrBuffer ;
import org.seaborne.dboe.base.buffer.RecordBuffer ;
import org.seaborne.dboe.base.page.PageBlockMgr ;

/** BPlusTreePageMgr = BPlusTreeNode manager */
public final class BPTreeNodeMgr extends PageBlockMgr<BPTreeNode>
{
    // Only "public" for external very low level tools in development to access this class.
    // Assume package access.

    public BPTreeNodeMgr(BPlusTree bpTree, BlockMgr blockMgr) {
        super(new Block2BPTreeNode(bpTree), blockMgr) ;
    }
   
    /** Allocate space for a fresh node. */
    public BPTreeNode createNode(int parent) {
        BPTreeNode n = create(BPTREE_BRANCH) ;
        n.isLeaf = false ;
        n.parent = parent ;
        return n ;
    }

    // Maybe we should not inherit but wrap.
    @Override
    public BPTreeNode getWrite(int id)
    { throw new UnsupportedOperationException("call getWrite(int, int)") ; }
    @Override
    public BPTreeNode getRead(int id)
    { throw new UnsupportedOperationException("call getRead(int, int)") ; }
    
    /** Fetch a block - fill in the parent id, which is not in the on-disk bytes */
    public BPTreeNode getRead(int id, int parent) {
        BPTreeNode n = super.getRead(id) ;
        n.parent = parent ;
        return n ;
    }

    /** Fetch a block - fill in the parent id, which is not in the on-disk bytes */
    public BPTreeNode getWrite(int id, int parent) {
        BPTreeNode n = super.getWrite(id) ;
        n.parent = parent ;
        return n ;
    }

    private static class Block2BPTreeNode implements BlockConverter<BPTreeNode>
    {
        private final BPlusTree bpTree ;

        Block2BPTreeNode(BPlusTree bpTree) { this.bpTree = bpTree ; }
        
        @Override
        public BPTreeNode createFromBlock(Block block, BlockType bType) {
            return overlay(bpTree, block, bType == RECORD_BLOCK, 0) ;
        }

        @Override
        public BPTreeNode fromBlock(Block block) {
            // synchronized - needed for multiple reader?
            synchronized (block) {
                int x = block.getByteBuffer().getInt(0) ;
                BlockType type = getType(x) ;

                if ( type != BPTREE_BRANCH && type != BPTREE_LEAF )
                    throw new BPTreeException("Wrong block type: " + type) ;
                int count = decodeCount(x) ;
                return overlay(bpTree, block, (type == BPTREE_LEAF), count) ;<commit_msg>Adjust to PageBlockMgr changes.<commit_after>/*
 *  Licensed 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.
 *
 *  See the NOTICE file distributed with this work for additional
 *  information regarding copyright ownership.
 */

package org.seaborne.dboe.index.bplustree;

import static org.seaborne.dboe.base.block.BlockType.BPTREE_BRANCH ;
import static org.seaborne.dboe.base.block.BlockType.BPTREE_LEAF ;
import static org.seaborne.dboe.base.block.BlockType.RECORD_BLOCK ;

import java.nio.ByteBuffer ;

import org.seaborne.dboe.base.block.Block ;
import org.seaborne.dboe.base.block.BlockConverter ;
import org.seaborne.dboe.base.block.BlockMgr ;
import org.seaborne.dboe.base.block.BlockType ;
import org.seaborne.dboe.base.buffer.PtrBuffer ;
import org.seaborne.dboe.base.buffer.RecordBuffer ;
import org.seaborne.dboe.base.page.PageBlockMgr ;

/** BPlusTreePageMgr = BPlusTreeNode manager */
public final class BPTreeNodeMgr extends PageBlockMgr<BPTreeNode>
{
    // Only "public" for external very low level tools in development to access this class.
    // Assume package access.

    public BPTreeNodeMgr(BPlusTree bpTree, BlockMgr blockMgr) {
        super(new Block2BPTreeNode(bpTree), blockMgr) ;
    }
   
    /** Allocate space for a fresh node. */
    public BPTreeNode createNode(int parent) {
        BPTreeNode n = create(BPTREE_BRANCH) ;
        n.isLeaf = false ;
        n.parent = parent ;
        return n ;
    }

    // Maybe we should not inherit but wrap.
    @Override
    public BPTreeNode getWrite(int id)
    { throw new UnsupportedOperationException("call getWrite(int, int)") ; }
    @Override
    public BPTreeNode getRead(int id)
    { throw new UnsupportedOperationException("call getRead(int, int)") ; }
    
    /** Fetch a block - fill in the parent id, which is not in the on-disk bytes */
    @Override
    public BPTreeNode getRead(int id, int parent) {
        BPTreeNode n = super.getRead$(id) ;
        n.parent = parent ;
        return n ;
    }

    /** Fetch a block - fill in the parent id, which is not in the on-disk bytes */
    @Override
    public BPTreeNode getWrite(int id, int parent) {
        BPTreeNode n = super.getWrite$(id) ;
        n.parent = parent ;
        return n ;
    }

    private static class Block2BPTreeNode implements BlockConverter<BPTreeNode>
    {
        private final BPlusTree bpTree ;

        Block2BPTreeNode(BPlusTree bpTree) { this.bpTree = bpTree ; }
        
        @Override
        public BPTreeNode createFromBlock(Block block, BlockType bType) {
            return overlay(bpTree, block, bType == RECORD_BLOCK, 0) ;
        }

        @Override
        public BPTreeNode fromBlock(Block block) {
            // synchronized - needed for multiple reader?
            synchronized (block) {
                int x = block.getByteBuffer().getInt(0) ;
                BlockType type = getType(x) ;

                if ( type != BPTREE_BRANCH && type != BPTREE_LEAF )
                    throw new BPTreeException("Wrong block type: " + type) ;
                int count = decodeCount(x) ;
                return overlay(bpTree, block, (type == BPTREE_LEAF), count) ;
{'fraction_non_alphanumeric': 0.0875, 'fraction_numerical': 0.0024725274725274724, 'mean_word_length': 2.766683911019141, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6651305', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2198, 'n_words': 647}