move schema_fill_constructed() call to dsdb_setup_sorted_accessors()
[amitay/samba.git] / source4 / dsdb / schema / schema_set.c
index 21341157fe5ed265c4cfce47effbc9e453ab6776..8cb06bb70e834d84c226440341e94d0c9bd20746 100644 (file)
@@ -4,6 +4,7 @@
 
    Copyright (C) Stefan Metzmacher <metze@samba.org> 2006-2007
    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006-2008
+   Copyright (C) Matthieu Patou <mat@matws.net> 2011
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "includes.h"
 #include "lib/util/dlinklist.h"
 #include "dsdb/samdb/samdb.h"
-#include "lib/ldb/include/ldb_module.h"
+#include <ldb_module.h>
 #include "param/param.h"
 #include "librpc/ndr/libndr.h"
 #include "librpc/gen_ndr/ndr_misc.h"
 #include "lib/util/tsort.h"
 
+/* change this when we change something in our schema code that
+ * requires a re-index of the database
+ */
+#define SAMDB_INDEXING_VERSION "2"
+
 /*
   override the name to attribute handler function
  */
@@ -94,6 +100,12 @@ static int dsdb_schema_set_attributes(struct ldb_context *ldb, struct dsdb_schem
                goto op_error;
        }
 
+
+       ret = ldb_msg_add_string(msg_idx, "@IDXVERSION", SAMDB_INDEXING_VERSION);
+       if (ret != LDB_SUCCESS) {
+               goto op_error;
+       }
+
        for (attr = schema->attributes; attr; attr = attr->next) {
                const char *syntax = attr->syntax->ldb_syntax;
 
@@ -199,6 +211,44 @@ op_error:
        return ldb_operr(ldb);
 }
 
+
+/*
+  create extra attribute shortcuts
+ */
+static void dsdb_setup_attribute_shortcuts(struct ldb_context *ldb, struct dsdb_schema *schema)
+{
+       struct dsdb_attribute *attribute;
+
+       /* setup fast access to one_way_link and DN format */
+       for (attribute=schema->attributes; attribute; attribute=attribute->next) {
+               attribute->dn_format = dsdb_dn_oid_to_format(attribute->syntax->ldap_oid);
+
+               if (attribute->dn_format == DSDB_INVALID_DN) {
+                       attribute->one_way_link = false;
+                       continue;
+               }
+
+               /* these are not considered to be one way links for
+                  the purpose of DN link fixups */
+               if (ldb_attr_cmp("distinguishedName", attribute->lDAPDisplayName) == 0 ||
+                   ldb_attr_cmp("objectCategory", attribute->lDAPDisplayName) == 0) {
+                       attribute->one_way_link = false;
+                       continue;
+               }
+
+               if (attribute->linkID == 0) {
+                       attribute->one_way_link = true;
+                       continue;
+               }
+               /* handle attributes with a linkID but no backlink */
+               if (dsdb_attribute_by_linkID(schema, attribute->linkID) == NULL) {
+                       attribute->one_way_link = true;
+                       continue;
+               }
+               attribute->one_way_link = false;
+       }
+}
+
 static int uint32_cmp(uint32_t c1, uint32_t c2)
 {
        if (c1 == c2) return 0;
@@ -271,6 +321,7 @@ int dsdb_setup_sorted_accessors(struct ldb_context *ldb,
        struct dsdb_attribute *a;
        unsigned int i;
        unsigned int num_int_id;
+       int ret;
 
        /* free all caches */
        dsdb_sorted_accessors_free(schema);
@@ -353,6 +404,14 @@ int dsdb_setup_sorted_accessors(struct ldb_context *ldb,
        TYPESAFE_QSORT(schema->attributes_by_attributeID_oid, schema->num_attributes, dsdb_compare_attribute_by_attributeID_oid);
        TYPESAFE_QSORT(schema->attributes_by_linkID, schema->num_attributes, dsdb_compare_attribute_by_linkID);
 
+       dsdb_setup_attribute_shortcuts(ldb, schema);
+
+       ret = schema_fill_constructed(schema);
+       if (ret != LDB_SUCCESS) {
+               dsdb_sorted_accessors_free(schema);
+               return ret;
+       }
+
        return LDB_SUCCESS;
 
 failed:
@@ -360,24 +419,6 @@ failed:
        return ldb_oom(ldb);
 }
 
-int dsdb_setup_schema_inversion(struct ldb_context *ldb, struct dsdb_schema *schema)
-{
-       /* Walk the list of schema classes */
-
-       /*  For each subClassOf, add us to subclasses of the parent */
-
-       /* collect these subclasses into a recursive list of total subclasses, preserving order */
-
-       /* For each subclass under 'top', write the index from it's
-        * order as an integer in the dsdb_class (for sorting
-        * objectClass lists efficiently) */
-
-       /* Walk the list of schema classes */
-
-       /*  Create a 'total possible superiors' on each class */
-       return LDB_SUCCESS;
-}
-
 /**
  * Attach the schema to an opaque pointer on the ldb,
  * so ldb modules can find it
@@ -392,11 +433,6 @@ int dsdb_set_schema(struct ldb_context *ldb, struct dsdb_schema *schema)
                return ret;
        }
 
-       ret = schema_fill_constructed(schema);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
-
        old_schema = ldb_get_opaque(ldb, "dsdb_schema");
 
        ret = ldb_set_opaque(ldb, "dsdb_schema", schema);
@@ -452,6 +488,12 @@ int dsdb_reference_schema(struct ldb_context *ldb, struct dsdb_schema *schema,
                return ldb_oom(ldb);
        }
 
+       /* Make this ldb use local schema preferably */
+       ret = ldb_set_opaque(ldb, "dsdb_use_global_schema", NULL);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
        ret = dsdb_schema_set_attributes(ldb, schema, write_attributes);
        if (ret != LDB_SUCCESS) {
                return ret;