assert indexes != null;
assert indexDefn.getName() != null;
// First find or create the provider node ...
MutableCachedNode providerNode = null;
final NodeKey providerKey = nodeKey(indexes.getKey(), indexDefn.getProviderName());
if (indexes.getChildReferences(system).hasChild(providerKey)) {
// The node already exists ...
providerNode = system.mutable(providerKey);
} else {
// Create the new provider node ...
Property primaryType = propertyFactory.create(JcrLexicon.PRIMARY_TYPE, ModeShapeLexicon.INDEX_PROVIDER);
Name providerName = names.create(indexDefn.getProviderName());
providerNode = indexes.createChild(system, providerKey, providerName, primaryType);
}
assert providerNode != null;
Name name = names.create(indexDefn.getName());
final NodeKey key = nodeKey(indexes.getKey(), indexDefn);
MutableCachedNode indexNode = null;
Set<NodeKey> existingChildKeys = null;
if (providerNode.getChildReferences(system).hasChild(key)) {
// The node already exists ...
if (!updateExisting) return;
indexNode = system.mutable(key);
// We'll need to delete any existing column that isn't there anymore ...
existingChildKeys = new HashSet<NodeKey>();
for (ChildReference childRef : indexNode.getChildReferences(system)) {
existingChildKeys.add(childRef.getKey());
}
}
// Define the properties for this index definition ...
List<Property> properties = new ArrayList<Property>();
// Add the extended properties first, in case the standard ones overwrite them ...
for (Map.Entry<String, Object> entry : indexDefn.getIndexProperties().entrySet()) {
Property prop = createProperty(entry.getKey(), entry.getValue());
if (prop != null) properties.add(prop);
}
// Now do the standard properties ...
properties.add(propertyFactory.create(JcrLexicon.PRIMARY_TYPE, ModeShapeLexicon.INDEX));
properties.add(propertyFactory.create(JcrLexicon.DESCRIPTION, indexDefn.getDescription()));
properties.add(propertyFactory.create(ModeShapeLexicon.KIND, indexDefn.getKind().name()));
properties.add(propertyFactory.create(ModeShapeLexicon.NODE_TYPE_NAME, indexDefn.getNodeTypeName()));
properties.add(propertyFactory.create(ModeShapeLexicon.SYNCHRONOUS, indexDefn.isSynchronous()));
// Now make or adjust the node for the index definition ...
if (indexNode != null) {
// Update the properties ...
indexNode.setProperties(system, properties);
} else {
// We have to create the index definition node ...
indexNode = providerNode.createChild(system, key, name, properties);
}
// And the column definitions ...
for (IndexColumnDefinition columnDefn : indexDefn) {
NodeKey columnDefnKey = store(indexNode, columnDefn);
if (existingChildKeys != null) existingChildKeys.remove(columnDefnKey);
}
// Remove any column defns that weren't represented in the index definition ...
if (existingChildKeys != null && !existingChildKeys.isEmpty()) {