KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > freespace > FreespaceManager


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.inside.freespace;
22
23 import com.db4o.*;
24
25 public abstract class FreespaceManager {
26     
27     final YapFile _file;
28
29     public static final byte FM_DEFAULT = 0;
30     public static final byte FM_LEGACY_RAM = 1;
31     public static final byte FM_RAM = 2;
32     public static final byte FM_IX = 3;
33     public static final byte FM_DEBUG = 4;
34     
35     private static final int INTS_IN_SLOT = 12;
36     
37     public FreespaceManager(YapFile file){
38         _file = file;
39     }
40
41     public static byte checkType(byte systemType){
42         if(systemType == FM_DEFAULT){
43             return FM_RAM;
44         }
45         return systemType;
46     }
47     
48     public static FreespaceManager createNew(YapFile file){
49         return createNew(file, file.systemData().freespaceSystem());
50     }
51     
52     public abstract void onNew(YapFile file);
53     
54     public static FreespaceManager createNew(YapFile file, byte systemType){
55         systemType = checkType(systemType);
56         switch(systemType){
57             case FM_IX:
58                 return new FreespaceManagerIx(file);
59             default:
60                 return new FreespaceManagerRam(file);
61                 
62         }
63     }
64     
65     public static int initSlot(YapFile file){
66         int address = file.getSlot(slotLength());
67         slotEntryToZeroes(file, address);
68         return address;
69     }
70     
71     static void slotEntryToZeroes(YapFile file, int address){
72         YapWriter writer = new YapWriter(file.getSystemTransaction(), address, slotLength());
73         for (int i = 0; i < INTS_IN_SLOT; i++) {
74             writer.writeInt(0);
75         }
76         if (Debug.xbytes) {
77             writer.setID(YapConst.IGNORE_ID); // no XBytes check
78
}
79         writer.writeEncrypt();
80     }
81     
82     
83     final static int slotLength(){
84         return YapConst.INT_LENGTH * INTS_IN_SLOT;
85     }
86     
87     public abstract void beginCommit();
88     
89     final int blockSize(){
90         return _file.blockSize();
91     }
92     
93     public abstract void debug();
94     
95     final int discardLimit(){
96         return _file.configImpl().discardFreeSpace();
97     }
98     
99     public abstract void endCommit();
100     
101     public abstract int entryCount();
102
103     public abstract void free(int a_address, int a_length);
104     
105     public abstract int freeSize();
106     
107     public abstract void freeSelf();
108     
109     public abstract int getSlot(int length);
110     
111     public abstract void migrate(FreespaceManager newFM);
112     
113     public abstract void read(int freeSlotsID);
114     
115     public abstract void start(int slotAddress);
116     
117     public abstract byte systemType();
118     
119     public abstract int write(boolean shuttingDown);
120
121     public boolean requiresMigration(byte configuredSystem, byte readSystem) {
122         return (configuredSystem != 0 || readSystem == FM_LEGACY_RAM ) && (systemType() != configuredSystem);
123     }
124
125     public static void migrate(FreespaceManager oldFM, FreespaceManager newFM) {
126         oldFM.migrate(newFM);
127         oldFM.freeSelf();
128         newFM.beginCommit();
129         newFM.endCommit();
130     }
131     
132 }
133
Popular Tags