// 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
// --------------------------------------------------------------------
Dn aliasedBase = null;
if ( db.getAliasCache() != null )
{
Element aliasBaseElement = db.getAliasCache().get( baseId );
if ( aliasBaseElement != null )
{
aliasedBase = (Dn)(aliasBaseElement).getObjectValue();
}
}
else
{
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 = aliasedBase.apply( schemaManager );
effectiveBaseId = db.getEntryId( effectiveBase );
}
// --------------------------------------------------------------------
// Specifically Handle Object Level Scope
// --------------------------------------------------------------------
if ( scope == SearchScope.OBJECT )
{
IndexEntry<String, String> indexEntry = new IndexEntry<String, String>();
indexEntry.setId( effectiveBaseId );
// Fetch the entry, as we have only one
Entry entry = db.fetch( indexEntry.getId(), effectiveBase );
Evaluator<? extends ExprNode> evaluator = null;
if ( filter instanceof ObjectClassNode )
{
ScopeNode node = new ScopeNode( aliasDerefMode, effectiveBase, effectiveBaseId, scope );
evaluator = new BaseLevelScopeEvaluator<Entry>( db, node );
}
else
{
optimizer.annotate( filter );
evaluator = evaluatorBuilder.build( filter );
// Special case if the filter selects no candidate
if ( evaluator == null )
{
ScopeNode node = new ScopeNode( aliasDerefMode, effectiveBase, effectiveBaseId, scope );
evaluator = new BaseLevelScopeEvaluator<Entry>( db, node );
}
}
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
ExprNode root = null;
if ( filter instanceof ObjectClassNode )
{
root = new ScopeNode( aliasDerefMode, effectiveBase, effectiveBaseId, scope );
}
else
{
root = new AndNode();
( ( AndNode ) root ).getChildren().add( filter );
ExprNode node = new ScopeNode( aliasDerefMode, effectiveBase, effectiveBaseId, scope );
( ( AndNode ) root ).getChildren().add( node );
}
// 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 );
LOG.debug( "Nb results : {} for filter : {}", nbResults, root );
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;
}