Wednesday, August 5, 2015

Today we discuss the logic to retrieve groups from LDAP. As you might know, LDIF is the query we specify for retrieving these groips. Very often the results are large in number causing the message SIZE_EXCEEDED from a query like this:
     ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
     ld = ldap.initialize("ldaps://myserver:636")
     ld.set_option(ldap.OPT_REFERRALS, 0)
     ld.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
     ld.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
     ld.set_option( ldap.OPT_X_TLS_DEMAND, True )
     ld.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
     ld.simple_bind_s(email, password)
     base_dn="OU=myOU,OU=Groups,DC=adobenet,DC=global,DC=my,DC=com"
     filter = "(|(cn=*))"
     results = ld.search_s(base_dn,ldap.SCOPE_SUBTREE, filter, ['sn'])
     print repr(results)

     return results

Instead we could do this:
     groups = []
     sizelimit = SIZELIMIT
     # If we have a sizelimit, we'll get results one by one so that we
     # can stop processing once we've hit the limit.
     if sizelimit:
       all = 0
     else:
       all = 1
     ix = 0
     cookie = ''
     while True:
      paged_results_control = SimplePagedResultsControl(
        ldap.LDAP_CONTROL_PAGE_OID, True, (PAGE_SIZE, cookie))
      serverctrls = [paged_results_control]
      msgid = ld.search_ext(base_dn, ldap.SCOPE_SUBTREE,
                                 filter, attrlist=['sn'], attrsonly=0, serverctrls=serverctrls) #, clientctrls=None, timeout=TIMEOUT, sizelimit=SIZELIMIT)
      res = ld.result3(msgid=msgid, timeout=TIMEOUT)
      unused_code, results, unused_msgid, serverctrls = res
      for result in results:
        ix += 1
        users.append(result)
        if sizelimit and ix >= sizelimit:
          break
      if sizelimit and ix >= sizelimit:
        break
      cookie = None
      for serverctrl in serverctrls:
        if serverctrl.controlType == ldap.LDAP_CONTROL_PAGE_OID:
          unused_est, cookie = serverctrl.controlValue
          if cookie:
            paged_results_control.controlValue = (PAGE_SIZE, cookie)
          break
      if not cookie:
        break

     return groups


#codingexercise
Question: Use node rotations to convert a binary search tree to a sorted double-linked list.
Answer : The conversion process begins form the root node. If the root node of a binary search tree has a left child, a right rotation occurs. The left child becomes the new root. This process is repeated until there are no left nodes at the root. The root’s right child is traversed until there is a node encountered with a left child. The steps above are then repeated here and the repetitions continue until the leaf node. At this point, we have a singly linked list. It can be made a doubly linked list by linking the parents.

Node ConvertToList(Node root)
{
 Node cur = root;
 Node head = null;
 Node parent = null;
 While (cur){
  While (cur && cur.left)
    {
        cur =  Right-Rotate(cur, parent);
     }
     if (head == null) head = cur;
    parent = cur;
    cur = cur. Right;
}
 CreateDLList(head);
}

Node Right-Rotate(Node root, Node parent)
{
Node cur = root;
Node left = cur.left;
Cur.left = left.right;
Left.right = cur;
Cur = left;
  If (parent != null)
     Parent.right = cur;
Return cur;
}

void CreateDLList(Node root)
{
 Node cur = root;
 If (cur != null) {
    Node next = cur.right;
    While (next ) {
Next.left = cur;
Cur = next;
Next = cur.right;
}
}
}

No comments:

Post a Comment