[Registry-dev] svn commit r19887 - in trunk/registry/modules/core/src/main/java/org/wso2/registry: . jdbc/dao jdbc/filecache

chathura at wso2.com chathura at wso2.com
Wed Jul 23 01:35:49 PDT 2008


Author: chathura
Date: Wed Jul 23 01:35:49 2008
New Revision: 19887
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=19887

Log:

Improved the content delete. Usage of content is considered before deleting the content, as multiple resource may share the same content in the database.



Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileData.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileManager.java

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java?rev=19887&r1=19886&r2=19887&view=diff
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java	Wed Jul 23 01:35:49 2008
@@ -504,7 +504,7 @@
     public void setContentStream(InputStream contentStream) throws RegistryException {
 
         if (fileBasedContentID != null) {
-            FileManager.getInstance().activateAutoDelete(fileBasedContentID);
+            FileManager.getInstance().decrementActiveUsage(fileBasedContentID);
         }
 
         fileBasedContentID = UUIDGenerator.generateUUID();
@@ -655,7 +655,7 @@
 
     public void discard() {
         if (fileBasedContentID != null) {
-            FileManager.getInstance().activateAutoDelete(fileBasedContentID);
+            FileManager.getInstance().decrementActiveUsage(fileBasedContentID);
         }
     }
 }

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java?rev=19887&r1=19886&r2=19887&view=diff
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/ResourceDAO.java	Wed Jul 23 01:35:49 2008
@@ -439,8 +439,8 @@
         deleteResource(resourceID);
 
         if (contentID != null) {
-            // todo: add usage counter column to CONTENT table and delete content only if it is 0
-            //deleteContent(contentID);
+            // delete content checks whether the content usage is zero before deleting the content
+            deleteContent(contentID);
         }
     }
 
@@ -1244,6 +1244,10 @@
      */
     private void deleteContent(String contentID) throws RegistryException {
 
+        if (getContentUsage(contentID) > 0) {
+            return;
+        }
+
         Connection conn = Transaction.getConnection();
 
         try {
@@ -1262,6 +1266,35 @@
         }
     }
 
+    private long getContentUsage(String contentID) throws RegistryException {
+
+        Connection conn = Transaction.getConnection();
+
+        String sql = "SELECT COUNT(CONTENT_ID) AS USAGE FROM RESOURCE WHERE CONTENT_ID=?";
+
+        try {
+            PreparedStatement ps = conn.prepareStatement(sql);
+            ps.setString(1, contentID);
+
+            long usage = 0;
+
+            ResultSet result = ps.executeQuery();
+            if (result.next()) {
+                usage = result.getLong("USAGE");
+            }
+            ps.close();
+
+            return usage;
+
+        } catch (SQLException e) {
+
+            String msg = "Failed to get content usage for the content ID " +
+                    contentID + ". " + e.getMessage();
+            log.error(msg, e);
+            throw new RegistryException(msg, e);
+        }
+    }
+
     public void setPath(String resourceID, String newPath) throws RegistryException {
 
         Connection conn = Transaction.getConnection();

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileData.java
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileData.java?rev=19887&r1=19886&r2=19887&view=diff
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileData.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileData.java	Wed Jul 23 01:35:49 2008
@@ -24,10 +24,9 @@
     private long connections;
 
     /**
-     * If set to true, this file data instance will be deleted eventually after connections
-     * become zero.
+     * Number of active (non discarded) resources using this content.
      */
-    private boolean autoDelete;
+    private long activeResourceCount;
 
     /**
      * Time at which the connections become 0.
@@ -58,17 +57,26 @@
     public void decrementConnection() {
         connections--;
 
-        if (connections <= 0) {
+        if (connections <= 0 && activeResourceCount <= 0) {
             idleFrom = System.currentTimeMillis();
         }
     }
 
-    public boolean isAutoDelete() {
-        return autoDelete;
+    public long getActiveResourceCount() {
+        return activeResourceCount;
     }
 
-    public void setAutoDelete(boolean autoDelete) {
-        this.autoDelete = autoDelete;
+    public void incrementActiveResource() {
+        activeResourceCount++;
+        idleFrom = -1;
+    }
+
+    public void decrementActiveResource() {
+        activeResourceCount--;
+
+        if (connections <= 0 && activeResourceCount <= 0) {
+            idleFrom = System.currentTimeMillis();
+        }
     }
 
     public long getIdleFrom() {

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileManager.java
URL: http://wso2.org/svn/browse/wso2/trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileManager.java?rev=19887&r1=19886&r2=19887&view=diff
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileManager.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/filecache/FileManager.java	Wed Jul 23 01:35:49 2008
@@ -99,7 +99,7 @@
 
         FileData fileData = fileDataMap.get(contentID);
         if (fileData != null) {
-            fileData.setAutoDelete(false);
+            fileData.incrementActiveResource();
             return true;
         }
 
@@ -142,7 +142,7 @@
 
                     // we increment the file data only after obtaining the input stream from file
                     fileData = new FileData(tempFile.getPath());
-                    fileData.setAutoDelete(false); // turn off auto delete
+                    fileData.incrementActiveResource();
                     fileDataMap.put(contentID, fileData);
 
                 } catch (IOException e) {
@@ -292,7 +292,7 @@
                 String contentID = contentIDs.next();
 
                 FileData fileData = fileDataMap.get(contentID);
-                if (fileData.isAutoDelete() && fileData.getConnections() <= 0 &&
+                if (fileData.getActiveResourceCount() <= 0 && fileData.getConnections() <= 0 &&
                         (currentTime - fileData.getIdleFrom()) >= MAXIMUM_IDLE_TIME_FOR_FILE) {
 
                     // We can't call deleteFileBasedContent(...) here, since it will remove the
@@ -333,10 +333,10 @@
         }
     }
 
-    public void activateAutoDelete(String contentID) {
+    public void decrementActiveUsage(String contentID) {
         FileData fileData = fileDataMap.get(contentID);
         if (fileData != null) {
-            fileData.setAutoDelete(true);
+            fileData.decrementActiveResource();
         }
     }
 



More information about the Registry-dev mailing list