1
/*******************************************************************************
2
* Copyright (c) 2002, 2008 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
9
* Rational Software - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.core.model;
15
import java.util.HashMap;
18
import org.eclipse.cdt.core.model.ICElement;
19
import org.eclipse.cdt.core.model.IOpenable;
20
import org.eclipse.cdt.internal.core.util.OverflowingLRUCache;
24
* The cache of C elements to their respective info.
26
* This class is similar to the JDT CModelCache class.
28
public class CModelCache {
29
public static final int PROJ_CACHE_SIZE = 50;
30
public static final int FOLDER_CACHE_SIZE = 500;
31
public static final int FILE_CACHE_SIZE = 2000;
32
public static final int CHILDREN_CACHE_SIZE = FILE_CACHE_SIZE * 20;
35
* Cache of open projects and roots.
37
protected Map<ICElement, Object> projectAndRootCache;
40
* Cache of open containers
42
protected Map<ICElement, Object> folderCache;
45
* Cache of open translation unit files
47
protected OverflowingLRUCache<IOpenable, Object> fileCache;
50
* Cache of children of C elements
52
protected Map<ICElement, Object> childrenCache;
54
public CModelCache() {
55
this.projectAndRootCache = new HashMap<ICElement, Object>(PROJ_CACHE_SIZE);
56
this.folderCache = new HashMap<ICElement, Object>(FOLDER_CACHE_SIZE);
57
this.fileCache = new ElementCache<Object>(FILE_CACHE_SIZE);
58
this.childrenCache = new HashMap<ICElement, Object>(CHILDREN_CACHE_SIZE); // average 20 children per openable
61
public double openableFillingRatio() {
62
return this.fileCache.fillingRatio();
66
* Returns the info for the element.
68
public Object getInfo(ICElement element) {
69
switch (element.getElementType()) {
70
case ICElement.C_MODEL:
71
case ICElement.C_PROJECT:
72
return this.projectAndRootCache.get(element);
73
case ICElement.C_CCONTAINER:
74
return this.folderCache.get(element);
75
case ICElement.C_ARCHIVE:
76
case ICElement.C_BINARY:
77
case ICElement.C_UNIT:
78
return this.fileCache.get(element);
80
return this.childrenCache.get(element);
85
* Returns the info for this element without
86
* disturbing the cache ordering.
88
protected Object peekAtInfo(ICElement element) {
89
switch (element.getElementType()) {
90
case ICElement.C_MODEL:
91
case ICElement.C_PROJECT:
92
return this.projectAndRootCache.get(element);
93
case ICElement.C_CCONTAINER:
94
return this.folderCache.get(element);
95
case ICElement.C_ARCHIVE:
96
case ICElement.C_BINARY:
97
case ICElement.C_UNIT:
98
return this.fileCache.peek((IOpenable) element);
100
return this.childrenCache.get(element);
105
* Remember the info for the element.
107
protected void putInfo(ICElement element, Object info) {
108
switch (element.getElementType()) {
109
case ICElement.C_MODEL:
110
case ICElement.C_PROJECT:
111
this.projectAndRootCache.put(element, info);
113
case ICElement.C_CCONTAINER:
114
this.folderCache.put(element, info);
116
case ICElement.C_ARCHIVE:
117
case ICElement.C_BINARY:
118
case ICElement.C_UNIT:
119
this.fileCache.put((IOpenable)element, info);
122
this.childrenCache.put(element, info);
126
* Removes the info of the element from the cache.
128
protected void removeInfo(ICElement element) {
129
switch (element.getElementType()) {
130
case ICElement.C_MODEL:
131
case ICElement.C_PROJECT:
132
this.projectAndRootCache.remove(element);
134
case ICElement.C_CCONTAINER:
135
this.folderCache.remove(element);
137
case ICElement.C_ARCHIVE:
138
case ICElement.C_BINARY:
139
case ICElement.C_UNIT:
140
this.fileCache.remove((IOpenable)element);
143
this.childrenCache.remove(element);