]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
Happy new year! (belated)
[openldap] / servers / slapd / back-ldbm / nextid.c
1 /* nextid.c - keep track of the next id to be given out */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2008 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23 #include <ac/param.h>
24
25 #include "slap.h"
26 #include "back-ldbm.h"
27
28 static int
29 next_id_read( Backend *be, ID *idp )
30 {
31         Datum key, data;
32         DBCache *db;
33
34         *idp = NOID;
35
36         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
37             == NULL ) {
38                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
39                         0, 0, 0 );
40
41                 return( -1 );
42         }
43
44         ldbm_datum_init( key );
45         key.dptr = (char *) idp;
46         key.dsize = sizeof(ID);
47
48         data = ldbm_cache_fetch( db, key );
49
50         if( data.dptr != NULL ) {
51                 AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
52                 ldbm_datum_free( db->dbc_db, data );
53
54         } else {
55                 *idp = 1;
56         }
57
58         ldbm_cache_close( be, db );
59         return( 0 );
60 }
61
62 int
63 next_id_write( Backend *be, ID id )
64 {
65         Datum key, data;
66         DBCache *db;
67         ID noid = NOID;
68         int flags, rc = 0;
69
70         if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
71             == NULL ) {
72                 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
73                     0, 0, 0 );
74
75                 return( -1 );
76         }
77
78         ldbm_datum_init( key );
79         ldbm_datum_init( data );
80
81         key.dptr = (char *) &noid;
82         key.dsize = sizeof(ID);
83
84         data.dptr = (char *) &id;
85         data.dsize = sizeof(ID);
86
87         flags = LDBM_REPLACE;
88         if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
89                 rc = -1;
90         }
91
92         ldbm_cache_close( be, db );
93         return( rc );
94 }
95
96 int
97 next_id_get( Backend *be, ID *idp )
98 {
99         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
100         int rc = 0;
101
102         *idp = NOID;
103
104         if ( li->li_nextid == NOID ) {
105                 if ( ( rc = next_id_read( be, idp ) ) ) {
106                         return( rc );
107                 }
108                 li->li_nextid = *idp;
109         }
110
111         *idp = li->li_nextid;
112
113         return( rc );
114 }
115
116 int
117 next_id( Backend *be, ID *idp )
118 {
119         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
120         int rc = 0;
121
122         if ( li->li_nextid == NOID ) {
123                 if ( ( rc = next_id_read( be, idp ) ) ) {
124                         return( rc );
125                 }
126                 li->li_nextid = *idp;
127         }
128
129         *idp = li->li_nextid++;
130         if ( next_id_write( be, li->li_nextid ) ) {
131                 rc = -1;
132         }
133
134         return( rc );
135 }