+ if ( avl_insert( &cache->c_dntree, (caddr_t) e,
+ entry_dn_cmp, avl_dup_error ) != 0 )
+ {
+ Debug( LDAP_DEBUG_TRACE,
+ "====> bdb2i_cache_add_entry( %ld ): \"%s\": already in dn cache\n",
+ e->e_id, e->e_dn, 0 );
+
+ /* free cache mutex */
+ ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
+ return( 1 );
+ }
+
+ /* id tree */
+ if ( avl_insert( &cache->c_idtree, (caddr_t) e,
+ entry_id_cmp, avl_dup_error ) != 0 )
+ {
+ Debug( LDAP_DEBUG_ANY,
+ "====> bdb2i_cache_update_entry( %ld ): \"%s\": already in id cache\n",
+ e->e_id, e->e_dn, 0 );
+
+ /* delete from dn tree inserted above */
+ if ( avl_delete( &cache->c_dntree, (caddr_t) e,
+ entry_dn_cmp ) == NULL )
+ {
+ Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
+ 0, 0, 0 );
+ }
+
+ /* free cache mutex */
+ ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
+ return( -1 );
+ }
+
+ /* put the entry into 'CREATING' state */
+ /* will be marked after when entry is returned */
+ LEI(e)->lei_state = CACHE_ENTRY_CREATING;
+
+ /* lru */
+ LRU_ADD( cache, e );
+ if ( ++cache->c_cursize > cache->c_maxsize ) {
+ /*
+ * find the lru entry not currently in use and delete it.
+ * in case a lot of entries are in use, only look at the
+ * first 10 on the tail of the list.
+ */
+ i = 0;
+ while ( cache->c_lrutail != NULL &&
+ LEI(cache->c_lrutail)->lei_refcnt != 0 &&
+ i < 10 )
+ {
+ /* move this in-use entry to the front of the q */
+ ee = cache->c_lrutail;
+ LRU_DELETE( cache, ee );
+ LRU_ADD( cache, ee );
+ i++;
+ }
+
+ /*
+ * found at least one to delete - try to get back under
+ * the max cache size.
+ */
+ while ( cache->c_lrutail != NULL &&
+ LEI(cache->c_lrutail)->lei_refcnt == 0 &&
+ cache->c_cursize > cache->c_maxsize )
+ {
+ e = cache->c_lrutail;
+