1998-08-09 08:43:13 +08:00
|
|
|
/* idl.c - ldap id list handling routines */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-29 05:08:20 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2004-01-02 03:15:16 +08:00
|
|
|
* Copyright 1998-2004 The OpenLDAP Foundation.
|
2003-11-29 05:08:20 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
1999-08-07 07:07:46 +08:00
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
#include "slap.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
#include "back-ldbm.h"
|
|
|
|
|
1999-02-03 12:45:08 +08:00
|
|
|
static ID_BLOCK* idl_dup( ID_BLOCK *idl );
|
1999-02-02 11:40:19 +08:00
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
static void cont_alloc( Datum *cont, Datum *key )
|
2000-06-05 06:59:38 +08:00
|
|
|
{
|
2000-06-15 07:48:13 +08:00
|
|
|
ldbm_datum_init( *cont );
|
|
|
|
cont->dsize = 1 + sizeof(ID) + key->dsize;
|
|
|
|
cont->dptr = ch_malloc( cont->dsize );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
* (unsigned char *) cont->dptr = SLAP_INDEX_CONT_PREFIX;
|
2000-06-15 07:28:52 +08:00
|
|
|
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY( &((unsigned char *)cont->dptr)[1 + sizeof(ID)],
|
2000-06-15 07:48:13 +08:00
|
|
|
key->dptr, key->dsize );
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
static void cont_id( Datum *cont, ID id )
|
2000-06-05 06:59:38 +08:00
|
|
|
{
|
2002-04-11 16:46:34 +08:00
|
|
|
unsigned int i;
|
2000-06-05 06:59:38 +08:00
|
|
|
|
|
|
|
for( i=1; i <= sizeof(id); i++) {
|
2000-06-15 07:48:13 +08:00
|
|
|
((unsigned char *)cont->dptr)[i] = (unsigned char)(id & 0xFF);
|
2000-06-05 06:59:38 +08:00
|
|
|
id >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
static void cont_free( Datum *cont )
|
2000-06-05 06:59:38 +08:00
|
|
|
{
|
2000-06-15 07:48:13 +08:00
|
|
|
ch_free( cont->dptr );
|
2000-06-05 06:59:38 +08:00
|
|
|
}
|
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
static void idl_check(ID_BLOCK *idl)
|
|
|
|
{
|
2002-08-23 11:07:33 +08:00
|
|
|
int i, max;
|
2001-09-08 05:58:31 +08:00
|
|
|
ID_BLOCK last;
|
|
|
|
|
2002-08-23 11:07:33 +08:00
|
|
|
if( ID_BLOCK_ALLIDS(idl) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef USE_INDIRECT_NIDS
|
|
|
|
if( ID_BLOCK_INDIRECT(idl) )
|
|
|
|
{
|
2002-08-24 06:29:08 +08:00
|
|
|
for ( max = 0; !ID_BLOCK_NOID(idl, max); max++ ) ;
|
2002-08-23 11:07:33 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
max = ID_BLOCK_NIDS(idl);
|
|
|
|
}
|
|
|
|
if ( max <= 1 )
|
2001-09-08 05:58:31 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( last = ID_BLOCK_ID(idl, 0), i = 1;
|
2002-08-23 11:07:33 +08:00
|
|
|
i < max;
|
2001-09-08 05:58:31 +08:00
|
|
|
last = ID_BLOCK_ID(idl, i), i++ )
|
|
|
|
{
|
|
|
|
assert (last < ID_BLOCK_ID(idl, i) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
/* Allocate an ID_BLOCK with room for nids ids */
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *
|
1999-03-02 06:37:05 +08:00
|
|
|
idl_alloc( unsigned int nids )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *new;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* nmax + nids + space for the ids */
|
1999-01-31 15:55:53 +08:00
|
|
|
new = (ID_BLOCK *) ch_calloc( (ID_BLOCK_IDS_OFFSET + nids), sizeof(ID) );
|
|
|
|
ID_BLOCK_NMAX(new) = nids;
|
|
|
|
ID_BLOCK_NIDS(new) = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
return( new );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
|
|
|
/* Allocate an empty ALLIDS ID_BLOCK */
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_allids( Backend *be )
|
|
|
|
{
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *idl;
|
2001-07-20 17:50:28 +08:00
|
|
|
ID id;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
idl = idl_alloc( 0 );
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
|
2001-07-20 17:50:28 +08:00
|
|
|
if ( next_id_get( be, &id ) ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ID_BLOCK_NIDS(idl) = id;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
return( idl );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
/* Free an ID_BLOCK */
|
1998-08-09 08:43:13 +08:00
|
|
|
void
|
1999-01-31 15:55:53 +08:00
|
|
|
idl_free( ID_BLOCK *idl )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
if ( idl == NULL ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, INFO, "idl_free: called with NULL pointer\n" , 0,0,0);
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1999-02-02 11:40:19 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"idl_free: called with NULL pointer\n",
|
|
|
|
0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free( (char *) idl );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
|
|
|
/* Fetch an single ID_BLOCK from the cache */
|
1999-01-31 15:55:53 +08:00
|
|
|
static ID_BLOCK *
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_fetch_one(
|
|
|
|
Backend *be,
|
1999-07-20 03:40:33 +08:00
|
|
|
DBCache *db,
|
1998-08-09 08:43:13 +08:00
|
|
|
Datum key
|
|
|
|
)
|
|
|
|
{
|
1998-11-05 14:49:49 +08:00
|
|
|
Datum data;
|
1999-08-21 06:37:30 +08:00
|
|
|
ID_BLOCK *idl;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
|
|
|
|
|
|
|
|
data = ldbm_cache_fetch( db, key );
|
|
|
|
|
1999-02-03 12:45:08 +08:00
|
|
|
if( data.dptr == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-01-16 17:58:11 +08:00
|
|
|
idl = (ID_BLOCK *) data.dptr;
|
|
|
|
if ( ID_BLOCK_ALLIDS(idl) ) {
|
|
|
|
/* make sure we have the current value of highest id */
|
|
|
|
idl = idl_allids( be );
|
|
|
|
} else {
|
|
|
|
idl = idl_dup((ID_BLOCK *) data.dptr);
|
|
|
|
}
|
1999-08-21 06:37:30 +08:00
|
|
|
|
1999-02-03 12:45:08 +08:00
|
|
|
ldbm_datum_free( db->dbc_db, data );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-08-21 06:37:30 +08:00
|
|
|
return idl;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
|
|
|
/* Fetch a set of ID_BLOCKs from the cache
|
|
|
|
* if not INDIRECT
|
2001-01-18 01:01:19 +08:00
|
|
|
* if block return is an ALLIDS block,
|
1999-02-02 11:40:19 +08:00
|
|
|
* return an new ALLIDS block
|
2001-01-18 01:01:19 +08:00
|
|
|
* otherwise
|
1999-02-02 11:40:19 +08:00
|
|
|
* return block
|
|
|
|
* construct super block from all blocks referenced by INDIRECT block
|
|
|
|
* return super block
|
|
|
|
*/
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_fetch(
|
|
|
|
Backend *be,
|
1999-07-20 03:40:33 +08:00
|
|
|
DBCache *db,
|
1998-08-09 08:43:13 +08:00
|
|
|
Datum key
|
|
|
|
)
|
|
|
|
{
|
1999-02-03 12:45:08 +08:00
|
|
|
Datum data;
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *idl;
|
|
|
|
ID_BLOCK **tmp;
|
2002-12-23 21:43:01 +08:00
|
|
|
unsigned i, nids, nblocks;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-02-04 01:14:01 +08:00
|
|
|
idl = idl_fetch_one( be, db, key );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-02-04 01:14:01 +08:00
|
|
|
if ( idl == NULL ) {
|
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-02-03 12:45:08 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS(idl) ) {
|
|
|
|
/* all ids block */
|
1999-02-05 21:20:57 +08:00
|
|
|
return( idl );
|
1999-02-03 12:45:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! ID_BLOCK_INDIRECT( idl ) ) {
|
|
|
|
/* regular block */
|
1998-08-09 08:43:13 +08:00
|
|
|
return( idl );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is an indirect block which points to other blocks.
|
|
|
|
* we need to read in all the blocks it points to and construct
|
|
|
|
* a big id list containing all the ids, which we will return.
|
|
|
|
*/
|
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
#ifndef USE_INDIRECT_NIDS
|
1998-08-09 08:43:13 +08:00
|
|
|
/* count the number of blocks & allocate space for pointers to them */
|
2002-12-23 20:51:21 +08:00
|
|
|
for ( nblocks = 0; !ID_BLOCK_NOID(idl, nblocks); nblocks++ )
|
1998-08-09 08:43:13 +08:00
|
|
|
; /* NULL */
|
2001-09-22 14:52:32 +08:00
|
|
|
#else
|
2002-12-23 20:51:21 +08:00
|
|
|
nblocks = ID_BLOCK_NIDS(idl);
|
2001-09-22 14:52:32 +08:00
|
|
|
#endif
|
2002-12-23 20:51:21 +08:00
|
|
|
tmp = (ID_BLOCK **) ch_malloc( nblocks * sizeof(ID_BLOCK *) );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* read in all the blocks */
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_alloc( &data, &key );
|
1998-08-09 08:43:13 +08:00
|
|
|
nids = 0;
|
2002-12-23 20:51:21 +08:00
|
|
|
for ( i = 0; i < nblocks; i++ ) {
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_id( &data, ID_BLOCK_ID(idl, i) );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-02-03 12:45:08 +08:00
|
|
|
if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, INFO,
|
|
|
|
"idl_fetch: idl_fetch_one returned NULL\n", 0,0,0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-06-11 06:39:30 +08:00
|
|
|
"idl_fetch: one returned NULL\n", 0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
nids += ID_BLOCK_NIDS(tmp[i]);
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_free( &data );
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_free( idl );
|
|
|
|
|
|
|
|
/* allocate space for the big block */
|
|
|
|
idl = idl_alloc( nids );
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NIDS(idl) = nids;
|
1998-08-09 08:43:13 +08:00
|
|
|
nids = 0;
|
|
|
|
|
|
|
|
/* copy in all the ids from the component blocks */
|
2002-12-23 20:51:21 +08:00
|
|
|
for ( i = 0; i < nblocks; i++ ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( tmp[i] == NULL ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY(
|
1999-01-31 15:55:53 +08:00
|
|
|
(char *) &ID_BLOCK_ID(idl, nids),
|
|
|
|
(char *) &ID_BLOCK_ID(tmp[i], 0),
|
|
|
|
ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
|
|
|
|
nids += ID_BLOCK_NIDS(tmp[i]);
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
idl_free( tmp[i] );
|
|
|
|
}
|
|
|
|
free( (char *) tmp );
|
|
|
|
|
2003-02-07 23:20:57 +08:00
|
|
|
assert( ID_BLOCK_NIDS(idl) == nids );
|
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(idl);
|
|
|
|
#endif
|
|
|
|
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, ENTRY,
|
2001-01-18 01:01:19 +08:00
|
|
|
"idl_fetch: %ld ids (%ld max)\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
ID_BLOCK_NIDS(idl), ID_BLOCK_NMAXN(idl), 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1999-02-12 02:19:52 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
|
2001-09-22 14:52:32 +08:00
|
|
|
ID_BLOCK_NIDS(idl), ID_BLOCK_NMAXN(idl), 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( idl );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
|
|
|
/* store a single block */
|
1998-08-09 08:43:13 +08:00
|
|
|
static int
|
|
|
|
idl_store(
|
|
|
|
Backend *be,
|
1999-07-20 03:40:33 +08:00
|
|
|
DBCache *db,
|
1998-08-09 08:43:13 +08:00
|
|
|
Datum key,
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *idl
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
1998-08-19 08:10:02 +08:00
|
|
|
int rc, flags;
|
1998-08-09 08:43:13 +08:00
|
|
|
Datum data;
|
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(idl);
|
|
|
|
#endif
|
|
|
|
|
1998-12-30 01:28:45 +08:00
|
|
|
ldbm_datum_init( data );
|
1998-09-03 08:50:13 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
|
|
|
|
|
|
|
|
data.dptr = (char *) idl;
|
2001-09-22 14:52:32 +08:00
|
|
|
data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAXN(idl)) * sizeof(ID);
|
1998-08-19 08:10:02 +08:00
|
|
|
|
2001-11-17 22:39:10 +08:00
|
|
|
flags = LDBM_REPLACE;
|
|
|
|
rc = ldbm_cache_store( db, key, data, flags );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
2001-09-22 04:30:27 +08:00
|
|
|
/* Binary search for id in block, return index
|
|
|
|
* an index is always returned, even with no match. If no
|
|
|
|
* match, the returned index is the insertion point.
|
|
|
|
*/
|
|
|
|
static unsigned int
|
|
|
|
idl_find(
|
|
|
|
ID_BLOCK *b,
|
|
|
|
ID id
|
|
|
|
)
|
|
|
|
{
|
2001-09-22 13:35:52 +08:00
|
|
|
int lo=0, hi=ID_BLOCK_NIDS(b)-1, nr=0;
|
2001-09-22 04:30:27 +08:00
|
|
|
|
|
|
|
for (;lo<=hi;)
|
|
|
|
{
|
|
|
|
nr = ( lo + hi ) / 2;
|
|
|
|
if (ID_BLOCK_ID(b, nr) == id)
|
|
|
|
break;
|
|
|
|
if (ID_BLOCK_ID(b, nr) > id)
|
|
|
|
hi = nr - 1;
|
|
|
|
else
|
|
|
|
lo = nr + 1;
|
|
|
|
}
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
/* split the block at id
|
|
|
|
* locate ID greater than or equal to id.
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
static void
|
|
|
|
idl_split_block(
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *b,
|
1998-08-09 08:43:13 +08:00
|
|
|
ID id,
|
1999-02-02 11:40:19 +08:00
|
|
|
ID_BLOCK **right,
|
|
|
|
ID_BLOCK **left
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
1999-02-02 11:40:19 +08:00
|
|
|
unsigned int nr, nl;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-09-22 04:30:27 +08:00
|
|
|
/* find where to split the block */
|
|
|
|
nr = idl_find(b, id);
|
|
|
|
if ( ID_BLOCK_ID(b,nr) < id )
|
|
|
|
nr++;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
nl = ID_BLOCK_NIDS(b) - nr;
|
|
|
|
|
|
|
|
*right = idl_alloc( nr == 0 ? 1 : nr );
|
|
|
|
*left = idl_alloc( nl + (nr == 0 ? 0 : 1));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* everything before the id being inserted in the first block
|
|
|
|
* unless there is nothing, in which case the id being inserted
|
|
|
|
* goes there.
|
|
|
|
*/
|
1999-02-02 11:40:19 +08:00
|
|
|
if ( nr == 0 ) {
|
|
|
|
ID_BLOCK_NIDS(*right) = 1;
|
|
|
|
ID_BLOCK_ID(*right, 0) = id;
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY(
|
1999-02-02 11:40:19 +08:00
|
|
|
(char *) &ID_BLOCK_ID(*right, 0),
|
|
|
|
(char *) &ID_BLOCK_ID(b, 0),
|
|
|
|
nr * sizeof(ID) );
|
|
|
|
ID_BLOCK_NIDS(*right) = nr;
|
|
|
|
ID_BLOCK_ID(*left, 0) = id;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the id being inserted & everything after in the second block */
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY(
|
1999-02-02 11:40:19 +08:00
|
|
|
(char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
|
|
|
|
(char *) &ID_BLOCK_ID(b, nr),
|
|
|
|
nl * sizeof(ID) );
|
|
|
|
ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
|
2001-09-08 05:58:31 +08:00
|
|
|
|
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(*right);
|
|
|
|
idl_check(*left);
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* idl_change_first - called when an indirect block's first key has
|
|
|
|
* changed, meaning it needs to be stored under a new key, and the
|
|
|
|
* header block pointing to it needs updating.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
idl_change_first(
|
|
|
|
Backend *be,
|
1999-07-20 03:40:33 +08:00
|
|
|
DBCache *db,
|
1998-08-09 08:43:13 +08:00
|
|
|
Datum hkey, /* header block key */
|
2001-01-18 01:01:19 +08:00
|
|
|
ID_BLOCK *h, /* header block */
|
1998-08-09 08:43:13 +08:00
|
|
|
int pos, /* pos in h to update */
|
|
|
|
Datum bkey, /* data block key */
|
2001-01-18 01:01:19 +08:00
|
|
|
ID_BLOCK *b /* data block */
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
|
|
|
|
|
|
|
|
/* delete old key block */
|
|
|
|
if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, INFO,
|
|
|
|
"idl_change_first: ldbm_cache_delete returned %d\n", rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-06-11 06:39:30 +08:00
|
|
|
"idl_change_first: ldbm_cache_delete returned %d\n",
|
|
|
|
rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write block with new key */
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_id( &bkey, ID_BLOCK_ID(b, 0) );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, INFO,
|
|
|
|
"idl_change_first: idl_store returned %d\n", rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-06-11 06:39:30 +08:00
|
|
|
"idl_change_first: idl_store returned %d\n", rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update + write indirect header block */
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, INFO,
|
|
|
|
"idl_change_first: idl_store returned %s\n", rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-06-11 06:39:30 +08:00
|
|
|
"idl_change_first: idl_store returned %d\n", rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
int
|
|
|
|
idl_insert_key(
|
|
|
|
Backend *be,
|
1999-07-20 03:40:33 +08:00
|
|
|
DBCache *db,
|
1998-08-09 08:43:13 +08:00
|
|
|
Datum key,
|
|
|
|
ID id
|
|
|
|
)
|
|
|
|
{
|
2001-11-17 22:39:10 +08:00
|
|
|
int i, j, first, rc = 0;
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *idl, *tmp, *tmp2, *tmp3;
|
1998-08-09 08:43:13 +08:00
|
|
|
Datum k2;
|
1998-09-03 08:50:13 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
|
|
|
|
idl = idl_alloc( 1 );
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = idl_store( be, db, key, idl );
|
|
|
|
|
|
|
|
idl_free( idl );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
1999-02-03 12:45:08 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS( idl ) ) {
|
|
|
|
/* ALLIDS */
|
|
|
|
idl_free( idl );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ! ID_BLOCK_INDIRECT( idl ) ) {
|
1999-02-03 12:45:08 +08:00
|
|
|
/* regular block */
|
1998-08-09 08:43:13 +08:00
|
|
|
switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
|
|
|
|
case 0: /* id inserted - store the updated block */
|
|
|
|
case 1:
|
|
|
|
rc = idl_store( be, db, key, idl );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* id already there - nothing to do */
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* id not inserted - block must be split */
|
|
|
|
/* check threshold for marking this an all-id block */
|
|
|
|
if ( db->dbc_maxindirect < 2 ) {
|
|
|
|
idl_free( idl );
|
|
|
|
idl = idl_allids( be );
|
|
|
|
rc = idl_store( be, db, key, idl );
|
1999-04-05 05:34:33 +08:00
|
|
|
break;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
idl_split_block( idl, id, &tmp, &tmp2 );
|
|
|
|
idl_free( idl );
|
|
|
|
|
|
|
|
/* create the header indirect block */
|
2001-09-22 14:52:32 +08:00
|
|
|
#ifndef USE_INDIRECT_NIDS
|
2002-01-16 17:58:11 +08:00
|
|
|
idl = idl_alloc( 3 );
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NMAX(idl) = 3;
|
|
|
|
ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
|
|
|
|
ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
|
|
|
|
ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
|
|
|
|
ID_BLOCK_ID(idl, 2) = NOID;
|
2001-09-22 14:52:32 +08:00
|
|
|
#else
|
2002-01-16 17:58:11 +08:00
|
|
|
idl = idl_alloc( 2 );
|
2001-09-22 14:52:32 +08:00
|
|
|
ID_BLOCK_NMAX(idl) = 2 | ID_BLOCK_INDIRECT_VALUE;
|
|
|
|
ID_BLOCK_NIDS(idl) = 2;
|
|
|
|
ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
|
|
|
|
ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* store it */
|
|
|
|
rc = idl_store( be, db, key, idl );
|
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_alloc( &k2, &key );
|
|
|
|
cont_id( &k2, ID_BLOCK_ID(tmp, 0) );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = idl_store( be, db, k2, tmp );
|
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = idl_store( be, db, k2, tmp2 );
|
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_free( &k2 );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_free( tmp );
|
|
|
|
idl_free( tmp2 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
idl_free( idl );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is an indirect block which points to other blocks.
|
|
|
|
* we need to read in the block into which the id should be
|
|
|
|
* inserted, then insert the id and store the block. we might
|
|
|
|
* have to split the block if it is full, which means we also
|
|
|
|
* need to write a new "header" block.
|
|
|
|
*/
|
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
#ifndef USE_INDIRECT_NIDS
|
1999-02-02 11:40:19 +08:00
|
|
|
/* select the block to try inserting into *//* XXX linear search XXX */
|
2003-03-12 21:40:54 +08:00
|
|
|
for ( i = 0; !ID_BLOCK_NOID(idl, i) && id >= ID_BLOCK_ID(idl, i); i++ )
|
1998-08-09 08:43:13 +08:00
|
|
|
; /* NULL */
|
2001-09-22 14:52:32 +08:00
|
|
|
#else
|
|
|
|
i = idl_find(idl, id);
|
2003-03-12 21:40:54 +08:00
|
|
|
if (ID_BLOCK_ID(idl, i) <= id)
|
2001-09-22 14:52:32 +08:00
|
|
|
i++;
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( i != 0 ) {
|
|
|
|
i--;
|
|
|
|
first = 0;
|
|
|
|
} else {
|
|
|
|
first = 1;
|
|
|
|
}
|
|
|
|
|
2003-03-12 21:40:54 +08:00
|
|
|
/* At this point, the following condition must be true:
|
|
|
|
* ID_BLOCK_ID(idl, i) <= id && id < ID_BLOCK_ID(idl, i+1)
|
|
|
|
* except when i is the first or the last block.
|
|
|
|
*/
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* get the block */
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_alloc( &k2, &key );
|
|
|
|
cont_id( &k2, ID_BLOCK_ID(idl, i) );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, ERR,
|
|
|
|
"idl_insert_key: nonexistent continuation block\n", 0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
2000-06-11 06:39:30 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "idl_insert_key: nonexistent continuation block\n",
|
|
|
|
0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_free( &k2 );
|
1999-04-05 05:34:33 +08:00
|
|
|
idl_free( idl );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert the id */
|
|
|
|
switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
|
|
|
|
case 0: /* id inserted ok */
|
|
|
|
if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, ERR,
|
|
|
|
"ids_insert_key: idl_store returned %d\n", rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-06-11 06:39:30 +08:00
|
|
|
"idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* id inserted - first id in block has changed */
|
|
|
|
/*
|
|
|
|
* key for this block has changed, so we have to
|
|
|
|
* write the block under the new key, delete the
|
|
|
|
* old key block + update and write the indirect
|
|
|
|
* header block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
rc = idl_change_first( be, db, key, idl, i, k2, tmp );
|
|
|
|
break;
|
|
|
|
|
1999-09-02 02:30:13 +08:00
|
|
|
case 2: /* id not inserted - already there, do nothing */
|
|
|
|
rc = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* id not inserted - block is full */
|
|
|
|
/*
|
|
|
|
* first, see if it will fit in the next block,
|
|
|
|
* without splitting, unless we're trying to insert
|
|
|
|
* into the beginning of the first block.
|
|
|
|
*/
|
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
#ifndef USE_INDIRECT_NIDS
|
1998-08-09 08:43:13 +08:00
|
|
|
/* is there a next block? */
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
|
2001-09-22 14:52:32 +08:00
|
|
|
#else
|
2002-01-16 01:18:18 +08:00
|
|
|
if ( !first && (unsigned long)(i + 1) < ID_BLOCK_NIDS(idl) ) {
|
2001-09-22 14:52:32 +08:00
|
|
|
#endif
|
2003-03-06 08:16:32 +08:00
|
|
|
Datum k3;
|
1998-08-09 08:43:13 +08:00
|
|
|
/* read it in */
|
2003-03-06 08:16:32 +08:00
|
|
|
cont_alloc( &k3, &key );
|
|
|
|
cont_id( &k3, ID_BLOCK_ID(idl, i + 1) );
|
|
|
|
if ( (tmp2 = idl_fetch_one( be, db, k3 )) == NULL ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, ERR,
|
|
|
|
"idl_insert_key: idl_fetch_one returned NULL\n", 0, 0, 0);
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-06-11 06:39:30 +08:00
|
|
|
"idl_insert_key: idl_fetch_one returned NULL\n",
|
|
|
|
0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1999-09-02 02:30:13 +08:00
|
|
|
/* split the original block */
|
2003-03-06 08:16:32 +08:00
|
|
|
cont_free( &k3 );
|
1999-09-02 02:30:13 +08:00
|
|
|
goto split;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-12-17 18:04:31 +08:00
|
|
|
/* If the new id is less than the last id in the
|
|
|
|
* current block, it must not be put into the next
|
|
|
|
* block. Push the last id of the current block
|
|
|
|
* into the next block instead.
|
|
|
|
*/
|
|
|
|
if (id < ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1)) {
|
|
|
|
ID id2 = ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1);
|
|
|
|
|
|
|
|
--ID_BLOCK_NIDS(tmp);
|
|
|
|
/* This must succeed since we just popped one
|
|
|
|
* ID off the end of it.
|
|
|
|
*/
|
|
|
|
rc = idl_insert( &tmp, id, db->dbc_maxids );
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2002-01-16 17:58:11 +08:00
|
|
|
if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, ERR,
|
|
|
|
"idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1999-12-17 18:04:31 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-06-11 06:39:30 +08:00
|
|
|
"idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1999-12-17 18:04:31 +08:00
|
|
|
}
|
2000-06-05 06:59:38 +08:00
|
|
|
|
1999-12-17 18:04:31 +08:00
|
|
|
id = id2;
|
|
|
|
/* This new id will necessarily be inserted
|
|
|
|
* as the first id of the next block by the
|
|
|
|
* following switch() statement.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
switch ( (rc = idl_insert( &tmp2, id,
|
|
|
|
db->dbc_maxids )) ) {
|
|
|
|
case 1: /* id inserted first in block */
|
|
|
|
rc = idl_change_first( be, db, key, idl,
|
2003-03-06 08:16:32 +08:00
|
|
|
i + 1, k3, tmp2 );
|
1998-08-09 08:43:13 +08:00
|
|
|
/* FALL */
|
|
|
|
|
|
|
|
case 2: /* id already there - how? */
|
1999-12-17 18:04:31 +08:00
|
|
|
case 0: /* id inserted: this can never be
|
|
|
|
* the result of idl_insert, because
|
|
|
|
* we guaranteed that idl_change_first
|
|
|
|
* will always be called.
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( rc == 2 ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, INFO,
|
2001-01-18 01:01:19 +08:00
|
|
|
"idl_insert_key: id %ld is already in next block\n",
|
2002-07-12 04:33:24 +08:00
|
|
|
id, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-06-11 06:39:30 +08:00
|
|
|
"idl_insert_key: id %ld already in next block\n",
|
1998-08-09 08:43:13 +08:00
|
|
|
id, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2000-06-05 06:59:38 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_free( tmp );
|
|
|
|
idl_free( tmp2 );
|
2003-03-06 08:16:32 +08:00
|
|
|
cont_free( &k3 );
|
2003-03-06 07:10:32 +08:00
|
|
|
cont_free( &k2 );
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_free( idl );
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
case 3: /* split the original block */
|
|
|
|
break;
|
|
|
|
}
|
1999-09-02 02:30:13 +08:00
|
|
|
|
1999-04-05 05:34:33 +08:00
|
|
|
idl_free( tmp2 );
|
2003-03-06 08:16:32 +08:00
|
|
|
cont_free( &k3 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-09-02 02:30:13 +08:00
|
|
|
split:
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* must split the block, write both new blocks + update
|
|
|
|
* and write the indirect header block.
|
|
|
|
*/
|
|
|
|
|
1999-09-02 02:30:13 +08:00
|
|
|
rc = 0; /* optimistic */
|
|
|
|
|
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
#ifndef USE_INDIRECT_NIDS
|
1999-02-02 11:40:19 +08:00
|
|
|
/* count how many indirect blocks *//* XXX linear count XXX */
|
1999-01-31 15:55:53 +08:00
|
|
|
for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
|
1998-08-09 08:43:13 +08:00
|
|
|
; /* NULL */
|
2001-09-22 14:52:32 +08:00
|
|
|
#else
|
|
|
|
j = ID_BLOCK_NIDS(idl);
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* check it against all-id thresholed */
|
|
|
|
if ( j + 1 > db->dbc_maxindirect ) {
|
|
|
|
/*
|
|
|
|
* we've passed the all-id threshold, meaning
|
|
|
|
* that this set of blocks should be replaced
|
2001-01-18 01:01:19 +08:00
|
|
|
* by a single "all-id" block. our job: delete
|
1998-08-09 08:43:13 +08:00
|
|
|
* all the indirect blocks, and replace the header
|
|
|
|
* block by an all-id block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* delete all indirect blocks */
|
2001-09-22 14:52:32 +08:00
|
|
|
#ifndef USE_INDIRECT_NIDS
|
1999-01-31 15:55:53 +08:00
|
|
|
for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
|
2001-09-22 14:52:32 +08:00
|
|
|
#else
|
2002-01-16 01:18:18 +08:00
|
|
|
for ( j = 0; (unsigned long) j < ID_BLOCK_NIDS(idl); j++ ) {
|
2001-09-22 14:52:32 +08:00
|
|
|
#endif
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_id( &k2, ID_BLOCK_ID(idl, j) );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
rc = ldbm_cache_delete( db, k2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store allid block in place of header block */
|
|
|
|
idl_free( idl );
|
|
|
|
idl = idl_allids( be );
|
|
|
|
rc = idl_store( be, db, key, idl );
|
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_free( &k2 );
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_free( idl );
|
|
|
|
idl_free( tmp );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
idl_split_block( tmp, id, &tmp2, &tmp3 );
|
|
|
|
idl_free( tmp );
|
|
|
|
|
|
|
|
/* create a new updated indirect header block */
|
2001-09-22 14:52:32 +08:00
|
|
|
tmp = idl_alloc( ID_BLOCK_NMAXN(idl) + 1 );
|
|
|
|
#ifndef USE_INDIRECT_NIDS
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
|
2001-09-22 14:52:32 +08:00
|
|
|
#else
|
|
|
|
ID_BLOCK_NMAX(tmp) |= ID_BLOCK_INDIRECT_VALUE;
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
/* everything up to the split block */
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY(
|
1999-01-31 15:55:53 +08:00
|
|
|
(char *) &ID_BLOCK_ID(tmp, 0),
|
|
|
|
(char *) &ID_BLOCK_ID(idl, 0),
|
1998-08-09 08:43:13 +08:00
|
|
|
i * sizeof(ID) );
|
|
|
|
/* the two new blocks */
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
|
|
|
|
ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
|
1998-08-09 08:43:13 +08:00
|
|
|
/* everything after the split block */
|
2001-09-22 14:52:32 +08:00
|
|
|
#ifndef USE_INDIRECT_NIDS
|
|
|
|
AC_MEMCPY(
|
|
|
|
(char *) &ID_BLOCK_ID(tmp, i + 2),
|
|
|
|
(char *) &ID_BLOCK_ID(idl, i + 1),
|
|
|
|
(ID_BLOCK_NMAXN(idl) - i - 1) * sizeof(ID) );
|
|
|
|
#else
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY(
|
1999-01-31 15:55:53 +08:00
|
|
|
(char *) &ID_BLOCK_ID(tmp, i + 2),
|
|
|
|
(char *) &ID_BLOCK_ID(idl, i + 1),
|
2001-09-22 14:52:32 +08:00
|
|
|
(ID_BLOCK_NIDS(idl) - i - 1) * sizeof(ID) );
|
2002-01-16 17:58:11 +08:00
|
|
|
ID_BLOCK_NIDS(tmp) = ID_BLOCK_NIDS(idl) + 1;
|
2001-09-22 14:52:32 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* store the header block */
|
|
|
|
rc = idl_store( be, db, key, tmp );
|
|
|
|
|
|
|
|
/* store the first id block */
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = idl_store( be, db, k2, tmp2 );
|
|
|
|
|
|
|
|
/* store the second id block */
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_id( &k2, ID_BLOCK_ID(tmp3, 0) );
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = idl_store( be, db, k2, tmp3 );
|
|
|
|
|
|
|
|
idl_free( tmp2 );
|
|
|
|
idl_free( tmp3 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_free( &k2 );
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_free( tmp );
|
|
|
|
idl_free( idl );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* idl_insert - insert an id into an id list.
|
1999-02-02 11:40:19 +08:00
|
|
|
*
|
|
|
|
* returns
|
2001-01-18 01:01:19 +08:00
|
|
|
* 0 id inserted
|
1998-08-09 08:43:13 +08:00
|
|
|
* 1 id inserted, first id in block has changed
|
|
|
|
* 2 id not inserted, already there
|
|
|
|
* 3 id not inserted, block must be split
|
|
|
|
*/
|
|
|
|
int
|
1999-03-02 06:37:05 +08:00
|
|
|
idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-08-10 02:51:02 +08:00
|
|
|
unsigned int i;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS( *idl ) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( 2 ); /* already there */
|
|
|
|
}
|
|
|
|
|
2001-09-22 04:30:27 +08:00
|
|
|
/* is it already there? */
|
|
|
|
i = idl_find(*idl, id);
|
|
|
|
if ( ID_BLOCK_ID(*idl, i) == id ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( 2 ); /* already there */
|
|
|
|
}
|
2001-09-22 13:35:52 +08:00
|
|
|
if ( ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) < id )
|
2001-09-22 04:30:27 +08:00
|
|
|
i++;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* do we need to make room for it? */
|
2001-09-22 14:52:32 +08:00
|
|
|
if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAXN(*idl) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
/* make room or indicate block needs splitting */
|
2001-09-22 14:52:32 +08:00
|
|
|
if ( ID_BLOCK_NMAXN(*idl) >= maxids ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( 3 ); /* block needs splitting */
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NMAX(*idl) *= 2;
|
2001-09-22 14:52:32 +08:00
|
|
|
if ( ID_BLOCK_NMAXN(*idl) > maxids ) {
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NMAX(*idl) = maxids;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-01-31 15:55:53 +08:00
|
|
|
*idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
|
2001-09-22 14:52:32 +08:00
|
|
|
(ID_BLOCK_NMAXN(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-08-08 07:25:48 +08:00
|
|
|
/* make a slot for the new id */
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
|
2001-01-18 01:01:19 +08:00
|
|
|
(ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
|
1999-08-07 07:07:46 +08:00
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_ID(*idl, i) = id;
|
|
|
|
ID_BLOCK_NIDS(*idl)++;
|
|
|
|
(void) memset(
|
|
|
|
(char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
|
|
|
|
'\0',
|
2001-09-22 14:52:32 +08:00
|
|
|
(ID_BLOCK_NMAXN(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(*idl);
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
1998-11-13 02:39:33 +08:00
|
|
|
int
|
|
|
|
idl_delete_key (
|
2001-01-18 01:01:19 +08:00
|
|
|
Backend *be,
|
|
|
|
DBCache *db,
|
|
|
|
Datum key,
|
|
|
|
ID id
|
1998-11-13 02:39:33 +08:00
|
|
|
)
|
|
|
|
{
|
1999-02-03 12:45:08 +08:00
|
|
|
Datum data;
|
1999-08-21 06:37:30 +08:00
|
|
|
ID_BLOCK *idl;
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
unsigned i;
|
|
|
|
int j, nids;
|
1998-11-13 02:39:33 +08:00
|
|
|
|
|
|
|
if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
|
|
|
|
{
|
|
|
|
/* It wasn't found. Hmm... */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-02-03 12:45:08 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS( idl ) ) {
|
|
|
|
idl_free( idl );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! ID_BLOCK_INDIRECT( idl ) ) {
|
2001-09-22 14:52:32 +08:00
|
|
|
i = idl_find(idl, id);
|
|
|
|
if ( ID_BLOCK_ID(idl, i) == id ) {
|
|
|
|
if( --ID_BLOCK_NIDS(idl) == 0 ) {
|
|
|
|
ldbm_cache_delete( db, key );
|
1999-02-03 12:45:08 +08:00
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
} else {
|
|
|
|
AC_MEMCPY(
|
|
|
|
&ID_BLOCK_ID(idl, i),
|
|
|
|
&ID_BLOCK_ID(idl, i+1),
|
|
|
|
(ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );
|
1999-01-31 15:55:53 +08:00
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;
|
1999-02-03 12:45:08 +08:00
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
idl_store( be, db, key, idl );
|
1998-11-13 02:39:33 +08:00
|
|
|
}
|
2001-09-22 14:52:32 +08:00
|
|
|
|
|
|
|
idl_free( idl );
|
|
|
|
return 0;
|
1998-11-13 02:39:33 +08:00
|
|
|
}
|
2001-09-22 14:52:32 +08:00
|
|
|
/* We didn't find the ID. Hmmm... */
|
1999-04-05 05:34:33 +08:00
|
|
|
idl_free( idl );
|
1998-11-13 02:39:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have to go through an indirect block and find the ID
|
|
|
|
in the list of IDL's
|
|
|
|
*/
|
2002-01-16 15:39:00 +08:00
|
|
|
cont_alloc( &data, &key );
|
2001-09-22 14:52:32 +08:00
|
|
|
#ifndef USE_INDIRECT_NIDS
|
1999-01-31 15:55:53 +08:00
|
|
|
for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
|
1998-11-13 02:39:33 +08:00
|
|
|
; /* NULL */
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
for ( j = 0; j<nids; j++ )
|
|
|
|
#else
|
|
|
|
nids = ID_BLOCK_NIDS(idl);
|
|
|
|
for ( j = idl_find(idl, id); j >= 0; j = -1) /* execute once */
|
|
|
|
#endif
|
1998-11-13 02:39:33 +08:00
|
|
|
{
|
1999-08-21 06:37:30 +08:00
|
|
|
ID_BLOCK *tmp;
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_id( &data, ID_BLOCK_ID(idl, j) );
|
1998-11-13 02:39:33 +08:00
|
|
|
|
1999-02-03 12:45:08 +08:00
|
|
|
if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
|
2001-01-18 01:01:19 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG( INDEX, INFO,
|
|
|
|
"idl_delete_key: idl_fetch_one returned NULL\n", 0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#else
|
1998-11-13 02:39:33 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-06-11 06:39:30 +08:00
|
|
|
"idl_delete_key: idl_fetch of returned NULL\n", 0, 0, 0 );
|
2001-01-18 01:01:19 +08:00
|
|
|
#endif
|
|
|
|
|
1998-11-13 02:39:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Now try to find the ID in tmp
|
|
|
|
*/
|
2001-09-22 14:52:32 +08:00
|
|
|
|
|
|
|
i = idl_find(tmp, id);
|
|
|
|
if ( ID_BLOCK_ID(tmp, i) == id )
|
1998-11-13 02:39:33 +08:00
|
|
|
{
|
2001-09-22 14:52:32 +08:00
|
|
|
AC_MEMCPY(
|
|
|
|
&ID_BLOCK_ID(tmp, i),
|
|
|
|
&ID_BLOCK_ID(tmp, i+1),
|
|
|
|
(ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
|
|
|
|
ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
|
|
|
|
ID_BLOCK_NIDS(tmp)--;
|
|
|
|
|
|
|
|
if ( ID_BLOCK_NIDS(tmp) ) {
|
|
|
|
idl_store ( be, db, data, tmp );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ldbm_cache_delete( db, data );
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY(
|
2001-09-22 14:52:32 +08:00
|
|
|
&ID_BLOCK_ID(idl, j),
|
|
|
|
&ID_BLOCK_ID(idl, j+1),
|
|
|
|
(nids-(j+1)) * sizeof(ID));
|
|
|
|
ID_BLOCK_ID(idl, nids-1) = NOID;
|
|
|
|
nids--;
|
|
|
|
#ifdef USE_INDIRECT_NIDS
|
|
|
|
ID_BLOCK_NIDS(idl)--;
|
|
|
|
#endif
|
|
|
|
if ( ! nids )
|
|
|
|
ldbm_cache_delete( db, key );
|
|
|
|
else
|
|
|
|
idl_store( be, db, key, idl );
|
1998-11-13 02:39:33 +08:00
|
|
|
}
|
2001-09-22 14:52:32 +08:00
|
|
|
idl_free( tmp );
|
|
|
|
cont_free( &data );
|
|
|
|
idl_free( idl );
|
|
|
|
return 0;
|
1998-11-13 02:39:33 +08:00
|
|
|
}
|
1999-04-05 05:34:33 +08:00
|
|
|
idl_free( tmp );
|
1998-11-13 02:39:33 +08:00
|
|
|
}
|
2000-06-05 06:59:38 +08:00
|
|
|
|
2000-06-15 07:48:13 +08:00
|
|
|
cont_free( &data );
|
1999-04-05 05:34:33 +08:00
|
|
|
idl_free( idl );
|
1998-11-13 02:39:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
|
|
|
/* return a duplicate of a single ID_BLOCK */
|
1999-01-31 15:55:53 +08:00
|
|
|
static ID_BLOCK *
|
|
|
|
idl_dup( ID_BLOCK *idl )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *new;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( idl == NULL ) {
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
new = idl_alloc( ID_BLOCK_NMAXN(idl) );
|
1999-01-31 15:55:53 +08:00
|
|
|
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY(
|
1999-01-31 15:55:53 +08:00
|
|
|
(char *) new,
|
|
|
|
(char *) idl,
|
2001-09-22 14:52:32 +08:00
|
|
|
(ID_BLOCK_NMAXN(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(new);
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( new );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
|
|
|
/* return the smaller ID_BLOCK */
|
1999-01-31 15:55:53 +08:00
|
|
|
static ID_BLOCK *
|
|
|
|
idl_min( ID_BLOCK *a, ID_BLOCK *b )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-01-31 15:55:53 +08:00
|
|
|
return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* idl_intersection - return a intersection b
|
|
|
|
*/
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_intersection(
|
|
|
|
Backend *be,
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *a,
|
|
|
|
ID_BLOCK *b
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
1998-11-05 14:49:49 +08:00
|
|
|
unsigned int ai, bi, ni;
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *n;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( a == NULL || b == NULL ) {
|
|
|
|
return( NULL );
|
|
|
|
}
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS( a ) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( idl_dup( b ) );
|
|
|
|
}
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS( b ) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( idl_dup( a ) );
|
|
|
|
}
|
2002-05-15 11:05:05 +08:00
|
|
|
if ( ID_BLOCK_NIDS(a) == 0 || ID_BLOCK_NIDS(b) == 0 ) {
|
|
|
|
return( NULL );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
n = idl_dup( idl_min( a, b ) );
|
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(a);
|
|
|
|
idl_check(b);
|
|
|
|
#endif
|
|
|
|
|
2002-05-15 11:05:05 +08:00
|
|
|
for ( ni = 0, ai = 0, bi = 0; ; ) {
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
|
|
|
|
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
|
2002-05-15 11:05:05 +08:00
|
|
|
ai++;
|
2002-04-11 16:01:56 +08:00
|
|
|
bi++;
|
2002-05-15 11:05:05 +08:00
|
|
|
if ( ai >= ID_BLOCK_NIDS(a) || bi >= ID_BLOCK_NIDS(b) )
|
|
|
|
break;
|
|
|
|
} else if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
|
|
|
|
ai++;
|
|
|
|
if ( ai >= ID_BLOCK_NIDS(a) )
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
bi++;
|
|
|
|
if ( bi >= ID_BLOCK_NIDS(b) )
|
|
|
|
break;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ni == 0 ) {
|
|
|
|
idl_free( n );
|
|
|
|
return( NULL );
|
|
|
|
}
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NIDS(n) = ni;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(n);
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( n );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* idl_union - return a union b
|
|
|
|
*/
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_union(
|
|
|
|
Backend *be,
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *a,
|
|
|
|
ID_BLOCK *b
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
1998-11-05 14:49:49 +08:00
|
|
|
unsigned int ai, bi, ni;
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *n;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( a == NULL ) {
|
|
|
|
return( idl_dup( b ) );
|
|
|
|
}
|
|
|
|
if ( b == NULL ) {
|
|
|
|
return( idl_dup( a ) );
|
|
|
|
}
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( idl_allids( be ) );
|
|
|
|
}
|
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(a);
|
|
|
|
idl_check(b);
|
|
|
|
#endif
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
n = a;
|
|
|
|
a = b;
|
|
|
|
b = n;
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
for ( ni = 0, ai = 0, bi = 0;
|
|
|
|
ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
|
|
|
|
)
|
|
|
|
{
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
|
|
|
|
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
|
|
|
|
|
|
|
|
} else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
|
|
|
|
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
} else {
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
|
1998-08-09 08:43:13 +08:00
|
|
|
ai++, bi++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
|
|
|
|
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-01-31 15:55:53 +08:00
|
|
|
for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
|
|
|
|
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NIDS(n) = ni;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(n);
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( n );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* idl_notin - return a intersection ~b (or a minus b)
|
|
|
|
*/
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_notin(
|
|
|
|
Backend *be,
|
2001-01-18 01:01:19 +08:00
|
|
|
ID_BLOCK *a,
|
|
|
|
ID_BLOCK *b
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
1998-11-05 14:49:49 +08:00
|
|
|
unsigned int ni, ai, bi;
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK *n;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( a == NULL ) {
|
|
|
|
return( NULL );
|
|
|
|
}
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( idl_dup( a ) );
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS( a ) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
|
|
|
|
ni = 0;
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
for ( ai = 1, bi = 0;
|
2001-09-22 14:52:32 +08:00
|
|
|
ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAXN(n) && bi < ID_BLOCK_NMAXN(b);
|
1999-01-31 15:55:53 +08:00
|
|
|
ai++ )
|
|
|
|
{
|
|
|
|
if ( ID_BLOCK_ID(b, bi) == ai ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
bi++;
|
|
|
|
} else {
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_ID(n, ni++) = ai;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAXN(n); ai++ ) {
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_ID(n, ni++) = ai;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2001-09-22 14:52:32 +08:00
|
|
|
if ( ni == ID_BLOCK_NMAXN(n) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
idl_free( n );
|
|
|
|
return( idl_allids( be ) );
|
|
|
|
} else {
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NIDS(n) = ni;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( n );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = idl_dup( a );
|
|
|
|
|
|
|
|
ni = 0;
|
1999-01-31 15:55:53 +08:00
|
|
|
for ( ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
|
|
|
|
for ( ;
|
|
|
|
bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
|
|
|
|
bi++ )
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
; /* NULL */
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( bi == ID_BLOCK_NIDS(b) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ID(b, bi) != ID_BLOCK_ID(a, ai) ) {
|
|
|
|
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
|
|
|
|
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-01-31 15:55:53 +08:00
|
|
|
ID_BLOCK_NIDS(n) = ni;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-09-08 05:58:31 +08:00
|
|
|
#ifdef LDBM_DEBUG_IDL
|
|
|
|
idl_check(n);
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( n );
|
|
|
|
}
|
|
|
|
|
1999-02-02 11:40:19 +08:00
|
|
|
/* return the first ID in the block
|
|
|
|
* if ALLIDS block
|
|
|
|
* NIDS > 1 return 1
|
|
|
|
* otherwise return NOID
|
|
|
|
* otherwise return first ID
|
1999-08-08 05:14:24 +08:00
|
|
|
*
|
|
|
|
* cursor is set to 1
|
2001-01-18 01:01:19 +08:00
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
ID
|
1999-08-08 05:14:24 +08:00
|
|
|
idl_firstid( ID_BLOCK *idl, ID *cursor )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-08-08 05:14:24 +08:00
|
|
|
*cursor = 1;
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( idl == NULL || ID_BLOCK_NIDS(idl) == 0 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NOID );
|
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS( idl ) ) {
|
1999-02-02 10:46:18 +08:00
|
|
|
return( ID_BLOCK_NIDS(idl) > 1 ? 1 : NOID );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-01-31 15:55:53 +08:00
|
|
|
return( ID_BLOCK_ID(idl, 0) );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-08-08 05:14:24 +08:00
|
|
|
/* return next ID
|
|
|
|
* if ALLIDS block, cursor is id.
|
|
|
|
* increment id
|
1999-02-02 11:40:19 +08:00
|
|
|
* if id < NIDS return id
|
|
|
|
* otherwise NOID.
|
1999-08-08 05:14:24 +08:00
|
|
|
* otherwise cursor is index into block
|
|
|
|
* if index < nids
|
|
|
|
* return id at index then increment
|
1999-02-02 11:40:19 +08:00
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
ID
|
1999-08-08 05:14:24 +08:00
|
|
|
idl_nextid( ID_BLOCK *idl, ID *cursor )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-01-31 15:55:53 +08:00
|
|
|
if ( ID_BLOCK_ALLIDS( idl ) ) {
|
1999-08-08 05:14:24 +08:00
|
|
|
if( ++(*cursor) < ID_BLOCK_NIDS(idl) ) {
|
|
|
|
return *cursor;
|
|
|
|
} else {
|
|
|
|
return NOID;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-08-08 05:14:24 +08:00
|
|
|
if ( *cursor < ID_BLOCK_NIDS(idl) ) {
|
|
|
|
return( ID_BLOCK_ID(idl, (*cursor)++) );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-08-08 05:14:24 +08:00
|
|
|
return( NOID );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|