You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2007/05/24 05:54:28 UTC

svn commit: r541146 - in /jackrabbit/trunk/jackrabbit-core/src/main: java/org/apache/jackrabbit/core/persistence/bundle/ java/org/apache/jackrabbit/core/persistence/bundle/util/ resources/org/apache/jackrabbit/core/persistence/bundle/

Author: jukka
Date: Wed May 23 20:54:25 2007
New Revision: 541146

URL: http://svn.apache.org/viewvc?view=rev&rev=541146
Log:
JCR-889: PostgreSQL bundle support contributed by Miguel Angel Jiménez Sampedro.

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/PostgreSQLPersistenceManager.java   (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/PostgreSQLNameIndex.java   (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/resources/org/apache/jackrabbit/core/persistence/bundle/postgresql.ddl

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/PostgreSQLPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/PostgreSQLPersistenceManager.java?view=auto&rev=541146
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/PostgreSQLPersistenceManager.java (added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/PostgreSQLPersistenceManager.java Wed May 23 20:54:25 2007
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.persistence.bundle;
+
+import org.apache.jackrabbit.core.NodeId;
+import org.apache.jackrabbit.core.persistence.PMContext;
+import org.apache.jackrabbit.core.persistence.bundle.util.DbNameIndex;
+import org.apache.jackrabbit.core.persistence.bundle.util.NodePropBundle;
+import org.apache.jackrabbit.core.persistence.bundle.util.PostgreSQLNameIndex;
+import org.apache.jackrabbit.core.persistence.bundle.util.TrackingInputStream;
+import org.apache.jackrabbit.core.state.ItemStateException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.DataInputStream;
+import java.io.InputStream;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Extends the {@link BundleDbPersistenceManager} by PostgreSQL specific code.
+ * <p/>
+ * Configuration:<br>
+ * <ul>
+ * <li>&lt;param name="{@link #setBundleCacheSize(String) bundleCacheSize}" value="8"/>
+ * <li>&lt;param name="{@link #setConsistencyCheck(String) consistencyCheck}" value="false"/>
+ * <li>&lt;param name="{@link #setMinBlobSize(String) minBlobSize}" value="16384"/>
+ * <li>&lt;param name="{@link #setDriver(String) driver}" value="org.postgresql.Driver"/>
+ * <li>&lt;param name="{@link #setUrl(String) url}" value=""/>
+ * <li>&lt;param name="{@link #setUser(String) user}" value=""/>
+ * <li>&lt;param name="{@link #setPassword(String) password}" value=""/>
+ * <li>&lt;param name="{@link #setSchema(String) schema}" value="postgresql"/>
+ * <li>&lt;param name="{@link #setSchemaObjectPrefix(String) schemaObjectPrefix}" value=""/>
+ * <li>&lt;param name="{@link #setErrorHandling(String) errorHandling}" value=""/>
+ * </ul>
+ */
+public class PostgreSQLPersistenceManager extends BundleDbPersistenceManager {
+
+    /**
+     * Logger instance.
+     */
+    private static final Logger log =
+        LoggerFactory.getLogger(PostgreSQLPersistenceManager.class);
+
+    /**
+     * {@inheritDoc}
+     */
+    public void init(PMContext context) throws Exception {
+        // init default values
+        if (getDriver() == null) {
+            setDriver("org.postgresql.Driver");
+        }
+        if (getSchema() == null) {
+            setSchema("postgresql");
+        }
+        super.init(context);
+    }
+
+    /**
+     * Retruns a new instance of a DbNameIndex.
+     * @return a new instance of a DbNameIndex.
+     * @throws java.sql.SQLException if an SQL error occurs.
+     */
+    protected DbNameIndex createDbNameIndex() throws SQLException {
+        return new PostgreSQLNameIndex(con, schemaObjectPrefix);
+    }
+
+    /**
+     * returns the storage model
+     * @return the storage model
+     */
+    public int getStorageModel() {
+        return SM_LONGLONG_KEYS;
+    }
+
+    protected synchronized NodePropBundle loadBundle(NodeId id)
+            throws ItemStateException {
+        PreparedStatement stmt = bundleSelect;
+        try {
+            setKey(stmt, id.getUUID(), 1);
+            ResultSet rs = stmt.executeQuery();
+            try {
+                if (rs.next()) {
+                    InputStream input = rs.getBinaryStream(1);
+                    try {
+                        TrackingInputStream cin = new TrackingInputStream(input);
+                        DataInputStream din = new DataInputStream(cin);
+                        NodePropBundle bundle = binding.readBundle(din, id);
+                        bundle.setSize(cin.getPosition());
+                        return bundle;
+                    } finally {
+                        input.close();
+                    }
+                } else {
+                    return null;
+                }
+            } finally {
+                rs.close();
+            }
+        } catch (Exception e) {
+            String msg = "failed to read bundle: " + id + ": " + e;
+            log.error(msg);
+            throw new ItemStateException(msg, e);
+        } finally {
+            resetStatement(stmt);
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/PostgreSQLPersistenceManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/PostgreSQLNameIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/PostgreSQLNameIndex.java?view=auto&rev=541146
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/PostgreSQLNameIndex.java (added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/PostgreSQLNameIndex.java Wed May 23 20:54:25 2007
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.persistence.bundle.util;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Same as {@link DbNameIndex} but does not make use of the
+ * {@link java.sql.Statement#RETURN_GENERATED_KEYS} feature as it is not
+ * provided by the underlying database driver for PostgreSQL.
+ */
+public class PostgreSQLNameIndex extends DbNameIndex {
+
+    protected PreparedStatement generatedKeySelect;
+
+    public PostgreSQLNameIndex(Connection con, String schemaObjectPrefix)
+            throws SQLException {
+        super(con, schemaObjectPrefix);
+    }
+
+    /**
+     * Inits this index and prepares the statements.
+     *
+     * @param con the jdbc connection
+     * @param schemaObjectPrefix the prefix for table names
+     * @throws SQLException if the statements cannot be prepared.
+     */
+    protected void init(Connection con, String schemaObjectPrefix)
+            throws SQLException {
+        nameSelect = con.prepareStatement(
+                "select NAME from " + schemaObjectPrefix + "NAMES where ID = ?");
+        indexSelect = con.prepareStatement(
+                "select ID from " + schemaObjectPrefix + "NAMES where NAME = ?");
+        nameInsert = con.prepareStatement(
+                "insert into " + schemaObjectPrefix + "NAMES (NAME) values (?)");
+        generatedKeySelect = con.prepareStatement(
+                "select currval('" + schemaObjectPrefix + "NAMES_ID_SEQ')");
+    }
+
+    /**
+     * Inserts a string into the database and returns the new index.
+     * <p/>
+     * Instead of using the {@link java.sql.Statement#RETURN_GENERATED_KEYS}
+     * feature, the newly inserted index is retrieved by a 2nd select statement.
+     *
+     * @param string the string to insert
+     * @return the new index.
+     */
+    protected int insertString(String string) {
+        // assert index does not exist
+        PreparedStatement stmt = nameInsert;
+        try {
+            stmt.setString(1, string);
+            stmt.executeUpdate();
+            return getGeneratedKey();
+        } catch (Exception e) {
+            throw new IllegalStateException("Unable to insert index: " + e);
+        } finally {
+            resetStatement(stmt);
+        }
+    }
+
+    /**
+     * Retrieves the last assigned key from the database.
+     * @return the index.
+     */
+    protected int getGeneratedKey() {
+        PreparedStatement stmt = generatedKeySelect;
+        try {
+            ResultSet rs = stmt.executeQuery();
+            try {
+                if (!rs.next()) {
+                    return -1;
+                } else {
+                    return rs.getInt(1);
+                }
+            } finally {
+                rs.close();
+            }
+        } catch (Exception e) {
+            throw new IllegalStateException("Unable to read index: " + e);
+        } finally {
+            resetStatement(stmt);
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/PostgreSQLNameIndex.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-core/src/main/resources/org/apache/jackrabbit/core/persistence/bundle/postgresql.ddl
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/resources/org/apache/jackrabbit/core/persistence/bundle/postgresql.ddl?view=auto&rev=541146
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/resources/org/apache/jackrabbit/core/persistence/bundle/postgresql.ddl (added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/resources/org/apache/jackrabbit/core/persistence/bundle/postgresql.ddl Wed May 23 20:54:25 2007
@@ -0,0 +1,5 @@
+create table ${schemaObjectPrefix}BUNDLE (NODE_ID_HI bigint not null, NODE_ID_LO bigint not null, BUNDLE_DATA bytea not null, PRIMARY KEY (NODE_ID_HI, NODE_ID_LO))
+create table ${schemaObjectPrefix}REFS (NODE_ID_HI bigint not null, NODE_ID_LO bigint not null, REFS_DATA bytea not null, PRIMARY KEY (NODE_ID_HI, NODE_ID_LO))
+create table ${schemaObjectPrefix}BINVAL (BINVAL_ID varchar(64) not null, BINVAL_DATA bytea not null)
+create unique index ${schemaObjectPrefix}BINVAL_IDX on ${schemaObjectPrefix}BINVAL (BINVAL_ID)
+create table ${schemaObjectPrefix}NAMES (ID SERIAL PRIMARY KEY, NAME varchar(255) not null)