1 11 package org.eclipse.core.internal.indexing; 12 13 import java.io.FileInputStream ; 14 import java.io.IOException ; 15 import java.util.Map ; 16 import java.util.TreeMap ; 17 18 class LogReader { 19 20 protected FileInputStream in; 21 protected PageStore store; 22 protected byte[] b4; 23 protected byte[] pageBuffer; 24 25 28 public static Map getModifiedPages(PageStore store) throws PageStoreException { 29 LogReader reader = new LogReader(store); 30 Map modifiedPages = null; 31 try { 32 reader.open(store); 33 modifiedPages = reader.getModifiedPages(); 34 } finally { 35 reader.close(); 36 } 37 return modifiedPages; 38 } 39 40 public LogReader(PageStore store) { 41 this.store = store; 42 this.pageBuffer = new byte[Page.SIZE]; 43 this.b4 = new byte[4]; 44 } 45 46 49 protected void open(PageStore pageStore) throws PageStoreException { 50 String name = pageStore.getName(); 51 if (!Log.exists(name)) 52 return; 53 try { 54 in = new FileInputStream (Log.name(name)); 55 } catch (IOException e) { 56 throw new PageStoreException(PageStoreException.LogOpenFailure, e); 57 } 58 } 59 60 63 protected void close() { 64 try { 65 if (in != null) 66 in.close(); 67 } catch (IOException e) { 68 } 70 in = null; 71 } 72 73 76 protected Map getModifiedPages() throws PageStoreException { 77 Map modifiedPages = new TreeMap (); 78 if (in == null) 79 return modifiedPages; 80 Field f4 = new Field(b4); 81 readBuffer(b4); 82 int numberOfPages = f4.getInt(); 83 int recordSize = 4 + Page.SIZE; 84 if (bytesAvailable() != (numberOfPages * recordSize)) 85 return modifiedPages; 86 for (int i = 0; i < numberOfPages; i++) { 87 readBuffer(b4); 88 readBuffer(pageBuffer); 89 int pageNumber = f4.getInt(); 90 Page page = store.getPolicy().createPage(pageNumber, pageBuffer, store); 91 Integer key = new Integer (pageNumber); 92 modifiedPages.put(key, page); 93 } 94 return modifiedPages; 95 } 96 97 public void readBuffer(byte[] buffer) throws PageStoreException { 98 try { 99 in.read(buffer); 100 } catch (IOException e) { 101 throw new PageStoreException(PageStoreException.LogReadFailure, e); 102 } 103 } 104 105 protected int bytesAvailable() throws PageStoreException { 106 try { 107 return in.available(); 108 } catch (IOException e) { 109 throw new PageStoreException(PageStoreException.LogReadFailure, e); 110 } 111 } 112 113 } 114 | Popular Tags |