KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > gammaStore > DataFileManager


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: DataFileManager.java,v 1.1.2.1 2004/04/10 10:06:51 per_nyfelt Exp $
7

8 package org.ozoneDB.core.storage.gammaStore;
9
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.LinkedHashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18 import org.ozoneDB.OzoneInternalException;
19
20 /**
21  *
22  * @author leo
23  */

24 public class DataFileManager {
25     
26     private static final String JavaDoc DATAFILEPREFIX = "";
27     private static final String JavaDoc DATAFILEPOSTFIX = ".datafile";
28
29     /**
30      * <p>Used to name indexfiles. We could take any value from 2 to
31      * <code>Character.MAX_RADIX</code> (36) here, but we settle for 16 because
32      * it looks nice and nerdy. 36 would look less nice because that would
33      * inevitably lead to filenames with very nasty words in them, like
34      * <code>fuck.datafile</code> and <code>microsoft.datafile</code>.</p>
35      * <p>Before you know it, someone sues you for trademark infringement.</p>
36      * <p>Insiders joke: CAFEBABE is not a trademark, is she?</p>
37      */

38     private static final int NAMECONVERTRADIX = 16;
39
40     private LinkedHashMap JavaDoc dataFiles = new LinkedHashMap JavaDoc() {
41         
42         public boolean removeEldestEntry(Map.Entry JavaDoc entry) {
43             if (size() > getMaxOpenDataFiles()) {
44                 Storage dataFile = (Storage) entry.getValue();
45                 try {
46                     dataFile.close();
47                 } catch (IOException JavaDoc e) {
48                     throw new OzoneInternalException(e);
49                 }
50             }
51             return false;
52         }
53         
54     };
55     
56     private int maxOpenDataFiles;
57     
58     private StorageFactory dataFileFactory;
59     
60     private ObjectStreamClasses objectStreamClasses;
61     
62     public DataFileManager() {
63     }
64     
65     public void setObjectStreamClasses(ObjectStreamClasses objectStreamClasses) {
66         this.objectStreamClasses = objectStreamClasses;
67     }
68
69     public ObjectStreamClasses getObjectStreamClasses() {
70         return objectStreamClasses;
71     }
72
73     private Map JavaDoc getDataFiles() {
74         return dataFiles;
75     }
76     
77     private static String JavaDoc dataFileIdToStorageName(int dataFileId) {
78         return DATAFILEPREFIX + Integer.toString(dataFileId, NAMECONVERTRADIX) + DATAFILEPOSTFIX;
79     }
80     
81     private Storage getDataFile(int dataFileId) {
82         Storage result = (Storage) getDataFiles().get(new Integer JavaDoc(dataFileId));
83         if (result == null) {
84             try {
85                 result = dataFileFactory.createStorage(dataFileIdToStorageName(dataFileId));
86             } catch (IOException JavaDoc e) {
87                 throw new OzoneInternalException(e);
88             }
89             dataFiles.put(new Integer JavaDoc(dataFileId), result);
90         }
91         return result;
92     }
93     
94     public GammaContainer getContainer(ContainerLocation containerLocation) throws ClassNotFoundException JavaDoc {
95         try {
96             Storage dataFile = getDataFile(containerLocation.getDataFileId());
97             dataFile.seek(containerLocation.getPosition());
98             long id = dataFile.readLong();
99             int size = dataFile.readInt();
100             byte[] buf = new byte[size];
101             dataFile.readFully(buf);
102             GammaObjectInputStream in = new GammaObjectInputStream(new ByteArrayInputStream JavaDoc(buf), getObjectStreamClasses());
103             GammaContainer result = (GammaContainer) in.readObject();
104             return result;
105         } catch (IOException JavaDoc e) {
106             throw new OzoneInternalException(e);
107         }
108     }
109     
110     public void saveContainer(ContainerLocation containerLocation, GammaContainer container, int freeSize) {
111         try {
112             Storage dataFile = getDataFile(containerLocation.getDataFileId());
113             dataFile.seek(containerLocation.getPosition());
114             dataFile.writeLong(container.getObjectId().value());
115             dataFile.writeInt(freeSize);
116             ByteArrayOutputStream JavaDoc buf = new ByteArrayOutputStream JavaDoc();
117             GammaObjectOutputStream out = new GammaObjectOutputStream(buf, getObjectStreamClasses());
118             out.writeObject(container);
119             dataFile.write(buf.toByteArray());
120         } catch (IOException JavaDoc e) {
121             throw new OzoneInternalException(e);
122         }
123     }
124     
125     public int getMaxOpenDataFiles() {
126         return maxOpenDataFiles;
127     }
128     
129     public void setMaxOpenDataFiles(int maxOpenDataFiles) {
130         this.maxOpenDataFiles = maxOpenDataFiles;
131         if (getDataFiles().size() > getMaxOpenDataFiles()) {
132             Iterator JavaDoc i = getDataFiles().values().iterator();
133             for (int cnt = 0; cnt < getDataFiles().size(); cnt++) {
134                 i.next();
135             }
136             while (i.hasNext()) {
137                 Storage dataFile = (Storage) i.next();
138                 try {
139                     dataFile.close();
140                 } catch (IOException JavaDoc e) {
141                     throw new OzoneInternalException(e);
142                 }
143                 i.remove();
144             }
145         }
146     }
147     
148 }
149
Popular Tags