1 package net.obsearch.ambient;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10
11 import org.apache.log4j.Logger;
12
13 import net.obsearch.Index;
14 import net.obsearch.OB;
15 import net.obsearch.exception.AlreadyFrozenException;
16 import net.obsearch.exception.IllegalIdException;
17 import net.obsearch.exception.NotFrozenException;
18 import net.obsearch.exception.OBException;
19 import net.obsearch.exception.OBStorageException;
20 import net.obsearch.exception.OutOfRangeException;
21 import net.obsearch.exception.PivotsUnavailableException;
22 import net.obsearch.storage.OBStoreFactory;
23
24
25 import com.thoughtworks.xstream.XStream;
26 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
27 import com.thoughtworks.xstream.io.xml.XppDriver;
28 import com.thoughtworks.xstream.io.xml.xppdom.Xpp3Dom;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public abstract class AbstractAmbient<O extends OB, I extends Index<O>> implements Ambient < O, I> {
67
68 private static Logger logger = Logger.getLogger(AbstractAmbient.class);
69
70
71
72
73
74 private I index;
75
76
77
78
79 private File directory;
80
81
82
83
84 protected OBStoreFactory fact;
85
86
87
88
89
90
91
92
93 protected AbstractAmbient(I index, File directory) throws AlreadyFrozenException, FileNotFoundException, OBStorageException,
94 NotFrozenException, IllegalAccessException, InstantiationException,
95 OBException, IOException{
96 if(index.isFrozen()){
97 throw new AlreadyFrozenException();
98 }
99 this.index = index;
100 this.directory = directory;
101 initIndex();
102 }
103
104
105
106
107
108
109 protected AbstractAmbient(File directory) throws FileNotFoundException, OBStorageException,
110 NotFrozenException, IllegalAccessException, InstantiationException,
111 OBException, IOException{
112 XStream xstream = new XStream(new XppDriver());
113 FileInputStream fs = new FileInputStream(metadataFile(directory));
114 BufferedInputStream bf = new BufferedInputStream(fs);
115 logger.debug("Reading seed");
116 index = (I) xstream.fromXML(bf);
117 logger.debug("Seed read!");
118 this.directory = directory;
119 initIndex();
120 }
121
122
123
124
125
126
127
128
129
130
131
132 private void initIndex() throws FileNotFoundException, OBStorageException,
133 NotFrozenException, IllegalAccessException, InstantiationException,
134 OBException, IOException{
135 File dbDirectory = dataFile(directory);
136 fact = createFactory(dbDirectory);
137 dbDirectory.mkdirs();
138 index.init(fact);
139 }
140
141 public void close() throws OBException{
142 index.close();
143 }
144
145
146
147
148 public I getIndex(){
149 return index;
150 }
151
152
153
154
155 public void freeze() throws IOException, AlreadyFrozenException,
156 IllegalIdException, IllegalAccessException, InstantiationException,
157 OBStorageException, OutOfRangeException, OBException, PivotsUnavailableException{
158 index.freeze();
159
160 XStream xstream = new XStream();
161
162 FileOutputStream fs = new FileOutputStream( metadataFile(directory) );
163 BufferedOutputStream bf = new BufferedOutputStream(fs);
164 xstream.toXML(index, bf);
165 }
166
167
168
169
170 private File metadataFile(File directory){
171 return new File(directory,
172 METADATA_FILENAME);
173 }
174
175
176
177
178
179
180 private File dataFile(File directory){
181 return new File(directory, "data");
182 }
183
184
185
186
187
188
189
190 protected abstract OBStoreFactory createFactory(File factoryDirectory) throws OBStorageException;
191
192 @Override
193 public OBStoreFactory getFactory() {
194
195 return fact;
196 }
197
198
199 }