Uploaded image for project: 'Directory Client API'
  1. Directory Client API
  2. DIRAPI-184

Supply one method in SearchCursor to judge it has element or not.

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: 1.0.0-M21
    • Fix Version/s: 1.0.0-M25
    • Labels:
      None

      Description

      In some case, I need one method in org.apache.directory.api.ldap.model.cursor.SearchCursor to judge it has element or not. If not, I would not process the later logic.

        Activity

        Hide
        akiran Kiran Ayyagari added a comment -

        The next() method already serves this purpose. Are you expecting something else other than this?

        Show
        akiran Kiran Ayyagari added a comment - The next() method already serves this purpose. Are you expecting something else other than this?
        Hide
        hxbon1y Robert Hou added a comment -

        But next() will advance the cursor, if I use next() to judge, in my later logic, I will lost one record.

        Show
        hxbon1y Robert Hou added a comment - But next() will advance the cursor, if I use next() to judge, in my later logic, I will lost one record.
        Hide
        elecharny Emmanuel Lecharny added a comment -

        available() will tell you if the get() call will work. That is what you want.

        Show
        elecharny Emmanuel Lecharny added a comment - available() will tell you if the get() call will work. That is what you want.
        Hide
        hxbon1y Robert Hou added a comment -

        I tested available() method before, no matter SearchCursor has element in it or not, it always return false. Following is my test code:
        @Test
        public void testSearchCursor()
        {
        LdapNetworkConnection connection = new LdapNetworkConnection(
        "192.168.80.223", 50001);
        DefaultConfigurableBinaryAttributeDetector bad = new DefaultConfigurableBinaryAttributeDetector();
        bad.addBinaryAttribute("objectSid","objectGUID");
        connection.setBinaryAttributeDetector(bad);
        try

        { connection.bind("cn=robert,cn=roles,dc=tibco,dc=com", "robert"); }

        catch (LdapException e1)

        { e1.printStackTrace(); }
        SearchRequest sr = new SearchRequestImpl();
        try { sr.setBase(new Dn("cn=roles1,dc=tibco,dc=com")); } catch (LdapInvalidDnException e1) { e1.printStackTrace(); }

        try

        { sr.setFilter("(objectClass=inetorgperson)"); }

        catch (LdapException e1)

        { e1.printStackTrace(); }
        sr.setScope(SearchScope.SUBTREE);
        // sr.addControl(new PersistentSearchImpl());
        SearchCursor cursor = null;
        try{ cursor = connection.search(sr); }catch(LdapException e){ e.printStackTrace(); }
        // SearchCursor cursorBackup = cursor;
        boolean done = cursor.isDone();
        System.out.println("done = " + done);
        done = cursor.available();
        System.out.println("done = " + done);
        SearchResultDone searchResultDone= cursor.getSearchResultDone();
        String str = (searchResultDone == null) ? null: searchResultDone.toString();
        System.out.println(str);
        try { cursor.getEntry(); } catch (LdapException e2) { e2.printStackTrace(); }
        MessageTypeEnum messageTypeEnum = null;
        try { Response response = cursor.get(); messageTypeEnum = response.getType(); } catch (CursorException e2) { e2.printStackTrace(); }
        try {
        // done = cursor.isFirst();
        // System.out.println("bdone = " + done);
        if(cursor.next()){ // done = cursor.isFirst(); // System.out.println("ndone = " + done); }
        } catch (LdapException e1) { e1.printStackTrace(); }

        catch (CursorException e1)

        { e1.printStackTrace(); }

        int i = 0;
        try {
        while(cursor.next()){
        i++;
        Entry entry = cursor.getEntry();
        Dn dn = entry.getDn();
        System.out.println("dn = " + dn);
        Collection<Attribute> colAttribute = entry.getAttributes();
        Iterator<Attribute> itAttribute = colAttribute.iterator();
        while(itAttribute.hasNext()){
        Attribute attribute = itAttribute.next();
        String attributeName = attribute.getUpId();
        Iterator<Value<?>> itValue = attribute.iterator();
        while(itValue.hasNext()){
        Value<?> value = itValue.next();
        if(value.isHumanReadable())

        { String strValue = value.getString(); System.out.println("\t" + attributeName + "=" + strValue); }

        else

        { System.out.println("\t" + attributeName + "withBinary=" + value.getValue().toString()); }

        }
        }
        }
        } catch (LdapException e)

        { e.printStackTrace(); } catch (CursorException e) { e.printStackTrace(); }

        done = cursor.isDone();
        System.out.println("itfinish = " + done);
        System.out.println("output " + i + "entries");

        }

        Show
        hxbon1y Robert Hou added a comment - I tested available() method before, no matter SearchCursor has element in it or not, it always return false. Following is my test code: @Test public void testSearchCursor() { LdapNetworkConnection connection = new LdapNetworkConnection( "192.168.80.223", 50001); DefaultConfigurableBinaryAttributeDetector bad = new DefaultConfigurableBinaryAttributeDetector(); bad.addBinaryAttribute("objectSid","objectGUID"); connection.setBinaryAttributeDetector(bad); try { connection.bind("cn=robert,cn=roles,dc=tibco,dc=com", "robert"); } catch (LdapException e1) { e1.printStackTrace(); } SearchRequest sr = new SearchRequestImpl(); try { sr.setBase(new Dn("cn=roles1,dc=tibco,dc=com")); } catch (LdapInvalidDnException e1) { e1.printStackTrace(); } try { sr.setFilter("(objectClass=inetorgperson)"); } catch (LdapException e1) { e1.printStackTrace(); } sr.setScope(SearchScope.SUBTREE); // sr.addControl(new PersistentSearchImpl()); SearchCursor cursor = null; try{ cursor = connection.search(sr); }catch(LdapException e){ e.printStackTrace(); } // SearchCursor cursorBackup = cursor; boolean done = cursor.isDone(); System.out.println("done = " + done); done = cursor.available(); System.out.println("done = " + done); SearchResultDone searchResultDone= cursor.getSearchResultDone(); String str = (searchResultDone == null) ? null: searchResultDone.toString(); System.out.println(str); try { cursor.getEntry(); } catch (LdapException e2) { e2.printStackTrace(); } MessageTypeEnum messageTypeEnum = null; try { Response response = cursor.get(); messageTypeEnum = response.getType(); } catch (CursorException e2) { e2.printStackTrace(); } try { // done = cursor.isFirst(); // System.out.println("bdone = " + done); if(cursor.next()){ // done = cursor.isFirst(); // System.out.println("ndone = " + done); } } catch (LdapException e1) { e1.printStackTrace(); } catch (CursorException e1) { e1.printStackTrace(); } int i = 0; try { while(cursor.next()){ i++; Entry entry = cursor.getEntry(); Dn dn = entry.getDn(); System.out.println("dn = " + dn); Collection<Attribute> colAttribute = entry.getAttributes(); Iterator<Attribute> itAttribute = colAttribute.iterator(); while(itAttribute.hasNext()){ Attribute attribute = itAttribute.next(); String attributeName = attribute.getUpId(); Iterator<Value<?>> itValue = attribute.iterator(); while(itValue.hasNext()){ Value<?> value = itValue.next(); if(value.isHumanReadable()) { String strValue = value.getString(); System.out.println("\t" + attributeName + "=" + strValue); } else { System.out.println("\t" + attributeName + "withBinary=" + value.getValue().toString()); } } } } } catch (LdapException e) { e.printStackTrace(); } catch (CursorException e) { e.printStackTrace(); } done = cursor.isDone(); System.out.println("itfinish = " + done); System.out.println("output " + i + "entries"); }
        Hide
        elecharny Emmanuel Lecharny added a comment -

        Thanks for the test.

        I'll check that this morning, it's very possible that there are some inconsistencies in the API.

        Show
        elecharny Emmanuel Lecharny added a comment - Thanks for the test. I'll check that this morning, it's very possible that there are some inconsistencies in the API.
        Hide
        akiran Kiran Ayyagari added a comment -

        But next() will advance the cursor ...

        it is not possible to find the availability of an element without advancing first. One other way is to backtrack with previous() after calling next()
        but that is not always supported in all types of cursors.

        available() can only give the correct result after calling next()/previosu().

        Show
        akiran Kiran Ayyagari added a comment - But next() will advance the cursor ... it is not possible to find the availability of an element without advancing first. One other way is to backtrack with previous() after calling next() but that is not always supported in all types of cursors. available() can only give the correct result after calling next()/previosu().
        Hide
        hxbon1y Robert Hou added a comment -

        Before I thought of the way to backtrack with previous() after calling next(), but I found it will casue exception when invoking previous(). So looks like it can't be supported in SearchCursor.

        Show
        hxbon1y Robert Hou added a comment - Before I thought of the way to backtrack with previous() after calling next(), but I found it will casue exception when invoking previous(). So looks like it can't be supported in SearchCursor.
        Hide
        elecharny Emmanuel Lecharny added a comment -

        I'm a bit puzzled...

        What exactly are you trying to achieve ?

        The logic is that when you call next(), the cursor will fetch the next entry from the queue, and store it internally into a response that you will get back when calling get(). If you don't call get(), then the entry is still present.

        What is the user case that requires you can do a next(), but not fetching the first entry ?

        Show
        elecharny Emmanuel Lecharny added a comment - I'm a bit puzzled... What exactly are you trying to achieve ? The logic is that when you call next(), the cursor will fetch the next entry from the queue, and store it internally into a response that you will get back when calling get(). If you don't call get(), then the entry is still present. What is the user case that requires you can do a next(), but not fetching the first entry ?
        Hide
        akiran Kiran Ayyagari added a comment -

        Before I thought of the way to backtrack with previous() after calling next(), but I found it will casue exception when invoking previous(). So looks like it can't be supported in SearchCursor.

        Yes, it is not supported on the client side, cause the entry is fetched over network.

        Closing it as won't fix.

        Show
        akiran Kiran Ayyagari added a comment - Before I thought of the way to backtrack with previous() after calling next(), but I found it will casue exception when invoking previous(). So looks like it can't be supported in SearchCursor. Yes, it is not supported on the client side, cause the entry is fetched over network. Closing it as won't fix.
        Hide
        elecharny Emmanuel Lecharny added a comment -

        Closing the resolved issues.

        Show
        elecharny Emmanuel Lecharny added a comment - Closing the resolved issues.

          People

          • Assignee:
            elecharny Emmanuel Lecharny
            Reporter:
            hxbon1y Robert Hou
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development