001: /*
002: * $Id: TTreeMetaData.java,v 1.1 2005/06/30 01:14:44 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 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.ext.indexes.ttree;
042:
043: import java.io.File;
044: import java.io.FileInputStream;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.io.ObjectInputStream;
048: import java.io.ObjectOutputStream;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.Map;
052:
053: import org.axiondb.AxionException;
054:
055: /**
056: * Metadata for {@link TTree}.
057: *
058: * @author Pavels Andrejevs
059: */
060: public final class TTreeMetaData {
061:
062: private static class TTreeMetaDataFile {
063:
064: public static TTreeMetaDataFile load(File file)
065: throws IOException {
066: ObjectInputStream in = null;
067: try {
068: in = new ObjectInputStream(new FileInputStream(file));
069: int value = in.readInt();
070: int rootFileId = in.readInt();
071: return new TTreeMetaDataFile(value, rootFileId);
072: } finally {
073: try {
074: in.close();
075: } catch (Exception e) {
076: }
077: }
078: }
079:
080: private int _rootFileId = 1;
081:
082: private int _treeSize = 0;
083:
084: private int _value = 0;
085:
086: public TTreeMetaDataFile() {
087: }
088:
089: private TTreeMetaDataFile(int value, int rootFileId) {
090: setValue(value);
091: setRootFileId(rootFileId);
092: }
093:
094: public int current() {
095: return _value;
096: }
097:
098: public int getRootFileId() {
099: return _rootFileId;
100: }
101:
102: public int getTreeSize() {
103: return _treeSize;
104: }
105:
106: public int increment() {
107: return _value++;
108: }
109:
110: public void save(File file) throws IOException {
111: ObjectOutputStream out = null;
112: try {
113: out = new ObjectOutputStream(new FileOutputStream(file));
114: out.writeInt(_value);
115: out.writeInt(_rootFileId);
116: } finally {
117: try {
118: out.close();
119: } catch (Exception e) {
120: }
121: }
122: }
123:
124: public void setRootFileId(int rootFileId) {
125: _rootFileId = rootFileId;
126: }
127:
128: public void setTreeSize(int treeSize) {
129: _treeSize = treeSize;
130: }
131:
132: private void setValue(int value) {
133: _value = value;
134: }
135: }
136:
137: private File _dataDirectory = null;
138: private Map _dirtyNodes = null;
139: private TTreeMetaDataFile _file;
140: private boolean _memoryOnly = true;
141: private TTreeMetaDataFile _metaDataFile = null;
142: private String _name = null;
143:
144: public TTreeMetaData(String name, File dataDir, boolean memoryOnly)
145: throws AxionException {
146: _name = name.toUpperCase();
147: _dataDirectory = dataDir;
148: _memoryOnly = memoryOnly;
149: _dirtyNodes = new HashMap();
150: loadMetaDataFile();
151: }
152:
153: public void clearDirty(AbstractTTreeNode node) {
154: clearDirty(node.getFileId());
155: }
156:
157: public File getDataDirectory() {
158: return _dataDirectory;
159: }
160:
161: public int getDirtyNodeCount() {
162: return _dirtyNodes.size();
163: }
164:
165: public Iterator getDirtyNodes() {
166: return _dirtyNodes.values().iterator();
167: }
168:
169: public TTreeMetaDataFile getFile() {
170: return _file;
171: }
172:
173: public final File getFileById(int fileid) {
174: return new File(getDataDirectory(), getName() + "." + fileid);
175: }
176:
177: public File getMetaDataFile() {
178: return new File(getDataDirectory(), getName() + ".CTR");
179: }
180:
181: public String getName() {
182: return _name;
183: }
184:
185: public int getRootFileId() {
186: return _metaDataFile.getRootFileId();
187: }
188:
189: public int getTreeSize() {
190: return _metaDataFile.getTreeSize();
191: }
192:
193: public boolean hasDirtyNodes() {
194: return !_dirtyNodes.isEmpty();
195: }
196:
197: public int incrementCounter() {
198: return _metaDataFile.increment();
199: }
200:
201: public boolean isMemoryOnly() {
202: return _memoryOnly;
203: }
204:
205: public void saveMetaDataFile() throws AxionException {
206: try {
207: _metaDataFile.save(getMetaDataFile());
208: } catch (IOException e) {
209: throw new AxionException(e);
210: }
211: }
212:
213: public void setAllClean() {
214: // Clear dirty nodes
215: _dirtyNodes.clear();
216: // Delete each node file
217: for (int i = 1; i < _metaDataFile.current(); i++) {
218: File file = getFileById(i);
219: if (file.exists()) {
220: file.delete();
221: }
222: }
223: // Delete metadata file
224: File file = getMetaDataFile();
225: if (file.exists()) {
226: file.delete();
227: }
228: newMetaDataFile();
229: }
230:
231: public void setDataDirectory(File dir) {
232: _dataDirectory = dir;
233: }
234:
235: public void setDirty(AbstractTTreeNode node) {
236: setDirty(node.getFileId(), node);
237: }
238:
239: public void setFile(TTreeMetaDataFile file) {
240: _file = file;
241: }
242:
243: public void setMemoryOnly(boolean memoryOnly) {
244: _memoryOnly = memoryOnly;
245: }
246:
247: public void setRootFileId(int rootFileId) {
248: _metaDataFile.setRootFileId(rootFileId);
249: }
250:
251: public void setTreeSize(int treeSize) {
252: _metaDataFile.setTreeSize(treeSize);
253: }
254:
255: private void clearDirty(int fileId) {
256: clearDirty(new Integer(fileId));
257: }
258:
259: private void clearDirty(Integer fileId) {
260: _dirtyNodes.remove(fileId);
261: }
262:
263: private void loadMetaDataFile() throws AxionException {
264: File file = getMetaDataFile();
265: if (file.exists()) {
266: try {
267: _metaDataFile = TTreeMetaDataFile.load(file);
268: } catch (IOException e) {
269: throw new AxionException(e);
270: }
271: } else {
272: newMetaDataFile();
273: }
274: }
275:
276: private void newMetaDataFile() {
277: _metaDataFile = new TTreeMetaDataFile();
278: _metaDataFile.increment(); // start from 1
279: }
280:
281: private void setDirty(int fileId, AbstractTTreeNode node) {
282: setDirty(new Integer(fileId), node);
283: }
284:
285: private void setDirty(Integer fileId, AbstractTTreeNode node) {
286: _dirtyNodes.put(fileId, node);
287: }
288:
289: }
|