abstractbundlepersistencemanager.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 705 行 · 第 1/2 页

JAVA
705 行
字号
/* * 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.slf4j.Logger;import org.slf4j.LoggerFactory;import org.apache.jackrabbit.core.fs.FileSystemResource;import org.apache.jackrabbit.core.fs.FileSystem;import org.apache.jackrabbit.core.state.ItemState;import org.apache.jackrabbit.core.state.ChangeLog;import org.apache.jackrabbit.core.state.ItemStateException;import org.apache.jackrabbit.core.state.NodeReferences;import org.apache.jackrabbit.core.state.NoSuchItemStateException;import org.apache.jackrabbit.core.state.NodeReferencesId;import org.apache.jackrabbit.core.state.PropertyState;import org.apache.jackrabbit.core.state.NodeState;import org.apache.jackrabbit.core.NodeId;import org.apache.jackrabbit.core.PropertyId;import org.apache.jackrabbit.core.NamespaceRegistryImpl;import org.apache.jackrabbit.core.nodetype.PropDefId;import org.apache.jackrabbit.core.value.InternalValue;import org.apache.jackrabbit.core.persistence.PMContext;import org.apache.jackrabbit.core.persistence.PersistenceManager;import org.apache.jackrabbit.core.persistence.bundle.util.StringIndex;import org.apache.jackrabbit.core.persistence.bundle.util.NamespaceIndex;import org.apache.jackrabbit.core.persistence.bundle.util.NodePropBundle;import org.apache.jackrabbit.core.persistence.bundle.util.BundleCache;import org.apache.jackrabbit.core.persistence.bundle.util.LRUNodeIdCache;import org.apache.jackrabbit.core.persistence.bundle.util.HashMapIndex;import org.apache.jackrabbit.name.QName;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import javax.jcr.PropertyType;/** * The <code>AbstractBundlePersistenceManager</code> acts as base for all * persistence managers that store the state in a {@link NodePropBundle}. * <p/> * The state and all property states of one node are stored together in one * record. Property values of a certain size can be store outside of the bundle. * This currently only works for binary properties. NodeReferences are not * included in the bundle since they are addressed by the target id. * <p/> * Some strings like namespaces and local names are additionally managed by * seperate indexes. only the index number is serialized to the records which * reduces the amount of memory used. * <p/> * Special treatment is performed for the properties "jcr:uuid", "jcr:primaryType" * and "jcr:mixinTypes". As they are also stored in the node state they are not * included in the bundle but generated when required. * <p/> * In order to increase performance, there are 2 caches maintained. One ist the * {@link BundleCache} that caches already loaded bundles. The other is the * {@link LRUNodeIdCache} that caches non-existent bundles. This is usefull * because a lot of {@link #exists(NodeId)} calls are issued that would result * in a useless SQL execution if the desired bundle does not exist. * <p/> * Configuration:<br> * <ul> * <li>&lt;param name="{@link #setBundleCacheSize(String) bundleCacheSize}" value="8"/> * </ul> */abstract public class AbstractBundlePersistenceManager implements         PersistenceManager, CachingPersistenceManager {    /** the cvs/svn id */    static final String CVS_ID = "$URL: http://svn.apache.org/repos/asf/jackrabbit/tags/1.3.3/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java $ $Rev: 517150 $ $Date: 2007-03-12 11:42:12 +0200 (Mon, 12 Mar 2007) $";    /** the default logger */    private static Logger log = LoggerFactory.getLogger(AbstractBundlePersistenceManager.class);    /** the prefix of a node file */    protected static final String NODEFILENAME = "n";    /** the prefix of a node references file */    protected static final String NODEREFSFILENAME = "r";    /** the name of the names-index resource */    protected static final String RES_NAME_INDEX = "/names.properties";    /** the name of the namespace-index resource */    protected static final String RES_NS_INDEX = "/namespaces.properties";    /** the index for namespaces */    private StringIndex nsIndex;    /** the index for local names */    private StringIndex nameIndex;    /** the cache of loaded bundles */    private BundleCache bundles;    /** the cahce of non-existent bundles */    private LRUNodeIdCache missing;    /** definition id of the jcr:uuid property */    private PropDefId ID_JCR_UUID;    /** definition id of the jcr:primaryType property */    private PropDefId ID_JCR_PRIMARYTYPE;    /** definition id of the jcr:mixinTypes property */    private PropDefId ID_JCR_MIXINTYPES;    /** the persistence manager context */    protected PMContext context;    /** default size of the bunlde cache */    private long bundleCacheSize = 8 * 1024 * 1024;    /**     * Returns the size of the bundlecache in megabytes.     * @return the size of the bundlecache in megabytes.     */    public String getBundleCacheSize() {        return String.valueOf(bundleCacheSize/(1024 * 1024));    }    /**     * Sets the size of the bundle cache in megabytes.     * the default is 8.     *     * @param bundleCacheSize the bundle cache size in megabytes.     */    public void setBundleCacheSize(String bundleCacheSize) {        this.bundleCacheSize = Long.parseLong(bundleCacheSize)*1024*1024;    }    /**     * Creates the folder path for the given node id that is suitable for     * storing states in a filesystem.     *     * @param buf buffer to append to or <code>null</code>     * @param id the id of the node     * @return the buffer with the appended data.     */    protected StringBuffer buildNodeFolderPath(StringBuffer buf, NodeId id) {        if (buf == null) {            buf = new StringBuffer();        }        char[] chars = id.getUUID().toString().toCharArray();        int cnt = 0;        for (int i = 0; i < chars.length; i++) {            if (chars[i] == '-') {                continue;            }            //if (cnt > 0 && cnt % 4 == 0) {            if (cnt == 2 || cnt == 4) {                buf.append(FileSystem.SEPARATOR_CHAR);            }            buf.append(chars[i]);            cnt++;        }        return buf;    }    /**     * Creates the folder path for the given property id that is suitable for     * storing states in a filesystem.     *     * @param buf buffer to append to or <code>null</code>     * @param id the id of the property     * @return the buffer with the appended data.     */    protected StringBuffer buildPropFilePath(StringBuffer buf, PropertyId id) {        if (buf == null) {            buf = new StringBuffer();        }        buildNodeFolderPath(buf, id.getParentId());        buf.append(FileSystem.SEPARATOR);        buf.append(getNsIndex().stringToIndex(id.getName().getNamespaceURI()));        buf.append('.');        buf.append(getNameIndex().stringToIndex(id.getName().getLocalName()));        return buf;    }    /**     * Creates the file path for the given property id and value index that is     * suitable for storing property values in a filesystem.     *     * @param buf buffer to append to or <code>null</code>     * @param id the id of the property     * @param i the index of the property value     * @return the buffer with the appended data.     */    protected StringBuffer buildBlobFilePath(StringBuffer buf, PropertyId id,                                             int i) {        if (buf == null) {            buf = new StringBuffer();        }        buildPropFilePath(buf, id);        buf.append('.');        buf.append(i);        return buf;    }    /**     * Creates the file path for the given node id that is     * suitable for storing node states in a filesystem.     *     * @param buf buffer to append to or <code>null</code>     * @param id the id of the node     * @return the buffer with the appended data.     */    protected StringBuffer buildNodeFilePath(StringBuffer buf, NodeId id) {        if (buf == null) {            buf = new StringBuffer();        }        buildNodeFolderPath(buf, id);        buf.append(FileSystem.SEPARATOR);        buf.append(NODEFILENAME);        return buf;    }    /**     * Creates the file path for the given references id that is     * suitable for storing reference states in a filesystem.     *     * @param buf buffer to append to or <code>null</code>     * @param id the id of the node     * @return the buffer with the appended data.     */    protected StringBuffer buildNodeReferencesFilePath(StringBuffer buf,                                                       NodeReferencesId id) {        if (buf == null) {            buf = new StringBuffer();        }        buildNodeFolderPath(buf, id.getTargetId());        buf.append(FileSystem.SEPARATOR);        buf.append(NODEREFSFILENAME);        return buf;    }    /**     * Returns the namespace index     * @return the namespace index     * @throws IllegalStateException if an error occurs.     */    public StringIndex getNsIndex() {        try {            if (nsIndex == null) {                // load name and ns index                FileSystemResource nsFile = new FileSystemResource(context.getFileSystem(), RES_NS_INDEX);                if (nsFile.exists()) {                    nsIndex = new HashMapIndex(nsFile);                } else {                    nsIndex = new NamespaceIndex((NamespaceRegistryImpl) context.getNamespaceRegistry());                }            }            return nsIndex;        } catch (Exception e) {            throw new IllegalStateException("Unable to create nsIndex." + e);        }    }    /**     * Returns the local name index     * @return the local name index     * @throws IllegalStateException if an error occurs.     */    public StringIndex getNameIndex() {        try {            if (nameIndex == null) {                nameIndex = new HashMapIndex(new FileSystemResource(context.getFileSystem(), RES_NAME_INDEX));            }            return nameIndex;        } catch (Exception e) {            throw new IllegalStateException("Unable to create nsIndex." + e);        }    }    //-----------------------------------------< CacheablePersistenceManager >--    /**     * {@inheritDoc}     */    public synchronized void onExternalUpdate(ChangeLog changes) {        Iterator iter = changes.modifiedStates();        while (iter.hasNext()) {            ItemState state = (ItemState) iter.next();            if (state.isNode()) {                bundles.remove((NodeId) state.getId());            } else {                bundles.remove(state.getParentId());            }        }        iter = changes.deletedStates();        while (iter.hasNext()) {            ItemState state = (ItemState) iter.next();            if (state.isNode()) {                bundles.remove((NodeId) state.getId());            } else {                bundles.remove(state.getParentId());            }        }        iter = changes.addedStates();        while (iter.hasNext()) {            ItemState state = (ItemState) iter.next();            if (state.isNode()) {                missing.remove((NodeId) state.getId());            } else {                missing.remove(state.getParentId());            }        }    }    //----------------------------------------------------------------< spi >---    /**     * Loads a bundle from the underlying system.     *     * @param id the node id of the bundle     * @return the loaded bunlde or <code>null</code> if the bundle does not     *         exist.     * @throws ItemStateException if an error while loading occurs.     */    protected abstract NodePropBundle loadBundle(NodeId id)            throws ItemStateException;    /**     * Checks if a bundle exists in the underlying system.     *     * @param id the node id of the bundle     * @return <code>true</code> if the bundle exists;     *         <code>false</code> otherwise.     * @throws ItemStateException if an error while checking occurs.     */    protected abstract boolean existsBundle(NodeId id)            throws ItemStateException;    /**     * Stores a bundle to the underlying system.     *     * @param bundle the bundle to store     * @throws ItemStateException if an error while storing occurs.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?