KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > index > MemoryIndex


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.core.index;
12
13 import org.eclipse.jdt.core.search.*;
14 import org.eclipse.jdt.internal.core.util.*;
15 import org.eclipse.jdt.internal.compiler.util.HashtableOfObject;
16 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
17 import org.eclipse.jdt.internal.compiler.util.SimpleSet;
18
19 public class MemoryIndex {
20
21 public int NUM_CHANGES = 100; // number of separate document changes... used to decide when to merge
22

23 SimpleLookupTable docsToReferences; // document paths -> HashtableOfObject(category names -> set of words)
24
SimpleWordSet allWords; // save space by locally interning the referenced words, since an indexer can generate numerous duplicates
25
String JavaDoc lastDocumentName;
26 HashtableOfObject lastReferenceTable;
27
28 MemoryIndex() {
29     this.docsToReferences = new SimpleLookupTable(7);
30     this.allWords = new SimpleWordSet(7);
31 }
32 void addDocumentNames(String JavaDoc substring, SimpleSet results) {
33     // assumed the disk index already skipped over documents which have been added/changed/deleted
34
Object JavaDoc[] paths = this.docsToReferences.keyTable;
35     Object JavaDoc[] referenceTables = this.docsToReferences.valueTable;
36     if (substring == null) { // add all new/changed documents
37
for (int i = 0, l = referenceTables.length; i < l; i++)
38             if (referenceTables[i] != null)
39                 results.add(paths[i]);
40     } else {
41         for (int i = 0, l = referenceTables.length; i < l; i++)
42             if (referenceTables[i] != null && ((String JavaDoc) paths[i]).startsWith(substring, 0))
43                 results.add(paths[i]);
44     }
45 }
46 void addIndexEntry(char[] category, char[] key, String JavaDoc documentName) {
47     HashtableOfObject referenceTable;
48     if (documentName.equals(this.lastDocumentName))
49         referenceTable = this.lastReferenceTable;
50     else {
51         // assumed a document was removed before its reindexed
52
referenceTable = (HashtableOfObject) this.docsToReferences.get(documentName);
53         if (referenceTable == null)
54             this.docsToReferences.put(documentName, referenceTable = new HashtableOfObject(3));
55         this.lastDocumentName = documentName;
56         this.lastReferenceTable = referenceTable;
57     }
58
59     SimpleWordSet existingWords = (SimpleWordSet) referenceTable.get(category);
60     if (existingWords == null)
61         referenceTable.put(category, existingWords = new SimpleWordSet(1));
62
63     existingWords.add(this.allWords.add(key));
64 }
65 HashtableOfObject addQueryResults(char[][] categories, char[] key, int matchRule, HashtableOfObject results) {
66     // assumed the disk index already skipped over documents which have been added/changed/deleted
67
// results maps a word -> EntryResult
68
Object JavaDoc[] paths = this.docsToReferences.keyTable;
69     Object JavaDoc[] referenceTables = this.docsToReferences.valueTable;
70     if (matchRule == (SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE) && key != null) {
71         nextPath : for (int i = 0, l = referenceTables.length; i < l; i++) {
72             HashtableOfObject categoryToWords = (HashtableOfObject) referenceTables[i];
73             if (categoryToWords != null) {
74                 for (int j = 0, m = categories.length; j < m; j++) {
75                     SimpleWordSet wordSet = (SimpleWordSet) categoryToWords.get(categories[j]);
76                     if (wordSet != null && wordSet.includes(key)) {
77                         if (results == null)
78                             results = new HashtableOfObject(13);
79                         EntryResult result = (EntryResult) results.get(key);
80                         if (result == null)
81                             results.put(key, result = new EntryResult(key, null));
82                         result.addDocumentName((String JavaDoc) paths[i]);
83                         continue nextPath;
84                     }
85                 }
86             }
87         }
88     } else {
89         for (int i = 0, l = referenceTables.length; i < l; i++) {
90             HashtableOfObject categoryToWords = (HashtableOfObject) referenceTables[i];
91             if (categoryToWords != null) {
92                 for (int j = 0, m = categories.length; j < m; j++) {
93                     SimpleWordSet wordSet = (SimpleWordSet) categoryToWords.get(categories[j]);
94                     if (wordSet != null) {
95                         char[][] words = wordSet.words;
96                         for (int k = 0, n = words.length; k < n; k++) {
97                             char[] word = words[k];
98                             if (word != null && Index.isMatch(key, word, matchRule)) {
99                                 if (results == null)
100                                     results = new HashtableOfObject(13);
101                                 EntryResult result = (EntryResult) results.get(word);
102                                 if (result == null)
103                                     results.put(word, result = new EntryResult(word, null));
104                                 result.addDocumentName((String JavaDoc) paths[i]);
105                             }
106                         }
107                     }
108                 }
109             }
110         }
111     }
112     return results;
113 }
114 boolean hasChanged() {
115     return this.docsToReferences.elementSize > 0;
116 }
117 void remove(String JavaDoc documentName) {
118     if (documentName.equals(this.lastDocumentName)) {
119         this.lastDocumentName = null;
120         this.lastReferenceTable = null;
121     }
122     this.docsToReferences.put(documentName, null);
123 }
124 boolean shouldMerge() {
125     return this.docsToReferences.elementSize >= NUM_CHANGES;
126 }
127 }
128
Popular Tags