1 17 package org.apache.excalibur.store.impl; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.Serializable ; 22 import java.util.Collections ; 23 import java.util.Enumeration ; 24 import java.util.NoSuchElementException ; 25 26 import com.coyotegulch.jisp.BTreeIndex; 27 import com.coyotegulch.jisp.BTreeIterator; 28 import com.coyotegulch.jisp.IndexedObjectDatabase; 29 import com.coyotegulch.jisp.KeyNotFound; 30 import com.coyotegulch.jisp.KeyObject; 31 32 import org.apache.avalon.framework.thread.ThreadSafe; 33 import org.apache.excalibur.store.Store; 34 35 43 public abstract class AbstractJispFilesystemStore 44 extends AbstractReadWriteStore 45 implements Store, ThreadSafe { 46 47 48 protected File m_directoryFile; 49 50 51 protected IndexedObjectDatabase m_Database; 52 53 54 protected BTreeIndex m_Index; 55 56 59 public void setDirectory(final File directory) 60 throws IOException 61 { 62 this.m_directoryFile = directory; 63 64 65 if (!this.m_directoryFile.exists()) 66 { 67 68 if (!this.m_directoryFile.mkdirs()) 69 { 70 throw new IOException ( 71 "Error creating store directory '" + this.m_directoryFile.getAbsolutePath() + "'. "); 72 } 73 } 74 75 76 if (!this.m_directoryFile.isDirectory()) 77 { 78 throw new IOException ("'" + this.m_directoryFile.getAbsolutePath() + "' is not a directory"); 79 } 80 81 82 if (!(this.m_directoryFile.canRead() && this.m_directoryFile.canWrite())) 83 { 84 throw new IOException ( 85 "Directory '" + this.m_directoryFile.getAbsolutePath() + "' is not readable/writable" 86 ); 87 } 88 } 89 90 96 protected Object doGet(Object key) 97 { 98 Object value = null; 99 100 try 101 { 102 value = m_Database.read(this.wrapKeyObject(key), m_Index); 103 if (getLogger().isDebugEnabled()) 104 { 105 if (value != null) 106 { 107 getLogger().debug("Found key: " + key); 108 } 109 else 110 { 111 getLogger().debug("NOT Found key: " + key); 112 } 113 } 114 } 115 catch (Exception e) 116 { 117 getLogger().error("get(..): Exception", e); 118 } 119 120 return value; 121 } 122 123 130 protected void doStore(Object key, Object value) 131 throws IOException 132 { 133 134 if (getLogger().isDebugEnabled()) 135 { 136 getLogger().debug("store(): Store file with key: " 137 + key.toString()); 138 getLogger().debug("store(): Store file with value: " 139 + value.toString()); 140 } 141 142 if (value instanceof Serializable ) 143 { 144 try 145 { 146 KeyObject[] keyArray = new KeyObject[1]; 147 keyArray[0] = this.wrapKeyObject(key); 148 m_Database.write(keyArray, (Serializable ) value); 149 } 150 catch (Exception e) 151 { 152 getLogger().error("store(..): Exception", e); 153 } 154 } 155 else 156 { 157 throw new IOException ("Object not Serializable"); 158 } 159 } 160 161 165 public void free() 166 { 167 } 169 170 173 protected void doFree() { 174 } 175 176 179 protected void doClear() 180 { 181 182 if (getLogger().isDebugEnabled()) 183 { 184 getLogger().debug("clear(): Clearing the database "); 185 } 186 187 try 188 { 189 190 m_Index.emptyPageCache(); 192 final BTreeIterator iter = new BTreeIterator(m_Index); 193 Object key; 194 KeyObject[] keyArray = new KeyObject[1]; 195 while(iter.isValid()) { 196 key = iter.getKey(); 197 if (key != null) { 198 keyArray[0] = (KeyObject) key; 200 m_Database.remove( keyArray ); 201 } 202 if (!iter.moveNext()){ 203 break; 204 } 205 } 206 } 207 catch (Exception ignore) 208 { 209 getLogger().error("store(..): Exception", ignore); 210 } 211 } 212 213 218 protected void doRemove(Object key) 219 { 220 if (getLogger().isDebugEnabled()) 221 { 222 getLogger().debug("remove(..) Remove item"); 223 } 224 225 try 226 { 227 KeyObject[] keyArray = new KeyObject[1]; 228 keyArray[0] = wrapKeyObject(key); 229 m_Database.remove(keyArray); 230 } 231 catch (KeyNotFound ignore) 232 { 233 } 234 catch (Exception e) 235 { 236 getLogger().error("remove(..): Exception", e); 237 } 238 } 239 240 246 protected boolean doContainsKey(Object key) 247 { 248 long res = -1; 249 250 try 251 { 252 res = m_Index.findKey(this.wrapKeyObject(key)); 253 if (getLogger().isDebugEnabled()) 254 { 255 getLogger().debug("containsKey(..): res=" + res); 256 } 257 } 258 catch (KeyNotFound ignore) 259 { 260 } 261 catch (Exception e) 262 { 263 getLogger().error("containsKey(..): Exception", e); 264 } 265 266 if (res > 0) 267 { 268 return true; 269 } 270 else 271 { 272 return false; 273 } 274 } 275 276 281 protected Enumeration doGetKeys() 282 { 283 try 284 { 285 return new BTreeObjectEnumeration(new BTreeIterator(m_Index), this); 286 } 287 catch (Exception ignore) 288 { 289 return Collections.enumeration(Collections.EMPTY_LIST); 290 } 291 } 292 293 protected int doGetSize() 294 { 295 return m_Index.count(); 296 } 297 298 304 protected KeyObject wrapKeyObject(Object key) 305 { 306 return new JispKey( key ); 307 } 308 309 312 protected KeyObject getNullKey() 313 { 314 return new JispKey().makeNullKey(); 315 } 316 317 class BTreeObjectEnumeration implements Enumeration 318 { 319 private Object m_Next; 320 private BTreeIterator m_Iterator; 321 private AbstractJispFilesystemStore m_Store; 322 323 public BTreeObjectEnumeration(BTreeIterator iterator, AbstractJispFilesystemStore store) 324 { 325 m_Iterator = iterator; 326 m_Store = store; 327 328 try 330 { 331 m_Next = m_Iterator.getKey(); 332 } 333 catch (IOException ioe) 334 { 335 m_Store.getLogger().error("store(..): Exception", ioe); 336 m_Next = null; 337 } 338 } 339 340 public boolean hasMoreElements() 341 { 342 return (m_Next != null); 343 } 344 345 public Object nextElement() throws NoSuchElementException 346 { 347 if (m_Next == null) 348 { 349 throw new NoSuchElementException (); 350 } 351 352 Object tmp = m_Next; 354 355 try 357 { 358 if (m_Iterator.moveNext()) 359 { 360 m_Next = m_Iterator.getKey(); 361 } 362 else 363 { 364 m_Next = null; 366 } 367 } 368 catch (IOException ioe) 369 { 370 m_Store.getLogger().error("store(..): Exception", ioe); 371 m_Next = null; 372 } 373 catch (ClassNotFoundException cnfe) 374 { 375 m_Store.getLogger().error("store(..): Exception", cnfe); 376 m_Next = null; 377 } 378 379 return ((JispKey) tmp).getKey(); 381 } 382 } 383 384 } 385 | Popular Tags |