KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > indexing > LogReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.indexing;
12
13 import java.io.FileInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.TreeMap JavaDoc;
17
18 class LogReader {
19
20     protected FileInputStream JavaDoc in;
21     protected PageStore store;
22     protected byte[] b4;
23     protected byte[] pageBuffer;
24
25     /**
26      * Returns the Hashmap of the modified pages.
27      */

28     public static Map JavaDoc getModifiedPages(PageStore store) throws PageStoreException {
29         LogReader reader = new LogReader(store);
30         Map JavaDoc 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     /**
47      * Open a log for reading.
48      */

49     protected void open(PageStore pageStore) throws PageStoreException {
50         String JavaDoc name = pageStore.getName();
51         if (!Log.exists(name))
52             return;
53         try {
54             in = new FileInputStream JavaDoc(Log.name(name));
55         } catch (IOException JavaDoc e) {
56             throw new PageStoreException(PageStoreException.LogOpenFailure, e);
57         }
58     }
59
60     /**
61      * Closes the log.
62      */

63     protected void close() {
64         try {
65             if (in != null)
66                 in.close();
67         } catch (IOException JavaDoc e) {
68             // ignore
69
}
70         in = null;
71     }
72
73     /**
74      * Returns the Hashmap of modified pages read from the log.
75      */

76     protected Map JavaDoc getModifiedPages() throws PageStoreException {
77         Map JavaDoc modifiedPages = new TreeMap JavaDoc();
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 JavaDoc key = new Integer JavaDoc(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 JavaDoc 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 JavaDoc e) {
109             throw new PageStoreException(PageStoreException.LogReadFailure, e);
110         }
111     }
112
113 }
114
Popular Tags