LDAPSearchResults getCount() method returns incorrect value

RESOLVED INVALID

Status

Directory
LDAP Java SDK
P3
normal
RESOLVED INVALID
18 years ago
18 years ago

People

(Reporter: Joerg Schaefer, Assigned: miodrag)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Reporter)

Description

18 years ago
In the code snippet below, I want to use getCount() to check how many entries 
are returned in the search result "res" in order to reject authentication in 
case of multiple entries. However, getCount() always incorrectly returns 0 (see 
line "System.out.println(res.getCount());").
A strange thing happens when I debug the code (I am running Jbuilder V3.0), the 
correct value (1 in our case) is returned in this case (and printed to 
System.out)!
I am using NT4.00.1381
Netscape Directory Java SDK 4.0
Borland Jbuilder V3.0

//Code Snippet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    try{
      ld.connect(MY_HOST, MY_PORT);
      LDAPSearchResults res = ld.search(MY_SEARCHBASE, LDAPConnection.SCOPE_SUB, 
MY_FILTER, null, false);

      // should include code for checking for multiple entries, but getCount() 
does not work
      System.out.println(res.getCount());
      if(res.getCount()!=0){
		if(res.hasMoreElements()){
	        findEntry = res.next();
	        dn = findEntry.getDN();
	        ld.authenticate(dn, pwd);
	        status = true;
	        if (ld.isConnected()) {
		        try {
		            ld.disconnect();
	            } catch ( LDAPException e ) {throw e;}
            }
          }
		}
      } catch(LDAPException e){
          if(e.getLDAPResultCode() == LDAPException.INVALID_CREDENTIALS){
            return status;
            }
          else{throw e;}
          }
          catch(Exception x){x.printStackTrace();}
        return status;
        }
(Reporter)

Comment 1

18 years ago
Created attachment 6326 [details] [diff] [review]
Source Code for class containing bug (see line containing get.Count() statement)

Comment 2

18 years ago
Reassigned to Miodrag.
Assignee: mcs → miodrag
(Assignee)

Comment 3

18 years ago
This is a normal behavior. LDAPSearchResults.getCount() returns the number of 
currently available search results in the search result queue, rather than the 
total number of entries that match the search filter.

When the directory sends a new search result, the result gets queued and the 
search result count is incremented. As you read search results from 
LDAPSearchResults, one at a time, the search result count gets decremeneted.

Only if you set the batch size in LDAPSearchConstrains to 0 --means search() 
should wait for all results to be received before proceeding --, you'll see the 
exact count of found entries. 

By default the batchSize is 1, which means results are to be processed as they 
come in. LDAPSearchResults.hasMoreElements() blocks until the batchSize number 
of  results is received. 
Status: NEW → RESOLVED
Last Resolved: 18 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.