]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapindex.c
New dn2id format with base/one/subtree indices (ldbm/bdb2)
[openldap] / servers / slapd / tools / slapindex.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 #include "portable.h"
6
7 #include <stdio.h>
8
9 #include <ac/stdlib.h>
10
11 #include <ac/ctype.h>
12 #include <ac/string.h>
13 #include <ac/socket.h>
14 #include <ac/unistd.h>
15
16 #include "slapcommon.h"
17
18 int
19 main( int argc, char **argv )
20 {
21         char            *type;
22         ID id;
23         int rc = EXIT_SUCCESS;
24
25         slap_tool_init( "slapindex", SLAPINDEX, argc, argv );
26
27         slap_startup( be );
28
29         if( !be->be_entry_open &&
30                 !be->be_entry_close &&
31                 !be->be_entry_first &&
32                 !be->be_entry_next &&
33                 !be->be_entry_get &&
34                 !be->be_index_attr &&
35                 !be->be_index_change )
36         {
37                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
38                         progname );
39                 exit( EXIT_FAILURE );
40         }
41
42         type = attr_normalize( argv[argc - 1] );
43
44         if ( !be->be_index_attr( be, type ) ) {
45                 fprintf( stderr, "attribute type \"%s\": no indices to generate\n",
46                         type );
47                 exit( EXIT_FAILURE );
48         }
49
50         if( be->be_entry_open( be, 0 ) != 0 ) {
51                 fprintf( stderr, "%s: could not open database.\n",
52                         progname );
53                 exit( EXIT_FAILURE );
54         }
55
56         for ( id = be->be_entry_first( be );
57                 id != NOID;
58                 id = be->be_entry_next( be ) )
59         {
60                 Attribute *attr;
61                 struct berval **values;
62                 Entry* e = be->be_entry_get( be, id );
63                 struct berval bv;
64                 struct berval *bvals[2];
65
66                 if ( e == NULL ) {
67                         fprintf( stderr,
68                                 "entry id=%08lx: no data\n", (long) id );
69                         rc = EXIT_FAILURE;
70                         continue;
71                 }
72
73                 if( verbose ) {
74                         printf("indexing id=%08lx dn=\"%s\"\n",
75                                 id, e->e_dn );
76                 }
77
78                 if( strcasecmp( type, "dn" ) == 0 ) {
79                         attr = attr_find( e->e_attrs, type );
80
81                         if( attr == NULL ) continue;
82
83                         values = attr->a_vals;
84
85                 } else {
86                         bv.bv_val = e->e_ndn;
87                         bv.bv_len = strlen( bv.bv_val );
88                         bvals[0] = &bv;
89                         bvals[1] = NULL;
90
91                         values = bvals;
92                 }
93
94                 if ( be->be_index_change( be,
95                         type, attr->a_vals, id, SLAP_INDEX_ADD_OP ) )
96                 {
97                         rc = EXIT_FAILURE;
98                 }
99
100                 entry_free( e );
101         }
102
103         (void) be->be_entry_close( be );
104
105         slap_shutdown( be );
106         slap_destroy();
107
108         return( rc );
109 }