[sikuli] 148/385: completely revised the purging of entries from Image cache

Gilles Filippini pini at moszumanska.debian.org
Sun Jun 29 19:26:02 UTC 2014


This is an automated email from the git hooks/post-receive script.

pini pushed a commit to tag upstream/1.1.0_beta1
in repository sikuli.

commit 24e2075ecc8584c436d5f2e99e7d47322644f29c
Author: Raimund Hocke <rmhdevelop at me.com>
Date:   Thu Jan 9 16:23:36 2014 +0100

    completely revised the purging of entries from Image cache
---
 API/src/main/java/org/sikuli/script/Image.java     | 74 +++++++++++++++++++---
 API/src/main/java/org/sikuli/script/ImagePath.java | 54 ++++++++++------
 2 files changed, 99 insertions(+), 29 deletions(-)

diff --git a/API/src/main/java/org/sikuli/script/Image.java b/API/src/main/java/org/sikuli/script/Image.java
index f21f50a..7e9ef3c 100644
--- a/API/src/main/java/org/sikuli/script/Image.java
+++ b/API/src/main/java/org/sikuli/script/Image.java
@@ -74,7 +74,8 @@ public class Image {
   }
 
   private static List<Image> images = Collections.synchronizedList(new ArrayList<Image>());
-  private static List<Image> purgeList = Collections.synchronizedList(new ArrayList<Image>());
+  private static List<Image> imagePurgeList = Collections.synchronizedList(new ArrayList<Image>());
+  private static List<String> imageNamePurgeList = Collections.synchronizedList(new ArrayList<String>());
   private static Map<URL, Image> imageFiles = Collections.synchronizedMap(new HashMap<URL, Image>());
   private static Map<String, URL> imageNames = Collections.synchronizedMap(new HashMap<String, URL>());
   private static int KB = 1024;
@@ -87,6 +88,7 @@ public class Image {
   private String imageName = null;
   private boolean imageIsText = false;
   private boolean imageIsAbsolute = false;
+  private boolean beSilent = false;
   private String filepath = null;
   private URL fileURL = null;
   private BufferedImage bimg = null;
@@ -131,11 +133,20 @@ public class Image {
    * @return the image
    */
   public static Image create(String fName) {
-    Image img = get(fName);
+    Image img = get(fName, false);
+    return createImageValidate(img);
+  }
+
+  public static Image createThumbNail(String fName) {
+    Image img = get(fName, true);
     return createImageValidate(img);
   }
 
   protected static Image get(String fname) {
+    return get(fname, false);
+  }
+
+  protected static Image get(String fname, boolean silent) {
     if (fname == null || fname.isEmpty()) {
       return null;
     }
@@ -178,6 +189,7 @@ public class Image {
     if (img == null) {
       img = new Image(fileName, fURL);
       img.setIsAbsolute(absoluteFileName);
+      img.setBeSilent(silent);
     }
     return img;
   }
@@ -224,7 +236,9 @@ public class Image {
       try {
         bimg = ImageIO.read(fileURL);
       } catch (Exception e) {
-        log(-1, "FatalError: image could not be loaded from " + filepath);
+        if (!beSilent) {
+          log(-1, "could not be loaded from " + filepath);
+        }
         return null;
       }
       if (imageName != null) {
@@ -275,6 +289,7 @@ public class Image {
    * already loaded image with same url is reused (reference) and taken from
    * cache
    *
+   * @param url
    * @return the image
    */
   public static Image create(URL url) {
@@ -375,6 +390,10 @@ public class Image {
       log(-1, "purge: not current bundlepath: " + pathURL);
       return;
     }
+    purge(pathURL);
+  }
+  
+  public static synchronized void purge(URL pathURL) {
     String pathStr = pathURL.toExternalForm();
     URL imgURL;
     Image img;
@@ -382,31 +401,64 @@ public class Image {
     Iterator<Map.Entry<URL, Image>> it = imageFiles.entrySet().iterator();
     Map.Entry<URL, Image> entry;
     Iterator<Image> bit;
-    purgeList.clear();
-
+    imagePurgeList.clear();
+    imageNamePurgeList.clear();
     while (it.hasNext()) {
       entry = it.next();
       imgURL = entry.getKey();
       if (imgURL.toExternalForm().startsWith(pathStr)) {
         log(lvl, "purge: entry: " + imgURL.toString());
-        purgeList.add(entry.getValue());
+        imagePurgeList.add(entry.getValue());
+        imageNamePurgeList.add(entry.getKey().toExternalForm());
         it.remove();
       }
     }
-    if (purgeList.size() > 0) {
+    if (!imagePurgeList.isEmpty()) {
       bit = images.iterator();
       while (bit.hasNext()) {
         img = bit.next();
-        if (purgeList.contains(img)) {
+        if (imagePurgeList.contains(img)) {
           bit.remove();
           log(lvl, "purge: bimg: " + img);
           currentMemory -= img.bsize;
         }
       }
     }
+    if (!imageNamePurgeList.isEmpty()) {
+      Iterator<Map.Entry<String, URL>> nit = imageNames.entrySet().iterator();
+      Map.Entry<String, URL> name;
+      while(nit.hasNext()) {
+        name = nit.next();
+        if(imageNamePurgeList.remove(name.getValue().toExternalForm())) {
+          nit.remove();
+        }
+      }      
+    }
     log(lvl, "After Purge (%d): Max %d MB (%d / %d %%) (%d))", 
-            purgeList.size(), (int) (maxMemory / MB), images.size(),
+            imagePurgeList.size(), (int) (maxMemory / MB), images.size(),
             (int) (100 * currentMemory / maxMemory), (int) (currentMemory / KB));
+    imagePurgeList.clear();
+    imageNamePurgeList.clear();    
+  }
+  
+  public static void dump() {
+    log(0, "--- start of Image dump ---");
+    ImagePath.printPaths();
+    log(0, "ImageFiles entries: %d", imageFiles.size());
+    Iterator<Map.Entry<URL, Image>> it = imageFiles.entrySet().iterator();
+    Map.Entry<URL, Image> entry;
+    while (it.hasNext()) {
+      entry = it.next();
+      log(lvl, entry.getKey().toExternalForm());
+    }
+    log(0, "ImageNames entries: %d", imageNames.size());
+    Iterator<Map.Entry<String, URL>> nit = imageNames.entrySet().iterator();
+    Map.Entry<String, URL> name;
+    while (nit.hasNext()) {
+      name = nit.next();
+      log(lvl, "%s (%s)", name.getKey(), name.getValue());
+    }
+    log(0, "--- end of Image dump ---");
   }
 
   /**
@@ -533,6 +585,10 @@ public class Image {
       group.addImageFacts(this, lastSeen, sim);
     }
   }
+  
+  public void setBeSilent(boolean val) {
+    beSilent = val;
+  }
 
   public BufferedImage resize(float factor) {
     int type = 0;
diff --git a/API/src/main/java/org/sikuli/script/ImagePath.java b/API/src/main/java/org/sikuli/script/ImagePath.java
index 5568cdb..9e5e083 100644
--- a/API/src/main/java/org/sikuli/script/ImagePath.java
+++ b/API/src/main/java/org/sikuli/script/ImagePath.java
@@ -7,6 +7,7 @@ import java.net.URL;
 import java.security.CodeSource;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import org.sikuli.basics.Debug;
 import org.sikuli.basics.FileManager;
@@ -223,14 +224,7 @@ public class ImagePath {
    * @return true on success, false ozherwise
    */
   public static boolean remove(String path) {
-    for (PathEntry p : imagePaths) {
-      if (!p.pathGiven.equals(path)) {
-        continue;
-      }
-      imagePaths.remove(p);
-      return true;
-    }
-    return false;
+    return remove(makePathURL(path, null).pathURL);
   }
   
   /**
@@ -240,39 +234,59 @@ public class ImagePath {
    * @return true on success, false ozherwise
    */
   public static boolean remove(URL pURL) {
-    for (PathEntry p : imagePaths) {
-      if (!p.pathGiven.equals("__PATH_URL__")) {
+    Iterator<PathEntry> it = imagePaths.iterator();
+    PathEntry p, p0;
+    p0 = imagePaths.get(0);
+    boolean success = false;
+    while (it.hasNext()) {
+      p = it.next();
+      if (!p.pathURL.toExternalForm().equals(pURL.toExternalForm())) {
         continue;
       }
-      if (!p.pathURL.toString().equals(pURL.toString())) {
-        continue;
+      it.remove();
+      Image.purge(p.pathURL);
+      success = true;
+    }
+    if (success) {
+      if (imagePaths.isEmpty()) {
+        imagePaths.add(p0);
+      } else if (!imagePaths.get(0).equals(p0)) {
+        imagePaths.add(0, p0);
       }
-      imagePaths.remove(p);
-      return true;
     }
-    return false;
+    return success;
   }
   
   /**
-   * empty list and add given path
+   * empty path list and add given path
    * 
    * @param path
-   * @return true on success, false ozherwise
+   * @return true on success, false otherwise
    */
   public static boolean reset(String path) {
     reset();
-    return add(path);
+    return setBundlePath(path);
   }
   
   /**
-   * empty list and add given path
+   * empty path list and restore entry 0 (bundlePath) 
    * 
    * @return true
    */
   public static boolean reset() {
     log(lvl, "reset");
+    for (PathEntry p : imagePaths) {
+      if (p == null) {
+        continue;
+      }
+      Image.purge(p.pathURL);
+    }
+    PathEntry bp = null;
+    if (!imagePaths.isEmpty()) {
+      bp = imagePaths.get(0);
+    }
     imagePaths.clear();
-    imagePaths.add(null);
+    imagePaths.add(bp);
     return true;
   }
   

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/sikuli.git



More information about the pkg-java-commits mailing list