[Pydev-cvs] org.python.pydev.core/src/org/python/pydev/core/cache PyPreferencesCache.java, 1.2, 1.3
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-09-27 19:58:10
|
Update of /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/cache In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19948/src/org/python/pydev/core/cache Modified Files: PyPreferencesCache.java DiskCache.java LRUCache.java Cache.java Log Message: Synching to latest changes: Pydev <ul> <li><strong>Editor</strong>: Cursor settings no longer overridden</li> <li><strong>Code-completion</strong>: If __all__ is defined with runtime elements (and not only in a single assign statement), it's ignored for code-completion purposes</li> <li><strong>Debugger</strong>: Pythonpath the same in debug and regular modes (sys.path[0] is the same directory as the file run)</li> <li><strong>Debugger</strong>: Persist choices done in the debugger when files from the debugger are not found</li> <li><strong>Interpreter config</strong>: "email" automatically added to the "forced builtins"</li> <li><strong>Parser</strong>: Correctly recognizing absolute import with 3 or more levels</li> <li><strong>Syntax check</strong>: Option to do only on active editor</li> </ul> Also: tabs changed for spaces Index: PyPreferencesCache.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/cache/PyPreferencesCache.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PyPreferencesCache.java 23 Jan 2007 22:56:29 -0000 1.2 --- PyPreferencesCache.java 27 Sep 2008 19:57:36 -0000 1.3 *************** *** 9,70 **** public class PyPreferencesCache implements IPropertyChangeListener { ! private IPreferenceStore preferenceStore; ! private HashMap<String, Object> cache = new HashMap<String, Object>(); ! ! public PyPreferencesCache(IPreferenceStore preferenceStore) { ! this.preferenceStore = preferenceStore; ! this.preferenceStore.addPropertyChangeListener(this); ! } ! public boolean getBoolean(String key) { ! Boolean b = (Boolean) cache.get(key); ! if(b == null){ ! b = this.preferenceStore.getBoolean(key); ! cache.put(key, b); ! } ! return b; ! } ! /** * This is for a 'special case', when the value must be higher than 0 * * @param key this is the key we're interested in * @param defaultIfZeroOrLess the value to be returned if the actual value found is 0 or less ! */ ! public int getInt(String key, int defaultIfZeroOrLess) { ! Integer b = (Integer) cache.get(key); ! if(b == null || b <= 0){ ! b = this.preferenceStore.getInt(key); if(b <= 0){ b = defaultIfZeroOrLess; } ! cache.put(key, b); ! } ! return b; ! } ! ! public int getInt(String key) { ! Integer b = (Integer) cache.get(key); ! if(b == null){ ! b = this.preferenceStore.getInt(key); ! cache.put(key, b); ! } ! return b; ! } ! public void propertyChange(PropertyChangeEvent event) { ! final Object newValue = event.getNewValue(); ! cache.put(event.getProperty(), newValue); //simply override the cache (do not care about whether it is null, Boolean, etc). ! } ! /** ! * Can be used to force clearing some value from the cache. ! */ ! public void clear(String key) { ! cache.put(key, null); ! } } --- 9,70 ---- public class PyPreferencesCache implements IPropertyChangeListener { ! private IPreferenceStore preferenceStore; ! private HashMap<String, Object> cache = new HashMap<String, Object>(); ! ! public PyPreferencesCache(IPreferenceStore preferenceStore) { ! this.preferenceStore = preferenceStore; ! this.preferenceStore.addPropertyChangeListener(this); ! } ! public boolean getBoolean(String key) { ! Boolean b = (Boolean) cache.get(key); ! if(b == null){ ! b = this.preferenceStore.getBoolean(key); ! cache.put(key, b); ! } ! return b; ! } ! /** * This is for a 'special case', when the value must be higher than 0 * * @param key this is the key we're interested in * @param defaultIfZeroOrLess the value to be returned if the actual value found is 0 or less ! */ ! public int getInt(String key, int defaultIfZeroOrLess) { ! Integer b = (Integer) cache.get(key); ! if(b == null || b <= 0){ ! b = this.preferenceStore.getInt(key); if(b <= 0){ b = defaultIfZeroOrLess; } ! cache.put(key, b); ! } ! return b; ! } ! ! public int getInt(String key) { ! Integer b = (Integer) cache.get(key); ! if(b == null){ ! b = this.preferenceStore.getInt(key); ! cache.put(key, b); ! } ! return b; ! } ! public void propertyChange(PropertyChangeEvent event) { ! final Object newValue = event.getNewValue(); ! cache.put(event.getProperty(), newValue); //simply override the cache (do not care about whether it is null, Boolean, etc). ! } ! /** ! * Can be used to force clearing some value from the cache. ! */ ! public void clear(String key) { ! cache.put(key, null); ! } } Index: Cache.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/cache/Cache.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Cache.java 14 Feb 2006 15:29:41 -0000 1.1 --- Cache.java 27 Sep 2008 19:57:36 -0000 1.2 *************** *** 8,24 **** public interface Cache<Key, Val> { ! /** ! * This method returns the value for the given key. ! */ ! public Val getObj(Key o); ! /** ! * This method removes some key from the cache ! */ ! public void remove(Key key); ! /** ! * Adds some value to the cache ! */ ! public void add(Key key, Val n); } --- 8,24 ---- public interface Cache<Key, Val> { ! /** ! * This method returns the value for the given key. ! */ ! public Val getObj(Key o); ! /** ! * This method removes some key from the cache ! */ ! public void remove(Key key); ! /** ! * Adds some value to the cache ! */ ! public void add(Key key, Val n); } Index: DiskCache.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/cache/DiskCache.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** DiskCache.java 22 Apr 2006 14:05:09 -0000 1.4 --- DiskCache.java 27 Sep 2008 19:57:36 -0000 1.5 *************** *** 23,44 **** public class DiskCache extends LRUCache<String, Serializable> implements Serializable{ ! private static final long serialVersionUID = 1L; private static final boolean DEBUG = false; ! ! /** ! * This is the folder that the cache can use to persist its values ! */ ! private String folderToPersist; ! ! /** ! * The keys will be in memory all the time... only the values will come and go to the disk. ! */ ! private Set<String> keys = new HashSet<String>(); ! ! /** ! * The files persisted should have this suffix (should start with .) ! */ ! private String suffix; /** --- 23,44 ---- public class DiskCache extends LRUCache<String, Serializable> implements Serializable{ ! private static final long serialVersionUID = 1L; private static final boolean DEBUG = false; ! ! /** ! * This is the folder that the cache can use to persist its values ! */ ! private String folderToPersist; ! ! /** ! * The keys will be in memory all the time... only the values will come and go to the disk. ! */ ! private Set<String> keys = new HashSet<String>(); ! ! /** ! * The files persisted should have this suffix (should start with .) ! */ ! private String suffix; /** *************** *** 46,51 **** */ @SuppressWarnings("unchecked") ! private void readObject(ObjectInputStream aStream) throws IOException, ClassNotFoundException { ! aStream.defaultReadObject(); keys = (Set<String>) aStream.readObject(); --- 46,51 ---- */ @SuppressWarnings("unchecked") ! private void readObject(ObjectInputStream aStream) throws IOException, ClassNotFoundException { ! aStream.defaultReadObject(); keys = (Set<String>) aStream.readObject(); *************** *** 74,91 **** //the cache will be re-created in a 'clear' state } ! ! public DiskCache(int maxSize, File folderToPersist, String suffix) { ! super(maxSize); ! this.folderToPersist = REF.getFileAbsolutePath(folderToPersist); ! this.suffix = suffix; ! } ! ! ! public synchronized Serializable getObj(String key) { ! synchronized(cache){ ! Serializable v = super.getObj(key); ! if(v == null && keys.contains(key)){ ! //miss in memory... get from disk ! File file = getFileForKey(key); if(file.exists()){ v = (Serializable) REF.readFromFile(file); --- 74,91 ---- //the cache will be re-created in a 'clear' state } ! ! public DiskCache(int maxSize, File folderToPersist, String suffix) { ! super(maxSize); ! this.folderToPersist = REF.getFileAbsolutePath(folderToPersist); ! this.suffix = suffix; ! } ! ! ! public synchronized Serializable getObj(String key) { ! synchronized(cache){ ! Serializable v = super.getObj(key); ! if(v == null && keys.contains(key)){ ! //miss in memory... get from disk ! File file = getFileForKey(key); if(file.exists()){ v = (Serializable) REF.readFromFile(file); *************** *** 99,174 **** return null; } ! //put it back in memory ! super.add(key, v); ! } ! return v; ! } ! } ! private synchronized File getFileForKey(String o) { ! return new File(folderToPersist, o+suffix); ! } ! /** ! * Removes both: from the memory and from the disk ! */ ! public synchronized void remove(String key) { ! synchronized(cache){ ! if(DEBUG){ ! System.out.println("Disk cache - Removing: "+key); } ! super.remove(key); ! File fileForKey = getFileForKey(key); ! fileForKey.delete(); ! keys.remove(key); ! } ! } ! /** ! * Adds to both: the memory and the disk ! */ ! public synchronized void add(String key, Serializable n) { ! synchronized(cache){ ! super.add(key, n); ! File fileForKey = getFileForKey(key); ! if(DEBUG){ ! System.out.println("Disk cache - Adding: "+key+" file: "+fileForKey); ! } ! REF.writeToFile(n, fileForKey); ! keys.add(key); ! } ! } ! /** ! * Clear the whole cache. ! */ ! public synchronized void clear() { ! synchronized(cache){ ! for(String key : keys){ ! super.remove(key); ! File fileForKey = getFileForKey(key); ! fileForKey.delete(); ! } ! keys.clear(); ! } ! } ! /** ! * @return a copy of the keys available ! */ ! public synchronized Set<String> keys() { ! synchronized(cache){ ! return new HashSet<String>(keys); ! } ! } ! public void setFolderToPersist(String folderToPersist) { ! this.folderToPersist = folderToPersist; ! } ! public String getFolderToPersist() { ! return folderToPersist; ! } } --- 99,174 ---- return null; } ! //put it back in memory ! super.add(key, v); ! } ! return v; ! } ! } ! private synchronized File getFileForKey(String o) { ! return new File(folderToPersist, o+suffix); ! } ! /** ! * Removes both: from the memory and from the disk ! */ ! public synchronized void remove(String key) { ! synchronized(cache){ ! if(DEBUG){ ! System.out.println("Disk cache - Removing: "+key); } ! super.remove(key); ! File fileForKey = getFileForKey(key); ! fileForKey.delete(); ! keys.remove(key); ! } ! } ! /** ! * Adds to both: the memory and the disk ! */ ! public synchronized void add(String key, Serializable n) { ! synchronized(cache){ ! super.add(key, n); ! File fileForKey = getFileForKey(key); ! if(DEBUG){ ! System.out.println("Disk cache - Adding: "+key+" file: "+fileForKey); ! } ! REF.writeToFile(n, fileForKey); ! keys.add(key); ! } ! } ! /** ! * Clear the whole cache. ! */ ! public synchronized void clear() { ! synchronized(cache){ ! for(String key : keys){ ! super.remove(key); ! File fileForKey = getFileForKey(key); ! fileForKey.delete(); ! } ! keys.clear(); ! } ! } ! /** ! * @return a copy of the keys available ! */ ! public synchronized Set<String> keys() { ! synchronized(cache){ ! return new HashSet<String>(keys); ! } ! } ! public void setFolderToPersist(String folderToPersist) { ! this.folderToPersist = folderToPersist; ! } ! public String getFolderToPersist() { ! return folderToPersist; ! } } Index: LRUCache.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/cache/LRUCache.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** LRUCache.java 2 Jul 2006 17:32:08 -0000 1.4 --- LRUCache.java 27 Sep 2008 19:57:36 -0000 1.5 *************** *** 17,99 **** public class LRUCache<Key, Val> implements Cache<Key, Val>, Serializable{ ! protected int maxSize; ! private static final long serialVersionUID = 1L; ! private transient int removeEntries; ! private transient int initialMaxSize; ! ! public LRUCache(int maxSize){ ! this.maxSize = maxSize; ! cache = createMap(maxSize); ! } ! protected LinkedHashMap<Key, Val> createMap(int maxSize) { ! return new LinkedHashMap<Key,Val>(maxSize+1, .75F, true) { ! // This method is called just after a new entry has been added ! public boolean removeEldestEntry(Map.Entry eldest) { ! return size() > LRUCache.this.maxSize; ! } ! }; ! } ! ! public void stopGrowAsNeeded(){ ! synchronized(this){ ! removeEntries--; ! if(removeEntries == 0){ ! maxSize = initialMaxSize; ! Iterator<Entry<Key, Val>> iter = cache.entrySet().iterator(); ! //go to the position of the 'eldest' entries ! for (int i = 0; i < cache.size() - maxSize; i++) { ! iter.next(); ! } ! //and now remove the eldest entries ! while(cache.size() > maxSize && iter.hasNext()){ ! iter.next(); ! iter.remove(); ! } ! } ! } ! } ! ! /** ! * Can be used to stop removing oldest entries (this can be useful when doing some 'local' operations that want ! * to be faster, having the new max size passed as the new 'value'. Note that other calls to this method will ! * be ignored and will not change that limit). ! * ! * Also, the link between startGrowAsNeeded and stopGrowAsNeeded should be synchronized to avoid that one thread ! * raises and another one stopping that raise (so, use this with care). ! */ ! public void startGrowAsNeeded(int newSize){ ! if(removeEntries > 0){ ! throw new RuntimeException("There is alrdeady a new size in action. This class should be synched for this access."); ! } ! synchronized(this){ ! removeEntries++; ! if(removeEntries == 1){ ! //start ! initialMaxSize = maxSize; ! maxSize = newSize; ! } ! } ! } ! ! //Create cache protected LinkedHashMap<Key,Val> cache; ! public Val getObj(Key key) { ! return cache.get(key); ! } ! public void remove(Key key) { ! cache.remove(key); ! } ! ! public void add(Key key, Val val) { ! cache.put(key, val); ! } public void clear(){ cache.clear(); } ! } --- 17,99 ---- public class LRUCache<Key, Val> implements Cache<Key, Val>, Serializable{ ! protected int maxSize; ! private static final long serialVersionUID = 1L; ! private transient int removeEntries; ! private transient int initialMaxSize; ! ! public LRUCache(int maxSize){ ! this.maxSize = maxSize; ! cache = createMap(maxSize); ! } ! protected LinkedHashMap<Key, Val> createMap(int maxSize) { ! return new LinkedHashMap<Key,Val>(maxSize+1, .75F, true) { ! // This method is called just after a new entry has been added ! public boolean removeEldestEntry(Map.Entry eldest) { ! return size() > LRUCache.this.maxSize; ! } ! }; ! } ! ! public void stopGrowAsNeeded(){ ! synchronized(this){ ! removeEntries--; ! if(removeEntries == 0){ ! maxSize = initialMaxSize; ! Iterator<Entry<Key, Val>> iter = cache.entrySet().iterator(); ! //go to the position of the 'eldest' entries ! for (int i = 0; i < cache.size() - maxSize; i++) { ! iter.next(); ! } ! //and now remove the eldest entries ! while(cache.size() > maxSize && iter.hasNext()){ ! iter.next(); ! iter.remove(); ! } ! } ! } ! } ! ! /** ! * Can be used to stop removing oldest entries (this can be useful when doing some 'local' operations that want ! * to be faster, having the new max size passed as the new 'value'. Note that other calls to this method will ! * be ignored and will not change that limit). ! * ! * Also, the link between startGrowAsNeeded and stopGrowAsNeeded should be synchronized to avoid that one thread ! * raises and another one stopping that raise (so, use this with care). ! */ ! public void startGrowAsNeeded(int newSize){ ! if(removeEntries > 0){ ! throw new RuntimeException("There is alrdeady a new size in action. This class should be synched for this access."); ! } ! synchronized(this){ ! removeEntries++; ! if(removeEntries == 1){ ! //start ! initialMaxSize = maxSize; ! maxSize = newSize; ! } ! } ! } ! ! //Create cache protected LinkedHashMap<Key,Val> cache; ! public Val getObj(Key key) { ! return cache.get(key); ! } ! public void remove(Key key) { ! cache.remove(key); ! } ! ! public void add(Key key, Val val) { ! cache.put(key, val); ! } public void clear(){ cache.clear(); } ! } |