KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > server > IfaIndex


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl.server;
25
26 import org.objectweb.jalisto.se.api.internal.InternalPhysicalFileAccess;
27 import org.objectweb.jalisto.se.api.JalistoProperties;
28 import org.objectweb.jalisto.se.impl.server.page.SystemPage;
29 import org.objectweb.jalisto.se.impl.InFileAddress;
30
31 import java.util.Collection JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Map JavaDoc;
34
35 public class IfaIndex {
36
37     private IfaIndex(InternalPhysicalFileAccess physicalAccess) {
38         this.physicalAccess = physicalAccess;
39         pidIndex = new Hashtable JavaDoc();
40         systemPageIndex = 0;
41         instancePageIndex = 0;
42         nodePageIndex = 0;
43         leafPageIndex = 0;
44     }
45
46     public Collection JavaDoc getAllPid() {
47         return pidIndex.keySet();
48     }
49
50     public Collection JavaDoc getAllPidIfa() {
51         return pidIndex.values();
52     }
53
54     public void setCurrentAvalaibleSystemPage(InFileAddress currentAvalaibleSystemPage) {
55         this.currentAvalaibleSystemPage = currentAvalaibleSystemPage.getClone();
56         updateStateInBase(true);
57     }
58
59     public InFileAddress getCurrentAvalaibleSystemPage() {
60         return currentAvalaibleSystemPage;
61     }
62
63     public String JavaDoc getNewInstancePageAddress() {
64         return "ip" + (instancePageIndex++);
65     }
66
67     public String JavaDoc getNewSystemPageAddress() {
68         return "sp" + (systemPageIndex++);
69     }
70
71     public String JavaDoc getNewNodePageAddress() {
72         return "no" + (nodePageIndex++);
73     }
74
75     public String JavaDoc getNewLeafPageAddress() {
76         return "le" + (leafPageIndex++);
77     }
78
79     public InFileAddress getPidIfa(Object JavaDoc pid) {
80         return (InFileAddress) pidIndex.get(pid);
81     }
82
83     public void putPidIfa(Object JavaDoc pid, InFileAddress ifa) {
84         pidIndex.put(pid, ifa);
85         physicalAccess.writeObjectInBase(OID_IFA_PID_INDEX_IFA, new Hashtable JavaDoc(pidIndex), true);
86     }
87
88     public String JavaDoc toString() {
89         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
90         sb.append("FileIndexImplBasic2 : \n");
91         sb.append("\tpid index : ").append(pidIndex).append("\n");
92         sb.append("\tsystemPageIndex : ").append(systemPageIndex).append("\n");
93         sb.append("\tinstancePageIndex : ").append(instancePageIndex).append("\n");
94         sb.append("\tcurrentAvalaibleSystemPage : ").append(currentAvalaibleSystemPage).append("\n");
95         return sb.toString();
96     }
97
98     private void init(short systemPageSize) {
99         currentAvalaibleSystemPage = new InFileAddress(getNewSystemPageAddress());
100         SystemPage firstSystemPage = new SystemPage(systemPageSize);
101         firstSystemPage.setIfa(currentAvalaibleSystemPage);
102         physicalAccess.insertFileObject(firstSystemPage);
103         updateStateInBase(false);
104         physicalAccess.writeObjectInBase(OID_IFA_PID_INDEX_IFA, new Hashtable JavaDoc(pidIndex), false);
105     }
106
107     public void getStateFromBase() {
108         pidIndex = (Map JavaDoc) physicalAccess.readFileObjectAt(OID_IFA_PID_INDEX_IFA).getData();
109         Object JavaDoc[] datas = (Object JavaDoc[]) physicalAccess.readFileObjectAt(OID_IFAINDEX_IFA).getData();
110         currentAvalaibleSystemPage = (InFileAddress) datas[0];
111         systemPageIndex = ((Integer JavaDoc) datas[1]).intValue();
112         instancePageIndex = ((Integer JavaDoc) datas[2]).intValue();
113         nodePageIndex = ((Integer JavaDoc) datas[3]).intValue();
114         leafPageIndex = ((Integer JavaDoc) datas[4]).intValue();
115     }
116
117     public void updateStateInBase(boolean isUpdate) {
118         Object JavaDoc[] datas = new Object JavaDoc[5];
119         datas[0] = currentAvalaibleSystemPage;
120         datas[1] = new Integer JavaDoc(systemPageIndex);
121         datas[2] = new Integer JavaDoc(instancePageIndex);
122         datas[3] = new Integer JavaDoc(nodePageIndex);
123         datas[4] = new Integer JavaDoc(leafPageIndex);
124         physicalAccess.writeObjectInBase(OID_IFAINDEX_IFA, datas, isUpdate);
125     }
126
127     /**
128      * ******************************* STATIC ***************************************
129      */

130
131     public static IfaIndex getAnIfaIndex(InternalPhysicalFileAccess physicalAccess, JalistoProperties properties) {
132         IfaIndex ifaIndex = new IfaIndex(physicalAccess);
133         try {
134             ifaIndex.getStateFromBase();
135         } catch (Exception JavaDoc e) {
136             ifaIndex.init(properties.getSystemPageSize());
137         }
138         return ifaIndex;
139     }
140
141
142     private InternalPhysicalFileAccess physicalAccess;
143     private InFileAddress currentAvalaibleSystemPage;
144
145     private Map JavaDoc pidIndex; // pid <-> ifa de la pageInfo corresponadnte
146

147     private int systemPageIndex;
148     private int instancePageIndex;
149     private int nodePageIndex;
150     private int leafPageIndex;
151
152
153     private static final InFileAddress OID_IFAINDEX_IFA = new InFileAddress("IFAIst");
154     private static final InFileAddress OID_IFA_PID_INDEX_IFA = new InFileAddress("pids");
155 }
156
Popular Tags