Mercurial > hg > blitz_stable
comparison src/org/dancres/blitz/meta/RegistryImpl.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.meta; | |
2 | |
3 import java.io.Serializable; | |
4 import java.io.IOException; | |
5 import java.io.FileNotFoundException; | |
6 import java.io.File; | |
7 | |
8 import java.util.logging.*; | |
9 | |
10 import com.sleepycat.je.DatabaseException; | |
11 import com.sleepycat.je.Database; | |
12 import com.sleepycat.je.DatabaseEntry; | |
13 import com.sleepycat.je.OperationStatus; | |
14 import com.sleepycat.je.DatabaseConfig; | |
15 | |
16 import org.dancres.blitz.disk.Disk; | |
17 import org.dancres.blitz.disk.DiskTxn; | |
18 import org.dancres.blitz.disk.Syncable; | |
19 | |
20 import org.dancres.blitz.Logging; | |
21 | |
22 import org.dancres.util.ObjectTransformer; | |
23 | |
24 class RegistryImpl implements Registry { | |
25 private Database theDb; | |
26 private String theName; | |
27 | |
28 static Logger theLogger = | |
29 Logging.newLogger("org.dancres.blitz.meta.Registry"); | |
30 | |
31 RegistryImpl(String aName, Initializer anInitializer) throws IOException { | |
32 theName = aName; | |
33 | |
34 try { | |
35 // theDb.set_flags(Db.DB_REVSPLITOFF); | |
36 | |
37 DiskTxn myTxn = DiskTxn.newStandalone(); | |
38 | |
39 DatabaseConfig myConfig = new DatabaseConfig(); | |
40 | |
41 try { | |
42 if (exists(aName)) { | |
43 theDb = Disk.newDb(myTxn.getDbTxn(), getDbNameFor(aName), | |
44 myConfig); | |
45 } else { | |
46 myConfig.setAllowCreate(true); | |
47 theDb = Disk.newDb(myTxn.getDbTxn(), getDbNameFor(aName), | |
48 myConfig); | |
49 | |
50 if (anInitializer != null) | |
51 anInitializer.execute(getAccessor(myTxn)); | |
52 } | |
53 } finally { | |
54 myTxn.commit(true); | |
55 } | |
56 | |
57 } catch (FileNotFoundException aFNFE) { | |
58 theLogger.log(Level.SEVERE, "File not found", aFNFE); | |
59 try { | |
60 theDb.close(); | |
61 } catch (DatabaseException aDbe) { | |
62 // Don't care anymore | |
63 } | |
64 | |
65 throw new IOException("File not found"); | |
66 } catch (DatabaseException aDBE) { | |
67 theLogger.log(Level.SEVERE, "DatabaseException", aDBE); | |
68 try { | |
69 theDb.close(); | |
70 } catch (DatabaseException aDbe) { | |
71 // Don't care anymore | |
72 } | |
73 throw new IOException("DatabaseException"); | |
74 } | |
75 } | |
76 | |
77 public RegistryAccessor getAccessor() throws IOException { | |
78 return new RegistryAccessorImpl(this); | |
79 } | |
80 | |
81 public RegistryAccessor getAccessor(DiskTxn aTxn) throws IOException { | |
82 return new RegistryAccessorImpl(this, aTxn); | |
83 } | |
84 | |
85 Serializable load(DiskTxn aTxn, byte[] aKey) throws IOException { | |
86 DatabaseEntry myKey = new DatabaseEntry(aKey); | |
87 DatabaseEntry myData = new DatabaseEntry(); | |
88 | |
89 try { | |
90 OperationStatus myResult = | |
91 theDb.get(aTxn.getDbTxn(), myKey, myData, null); | |
92 | |
93 if (myResult.equals(OperationStatus.NOTFOUND)) | |
94 return null; | |
95 else { | |
96 try { | |
97 return ObjectTransformer.toObject(myData.getData()); | |
98 } catch (Exception anE) { | |
99 theLogger.log(Level.SEVERE, "Failed to unpack metadata", | |
100 anE); | |
101 throw new IOException("Failed to unpack metadata"); | |
102 } | |
103 } | |
104 } catch (Exception anE) { | |
105 theLogger.log(Level.SEVERE, "Failed to load metadata", | |
106 anE); | |
107 throw new IOException("Failed to load metadata"); | |
108 } | |
109 } | |
110 | |
111 byte[] loadRaw(DiskTxn aTxn, byte[] aKey) throws IOException { | |
112 DatabaseEntry myKey = new DatabaseEntry(aKey); | |
113 DatabaseEntry myData = new DatabaseEntry(); | |
114 | |
115 try { | |
116 OperationStatus myResult = | |
117 theDb.get(aTxn.getDbTxn(), myKey, myData, null); | |
118 | |
119 if (myResult.equals(OperationStatus.NOTFOUND)) | |
120 return null; | |
121 else | |
122 return myData.getData(); | |
123 } catch (DatabaseException aDbe) { | |
124 theLogger.log(Level.SEVERE, "Got DatabaseException", aDbe); | |
125 throw new IOException("Got Dbe"); | |
126 } | |
127 } | |
128 | |
129 void save(DiskTxn aTxn, byte[] aKey, Serializable anObject) | |
130 throws IOException { | |
131 | |
132 DatabaseEntry myKey = new DatabaseEntry(aKey); | |
133 | |
134 byte[] myFlatObject = null; | |
135 | |
136 try { | |
137 myFlatObject = ObjectTransformer.toByte(anObject); | |
138 } catch (Exception anE) { | |
139 theLogger.log(Level.SEVERE, "Failed to pack metadata", | |
140 anE); | |
141 throw new IOException("Failed to pack metadata"); | |
142 } | |
143 | |
144 DatabaseEntry myData = new DatabaseEntry(myFlatObject); | |
145 | |
146 try { | |
147 theDb.put(aTxn.getDbTxn(), myKey, myData); | |
148 } catch (DatabaseException aDbe) { | |
149 theLogger.log(Level.SEVERE, "Got DatabaseException", aDbe); | |
150 throw new IOException("Got Dbe"); | |
151 } | |
152 } | |
153 | |
154 MetaIterator readAll(DiskTxn aTxn) throws IOException { | |
155 return new MetaIteratorImpl(theDb, aTxn); | |
156 } | |
157 | |
158 void delete(DiskTxn aTxn, byte[] aKey) throws IOException { | |
159 | |
160 try { | |
161 theDb.delete(aTxn.getDbTxn(), new DatabaseEntry(aKey)); | |
162 } catch (DatabaseException aDbe) { | |
163 theLogger.log(Level.SEVERE, "Got DatabaseException", aDbe); | |
164 throw new IOException("Got Dbe"); | |
165 } | |
166 } | |
167 | |
168 public void close() throws IOException { | |
169 try { | |
170 theDb.close(); | |
171 } catch (DatabaseException aDbe) { | |
172 theLogger.log(Level.SEVERE, "Got DatabaseException", aDbe); | |
173 throw new IOException("Got Dbe"); | |
174 } | |
175 } | |
176 | |
177 void delete() throws IOException { | |
178 Disk.deleteDb(getDbNameFor(theName)); | |
179 } | |
180 | |
181 static boolean exists(String aName) { | |
182 return Disk.dbExists(getDbNameFor(aName)); | |
183 } | |
184 | |
185 private static String getDbNameFor(String aRegName) { | |
186 return aRegName + "-meta"; | |
187 } | |
188 } |