// Compute the UUID of the baseDN entry
String baseId = db.getEntryId( baseDn );
// Prepare the instance containing the search result
PartitionSearchResult searchResult = new PartitionSearchResult( schemaManager );
Set<IndexEntry<String, String>> resultSet = new HashSet<IndexEntry<String, String>>();
// Check that we have an entry, otherwise we can immediately get out
if ( baseId == null )
{
if ( ( ( Partition ) db ).getSuffixDn().equals( baseDn ) )
{
// The context entry is not created yet, return an empty result
searchResult.setResultSet( resultSet );
return searchResult;
}
else
{
// The search base doesn't exist
throw new LdapNoSuchObjectException( I18n.err( I18n.ERR_648, baseDn ) );
}
}
// --------------------------------------------------------------------
// Determine the effective base with aliases
// --------------------------------------------------------------------
String aliasedBase = db.getAliasIndex().reverseLookup( baseId );
Dn effectiveBase = baseDn;
String effectiveBaseId = baseId;
if ( ( aliasedBase != null ) && aliasDerefMode.isDerefFindingBase() )
{
/*
* If the base is an alias and alias dereferencing does occur on
* finding the base, or always then we set the effective base to the alias target
* got from the alias index.
*/
effectiveBase = new Dn( schemaManager, aliasedBase );
effectiveBaseId = db.getEntryId( effectiveBase );
}
// --------------------------------------------------------------------
// Specifically Handle Object Level Scope
// --------------------------------------------------------------------
if ( scope == SearchScope.OBJECT )
{
IndexEntry<String, String> indexEntry = new IndexEntry<String, String>();
indexEntry.setId( effectiveBaseId );
optimizer.annotate( filter );
Evaluator<? extends ExprNode> evaluator = evaluatorBuilder.build( filter );
// Fetch the entry, as we have only one
Entry entry = db.lookup( indexEntry.getId(), effectiveBase );
indexEntry.setEntry( entry );
resultSet.add( indexEntry );
searchResult.setEvaluator( evaluator );
searchResult.setResultSet( resultSet );
return searchResult;
}
// This is not a BaseObject scope search.
// Add the scope node using the effective base to the filter
BranchNode root = new AndNode();
ExprNode node = new ScopeNode( aliasDerefMode, effectiveBase, effectiveBaseId, scope );
root.getChildren().add( node );
root.getChildren().add( filter );
// Annotate the node with the optimizer and return search enumeration.
optimizer.annotate( root );
Evaluator<? extends ExprNode> evaluator = evaluatorBuilder.build( root );
Set<String> uuidSet = new HashSet<String>();
searchResult.setAliasDerefMode( aliasDerefMode );
searchResult.setCandidateSet( uuidSet );
long nbResults = cursorBuilder.build( root, searchResult );
if ( nbResults < Long.MAX_VALUE )
{
for ( String uuid : uuidSet )
{
IndexEntry<String, String> indexEntry = new IndexEntry<String, String>();
indexEntry.setId( uuid );
resultSet.add( indexEntry );
}
}
else
{
// Full scan : use the MasterTable
Cursor<IndexEntry<String, String>> cursor = new IndexCursorAdaptor( db.getMasterTable().cursor(), true );
while ( cursor.next() )
{
IndexEntry<String, String> indexEntry = cursor.get();
// Here, the indexEntry contains a <UUID, Entry> tuple. Convert it to <UUID, UUID>
IndexEntry<String, String> forwardIndexEntry = new IndexEntry<String, String>();
forwardIndexEntry.setKey( indexEntry.getKey() );
forwardIndexEntry.setId( indexEntry.getKey() );
forwardIndexEntry.setEntry( null );
resultSet.add( forwardIndexEntry );
}
}
searchResult.setEvaluator( evaluator );
searchResult.setResultSet( resultSet );
return searchResult;
}