]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/pcache.c
trim listed modules
[openldap] / servers / slapd / overlays / pcache.c
index 0fc10352e1e6ae31eb9f9f1bb31c352fe27c8bea..56bf2a29b21bdf7d51abf6d18dca544022268a49 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2003-2006 The OpenLDAP Foundation.
+ * Copyright 2003-2007 The OpenLDAP Foundation.
  * Portions Copyright 2003 IBM Corporation.
  * Portions Copyright 2003 Symas Corporation.
  * All rights reserved.
@@ -31,6 +31,7 @@
 #include "slap.h"
 #include "lutil.h"
 #include "ldap_rq.h"
+#include "avl.h"
 
 #include "config.h"
 
 
 typedef struct Query_s {
        Filter*         filter;         /* Search Filter */
-       AttributeName*  attrs;          /* Projected attributes */
-       AttributeName*  save_attrs;     /* original attributes, saved for response */
        struct berval   base;           /* Search Base */
        int             scope;          /* Search scope */
 } Query;
 
+struct query_template_s;
+
+typedef struct Qbase_s {
+       Avlnode *scopes[4];             /* threaded AVL trees of cached queries */
+       struct berval base;
+       int queries;
+} Qbase;
+
 /* struct representing a cached query */
 typedef struct cached_query_s {
-       Query                           query;          /* LDAP query */
+       Filter                                  *filter;
+       Filter                                  *first;
+       Qbase                                   *qbase;
+       int                                             scope;
        struct berval                   q_uuid;         /* query identifier */
-       int                             template_id;    /* template of the query */
+       struct query_template_s *qtemp; /* template of the query */
        time_t                          expiry_time;    /* time till the query is considered valid */
        struct cached_query_s           *next;          /* next query in the template */
        struct cached_query_s           *prev;          /* previous query in the template */
@@ -57,42 +67,47 @@ typedef struct cached_query_s {
        struct cached_query_s           *lru_down;      /* next query in the LRU list */
 } CachedQuery;
 
+/*
+ * Represents a set of projected attributes.
+ */
+
+struct attr_set {
+       struct query_template_s *templates;
+       AttributeName*  attrs;          /* specifies the set */
+       unsigned        flags;
+#define        PC_CONFIGURED   (0x1)
+#define        PC_REFERENCED   (0x2)
+#define        PC_GOT_OC               (0x4)
+       int             count;          /* number of attributes */
+};
+
 /* struct representing a query template
  * e.g. template string = &(cn=)(mail=)
  */
 typedef struct query_template_s {
-       struct berval   querystr;       /* Filter string corresponding to the QT */
-       int             attr_set_index; /* determines the projected attributes */
+       struct query_template_s *qtnext;
+       struct query_template_s *qmnext;
 
+       Avlnode*                qbase;
        CachedQuery*    query;          /* most recent query cached for the template */
        CachedQuery*    query_last;     /* oldest query cached for the template */
+       ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
+       struct berval   querystr;       /* Filter string corresponding to the QT */
 
+       int             attr_set_index; /* determines the projected attributes */
        int             no_of_queries;  /* Total number of queries in the template */
        time_t          ttl;            /* TTL for the queries of this template */
        time_t          negttl;         /* TTL for negative results */
-        ldap_pvt_thread_rdwr_t t_rwlock; /* Rd/wr lock for accessing queries in the template */
+       struct attr_set t_attrs;        /* filter attrs + attr_set */
 } QueryTemplate;
 
-/*
- * Represents a set of projected attributes.
- */
-
-struct attr_set {
-       unsigned        flags;
-#define        PC_CONFIGURED   (0x1)
-#define        PC_REFERENCED   (0x2)
-#define        PC_GOT_OC               (0x4)
-       AttributeName*  attrs;          /* specifies the set */
-       int             count;          /* number of attributes */
-};
-
 struct query_manager_s;
 
 /* prototypes for functions for 1) query containment
  * 2) query addition, 3) cache replacement
  */
-typedef CachedQuery *  (QCfunc)(Operation *op, struct query_manager_s*, Query*, int );
-typedef void   (AddQueryfunc)(struct query_manager_s*, Query*, int, struct berval*);
+typedef CachedQuery *  (QCfunc)(Operation *op, struct query_manager_s*, Query*, QueryTemplate*);
+typedef CachedQuery *  (AddQueryfunc)(Operation *op, struct query_manager_s*, Query*, QueryTemplate*, int positive);
 typedef void   (CRfunc)(struct query_manager_s*, struct berval * );
 
 /* LDAP query cache */
@@ -117,7 +132,6 @@ typedef struct cache_manager_s {
        unsigned long   num_cached_queries;             /* total number of cached queries */
        unsigned long   max_queries;                    /* upper bound on # of cached queries */
        int     numattrsets;                    /* number of attribute sets */
-       int     numtemplates;                   /* number of cacheable templates */
        int     cur_entries;                    /* current number of entries cached */
        int     max_entries;                    /* max number of entries cached */
         int     num_entries_limit;             /* max # of entries in a cacheable query */
@@ -132,11 +146,12 @@ typedef struct cache_manager_s {
        void    *cc_arg;
 
        ldap_pvt_thread_mutex_t         cache_mutex;
-       ldap_pvt_thread_mutex_t         remove_mutex;
 
        query_manager*   qm;    /* query cache managed by the cache manager */
 } cache_manager;
 
+static int pcache_debug;
+
 static AttributeDescription *ad_queryid;
 static char *queryid_schema = "( 1.3.6.1.4.1.4203.666.1.12 NAME 'queryid' "
                        "DESC 'list of queries the entry belongs to' "
@@ -201,51 +216,118 @@ merge_entry(
                        rc = 0;
                }
        } else {
-               be_entry_release_w( op, e );
+               if ( op->ora_e == e )
+                       be_entry_release_w( op, e );
                rc = 1;
        }
 
        return rc;
 }
 
-/* compare base and scope of incoming and cached queries */
-static int base_scope_compare(
-       struct berval* ndn_stored,
-       struct berval* ndn_incoming,
-       int scope_stored,
-       int scope_incoming      )
+/* Length-ordered sort on normalized DNs */
+static int pcache_dn_cmp( const void *v1, const void *v2 )
 {
-       struct berval pdn_incoming = BER_BVNULL;
-
-       if (scope_stored < scope_incoming)
-               return 0;
-
-       if ( !dnIsSuffix(ndn_incoming, ndn_stored))
-               return 0;
+       const Qbase *q1 = v1, *q2 = v2;
 
-       switch(scope_stored) {
-       case LDAP_SCOPE_BASE:
-               return (ndn_incoming->bv_len == ndn_stored->bv_len);
+       int rc = q1->base.bv_len - q2->base.bv_len;
+       if ( rc == 0 )
+               rc = strncmp( q1->base.bv_val, q2->base.bv_val, q1->base.bv_len );
+       return rc;
+}
 
-       case LDAP_SCOPE_ONELEVEL:
-               switch(scope_incoming){
-               case LDAP_SCOPE_BASE:
-                       dnParent(ndn_incoming, &pdn_incoming);
-                       return (pdn_incoming.bv_len == ndn_stored->bv_len);
+static int lex_bvcmp( struct berval *bv1, struct berval *bv2 )
+{
+       int len, dif;
+       dif = bv1->bv_len - bv2->bv_len;
+       len = bv1->bv_len;
+       if ( dif > 0 ) len -= dif;
+       len = memcmp( bv1->bv_val, bv2->bv_val, len );
+       if ( !len )
+               len = dif;
+       return len;
+}
 
-               case LDAP_SCOPE_ONELEVEL:
-                       return (ndn_incoming->bv_len == ndn_stored->bv_len);
+/* compare the first value in each filter */
+static int pcache_filter_cmp( const void *v1, const void *v2 )
+{
+       const CachedQuery *q1 = v1, *q2 =v2;
+       int rc, weight1, weight2;
 
-               default:
-                       return 0;
-               }
-       case LDAP_SCOPE_SUBTREE:
-               return 1;
+       switch( q1->first->f_choice ) {
+       case LDAP_FILTER_PRESENT:
+               weight1 = 0;
+               break;
+       case LDAP_FILTER_EQUALITY:
+       case LDAP_FILTER_GE:
+       case LDAP_FILTER_LE:
+               weight1 = 1;
                break;
        default:
-               return 0;
+               weight1 = 2;
+       }
+       switch( q2->first->f_choice ) {
+       case LDAP_FILTER_PRESENT:
+               weight2 = 0;
                break;
-    }
+       case LDAP_FILTER_EQUALITY:
+       case LDAP_FILTER_GE:
+       case LDAP_FILTER_LE:
+               weight2 = 1;
+               break;
+       default:
+               weight2 = 2;
+       }
+       rc = weight1 - weight2;
+       if ( !rc ) {
+               switch( weight1 ) {
+               case 0: return 0;
+               case 1:
+                       rc = lex_bvcmp( &q1->first->f_av_value, &q2->first->f_av_value );
+                       break;
+               case 2:
+                       if ( q1->first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
+                               rc = 0;
+                               if ( !BER_BVISNULL( &q1->first->f_sub_initial )) {
+                                       if ( !BER_BVISNULL( &q2->first->f_sub_initial )) {
+                                               rc = lex_bvcmp( &q1->first->f_sub_initial,
+                                                       &q2->first->f_sub_initial );
+                                       } else {
+                                               rc = 1;
+                                       }
+                               } else if ( !BER_BVISNULL( &q2->first->f_sub_initial )) {
+                                       rc = -1;
+                               }
+                               if ( rc ) break;
+                               if ( q1->first->f_sub_any ) {
+                                       if ( q2->first->f_sub_any ) {
+                                               rc = lex_bvcmp( q1->first->f_sub_any,
+                                                       q2->first->f_sub_any );
+                                       } else {
+                                               rc = 1;
+                                       }
+                               } else if ( q2->first->f_sub_any ) {
+                                       rc = -1;
+                               }
+                               if ( rc ) break;
+                               if ( !BER_BVISNULL( &q1->first->f_sub_final )) {
+                                       if ( !BER_BVISNULL( &q2->first->f_sub_final )) {
+                                               rc = lex_bvcmp( &q1->first->f_sub_final,
+                                                       &q2->first->f_sub_final );
+                                       } else {
+                                               rc = 1;
+                                       }
+                               } else if ( !BER_BVISNULL( &q2->first->f_sub_final )) {
+                                       rc = -1;
+                               }
+                       } else {
+                               rc = lex_bvcmp( &q1->first->f_mr_value,
+                                       &q2->first->f_mr_value );
+                       }
+                       break;
+               }
+       }
+
+       return rc;
 }
 
 /* add query on top of LRU list */
@@ -253,7 +335,6 @@ static void
 add_query_on_top (query_manager* qm, CachedQuery* qc)
 {
        CachedQuery* top = qm->lru_top;
-       Query* q = (Query*)qc;
 
        qm->lru_top = qc;
 
@@ -264,8 +345,8 @@ add_query_on_top (query_manager* qm, CachedQuery* qc)
 
        qc->lru_down = top;
        qc->lru_up = NULL;
-       Debug( LDAP_DEBUG_TRACE, "Base of added query = %s\n",
-                       q->base.bv_val, 0, 0 );
+       Debug( pcache_debug, "Base of added query = %s\n",
+                       qc->qbase->base.bv_val, 0, 0 );
 }
 
 /* remove_query from LRU list */
@@ -416,7 +497,6 @@ strings_containment(struct berval* stored, struct berval* incoming)
 static int
 substr_containment_substr(Operation *op, Filter* stored, Filter* incoming)
 {
-       int i;
        int rc = 0;
 
        struct berval init_incoming;
@@ -471,134 +551,253 @@ final:
        return rc;
 }
 
+static Filter *
+filter_first( Filter *f )
+{
+       while ( f->f_choice == LDAP_FILTER_OR || f->f_choice == LDAP_FILTER_AND )
+               f = f->f_and;
+       return f;
+}
+
+
+static CachedQuery *
+find_filter( Operation *op, Avlnode *root, Filter *inputf, Filter *first )
+{
+       Filter* fs;
+       Filter* fi;
+       MatchingRule* mrule = NULL;
+       int res=0, eqpass= 0;
+       int ret, rc, dir;
+       Avlnode *ptr;
+       CachedQuery cq, *qc;
+
+       cq.filter = inputf;
+       cq.first = first;
+
+       /* substring matches sort to the end, and we just have to
+        * walk the entire list.
+        */
+       if ( first->f_choice == LDAP_FILTER_SUBSTRINGS ) {
+               ptr = tavl_end( root, 1 );
+               dir = TAVL_DIR_LEFT;
+       } else {
+               ptr = tavl_find3( root, &cq, pcache_filter_cmp, &ret );
+               dir = (first->f_choice == LDAP_FILTER_GE) ? TAVL_DIR_LEFT :
+                       TAVL_DIR_RIGHT;
+       }
+
+       while (ptr) {
+               qc = ptr->avl_data;
+               fi = inputf;
+               fs = qc->filter;
+
+               /* an incoming substr query can only be satisfied by a cached
+                * substr query.
+                */
+               if ( first->f_choice == LDAP_FILTER_SUBSTRINGS &&
+                       qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
+                       break;
+
+               /* an incoming eq query can be satisfied by a cached eq or substr
+                * query
+                */
+               if ( first->f_choice == LDAP_FILTER_EQUALITY ) {
+                       if ( eqpass == 0 ) {
+                               if ( qc->first->f_choice != LDAP_FILTER_EQUALITY ) {
+nextpass:                      eqpass = 1;
+                                       ptr = tavl_end( root, 1 );
+                                       dir = TAVL_DIR_LEFT;
+                                       continue;
+                               }
+                       } else {
+                               if ( qc->first->f_choice != LDAP_FILTER_SUBSTRINGS )
+                                       break;
+                       }
+               }
+               do {
+                       res=0;
+                       switch (fs->f_choice) {
+                       case LDAP_FILTER_EQUALITY:
+                               if (fi->f_choice == LDAP_FILTER_EQUALITY)
+                                       mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
+                               else
+                                       ret = 1;
+                               break;
+                       case LDAP_FILTER_GE:
+                       case LDAP_FILTER_LE:
+                               mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
+                               break;
+                       default:
+                               mrule = NULL; 
+                       }
+                       if (mrule) {
+                               const char *text;
+                               rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
+                                       SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
+                                       &(fi->f_ava->aa_value),
+                                       &(fs->f_ava->aa_value), &text);
+                               if (rc != LDAP_SUCCESS) {
+                                       return NULL;
+                               }
+                               if ( fi==first && fi->f_choice==LDAP_FILTER_EQUALITY && ret )
+                                       goto nextpass;
+                       }
+                       switch (fs->f_choice) {
+                       case LDAP_FILTER_OR:
+                       case LDAP_FILTER_AND:
+                               fs = fs->f_and;
+                               fi = fi->f_and;
+                               res=1;
+                               break;
+                       case LDAP_FILTER_SUBSTRINGS:
+                               /* check if the equality query can be
+                               * answered with cached substring query */
+                               if ((fi->f_choice == LDAP_FILTER_EQUALITY)
+                                       && substr_containment_equality( op,
+                                       fs, fi))
+                                       res=1;
+                               /* check if the substring query can be
+                               * answered with cached substring query */
+                               if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
+                                       ) && substr_containment_substr( op,
+                                       fs, fi))
+                                       res= 1;
+                               fs=fs->f_next;
+                               fi=fi->f_next;
+                               break;
+                       case LDAP_FILTER_PRESENT:
+                               res=1;
+                               fs=fs->f_next;
+                               fi=fi->f_next;
+                               break;
+                       case LDAP_FILTER_EQUALITY:
+                               if (ret == 0)
+                                       res = 1;
+                               fs=fs->f_next;
+                               fi=fi->f_next;
+                               break;
+                       case LDAP_FILTER_GE:
+                               if (mrule && ret >= 0)
+                                       res = 1;
+                               fs=fs->f_next;
+                               fi=fi->f_next;
+                               break;
+                       case LDAP_FILTER_LE:
+                               if (mrule && ret <= 0)
+                                       res = 1;
+                               fs=fs->f_next;
+                               fi=fi->f_next;
+                               break;
+                       case LDAP_FILTER_NOT:
+                               res=0;
+                               break;
+                       default:
+                               break;
+                       }
+               } while((res) && (fi != NULL) && (fs != NULL));
+
+               if ( res )
+                       return qc;
+               ptr = tavl_next( ptr, dir );
+       }
+       return NULL;
+}
+
 /* check whether query is contained in any of
- * the cached queries in template template_index
+ * the cached queries in template
  */
 static CachedQuery *
 query_containment(Operation *op, query_manager *qm,
                  Query *query,
-                 int template_index)
+                 QueryTemplate *templa)
 {
-       QueryTemplate* templa= qm->templates;
        CachedQuery* qc;
-       Query* q;
-       Filter* inputf = query->filter;
-       struct berval* base = &(query->base);
-       int scope = query->scope;
-       int res=0;
-       Filter* fs;
-       Filter* fi;
-       int ret, rc;
-       const char* text;
-
-       MatchingRule* mrule = NULL;
-       if (inputf != NULL) {
-               Debug( LDAP_DEBUG_TRACE, "Lock QC index = %d\n",
-                               template_index, 0, 0 );
-               ldap_pvt_thread_rdwr_rlock(&(templa[template_index].t_rwlock));
-               for(qc=templa[template_index].query; qc != NULL; qc= qc->next) {
-                       q = (Query*)qc;
-                       if(base_scope_compare(&(q->base), base, q->scope, scope)) {
-                               fi = inputf;
-                               fs = q->filter;
-                               do {
-                                       res=0;
-                                       switch (fs->f_choice) {
-                                       case LDAP_FILTER_EQUALITY:
-                                               if (fi->f_choice == LDAP_FILTER_EQUALITY)
-                                                       mrule = fs->f_ava->aa_desc->ad_type->sat_equality;
-                                               else
-                                                       ret = 1;
+       int depth = 0, tscope;
+       Qbase qbase, *qbptr = NULL;
+       struct berval pdn;
+
+       if (query->filter != NULL) {
+               Filter *first;
+
+               Debug( pcache_debug, "Lock QC index = %p\n",
+                               (void *) templa, 0, 0 );
+               qbase.base = query->base;
+
+               first = filter_first( query->filter );
+
+               ldap_pvt_thread_rdwr_rlock(&templa->t_rwlock);
+               for( ;; ) {
+                       /* Find the base */
+                       qbptr = avl_find( templa->qbase, &qbase, pcache_dn_cmp );
+                       if ( qbptr ) {
+                               tscope = query->scope;
+                               /* Find a matching scope:
+                                * match at depth 0 OK
+                                * scope is BASE,
+                                *      one at depth 1 OK
+                                *  subord at depth > 0 OK
+                                *      subtree at any depth OK
+                                * scope is ONE,
+                                *  subtree or subord at any depth OK
+                                * scope is SUBORD,
+                                *  subtree or subord at any depth OK
+                                * scope is SUBTREE,
+                                *  subord at depth > 0 OK
+                                *  subtree at any depth OK
+                                */
+                               for ( tscope = 0 ; tscope <= LDAP_SCOPE_CHILDREN; tscope++ ) {
+                                       switch ( query->scope ) {
+                                       case LDAP_SCOPE_BASE:
+                                               if ( tscope == LDAP_SCOPE_BASE && depth ) continue;
+                                               if ( tscope == LDAP_SCOPE_ONE && depth != 1) continue;
+                                               if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
                                                break;
-                                       case LDAP_FILTER_GE:
-                                       case LDAP_FILTER_LE:
-                                               mrule = fs->f_ava->aa_desc->ad_type->sat_ordering;
+                                       case LDAP_SCOPE_ONE:
+                                               if ( tscope == LDAP_SCOPE_BASE )
+                                                       tscope = LDAP_SCOPE_ONE;
+                                               if ( tscope == LDAP_SCOPE_ONE && depth ) continue;
+                                               if ( !depth ) break;
+                                               if ( tscope < LDAP_SCOPE_SUBTREE )
+                                                       tscope = LDAP_SCOPE_SUBTREE;
                                                break;
-                                       default:
-                                               mrule = NULL; 
-                                       }
-                                       if (mrule) {
-                                               rc = value_match(&ret, fs->f_ava->aa_desc, mrule,
-                                                       SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
-                                                       &(fi->f_ava->aa_value),
-                                                       &(fs->f_ava->aa_value), &text);
-                                               if (rc != LDAP_SUCCESS) {
-                                                       ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
-                                                       Debug( LDAP_DEBUG_TRACE,
-                                                       "Unlock: Exiting QC index=%d\n",
-                                                       template_index, 0, 0 );
-                                                       return NULL;
-                                               }
-                                       }
-                                       switch (fs->f_choice) {
-                                       case LDAP_FILTER_OR:
-                                       case LDAP_FILTER_AND:
-                                               fs = fs->f_and;
-                                               fi = fi->f_and;
-                                               res=1;
-                                               break;
-                                       case LDAP_FILTER_SUBSTRINGS:
-                                               /* check if the equality query can be
-                                               * answered with cached substring query */
-                                               if ((fi->f_choice == LDAP_FILTER_EQUALITY)
-                                                       && substr_containment_equality( op,
-                                                       fs, fi))
-                                                       res=1;
-                                               /* check if the substring query can be
-                                               * answered with cached substring query */
-                                               if ((fi->f_choice ==LDAP_FILTER_SUBSTRINGS
-                                                       ) && substr_containment_substr( op,
-                                                       fs, fi))
-                                                       res= 1;
-                                               fs=fs->f_next;
-                                               fi=fi->f_next;
-                                               break;
-                                       case LDAP_FILTER_PRESENT:
-                                               res=1;
-                                               fs=fs->f_next;
-                                               fi=fi->f_next;
-                                               break;
-                                       case LDAP_FILTER_EQUALITY:
-                                               if (ret == 0)
-                                                       res = 1;
-                                               fs=fs->f_next;
-                                               fi=fi->f_next;
-                                               break;
-                                       case LDAP_FILTER_GE:
-                                               if (ret >= 0)
-                                                       res = 1;
-                                               fs=fs->f_next;
-                                               fi=fi->f_next;
-                                               break;
-                                       case LDAP_FILTER_LE:
-                                               if (ret <= 0)
-                                                       res = 1;
-                                               fs=fs->f_next;
-                                               fi=fi->f_next;
+                                       case LDAP_SCOPE_SUBTREE:
+                                               if ( tscope < LDAP_SCOPE_SUBTREE )
+                                                       tscope = LDAP_SCOPE_SUBTREE;
+                                               if ( tscope == LDAP_SCOPE_CHILDREN && !depth ) continue;
                                                break;
-                                       case LDAP_FILTER_NOT:
-                                               res=0;
-                                               break;
-                                       default:
+                                       case LDAP_SCOPE_CHILDREN:
+                                               if ( tscope < LDAP_SCOPE_SUBTREE )
+                                                       tscope = LDAP_SCOPE_SUBTREE;
                                                break;
                                        }
-                               } while((res) && (fi != NULL) && (fs != NULL));
-
-                               if(res) {
-                                       ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
-                                       if (qm->lru_top != qc) {
-                                               remove_query(qm, qc);
-                                               add_query_on_top(qm, qc);
+                                       if ( !qbptr->scopes[tscope] ) continue;
+
+                                       /* Find filter */
+                                       qc = find_filter( op, qbptr->scopes[tscope],
+                                                       query->filter, first );
+                                       if ( qc ) {
+                                               ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
+                                               if (qm->lru_top != qc) {
+                                                       remove_query(qm, qc);
+                                                       add_query_on_top(qm, qc);
+                                               }
+                                               ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
+                                               return qc;
                                        }
-                                       ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
-                                       return qc;
                                }
                        }
+                       if ( be_issuffix( op->o_bd, &qbase.base ))
+                               break;
+                       /* Up a level */
+                       dnParent( &qbase.base, &pdn );
+                       qbase.base = pdn;
+                       depth++;
                }
-               Debug( LDAP_DEBUG_TRACE,
-                       "Not answerable: Unlock QC index=%d\n",
-                       template_index, 0, 0 );
-               ldap_pvt_thread_rdwr_runlock(&(templa[template_index].t_rwlock));
+
+               Debug( pcache_debug,
+                       "Not answerable: Unlock QC index=%p\n",
+                       (void *) templa, 0, 0 );
+               ldap_pvt_thread_rdwr_runlock(&templa->t_rwlock);
        }
        return NULL;
 }
@@ -606,69 +805,89 @@ query_containment(Operation *op, query_manager *qm,
 static void
 free_query (CachedQuery* qc)
 {
-       Query* q = (Query*)qc;
-       int i;
-
        free(qc->q_uuid.bv_val);
-       filter_free(q->filter);
-       free (q->base.bv_val);
-       free(q->attrs);
+       filter_free(qc->filter);
        free(qc);
 }
 
 
 /* Add query to query cache */
-static void add_query(
+static CachedQuery * add_query(
+       Operation *op,
        query_manager* qm,
        Query* query,
-       int template_index,
-       struct berval* uuid)
+       QueryTemplate *templ,
+       int positive)
 {
        CachedQuery* new_cached_query = (CachedQuery*) ch_malloc(sizeof(CachedQuery));
-       QueryTemplate* templ = (qm->templates)+template_index;
-       Query* new_query;
-       new_cached_query->template_id = template_index;
-       if ( uuid ) {
-               new_cached_query->q_uuid = *uuid;
+       Qbase *qbase, qb;
+       Filter *first;
+       int rc;
+
+       new_cached_query->qtemp = templ;
+       BER_BVZERO( &new_cached_query->q_uuid );
+       if ( positive ) {
                new_cached_query->expiry_time = slap_get_time() + templ->ttl;
        } else {
-               BER_BVZERO( &new_cached_query->q_uuid );
                new_cached_query->expiry_time = slap_get_time() + templ->negttl;
        }
        new_cached_query->lru_up = NULL;
        new_cached_query->lru_down = NULL;
-       Debug( LDAP_DEBUG_TRACE, "Added query expires at %ld\n",
+       Debug( pcache_debug, "Added query expires at %ld\n",
                        (long) new_cached_query->expiry_time, 0, 0 );
-       new_query = (Query*)new_cached_query;
 
-       ber_dupbv(&new_query->base, &query->base);
-       new_query->scope = query->scope;
-       new_query->filter = query->filter;
-       new_query->attrs = query->attrs;
+       new_cached_query->scope = query->scope;
+       new_cached_query->filter = query->filter;
+       new_cached_query->first = first = filter_first( query->filter );
+
+       qb.base = query->base;
 
        /* Adding a query    */
-       Debug( LDAP_DEBUG_TRACE, "Lock AQ index = %d\n",
-                       template_index, 0, 0 );
+       Debug( pcache_debug, "Lock AQ index = %p\n",
+                       (void *) templ, 0, 0 );
        ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
-       if (templ->query == NULL)
-               templ->query_last = new_cached_query;
-       else
-               templ->query->prev = new_cached_query;
+       qbase = avl_find( templ->qbase, &qb, pcache_dn_cmp );
+       if ( !qbase ) {
+               qbase = ch_calloc( 1, sizeof(Qbase) + qb.base.bv_len + 1 );
+               qbase->base.bv_len =qb.base.bv_len;
+               qbase->base.bv_val = (char *)(qbase+1);
+               memcpy( qbase->base.bv_val, qb.base.bv_val, qb.base.bv_len );
+               qbase->base.bv_val[qbase->base.bv_len] = '\0';
+               avl_insert( &templ->qbase, qbase, pcache_dn_cmp, avl_dup_error );
+       }
        new_cached_query->next = templ->query;
        new_cached_query->prev = NULL;
-       templ->query = new_cached_query;
-       templ->no_of_queries++;
-       Debug( LDAP_DEBUG_TRACE, "TEMPLATE %d QUERIES++ %d\n",
-                       template_index, templ->no_of_queries, 0 );
+       new_cached_query->qbase = qbase;
+       rc = tavl_insert( &qbase->scopes[query->scope], new_cached_query,
+               pcache_filter_cmp, avl_dup_error );
+       if ( rc == 0 ) {
+               qbase->queries++;
+               if (templ->query == NULL)
+                       templ->query_last = new_cached_query;
+               else
+                       templ->query->prev = new_cached_query;
+               templ->query = new_cached_query;
+               templ->no_of_queries++;
+       } else {
+               ch_free( new_cached_query );
+               new_cached_query = find_filter( op, qbase->scopes[query->scope],
+                                                       query->filter, first );
+               filter_free( query->filter );
+       }
+       Debug( pcache_debug, "TEMPLATE %p QUERIES++ %d\n",
+                       (void *) templ, templ->no_of_queries, 0 );
 
-       Debug( LDAP_DEBUG_TRACE, "Unlock AQ index = %d \n",
-                       template_index, 0, 0 );
+       Debug( pcache_debug, "Unlock AQ index = %p \n",
+                       (void *) templ, 0, 0 );
        ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
 
        /* Adding on top of LRU list  */
-       ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
-       add_query_on_top(qm, new_cached_query);
-       ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
+       if ( rc == 0 ) {
+               ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
+               add_query_on_top(qm, new_cached_query);
+               ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
+       }
+       return rc == 0 ? new_cached_query : NULL;
 }
 
 static void
@@ -686,6 +905,13 @@ remove_from_template (CachedQuery* qc, QueryTemplate* template)
                qc->next->prev = qc->prev;
                qc->prev->next = qc->next;
        }
+       tavl_delete( &qc->qbase->scopes[qc->scope], qc, pcache_filter_cmp );
+       qc->qbase->queries--;
+       if ( qc->qbase->queries == 0 ) {
+               avl_delete( &template->qbase, qc->qbase, pcache_dn_cmp );
+               ch_free( qc->qbase );
+               qc->qbase = NULL;
+       }
 
        template->no_of_queries--;
 }
@@ -694,7 +920,7 @@ remove_from_template (CachedQuery* qc, QueryTemplate* template)
 static void cache_replacement(query_manager* qm, struct berval *result)
 {
        CachedQuery* bottom;
-       int temp_id;
+       QueryTemplate *temp;
 
        ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
        bottom = qm->lru_bottom;
@@ -703,27 +929,27 @@ static void cache_replacement(query_manager* qm, struct berval *result)
        result->bv_len = 0;
 
        if (!bottom) {
-               Debug ( LDAP_DEBUG_TRACE,
+               Debug ( pcache_debug,
                        "Cache replacement invoked without "
                        "any query in LRU list\n", 0, 0, 0 );
                ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
                return;
        }
 
-       temp_id = bottom->template_id;
+       temp = bottom->qtemp;
        remove_query(qm, bottom);
        ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
 
        *result = bottom->q_uuid;
        bottom->q_uuid.bv_val = NULL;
 
-       Debug( LDAP_DEBUG_TRACE, "Lock CR index = %d\n", temp_id, 0, 0 );
-       ldap_pvt_thread_rdwr_wlock(&(qm->templates[temp_id].t_rwlock));
-       remove_from_template(bottom, (qm->templates+temp_id));
-       Debug( LDAP_DEBUG_TRACE, "TEMPLATE %d QUERIES-- %d\n",
-               temp_id, qm->templates[temp_id].no_of_queries, 0 );
-       Debug( LDAP_DEBUG_TRACE, "Unlock CR index = %d\n", temp_id, 0, 0 );
-       ldap_pvt_thread_rdwr_wunlock(&(qm->templates[temp_id].t_rwlock));
+       Debug( pcache_debug, "Lock CR index = %p\n", (void *) temp, 0, 0 );
+       ldap_pvt_thread_rdwr_wlock(&temp->t_rwlock);
+       remove_from_template(bottom, temp);
+       Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
+               (void *) temp, temp->no_of_queries, 0 );
+       Debug( pcache_debug, "Unlock CR index = %p\n", (void *) temp, 0, 0 );
+       ldap_pvt_thread_rdwr_wunlock(&temp->t_rwlock);
        free_query(bottom);
 }
 
@@ -815,7 +1041,7 @@ remove_query_data (
                op->o_req_ndn = qi->xdn;
 
                if ( qi->del) {
-                       Debug( LDAP_DEBUG_TRACE, "DELETING ENTRY TEMPLATE=%s\n",
+                       Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
                                query_uuid->bv_val, 0, 0 );
 
                        op->o_tag = LDAP_REQ_DELETE;
@@ -837,7 +1063,7 @@ remove_query_data (
                        mod.sml_values = vals;
                        mod.sml_nvalues = NULL;
                        mod.sml_next = NULL;
-                       Debug( LDAP_DEBUG_TRACE,
+                       Debug( pcache_debug,
                                "REMOVING TEMP ATTR : TEMPLATE=%s\n",
                                query_uuid->bv_val, 0, 0 );
 
@@ -949,7 +1175,8 @@ filter2template(
 struct search_info {
        slap_overinst *on;
        Query query;
-       int template_id;
+       QueryTemplate *qtemp;
+       AttributeName*  save_attrs;     /* original attributes, saved for response */
        int max;
        int over;
        int count;
@@ -979,35 +1206,32 @@ cache_entries(
        op_tmp.o_dn = cm->db.be_rootdn;
        op_tmp.o_ndn = cm->db.be_rootndn;
 
-       Debug( LDAP_DEBUG_TRACE, "UUID for query being added = %s\n",
+       Debug( pcache_debug, "UUID for query being added = %s\n",
                        uuidbuf, 0, 0 );
 
        for ( e=si->head; e; e=si->head ) {
                si->head = e->e_private;
                e->e_private = NULL;
-               Debug( LDAP_DEBUG_NONE, "LOCKING REMOVE MUTEX\n", 0, 0, 0 );
-               ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
-               Debug( LDAP_DEBUG_NONE, "LOCKED REMOVE MUTEX\n", 0, 0, 0);
                while ( cm->cur_entries > (cm->max_entries) ) {
                                qm->crfunc(qm, &crp_uuid);
                                if (crp_uuid.bv_val) {
-                                       Debug( LDAP_DEBUG_TRACE,
+                                       Debug( pcache_debug,
                                                "Removing query UUID %s\n",
                                                crp_uuid.bv_val, 0, 0 );
                                        return_val = remove_query_data(&op_tmp, rs, &crp_uuid);
-                                       Debug( LDAP_DEBUG_TRACE,
+                                       Debug( pcache_debug,
                                                "QUERY REMOVED, SIZE=%d\n",
                                                return_val, 0, 0);
                                        ldap_pvt_thread_mutex_lock(
                                                        &cm->cache_mutex );
                                        cm->cur_entries -= return_val;
                                        cm->num_cached_queries--;
-                                       Debug( LDAP_DEBUG_TRACE,
+                                       Debug( pcache_debug,
                                                "STORED QUERIES = %lu\n",
                                                cm->num_cached_queries, 0, 0 );
                                        ldap_pvt_thread_mutex_unlock(
                                                        &cm->cache_mutex );
-                                       Debug( LDAP_DEBUG_TRACE,
+                                       Debug( pcache_debug,
                                                "QUERY REMOVED, CACHE ="
                                                "%d entries\n",
                                                cm->cur_entries, 0, 0 );
@@ -1015,10 +1239,9 @@ cache_entries(
                }
 
                return_val = merge_entry(&op_tmp, e, query_uuid);
-               ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
                ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
                cm->cur_entries += return_val;
-               Debug( LDAP_DEBUG_TRACE,
+               Debug( pcache_debug,
                        "ENTRY ADDED/MERGED, CACHED ENTRIES=%d\n",
                        cm->cur_entries, 0, 0 );
                return_val = 0;
@@ -1037,12 +1260,11 @@ pcache_response(
        slap_overinst *on = si->on;
        cache_manager *cm = on->on_bi.bi_private;
        query_manager*          qm = cm->qm;
-       struct berval uuid;
 
-       if ( si->query.save_attrs != NULL ) {
-               rs->sr_attrs = si->query.save_attrs;
-               op->ors_attrs = si->query.save_attrs;
-               si->query.save_attrs = NULL;
+       if ( si->save_attrs != NULL ) {
+               rs->sr_attrs = si->save_attrs;
+               op->ors_attrs = si->save_attrs;
+               si->save_attrs = NULL;
        }
 
        if ( rs->sr_type == REP_SEARCH ) {
@@ -1071,31 +1293,42 @@ pcache_response(
                }
 
        } else if ( rs->sr_type == REP_RESULT ) {
-               QueryTemplate* templ = (qm->templates)+si->template_id;
-               if (( si->count && cache_entries( op, rs, &uuid ) == 0 ) ||
-                       ( !si->count && templ->negttl && rs->sr_err == LDAP_SUCCESS )) {
-                       qm->addfunc(qm, &si->query, si->template_id,
-                               si->count ? &uuid : NULL);
-
-                       ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
-                       cm->num_cached_queries++;
-                       Debug( LDAP_DEBUG_TRACE, "STORED QUERIES = %lu\n",
-                                       cm->num_cached_queries, 0, 0 );
-                       ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
+               if ( si->count ||
+                       ( si->qtemp->negttl && !si->count && !si->over &&
+                               rs->sr_err == LDAP_SUCCESS )) {
+                       CachedQuery *qc = qm->addfunc(op, qm, &si->query, si->qtemp,
+                               si->count);
+
+                       if ( qc != NULL ) {
+                               if ( si->count )
+                                       cache_entries( op, rs, &qc->q_uuid );
+                               ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
+                               cm->num_cached_queries++;
+                               Debug( pcache_debug, "STORED QUERIES = %lu\n",
+                                               cm->num_cached_queries, 0, 0 );
+                               ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
 
-                       /* If the consistency checker suspended itself,
-                        * wake it back up
-                        */
-                       if ( cm->cc_paused ) {
-                               ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
+                               /* If the consistency checker suspended itself,
+                                * wake it back up
+                                */
                                if ( cm->cc_paused ) {
-                                       cm->cc_paused = 0;
-                                       ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
+                                       ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
+                                       if ( cm->cc_paused ) {
+                                               cm->cc_paused = 0;
+                                               ldap_pvt_runqueue_resched( &slapd_rq, cm->cc_arg, 0 );
+                                       }
+                                       ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
+                               }
+                       } else if ( si->count ) {
+                               /* duplicate query, free it */
+                               Entry *e;
+                               for (;si->head; si->head=e) {
+                                       e = si->head->e_private;
+                                       si->head->e_private = NULL;
+                                       entry_free(si->head);
                                }
-                               ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
                        }
                } else {
-                       free( si->query.attrs );
                        filter_free( si->query.filter );
                }
 
@@ -1182,7 +1415,7 @@ pcache_chk_controls(
                /* fallthru */
 
        case SLAP_CONTROL_CRITICAL:
-               Debug( LDAP_DEBUG_TRACE, "%s: "
+               Debug( pcache_debug, "%s: "
                        "%scritical pagedResults control "
                        "disabled with proxy cache%s.\n",
                        op->o_log_prefix, non, stripped );
@@ -1207,21 +1440,18 @@ pcache_op_search(
        cache_manager *cm = on->on_bi.bi_private;
        query_manager*          qm = cm->qm;
 
-       int count;
-
        int i = -1;
 
        AttributeName   *filter_attrs = NULL;
 
        Query           query;
+       QueryTemplate *qtemp = NULL;
 
        int             attr_set = -1;
-       int             template_id = -1;
        CachedQuery     *answerable = NULL;
        int             cacheable = 0;
        int             fattr_cnt=0;
        int             fattr_got_oc = 0;
-       int             oc_attr_absent = 1;
 
        struct berval tempstr;
 
@@ -1233,35 +1463,31 @@ pcache_op_search(
                return SLAP_CB_CONTINUE;
        }
 
-       Debug( LDAP_DEBUG_TRACE, "query template of incoming query = %s\n",
+       Debug( pcache_debug, "query template of incoming query = %s\n",
                                        tempstr.bv_val, 0, 0 );
 
        /* FIXME: cannot cache/answer requests with pagedResults control */
-       
 
        /* find attr set */
        attr_set = get_attr_set(op->ors_attrs, qm, cm->numattrsets);
 
        query.filter = op->ors_filter;
-       query.attrs = op->ors_attrs;
-       query.save_attrs = NULL;
        query.base = op->o_req_ndn;
        query.scope = op->ors_scope;
 
        /* check for query containment */
        if (attr_set > -1) {
-               QueryTemplate *qt = qm->templates;
-               for (i=0; i<cm->numtemplates; i++, qt++) {
+               QueryTemplate *qt = qm->attr_sets[attr_set].templates;
+               for (; qt; qt = qt->qtnext ) {
                        /* find if template i can potentially answer tempstr */
-                       if ( qt->attr_set_index != attr_set ||
-                               qt->querystr.bv_len != tempstr.bv_len ||
+                       if (qt->querystr.bv_len != tempstr.bv_len ||
                                strcasecmp( qt->querystr.bv_val, tempstr.bv_val ))
                                continue;
                        cacheable = 1;
-                       template_id = i;
-                       Debug( LDAP_DEBUG_NONE, "Entering QC, querystr = %s\n",
+                       qtemp = qt;
+                       Debug( pcache_debug, "Entering QC, querystr = %s\n",
                                        op->ors_filterstr.bv_val, 0, 0 );
-                       answerable = (*(qm->qcfunc))(op, qm, &query, i);
+                       answerable = (*(qm->qcfunc))(op, qm, &query, qt);
 
                        if (answerable)
                                break;
@@ -1269,18 +1495,14 @@ pcache_op_search(
        }
        op->o_tmpfree( tempstr.bv_val, op->o_tmpmemctx );
 
-       query.save_attrs = op->ors_attrs;
-       query.attrs = NULL;
-
        if (answerable) {
                /* Need to clear the callbacks of the original operation,
                 * in case there are other overlays */
                BackendDB       *save_bd = op->o_bd;
                slap_callback   *save_cb = op->o_callback;
 
-               Debug( LDAP_DEBUG_TRACE, "QUERY ANSWERABLE\n", 0, 0, 0 );
+               Debug( pcache_debug, "QUERY ANSWERABLE\n", 0, 0, 0 );
                op->o_tmpfree( filter_attrs, op->o_tmpmemctx );
-               ldap_pvt_thread_rdwr_runlock(&qm->templates[i].t_rwlock);
                if ( BER_BVISNULL( &answerable->q_uuid )) {
                        /* No entries cached, just an empty result set */
                        i = rs->sr_err = 0;
@@ -1290,12 +1512,13 @@ pcache_op_search(
                        op->o_callback = NULL;
                        i = cm->db.bd_info->bi_op_search( op, rs );
                }
+               ldap_pvt_thread_rdwr_runlock(&qtemp->t_rwlock);
                op->o_bd = save_bd;
                op->o_callback = save_cb;
                return i;
        }
 
-       Debug( LDAP_DEBUG_TRACE, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
+       Debug( pcache_debug, "QUERY NOT ANSWERABLE\n", 0, 0, 0 );
 
        ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
        if (cm->num_cached_queries >= cm->max_queries) {
@@ -1303,16 +1526,22 @@ pcache_op_search(
        }
        ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
 
+       if (op->ors_attrsonly)
+               cacheable = 0;
+
        if (cacheable) {
                slap_callback           *cb;
                struct search_info      *si;
 
-               Debug( LDAP_DEBUG_TRACE, "QUERY CACHEABLE\n", 0, 0, 0 );
+               Debug( pcache_debug, "QUERY CACHEABLE\n", 0, 0, 0 );
                query.filter = filter_dup(op->ors_filter, NULL);
-               add_filter_attrs(op, &query.attrs, &qm->attr_sets[attr_set],
-                       filter_attrs, fattr_cnt, fattr_got_oc);
-
-               op->ors_attrs = query.attrs;
+               ldap_pvt_thread_rdwr_wlock(&qtemp->t_rwlock);
+               if ( !qtemp->t_attrs.count ) {
+                       add_filter_attrs(op, &qtemp->t_attrs.attrs,
+                               &qm->attr_sets[attr_set],
+                               filter_attrs, fattr_cnt, fattr_got_oc);
+               }
+               ldap_pvt_thread_rdwr_wunlock(&qtemp->t_rwlock);
 
                cb = op->o_tmpalloc( sizeof(*cb) + sizeof(*si), op->o_tmpmemctx);
                cb->sc_response = pcache_response;
@@ -1321,12 +1550,15 @@ pcache_op_search(
                si = cb->sc_private;
                si->on = on;
                si->query = query;
-               si->template_id = template_id;
+               si->qtemp = qtemp;
                si->max = cm->num_entries_limit ;
                si->over = 0;
                si->count = 0;
                si->head = NULL;
                si->tail = NULL;
+               si->save_attrs = op->ors_attrs;
+
+               op->ors_attrs = qtemp->t_attrs.attrs;
 
                if ( cm->response_cb == PCACHE_RESPONSE_CB_HEAD ) {
                        cb->sc_next = op->o_callback;
@@ -1344,7 +1576,7 @@ pcache_op_search(
                }
 
        } else {
-               Debug( LDAP_DEBUG_TRACE, "QUERY NOT CACHEABLE\n",
+               Debug( pcache_debug, "QUERY NOT CACHEABLE\n",
                                        0, 0, 0);
        }
 
@@ -1405,8 +1637,8 @@ consistency_check(
        Operation *op;
 
        SlapReply rs = {REP_RESULT};
-       CachedQuery* query, *query_prev;
-       int i, return_val, pause = 1;
+       CachedQuery* query;
+       int return_val, pause = 1;
        QueryTemplate* templ;
 
        op = (Operation *) &opbuf;
@@ -1418,46 +1650,50 @@ consistency_check(
 
        cm->cc_arg = arg;
 
-       for (i=0; qm->templates[i].querystr.bv_val; i++) {
-               templ = qm->templates + i;
+       for (templ = qm->templates; templ; templ=templ->qmnext) {
                query = templ->query_last;
                if ( query ) pause = 0;
                op->o_time = slap_get_time();
-               ldap_pvt_thread_mutex_lock(&cm->remove_mutex);
                while (query && (query->expiry_time < op->o_time)) {
+                       int rem = 0;
+                       Debug( pcache_debug, "Lock CR index = %p\n",
+                                       (void *) templ, 0, 0 );
+                       ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
+                       if ( query == templ->query_last ) {
+                               rem = 1;
+                               remove_from_template(query, templ);
+                               Debug( pcache_debug, "TEMPLATE %p QUERIES-- %d\n",
+                                               (void *) templ, templ->no_of_queries, 0 );
+                               Debug( pcache_debug, "Unlock CR index = %p\n",
+                                               (void *) templ, 0, 0 );
+                       }
+                       ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
+                       if ( !rem ) {
+                               query = templ->query_last;
+                               continue;
+                       }
                        ldap_pvt_thread_mutex_lock(&qm->lru_mutex);
                        remove_query(qm, query);
                        ldap_pvt_thread_mutex_unlock(&qm->lru_mutex);
-                       Debug( LDAP_DEBUG_TRACE, "Lock CR index = %d\n",
-                                       i, 0, 0 );
-                       ldap_pvt_thread_rdwr_wlock(&templ->t_rwlock);
-                       remove_from_template(query, templ);
-                       Debug( LDAP_DEBUG_TRACE, "TEMPLATE %d QUERIES-- %d\n",
-                                       i, templ->no_of_queries, 0 );
-                       Debug( LDAP_DEBUG_TRACE, "Unlock CR index = %d\n",
-                                       i, 0, 0 );
-                       ldap_pvt_thread_rdwr_wunlock(&templ->t_rwlock);
                        if ( BER_BVISNULL( &query->q_uuid ))
                                return_val = 0;
                        else
                                return_val = remove_query_data(op, &rs, &query->q_uuid);
-                       Debug( LDAP_DEBUG_TRACE, "STALE QUERY REMOVED, SIZE=%d\n",
+                       Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
                                                return_val, 0, 0 );
                        ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
                        cm->cur_entries -= return_val;
                        cm->num_cached_queries--;
-                       Debug( LDAP_DEBUG_TRACE, "STORED QUERIES = %lu\n",
+                       Debug( pcache_debug, "STORED QUERIES = %lu\n",
                                        cm->num_cached_queries, 0, 0 );
                        ldap_pvt_thread_mutex_unlock(&cm->cache_mutex);
-                       Debug( LDAP_DEBUG_TRACE,
+                       Debug( pcache_debug,
                                "STALE QUERY REMOVED, CACHE ="
                                "%d entries\n",
                                cm->cur_entries, 0, 0 );
-                       query_prev = query;
-                       query = query->prev;
-                       free_query(query_prev);
+                       free_query(query);
+                       query = templ->query_last;
                }
-               ldap_pvt_thread_mutex_unlock(&cm->remove_mutex);
        }
        ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
        if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
@@ -1617,23 +1853,23 @@ pc_cf_gen( ConfigArgs *c )
                                rc = 1;
                        break;
                case PC_TEMP:
-                       for (i=0; i<cm->numtemplates; i++) {
-                               if ( qm->templates[i].negttl ) {
+                       for (temp=qm->templates; temp; temp=temp->qmnext) {
+                               if ( temp->negttl ) {
                                        bv.bv_len = snprintf( c->msg, sizeof( c->msg ),
                                                " %d %ld %ld",
-                                               qm->templates[i].attr_set_index,
-                                               qm->templates[i].ttl,
-                                               qm->templates[i].negttl );
+                                               temp->attr_set_index,
+                                               temp->ttl,
+                                               temp->negttl );
                                } else {
                                        bv.bv_len = snprintf( c->msg, sizeof( c->msg ), " %d %ld",
-                                               qm->templates[i].attr_set_index,
-                                               qm->templates[i].ttl );
+                                               temp->attr_set_index,
+                                               temp->ttl );
                                }
-                               bv.bv_len += qm->templates[i].querystr.bv_len + 2;
+                               bv.bv_len += temp->querystr.bv_len + 2;
                                bv.bv_val = ch_malloc( bv.bv_len+1 );
                                ptr = bv.bv_val;
                                *ptr++ = '"';
-                               ptr = lutil_strcopy( ptr, qm->templates[i].querystr.bv_val );
+                               ptr = lutil_strcopy( ptr, temp->querystr.bv_val );
                                *ptr++ = '"';
                                strcpy( ptr, c->msg );
                                ber_bvarray_add( &c->rvalue_vals, &bv );
@@ -1690,7 +1926,7 @@ pc_cf_gen( ConfigArgs *c )
                        return( 1 );
                }
 
-               if ( !backend_db_init( c->argv[1], &cm->db )) {
+               if ( !backend_db_init( c->argv[1], &cm->db, -1 )) {
                        snprintf( c->msg, sizeof( c->msg ), "unknown backend type (arg #1)" );
                        Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
                        return( 1 );
@@ -1732,7 +1968,7 @@ pc_cf_gen( ConfigArgs *c )
                        return( 1 );
                }
                cm->cc_period = (time_t)t;
-               Debug( LDAP_DEBUG_TRACE,
+               Debug( pcache_debug,
                                "Total # of attribute sets to be cached = %d.\n",
                                cm->numattrsets, 0, 0 );
                qm->attr_sets = ( struct attr_set * )ch_calloc( cm->numattrsets,
@@ -1798,16 +2034,16 @@ pc_cf_gen( ConfigArgs *c )
                        return( 1 );
                }
 
-               if ( i < 0 || i >= cm->numattrsets ) {
+               if ( i < 0 || i >= cm->numattrsets || 
+                       !(qm->attr_sets[i].flags & PC_CONFIGURED )) {
                        snprintf( c->msg, sizeof( c->msg ), "template index %d invalid (%s%d)",
                                i, cm->numattrsets > 1 ? "0->" : "", cm->numattrsets - 1 );
                        Debug( LDAP_DEBUG_CONFIG, "%s: %s.\n", c->log, c->msg, 0 );
                        return 1;
                }
-               num = cm->numtemplates;
-               qm->templates = ( QueryTemplate* )ch_realloc( qm->templates,
-                               ( num + 2 ) * sizeof( QueryTemplate ));
-               temp = qm->templates + num;
+               temp = ch_calloc( 1, sizeof( QueryTemplate ));
+               temp->qmnext = qm->templates;
+               qm->templates = temp;
                ldap_pvt_thread_rdwr_init( &temp->t_rwlock );
                temp->query = temp->query_last = NULL;
                if ( lutil_parse_time( c->argv[3], &t ) != 0 ) {
@@ -1833,20 +2069,19 @@ pc_cf_gen( ConfigArgs *c )
                temp->no_of_queries = 0;
 
                ber_str2bv( c->argv[1], 0, 1, &temp->querystr );
-               Debug( LDAP_DEBUG_TRACE, "Template:\n", 0, 0, 0 );
-               Debug( LDAP_DEBUG_TRACE, "  query template: %s\n",
+               Debug( pcache_debug, "Template:\n", 0, 0, 0 );
+               Debug( pcache_debug, "  query template: %s\n",
                                temp->querystr.bv_val, 0, 0 );
                temp->attr_set_index = i;
                qm->attr_sets[i].flags |= PC_REFERENCED;
-               Debug( LDAP_DEBUG_TRACE, "  attributes: \n", 0, 0, 0 );
+               temp->qtnext = qm->attr_sets[i].templates;
+               qm->attr_sets[i].templates = temp;
+               Debug( pcache_debug, "  attributes: \n", 0, 0, 0 );
                if ( ( attrarray = qm->attr_sets[i].attrs ) != NULL ) {
                        for ( i=0; attrarray[i].an_name.bv_val; i++ )
-                               Debug( LDAP_DEBUG_TRACE, "\t%s\n",
+                               Debug( pcache_debug, "\t%s\n",
                                        attrarray[i].an_name.bv_val, 0, 0 );
                }
-               temp++; 
-               temp->querystr.bv_val = NULL;
-               cm->numtemplates++;
                break;
        case PC_RESP:
                if ( strcasecmp( c->argv[1], "head" ) == 0 ) {
@@ -1894,8 +2129,7 @@ pcache_db_config(
 
 static int
 pcache_db_init(
-       BackendDB *be
-)
+       BackendDB *be )
 {
        slap_overinst *on = (slap_overinst *)be->bd_info;
        cache_manager *cm;
@@ -1912,7 +2146,6 @@ pcache_db_init(
        cm->db.be_pcl_mutexp = &cm->db.be_pcl_mutex;
        cm->qm = qm;
        cm->numattrsets = 0;
-       cm->numtemplates = 0; 
        cm->num_entries_limit = 5;
        cm->num_cached_queries = 0;
        cm->max_entries = 0;
@@ -1933,14 +2166,12 @@ pcache_db_init(
        ldap_pvt_thread_mutex_init(&qm->lru_mutex);
 
        ldap_pvt_thread_mutex_init(&cm->cache_mutex);
-       ldap_pvt_thread_mutex_init(&cm->remove_mutex);
        return 0;
 }
 
 static int
 pcache_db_open(
-       BackendDB *be
-)
+       BackendDB *be )
 {
        slap_overinst   *on = (slap_overinst *)be->bd_info;
        cache_manager   *cm = on->on_bi.bi_private;
@@ -1981,6 +2212,13 @@ pcache_db_open(
        cm->db.be_acl = be->be_acl;
        cm->db.be_dfltaccess = be->be_dfltaccess;
 
+       if ( SLAP_DBMONITORING( be ) ) {
+               SLAP_DBFLAGS( &cm->db ) |= SLAP_DBFLAG_MONITORING;
+
+       } else {
+               SLAP_DBFLAGS( &cm->db ) &= ~SLAP_DBFLAG_MONITORING;
+       }
+
        rc = backend_startup_one( &cm->db );
 
        /* There is no runqueue in TOOL mode */
@@ -2008,6 +2246,17 @@ pcache_db_open(
        return rc;
 }
 
+static void
+pcache_free_qbase( void *v )
+{
+       Qbase *qb = v;
+       int i;
+
+       for (i=0; i<3; i++)
+               tavl_free( qb->scopes[i], NULL );
+       ch_free( qb );
+}
+
 static int
 pcache_db_close(
        BackendDB *be
@@ -2016,7 +2265,8 @@ pcache_db_close(
        slap_overinst *on = (slap_overinst *)be->bd_info;
        cache_manager *cm = on->on_bi.bi_private;
        query_manager *qm = cm->qm;
-       int i, j, rc = 0;
+       QueryTemplate *tm;
+       int i, rc = 0;
 
        /* cleanup stuff inherited from the original database... */
        cm->db.be_limits = NULL;
@@ -2025,17 +2275,19 @@ pcache_db_close(
        if ( cm->db.bd_info->bi_db_close ) {
                rc = cm->db.bd_info->bi_db_close( &cm->db );
        }
-       for ( i=0; i<cm->numtemplates; i++ ) {
+       while ( (tm = qm->templates) != NULL ) {
                CachedQuery *qc, *qn;
-               for ( qc = qm->templates[i].query; qc; qc = qn ) {
+               qm->templates = tm->qmnext;
+               for ( qc = tm->query; qc; qc = qn ) {
                        qn = qc->next;
                        free_query( qc );
                }
-               free( qm->templates[i].querystr.bv_val );
-               ldap_pvt_thread_rdwr_destroy( &qm->templates[i].t_rwlock );
+               avl_free( tm->qbase, pcache_free_qbase );
+               free( tm->querystr.bv_val );
+               ldap_pvt_thread_rdwr_destroy( &tm->t_rwlock );
+               free( tm->t_attrs.attrs );
+               free( tm );
        }
-       free( qm->templates );
-       qm->templates = NULL;
 
        for ( i=0; i<cm->numattrsets; i++ ) {
                free( qm->attr_sets[i].attrs );
@@ -2055,21 +2307,12 @@ pcache_db_destroy(
        cache_manager *cm = on->on_bi.bi_private;
        query_manager *qm = cm->qm;
 
-       /* cleanup stuff inherited from the original database... */
-       cm->db.be_suffix = NULL;
-       cm->db.be_nsuffix = NULL;
-       BER_BVZERO( &cm->db.be_rootdn );
-       BER_BVZERO( &cm->db.be_rootndn );
-       BER_BVZERO( &cm->db.be_rootpw );
-       /* FIXME: there might be more... */
-
        if ( cm->db.be_private != NULL ) {
-               backend_destroy_one( &cm->db, 0 );
+               backend_stopdown_one( &cm->db );
        }
 
        ldap_pvt_thread_mutex_destroy( &qm->lru_mutex );
        ldap_pvt_thread_mutex_destroy( &cm->cache_mutex );
-       ldap_pvt_thread_mutex_destroy( &cm->remove_mutex );
        free( qm );
        free( cm );
 
@@ -2085,27 +2328,16 @@ static char *obsolete_names[] = {
 
 int pcache_initialize()
 {
-       LDAPAttributeType *at;
        int code;
-       const char *err;
+       struct berval debugbv = BER_BVC("pcache");
 
-       at = ldap_str2attributetype( queryid_schema, &code, &err,
-               LDAP_SCHEMA_ALLOW_ALL );
-       if ( !at ) {
-               Debug( LDAP_DEBUG_ANY,
-                       "pcache_initialize: ldap_str2attributetype failed %s %s\n",
-                       ldap_scherr2str(code), err, 0 );
+       if (( code = slap_loglevel_get( &debugbv, &pcache_debug )))
                return code;
-       }
-       code = at_add( at, 0, NULL, &err );
-       if ( !code ) {
-               slap_str2ad( at->at_names[0], &ad_queryid, &err );
-       }
-       ldap_memfree( at );
+
+       code = register_at( queryid_schema, &ad_queryid, 0 );
        if ( code ) {
                Debug( LDAP_DEBUG_ANY,
-                       "pcache_initialize: at_add failed %s %s\n",
-                       scherr2str(code), err, 0 );
+                       "pcache_initialize: register_at failed\n", 0, 0, 0 );
                return code;
        }