use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.
the class BatchedItemOperations method createNodeState.
/**
* Creates a new node based on the given definition.
* <p>
* Note that access rights are <b><i>not</i></b> enforced!
* <p>
* <b>Precondition:</b> the state manager needs to be in edit mode.
*
* @param parent
* @param nodeName
* @param nodeTypeName
* @param mixinNames
* @param id
* @param def
* @return
* @throws ItemExistsException
* @throws ConstraintViolationException
* @throws RepositoryException
* @throws IllegalStateException
*/
public NodeState createNodeState(NodeState parent, Name nodeName, Name nodeTypeName, Name[] mixinNames, NodeId id, QNodeDefinition def) throws ItemExistsException, ConstraintViolationException, RepositoryException, IllegalStateException {
// check for name collisions with existing nodes
if (!def.allowsSameNameSiblings() && parent.hasChildNodeEntry(nodeName)) {
NodeId errorId = parent.getChildNodeEntry(nodeName, 1).getId();
throw new ItemExistsException(safeGetJCRPath(errorId));
}
if (nodeTypeName == null) {
// no primary node type specified,
// try default primary type from definition
nodeTypeName = def.getDefaultPrimaryType();
if (nodeTypeName == null) {
String msg = "an applicable node type could not be determined for " + nodeName;
log.debug(msg);
throw new ConstraintViolationException(msg);
}
}
NodeState node = stateMgr.createNew(id, nodeTypeName, parent.getNodeId());
if (mixinNames != null && mixinNames.length > 0) {
node.setMixinTypeNames(new HashSet<Name>(Arrays.asList(mixinNames)));
}
// now add new child node entry to parent
parent.addChildNodeEntry(nodeName, node.getNodeId());
EffectiveNodeType ent = getEffectiveNodeType(node);
// check shareable
if (ent.includesNodeType(NameConstants.MIX_SHAREABLE)) {
node.addShare(parent.getNodeId());
}
if (!node.getMixinTypeNames().isEmpty()) {
// create jcr:mixinTypes property
QPropertyDefinition pd = ent.getApplicablePropertyDef(NameConstants.JCR_MIXINTYPES, PropertyType.NAME, true);
createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
}
// add 'auto-create' properties defined in node type
for (QPropertyDefinition pd : ent.getAutoCreatePropDefs()) {
createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
}
// recursively add 'auto-create' child nodes defined in node type
for (QNodeDefinition nd : ent.getAutoCreateNodeDefs()) {
createNodeState(node, nd.getName(), nd.getDefaultPrimaryType(), null, null, nd);
}
// store node
stateMgr.store(node);
// store parent
stateMgr.store(parent);
return node;
}
use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.
the class CachingHierarchyManager method nodeAdded.
/**
* Invoked when a notification about a child node addition has been received.
*
* @param state node state where child was added
* @param path path to child node
* @param id child node id
*
* @throws PathNotFoundException if the path was not found
* @throws RepositoryException If the path's direct ancestor cannot be determined.
* @throws ItemStateException If the id cannot be resolved to a NodeState.
*/
private void nodeAdded(NodeState state, Path path, NodeId id) throws RepositoryException, ItemStateException {
// assert: synchronized (cacheMonitor)
PathMap.Element<LRUEntry> element = null;
LRUEntry entry = (LRUEntry) idCache.get(id);
if (entry != null) {
// child node already cached: this can have the following
// reasons:
// 1) node was moved, cached path is outdated
// 2) node was cloned, cached path is still valid
NodeState child = null;
if (hasItemState(id)) {
child = (NodeState) getItemState(id);
}
if (child == null || !child.isShareable()) {
PathMap.Element<LRUEntry>[] elements = entry.getElements();
element = elements[0];
for (int i = 0; i < elements.length; i++) {
elements[i].remove();
}
}
}
PathMap.Element<LRUEntry> parent = pathCache.map(path.getAncestor(1), true);
if (parent != null) {
parent.insert(path.getNameElement());
}
if (element != null) {
// store remembered element at new position
pathCache.put(path, element);
}
}
use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.
the class CachingHierarchyManager method nodeRemoved.
/**
* Invoked when a notification about a child node removal has been received.
*
* @param state node state
* @param path node path
* @param id node id
*
* @throws PathNotFoundException if the path was not found.
* @throws RepositoryException If the path's direct ancestor cannot be determined.
* @throws ItemStateException If the id cannot be resolved to a NodeState.
*/
private void nodeRemoved(NodeState state, Path path, NodeId id) throws RepositoryException, ItemStateException {
// assert: synchronized (cacheMonitor)
PathMap.Element<LRUEntry> parent = pathCache.map(path.getAncestor(1), true);
if (parent == null) {
return;
}
PathMap.Element<LRUEntry> element = parent.getDescendant(path.getLastElement(), true);
if (element != null) {
// with SNS, this might evict a child that is NOT the one
// having <code>id</code>, check first whether item has
// the id passed as argument
LRUEntry entry = (LRUEntry) element.get();
if (entry != null && !entry.getId().equals(id)) {
return;
}
// if item is shareable, remove this path only, otherwise
// every path this item has been mapped to
NodeState child = null;
if (hasItemState(id)) {
child = (NodeState) getItemState(id);
}
if (child == null || !child.isShareable()) {
evictAll(id, true);
} else {
evict(element, true);
}
} else {
// element itself is not cached, but removal might cause SNS
// index shifting
parent.remove(path.getNameElement());
}
}
use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.
the class MultiIndex method createInitialIndex.
/**
* Creates an initial index by traversing the node hierarchy starting at the
* node with <code>rootId</code>.
*
* @param stateMgr the item state manager.
* @param rootId the id of the node from where to start.
* @param rootPath the path of the node from where to start.
* @throws IOException if an error occurs while indexing the
* workspace.
* @throws IllegalStateException if this index is not empty.
*/
void createInitialIndex(ItemStateManager stateMgr, NodeId rootId, Path rootPath) throws IOException {
// only do an initial index if there are no indexes at all
if (indexNames.size() == 0) {
reindexing = true;
try {
long count = 0;
// traverse and index workspace
executeAndLog(new Start(Action.INTERNAL_TRANSACTION));
NodeState rootState = (NodeState) stateMgr.getItemState(rootId);
count = createIndex(rootState, rootPath, stateMgr, count);
checkIndexingQueue(true);
executeAndLog(new Commit(getTransactionId()));
log.debug("Created initial index for {} nodes", count);
releaseMultiReader();
safeFlush();
} catch (Exception e) {
String msg = "Error indexing workspace";
IOException ex = new IOException(msg);
ex.initCause(e);
throw ex;
} finally {
reindexing = false;
scheduleFlushTask();
}
} else {
throw new IllegalStateException("Index already present");
}
}
use of org.apache.jackrabbit.core.state.NodeState in project jackrabbit by apache.
the class MultiIndex method createIndex.
/**
* Recursively creates an index starting with the NodeState
* <code>node</code>.
*
* @param node the current NodeState.
* @param path the path of the current <code>node</code> state.
* @param stateMgr the shared item state manager.
* @param count the number of nodes already indexed.
* @return the number of nodes indexed so far.
* @throws IOException if an error occurs while writing to the
* index.
* @throws ItemStateException if an node state cannot be found.
* @throws RepositoryException if any other error occurs
*/
private long createIndex(NodeState node, Path path, ItemStateManager stateMgr, long count) throws IOException, ItemStateException, RepositoryException {
NodeId id = node.getNodeId();
if (excludedIDs.contains(id)) {
return count;
}
executeAndLog(new AddNode(getTransactionId(), id));
if (++count % 100 == 0) {
PathResolver resolver = new DefaultNamePathResolver(handler.getContext().getNamespaceRegistry());
log.info("indexing... {} ({})", resolver.getJCRPath(path), count);
}
if (count % 10 == 0) {
checkIndexingQueue(true);
}
checkVolatileCommit();
for (ChildNodeEntry child : node.getChildNodeEntries()) {
Path childPath = PATH_FACTORY.create(path, child.getName(), child.getIndex(), false);
NodeState childState = null;
try {
childState = (NodeState) stateMgr.getItemState(child.getId());
} catch (NoSuchItemStateException e) {
handler.getOnWorkspaceInconsistencyHandler().handleMissingChildNode(e, handler, path, node, child);
} catch (ItemStateException e) {
// JCR-3268 log bundle corruption and continue
handler.getOnWorkspaceInconsistencyHandler().logError(e, handler, childPath, node, child);
}
if (childState != null) {
count = createIndex(childState, childPath, stateMgr, count);
}
}
return count;
}
Aggregations