Commits

Jan Lahoda  committed 85f88be

Add actual index size into info response.

  • Participants
  • Parent commits c12e293

Comments (0)

Files changed (2)

File remoting/server/web/base.web.api/src/org/netbeans/modules/jackpot30/backend/base/CategoryStorage.java

 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import org.apache.lucene.analysis.KeywordAnalyzer;
 import org.codeviation.pojson.Pojson;
 import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
-import org.netbeans.modules.parsing.lucene.LuceneIndexFactory;
 import org.netbeans.modules.parsing.lucene.support.Index;
 import org.netbeans.modules.parsing.lucene.support.IndexManager;
 import org.openide.filesystems.FileObject;
         }
         return null;
     }
+
+
+    private long getSize() {
+        long result = 0;
+
+        for (Enumeration<? extends FileObject> en = getCacheRoot().getChildren(true); en.hasMoreElements(); ) {
+            FileObject f = en.nextElement();
+
+            if (f.isData()) {
+                result += f.getSize();
+            }
+        }
+
+        return result;
+    }
+
+    private AtomicReference<String> info = new AtomicReference<String>();
+
+    public String getInfo() {
+        String result = info.get();
+
+        if (result != null) return result;
+
+        FileObject infoFile = getCacheRoot().getFileObject("info");
+        String content;
+        try {
+            content = infoFile != null ? infoFile.asText("UTF-8") : "{}";
+        } catch (IOException ex) {
+            Logger.getLogger(CategoryStorage.class.getName()).log(Level.SEVERE, null, ex);
+            content = "{}";
+        }
+        Map<String, Object> infoData = Pojson.load(HashMap.class, content);
+
+        if (!infoData.containsKey("indexSize")) {
+            infoData.put("indexSize", getSize());
+        }
+
+        info.set(result = Pojson.save(infoData));
+
+        return result;
+    }
 }

File remoting/server/web/base.web.api/src/org/netbeans/modules/jackpot30/backend/base/api/API.java

     @GET
     @Path("/info")
     @Produces("text/plain")
-    public Response info(@QueryParam("path") String segment) throws IOException {
+    public String info(@QueryParam("path") String segment) throws IOException {
         CategoryStorage cat = CategoryStorage.forId(segment);
 
-        if (cat == null) {
-            return Response.status(Status.NOT_FOUND).build();
-        } else {
-            FileObject info = cat.getCacheRoot().getFileObject("info");
-            String content = info != null ? info.asText("UTF-8") : "{}";
-            return Response.ok().entity(content).build();
-        }
+        return cat.getInfo();
     }
 
 }