abstractbundlepersistencemanager.java

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

JAVA
705 行
字号
     */    protected abstract void storeBundle(NodePropBundle bundle)            throws ItemStateException;    /**     * Deletes the bundle from the underlying system.     *     * @param bundle the bundle to destroy     *     * @throws ItemStateException if an error while destroying occurs.     */    protected abstract void destroyBundle(NodePropBundle bundle)            throws ItemStateException;    /**     * {@inheritDoc}     */    abstract public NodeReferences load(NodeReferencesId targetId)            throws NoSuchItemStateException, ItemStateException;    /**     * Deletes the node references from the undelying system.     *     * @param refs the node references to destroy.     * @throws ItemStateException if an error while destroying occurs.     */    protected abstract void destroy(NodeReferences refs)            throws ItemStateException;    /**     * Stores a node references to the underlying system.     *     * @param refs the node references to store.     * @throws ItemStateException if an error while storing occurs.     */    protected abstract void store(NodeReferences refs)            throws ItemStateException;    //-------------------------------------------------< PersistenceManager >---    /**     * {@inheritDoc}     *     * Initializes the internal structures of this abstract persistence manager.     */    public void init(PMContext context) throws Exception {        this.context = context;        // init bundle cache        bundles = new BundleCache(bundleCacheSize);        missing = new LRUNodeIdCache();        // init prop defs        if (context.getNodeTypeRegistry() != null) {            ID_JCR_UUID = context.getNodeTypeRegistry().getEffectiveNodeType(QName.MIX_REFERENCEABLE).getApplicablePropertyDef(                    QName.JCR_UUID, PropertyType.STRING, false).getId();            ID_JCR_PRIMARYTYPE = context.getNodeTypeRegistry().getEffectiveNodeType(QName.NT_BASE).getApplicablePropertyDef(                    QName.JCR_PRIMARYTYPE, PropertyType.NAME, false).getId();            ID_JCR_MIXINTYPES = context.getNodeTypeRegistry().getEffectiveNodeType(QName.NT_BASE).getApplicablePropertyDef(                    QName.JCR_MIXINTYPES, PropertyType.NAME, true).getId();        }    }    /**     * {@inheritDoc}     *     * Loads the state via the appropriate NodePropBundle.     */    public synchronized NodeState load(NodeId id)            throws NoSuchItemStateException, ItemStateException {        NodePropBundle bundle = getBundle(id);        if (bundle == null) {            throw new NoSuchItemStateException(id.toString());        }        return bundle.createNodeState(this);    }    /**     * {@inheritDoc}     *     * Loads the state via the appropriate NodePropBundle.     */    public synchronized PropertyState load(PropertyId id)            throws NoSuchItemStateException, ItemStateException {        NodePropBundle bundle = getBundle(id.getParentId());        if (bundle == null) {            throw new NoSuchItemStateException(id.toString());        }        PropertyState state = bundle.createPropertyState(this, id.getName());        if (state == null) {            // check if autocreated property state            if (id.getName().equals(QName.JCR_UUID)) {                state = createNew(id);                state.setType(PropertyType.STRING);                state.setDefinitionId(ID_JCR_UUID);                state.setMultiValued(false);                state.setValues(new InternalValue[]{InternalValue.create(id.getParentId().getUUID().toString())});            } else if (id.getName().equals(QName.JCR_PRIMARYTYPE)) {                state = createNew(id);                state.setType(PropertyType.NAME);                state.setDefinitionId(ID_JCR_PRIMARYTYPE);                state.setMultiValued(false);                state.setValues(new InternalValue[]{InternalValue.create(bundle.getNodeTypeName())});            } else if (id.getName().equals(QName.JCR_MIXINTYPES)) {                Set mixins = bundle.getMixinTypeNames();                state = createNew(id);                state.setType(PropertyType.NAME);                state.setDefinitionId(ID_JCR_MIXINTYPES);                state.setMultiValued(true);                state.setValues(InternalValue.create((QName[]) mixins.toArray(new QName[mixins.size()])));            } else {                throw new NoSuchItemStateException(id.toString());            }            bundle.addProperty(state);        }        return state;    }    /**     * {@inheritDoc}     *     * Loads the state via the appropriate NodePropBundle.     */    public synchronized boolean exists(PropertyId id) throws ItemStateException {        NodePropBundle bundle = getBundle(id.getParentId());        return bundle !=null && bundle.hasProperty(id.getName());    }    /**     * {@inheritDoc}     *     * Checks the existance via the appropriate NodePropBundle.     */    public synchronized boolean exists(NodeId id) throws ItemStateException {        // anticipating a load followed by a exists        return getBundle(id) != null;    }    /**     * {@inheritDoc}     */    public NodeState createNew(NodeId id) {        return new NodeState(id, null, null, NodeState.STATUS_NEW, false);    }    /**     * {@inheritDoc}     */    public PropertyState createNew(PropertyId id) {        return new PropertyState(id, PropertyState.STATUS_NEW, false);    }    /**     * Right now, this iterates over all items in the changelog and     * calls the individual methods that handle single item states     * or node references objects. Properly implemented, this method     * should ensure that changes are either written completely to     * the underlying persistence layer, or not at all.     *     * {@inheritDoc}     */    public synchronized void store(ChangeLog changeLog)            throws ItemStateException {        // delete bundles        HashSet deleted = new HashSet();        Iterator iter = changeLog.deletedStates();        while (iter.hasNext()) {            ItemState state = (ItemState) iter.next();            if (state.isNode()) {                NodePropBundle bundle = getBundle((NodeId) state.getId());                if (bundle == null) {                    throw new NoSuchItemStateException(state.getId().toString());                }                deleteBundle(bundle);                deleted.add(state.getId());            }        }        // gather added node states        HashMap modified = new HashMap();        iter = changeLog.addedStates();        while (iter.hasNext()) {            ItemState state = (ItemState) iter.next();            if (state.isNode()) {                NodePropBundle bundle = new NodePropBundle((NodeState) state);                modified.put(state.getId(), bundle);            }        }        // gather modified node states        iter = changeLog.modifiedStates();        while (iter.hasNext()) {            ItemState state = (ItemState) iter.next();            if (state.isNode()) {                NodeId nodeId = (NodeId) state.getId();                NodePropBundle bundle = (NodePropBundle) modified.get(nodeId);                if (bundle == null) {                    bundle = getBundle(nodeId);                    if (bundle == null) {                        throw new NoSuchItemStateException(nodeId.toString());                    }                    modified.put(nodeId, bundle);                }                bundle.update((NodeState) state);            } else {                PropertyId id = (PropertyId) state.getId();                // skip primaryType pr mixinTypes properties                if (id.getName().equals(QName.JCR_PRIMARYTYPE)                    || id.getName().equals(QName.JCR_MIXINTYPES)                    || id.getName().equals(QName.JCR_UUID)) {                    continue;                }                NodeId nodeId = id.getParentId();                NodePropBundle bundle = (NodePropBundle) modified.get(nodeId);                if (bundle == null) {                    bundle = getBundle(nodeId);                    if (bundle == null) {                        throw new NoSuchItemStateException(nodeId.toString());                    }                    modified.put(nodeId, bundle);                }                bundle.addProperty((PropertyState) state);            }        }        // add removed properties        iter = changeLog.deletedStates();        while (iter.hasNext()) {            ItemState state = (ItemState) iter.next();            if (state.isNode()) {                // check consistency                NodeId parentId = state.getParentId();                if (!modified.containsKey(parentId) && !deleted.contains(parentId)) {                    log.warn("Deleted node state's parent is not modified or deleted: " + parentId + "/" + state.getId());                }            } else {                PropertyId id = (PropertyId) state.getId();                NodeId nodeId = id.getParentId();                if (!deleted.contains(nodeId)) {                    NodePropBundle bundle = (NodePropBundle) modified.get(nodeId);                    if (bundle == null) {                        // should actually not happen                        log.warn("deleted property state's parent not modified!");                        bundle = getBundle(nodeId);                        if (bundle == null) {                            throw new NoSuchItemStateException(nodeId.toString());                        }                        modified.put(nodeId, bundle);                    }                    bundle.removeProperty(id.getName());                }            }        }        // add added properties        iter = changeLog.addedStates();        while (iter.hasNext()) {            ItemState state = (ItemState) iter.next();            if (!state.isNode()) {                PropertyId id = (PropertyId) state.getId();                // skip primaryType pr mixinTypes properties                if (id.getName().equals(QName.JCR_PRIMARYTYPE)                    || id.getName().equals(QName.JCR_MIXINTYPES)                    || id.getName().equals(QName.JCR_UUID)) {                    continue;                }                NodeId nodeId = id.getParentId();                NodePropBundle bundle = (NodePropBundle) modified.get(nodeId);                if (bundle == null) {                    // should actually not happen                    log.warn("added property state's parent not modified!");                    bundle = getBundle(nodeId);                    if (bundle == null) {                        throw new NoSuchItemStateException(nodeId.toString());                    }                    modified.put(nodeId, bundle);                }                bundle.addProperty((PropertyState) state);            }        }        // now store all modified bundles        iter = modified.values().iterator();        while (iter.hasNext()) {            NodePropBundle bundle = (NodePropBundle) iter.next();            putBundle(bundle);        }        // store the refs        iter = changeLog.modifiedRefs();        while (iter.hasNext()) {            NodeReferences refs = (NodeReferences) iter.next();            if (refs.hasReferences()) {                store(refs);            } else {                destroy(refs);            }        }    }    /**     * Gets the bundle for the given nodeid.     *     * @param id the id of the bundle to retrieve.     * @return the bundle or <code>null</code> if the bunlde does not exist     *     * @throws ItemStateException if an error occurs.     */    private NodePropBundle getBundle(NodeId id) throws ItemStateException {        if (missing.contains(id)) {            return null;        }        NodePropBundle bundle = bundles.get(id);        if (bundle == null) {            bundle = loadBundle(id);            if (bundle != null) {                bundle.markOld();                bundles.put(bundle);            } else {                missing.put(id);            }        }        return bundle;    }    /**     * Deletes the bundle     *     * @param bundle the bundle to delete     * @throws ItemStateException if an error occurs     */    private void deleteBundle(NodePropBundle bundle) throws ItemStateException {        destroyBundle(bundle);        bundles.remove(bundle.getId());        missing.put(bundle.getId());    }    /**     * Stores the bundle and puts it to the cache.     *     * @param bundle the bundle to store     * @throws ItemStateException if an error occurs     */    private void putBundle(NodePropBundle bundle) throws ItemStateException {        storeBundle(bundle);        bundle.markOld();        log.debug("stored bundle " + bundle.getId());        missing.remove(bundle.getId());        // only put to cache if already exists. this is to ensure proper overwrite        // and not creating big contention during bulk loads        if (bundles.contains(bundle.getId())) {            bundles.put(bundle);        }    }}

⌨️ 快捷键说明

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