alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.mdr.persistence.btreeimpl.btreeindex;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.netbeans.mdr.persistence.StorageException;
import org.netbeans.mdr.persistence.Storage;
import org.netbeans.mdr.persistence.MOFID;
import org.netbeans.mdr.persistence.btreeimpl.btreestorage.*;

/**
 * In-memory page source for unit testing
 */
public class BtreeCacheSource implements BtreePageSource {

    private MDRCache		cache;
    private int		pageSize;
    private EntryTypeInfo	pageIdInfo;
    private byte[]	noPageId;
    private long 	nextID = 0;
    private BtreeStorage     storage;

    public BtreeCacheSource(MDRCache cache, int pageSize, BtreeStorage storage)
			    throws StorageException {

	this.cache = cache;
	this.pageSize = pageSize;
        this.storage = storage;
	pageIdInfo = 
		EntryTypeInfo.getEntryTypeInfo(Storage.EntryType.MOFID, storage);
	noPageId = this.storage.getMOFIDData (BtreeFactory.nullMOFID);
    }

    public BtreePage getPage(byte[] pageId, Btree btree) throws StorageException {
        MOFID pageMofId = this.storage.readMOFIDData (new ByteArrayInputStream (pageId));
        return (BtreePage) cache.get(pageMofId);
    }

    public BigKeyPage newBigKeyPage(Btree btree) throws StorageException {
        BigKeyPage page = new BigKeyPage();
	initNewPage(page, btree);
	return page;
    }

    public BtreePage newPage(Btree btree) throws StorageException {

	BtreePage	page;

	page = btree.pageFactory();
	initNewPage(page, btree);
	return page;
    }

    private void initNewPage(BtreePage page, Btree btree) throws StorageException {
	MOFID pageMOFID = new MOFID(this.storage);
        byte[] mofidBytes = this.storage.getMOFIDData (pageMOFID);
	page.init(btree, mofidBytes, new byte[pageSize], true);
	cache.put(pageMOFID, page);
	cache.setDirty(pageMOFID);
    }

    public void unpinPage(BtreePage page) {
        /* nothing we need to do here */
    }

    public void unpinPage(BigKeyPage page) {
        /* nothing we need to do here */
    }

    public void dirtyPage(BtreePage page) throws StorageException {
        MOFID dirtyPageMOFId = this.storage.readMOFIDData (new ByteArrayInputStream(page.pageId));
        cache.setDirty(dirtyPageMOFId);
    }

    public BtreePage getRootPage(Btree btree) throws StorageException {
        return newPage(btree);
    }

    public EntryTypeInfo getPageIdInfo() {
        return pageIdInfo;
    }

    public int getPageIdLength() {
        return pageIdInfo.getLength();
    }

    public int getPageSize() {
        return pageSize;
    }

    /**
     * Set the passed-in pageId to contain the special value noPageId
     */
    public void setNoPage(byte[] pageId) {

        System.arraycopy(noPageId, 0, pageId, 0, pageId.length);
    }

    /**
     * Test whether the passed-in pageId contains the special value noPageId
     */
    public boolean isNoPage(byte[] pageId) {
	
	for (int i = 0; i < pageId.length; i++) {
	    if (pageId[i] != noPageId[i]) {
	        return false;
	    }
	}
	return true;
    }

    public synchronized long getNextMofid() {
		nextID++;
		return nextID;
    }

    public String getMofidPrefix() {
	    return "01234567-89AB-CDEF-GHIJ-KLMNOPQRSTUV";
    }
    
    public BtreeStorage getStorage() {
        return storage;
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.