KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > indexing > SpaceMapPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.indexing;
12
13 class SpaceMapPage extends ObjectStorePage {
14     private static int[] SpaceClassSize = {7616, 6906, 6196, 5486, 4776, 4066, 3356, 2646, 1936, 1226, 516, 258, 129, 64, 32, 0};
15
16     /**
17      * Returns the guaranteed amount of free space available for a given space class.
18      */

19     public static int freeSpaceForClass(int spaceClass) {
20         return SpaceClassSize[spaceClass];
21     }
22
23     /**
24      * Creates a new page from a buffer.
25      */

26     public SpaceMapPage(int pageNumber, byte[] buffer, PageStore pageStore) {
27         super(pageNumber, buffer, pageStore);
28     }
29
30     /**
31      * Writes the contents of the page to a buffer.
32      */

33     public void toBuffer(byte[] buffer) {
34         int n = Math.min(buffer.length, pageBuffer.length());
35         System.arraycopy(pageBuffer.getByteArray(), 0, buffer, 0, n);
36     }
37
38     /**
39      * Searches a space map page in the page file for an object page
40      * that has at least "bytesNeeded" bytes free. Returns 0 if there is no
41      * object page in this space map page that meets this criteria. 0 is not a valid
42      * object page number. All page numbers that are 0 mod 8192 are space map pages.
43      */

44     // public int findObjectPageNumberForSize(int bytesNeeded) {
45
// for (int i = 1; i < SIZE; i++) { // begin at 1, 0 is the space map page
46
// int spaceClass = pageBuffer.getByte(i);
47
// int freeSpace = freeSpaceForClass(spaceClass);
48
// if (freeSpace >= bytesNeeded) return pageNumber + i;
49
// }
50
// return 0;
51
// }
52
/**
53      * Returns the guaranteed amount of free space on a page.
54      * If the page number is a space map page number, 0 is returned.
55      */

56     public int getFreeSpace(int pageNumber) {
57         int slot = pageNumber - this.pageNumber;
58         if (slot < 1 || slot >= SIZE)
59             return 0;
60         int spaceClass = pageBuffer.getByte(slot);
61         int freeSpace = freeSpaceForClass(spaceClass);
62         return freeSpace;
63     }
64
65     /**
66      * Sets the spaceClass for a given object page.
67      */

68     public void setFreeSpace(int pageNumber, int freeSpace) {
69         int slot = pageNumber - this.pageNumber;
70         if (slot < 1 || slot >= SIZE)
71             return;
72         byte spaceClass = 0;
73         while (SpaceClassSize[spaceClass] > freeSpace)
74             spaceClass++;
75         pageBuffer.put(slot, spaceClass);
76         setChanged();
77         notifyObservers();
78     }
79
80     protected void materialize() {
81     }
82 }
83
Popular Tags