001: /*
002: * $Id: BTreeMetaData.java,v 1.10 2005/12/20 18:32:42 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.util;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.lang.ref.Reference;
046: import java.lang.ref.SoftReference;
047:
048: import org.axiondb.engine.rowcollection.IntHashMap;
049:
050: /**
051: * Manages the meta-data for a BTree.
052: *
053: * @version $Revision: 1.10 $ $Date: 2005/12/20 18:32:42 $
054: * @author Rodney Waldhoff
055: */
056: class BTreeMetaData {
057: public BTreeMetaData(File dataDirectory, String name,
058: int minimizationFactor, BaseBTree root) throws IOException,
059: ClassNotFoundException {
060: _dataDirectory = dataDirectory;
061: _name = name.toUpperCase();
062: _minimizationFactor = minimizationFactor;
063: _root = root;
064: _fileIdCounter = new CounterFile();
065: _nodeCache = new IntHashMap();
066: _dirtyNodes = new IntHashMap();
067: }
068:
069: public boolean isRoot(BaseBTree node) {
070: return node == _root;
071: }
072:
073: public void assignFileId(BaseBTree node) {
074: node.setFileId(_fileIdCounter.increment());
075: }
076:
077: public File getDataDirectory() {
078: return _dataDirectory;
079: }
080:
081: public void setDataDirectory(File dir) {
082: _dataDirectory = dir;
083: }
084:
085: public String getName() {
086: return _name;
087: }
088:
089: public int getMinimizationFactor() {
090: return _minimizationFactor;
091: }
092:
093: public int incrementCounter() {
094: return _fileIdCounter.increment();
095: }
096:
097: public void saveCounter() throws IOException {
098: _fileIdCounter.save(getCounterFile());
099: }
100:
101: public void loadCounter() throws IOException {
102: _fileIdCounter = CounterFile.load(getCounterFile());
103: }
104:
105: public File getCounterFile() {
106: return new File(getDataDirectory(), getName() + ".CTR");
107: }
108:
109: public final File getFileById(int fileid) {
110: return new File(getDataDirectory(), getName() + "." + fileid);
111: }
112:
113: public void cacheNode(int fileId, BaseBTree tree) {
114: _nodeCache.put(fileId, new SoftReference(tree));
115: }
116:
117: public BaseBTree getCachedNode(int fileId) {
118: Reference ref = (Reference) (_nodeCache.get(fileId));
119: if (null != ref) {
120: BaseBTree node = (BaseBTree) ref.get();
121: if (null == node) {
122: _nodeCache.remove(fileId);
123: }
124: return node;
125: }
126: return null;
127: }
128:
129: public BaseBTree getCachedNode(Integer fileId) {
130: return getCachedNode(fileId.intValue());
131: }
132:
133: public void cacheNode(Integer fileId, BaseBTree tree) {
134: cacheNode(fileId.intValue(), tree);
135: }
136:
137: public void setDirty(BaseBTree tree) {
138: setDirty(tree.getFileId(), tree);
139: }
140:
141: public void setDirty(int fileId, BaseBTree tree) {
142: _dirtyNodes.put(fileId, tree);
143: }
144:
145: public void setDirty(Integer fileId, BaseBTree tree) {
146: setDirty(fileId.intValue(), tree);
147: }
148:
149: public void setAllClean() {
150: _dirtyNodes.clear();
151: _nodeCache.clear();
152:
153: // Delete each node file.
154: for (int i = 0, I = _fileIdCounter.current(); i < I; i++) {
155: File file = getFileById(i);
156: if (file.exists()) {
157: file.delete();
158: }
159: }
160:
161: _fileIdCounter = new CounterFile();
162: }
163:
164: public IntHashMap.ValueIterator getDirtyNodes() {
165: return _dirtyNodes.valueIterator();
166: }
167:
168: public int getDirtyNodeCount() {
169: return _dirtyNodes.size();
170: }
171:
172: public boolean hasDirtyNodes() {
173: return !_dirtyNodes.isEmpty();
174: }
175:
176: private BaseBTree _root;
177: private CounterFile _fileIdCounter;
178: private String _name;
179: private File _dataDirectory;
180: private int _minimizationFactor = 1000;
181: private IntHashMap _nodeCache;
182: private IntHashMap _dirtyNodes;
183:
184: }
|