You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by su...@apache.org on 2012/08/02 14:56:06 UTC

svn commit: r1368440 - in /incubator/stanbol/branches/contenthub-two-layered-structure: commons/semanticindex/servicesapi/src/main/java/org/apache/stanbol/commons/semanticindex/store/ contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenth...

Author: suat
Date: Thu Aug  2 12:56:06 2012
New Revision: 1368440

URL: http://svn.apache.org/viewvc?rev=1368440&view=rev
Log:
STANBOL-498: Improved revision management, fixed some small bugs related with the operations dealing with the underlying database table keeping the revision specific data

Modified:
    incubator/stanbol/branches/contenthub-two-layered-structure/commons/semanticindex/servicesapi/src/main/java/org/apache/stanbol/commons/semanticindex/store/EpochException.java
    incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/RevisionManager.java
    incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/StoreDBManager.java

Modified: incubator/stanbol/branches/contenthub-two-layered-structure/commons/semanticindex/servicesapi/src/main/java/org/apache/stanbol/commons/semanticindex/store/EpochException.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/commons/semanticindex/servicesapi/src/main/java/org/apache/stanbol/commons/semanticindex/store/EpochException.java?rev=1368440&r1=1368439&r2=1368440&view=diff
==============================================================================
--- incubator/stanbol/branches/contenthub-two-layered-structure/commons/semanticindex/servicesapi/src/main/java/org/apache/stanbol/commons/semanticindex/store/EpochException.java (original)
+++ incubator/stanbol/branches/contenthub-two-layered-structure/commons/semanticindex/servicesapi/src/main/java/org/apache/stanbol/commons/semanticindex/store/EpochException.java Thu Aug  2 12:56:06 2012
@@ -1,22 +1,30 @@
 package org.apache.stanbol.commons.semanticindex.store;
 
-
 /**
- * Indicates that the active Epoch of an IndexingSource is different from the
- * requested one.
- *
+ * Indicates that the active Epoch of an IndexingSource is different from the requested one.
+ * 
  */
 public class EpochException extends RuntimeException {
 
-	/**
-	 * default serial version UID
-	 */
-	private static final long serialVersionUID = 1L;
-
-	public EpochException(IndexingSource<?> source, long activeEpoch, long requestedEpoch) {
-		super(String.format("The Epoch %s was requested but the %s '%s' uses %s as active Epoch",
-				requestedEpoch,source.getClass().getSimpleName(),source.getName(),activeEpoch));
-	}
-
-
+    /**
+     * default serial version UID
+     */
+    private static final long serialVersionUID = 1L;
+
+    private long requestedEpoch;
+
+    private long activeEpoch;
+
+    public EpochException(IndexingSource<?> source, long activeEpoch, long requestedEpoch) {
+        super(String.format("The Epoch %s was requested but the %s '%s' uses %s as active Epoch",
+            requestedEpoch, source.getClass().getSimpleName(), source.getName(), activeEpoch));
+    }
+
+    public long getRequestEpoch() {
+        return this.requestedEpoch;
+    }
+
+    public long getActiveEpoch() {
+        return this.activeEpoch;
+    }
 }

Modified: incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/RevisionManager.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/RevisionManager.java?rev=1368440&r1=1368439&r2=1368440&view=diff
==============================================================================
--- incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/RevisionManager.java (original)
+++ incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/RevisionManager.java Thu Aug  2 12:56:06 2012
@@ -65,7 +65,8 @@ public class RevisionManager {
 
     private String SELECT_MORECHANGES = "SELECT id, revision FROM %s WHERE revision >= ? ORDER BY revision ASC";
 
-    private String SELECT_EPOCH = "SELECT * FROM " + StoreDBManager.EPOCH_TABLE_NAME + " WHERE tableName = ?";
+    private String SELECT_EPOCH = "SELECT epoch FROM " + StoreDBManager.EPOCH_TABLE_NAME
+                                  + " WHERE tableName = ?";
 
     private String INSERT_EPOCH = "INSERT INTO " + StoreDBManager.EPOCH_TABLE_NAME
                                   + " (epoch, tableName) values (?, ?)";
@@ -86,9 +87,10 @@ public class RevisionManager {
      *            {@code contentItemID} is stored
      * @param contentItemID
      *            ID of the {@link ContentItem} of which revision to be updated
+     * @return the new revision
      * @throws StoreException
      */
-    public <Item> void updateRevision(Store<Item> store, String contentItemID) throws StoreException {
+    public <Item> long updateRevision(Store<Item> store, String contentItemID) throws StoreException {
         // get connection
         Connection con = dbManager.getConnection();
         String revisionTableName = getStoreID(store);
@@ -136,6 +138,7 @@ public class RevisionManager {
             if (updatedRecordNum != 1) {
                 log.warn("Unexpected number of updated records: {}, should be 1", updatedRecordNum);
             }
+            return newRevision;
         } catch (SQLException e) {
             log.error("Failed to update revision", e);
             throw new StoreException("Failed to update revision", e);
@@ -167,6 +170,7 @@ public class RevisionManager {
         // get connection
         Connection con = dbManager.getConnection();
         String revisionTableName = getStoreID(store);
+        batchSize = batchSize == Integer.MAX_VALUE ? batchSize - 1 : batchSize;
 
         // check existence of record for the given content item id
         PreparedStatement ps = null;
@@ -237,6 +241,37 @@ public class RevisionManager {
         }
     }
 
+    public <Item> long getEpoch(Store<Item> store) throws StoreException {
+        // get connection
+        Connection con = dbManager.getConnection();
+
+        PreparedStatement ps = null;
+        ResultSet rs = null;
+        long epoch;
+        try {
+            ps = con.prepareStatement(SELECT_EPOCH);
+            ps.setString(1, getStoreID(store));
+            rs = ps.executeQuery();
+
+            if (rs.next()) {
+                epoch = rs.getLong(1);
+            } else {
+                log.error(String.format("There is not an epoch record for the Store: %s", store.getName()));
+                throw new StoreException(String.format("There is not an epoch record for the Store: %s",
+                    store.getName()));
+            }
+
+        } catch (SQLException e) {
+            log.error("Failed to execute query", e);
+            throw new StoreException("Failed to execute query", e);
+        } finally {
+            dbManager.closeResultSet(rs);
+            dbManager.closeStatement(ps);
+            dbManager.closeConnection(con);
+        }
+        return epoch;
+    }
+
     /**
      * Updates the epoch of the given {@link Store} with the {@link System#currentTimeMillis()}
      * 
@@ -322,7 +357,7 @@ public class RevisionManager {
      */
     public <Item> void initializeRevisionTables(Store<Item> store) throws StoreException {
         // initialize tables if not already
-        dbManager.createRevisionTable(store.getName());
+        dbManager.createRevisionTable(getStoreID(store));
 
         // add initial epoch for the store
         updateEpoch(store, true);

Modified: incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/StoreDBManager.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/StoreDBManager.java?rev=1368440&r1=1368439&r2=1368440&view=diff
==============================================================================
--- incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/StoreDBManager.java (original)
+++ incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/revisionmanager/src/main/java/org/apache/stanbol/contenthub/revisionmanager/StoreDBManager.java Thu Aug  2 12:56:06 2012
@@ -81,9 +81,8 @@ public class StoreDBManager {
         try {
             // try to create revision table
             if (!existsTable(tableName)) {
-                String createRevisionTable = "CREATE TABLE " + tableName + " (" + "id VARCHAR("
-                                             + MAX_ID_LENGTH + ") NOT NULL PRIMARY KEY,"
-                                             + "revision BIGINT NOT NULL)";
+                String createRevisionTable = "CREATE TABLE " + tableName + " (id VARCHAR(" + MAX_ID_LENGTH
+                                             + ") NOT NULL PRIMARY KEY," + "revision BIGINT NOT NULL)";
                 stmt = con.createStatement();
                 stmt.executeUpdate(createRevisionTable);
                 log.info("Revision table created for {}.", tableName);
@@ -106,7 +105,7 @@ public class StoreDBManager {
         try {
             // try to create revision table
             if (!existsTable(EPOCH_TABLE_NAME)) {
-                String createRevisionTable = "CREATE TABLE " + EPOCH_TABLE_NAME + " (" + "tableName VARCHAR("
+                String createRevisionTable = "CREATE TABLE " + EPOCH_TABLE_NAME + " (tableName VARCHAR("
                                              + MAX_ID_LENGTH + ") NOT NULL PRIMARY KEY,"
                                              + "epoch BIGINT NOT NULL)";
                 stmt = con.createStatement();
@@ -136,9 +135,9 @@ public class StoreDBManager {
     public boolean existsTable(String tableName) throws StoreException {
         boolean exists = false;
         ResultSet rs = null;
-        Connection con = getConnection();
+        Connection con = null;
         try {
-            con = DriverManager.getConnection(DB_URL);
+            con = getConnection();
             DatabaseMetaData meta = con.getMetaData();
             rs = meta.getTables(null, null, null, new String[] {"TABLE"});
             while (rs.next()) {
@@ -166,31 +165,12 @@ public class StoreDBManager {
      * @throws StoreException
      */
     public void truncateTable(String tableName) throws StoreException {
-        boolean exists = false;
-        ResultSet rs = null;
-        Connection con = getConnection();
-        try {
-            con = DriverManager.getConnection(DB_URL);
-            DatabaseMetaData meta = con.getMetaData();
-            rs = meta.getTables(null, null, null, new String[] {"TABLE"});
-            while (rs.next()) {
-                if (rs.getString("TABLE_NAME").equalsIgnoreCase(tableName)) {
-                    exists = true;
-                    break;
-                }
-            }
-        } catch (SQLException e) {
-            log.error("Failed to check existence of the table: {}", tableName);
-            throw new StoreException(String.format("Failed to check existence of the table: %s", tableName),
-                    e);
-        } finally {
-            closeResultSet(rs);
-            closeConnection(con);
-        }
+        boolean exists = existsTable(tableName);
         if (!exists) {
             throw new IllegalArgumentException(String.format("There is no table having name: %s", tableName));
         }
         String truncateTable = "TRUNCATE TABLE " + tableName;
+        Connection con = getConnection();
         Statement stmt = null;
         try {
             stmt = con.createStatement();
@@ -198,6 +178,9 @@ public class StoreDBManager {
         } catch (SQLException e) {
             log.error("Failed to truncate table: {}", tableName, e);
             throw new StoreException(String.format("Failed to truncate table: %s", tableName), e);
+        } finally {
+            closeStatement(stmt);
+            closeConnection(con);
         }
         log.debug("Table having name: {} has been truncated", tableName);
     }