summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/csn.c
blob: 213305f061b95e9fdd7ad99d1a75ee9622525522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/*
 *  csn.c - CSN
 */

#include <string.h>
#include "slap.h"
#include <prcountr.h>

#define _CSN_TSTAMP_STRSIZE_STR "8"
#define _CSN_SEQNUM_STRSIZE_STR "4"
#define _CSN_REPLID_STRSIZE_STR "4"
#define _CSN_SUBSEQNUM_STRSIZE_STR "4"

#define _CSN_TSTAMP_SCANSTR "%"_CSN_TSTAMP_STRSIZE_STR"lx"
#define _CSN_SEQNUM_SCANSTR "%"_CSN_SEQNUM_STRSIZE_STR"hx"
#define _CSN_REPLID_SCANSTR "%"_CSN_REPLID_STRSIZE_STR"hx"
#define _CSN_SUBSEQNUM_SCANSTR "%"_CSN_SUBSEQNUM_STRSIZE_STR"hx" 

#define _CSN_TSORDER_SPRINTSTR "%08x%04x%04x%04x"

#define _CSN_TSORDER_TSTAMP_OFFSET 0
#define _CSN_TSORDER_SEQNUM_OFFSET 8
#define _CSN_TSORDER_REPLID_OFFSET 12
#define _CSN_TSORDER_SUBSEQNUM_OFFSET 16

static PRBool _csnIsValidString(const char *csnStr);

/*
 * Debugging counters.
 */
static int counters_created= 0;
PR_DEFINE_COUNTER(slapi_csn_counter_created);
PR_DEFINE_COUNTER(slapi_csn_counter_deleted);
PR_DEFINE_COUNTER(slapi_csn_counter_exist);

/*
 * **************************************************************************
 * CSN Functions
 * **************************************************************************
 */

static void
csn_create_counters()
{
	PR_CREATE_COUNTER(slapi_csn_counter_created,"Slapi_CSN","created","");
	PR_CREATE_COUNTER(slapi_csn_counter_deleted,"Slapi_CSN","deleted","");
	PR_CREATE_COUNTER(slapi_csn_counter_exist,"Slapi_CSN","exist","");
	counters_created= 1;
}

CSN *csn_new()
{
	if(!counters_created)
	{
		csn_create_counters();
	}
	PR_INCREMENT_COUNTER(slapi_csn_counter_created);
	PR_INCREMENT_COUNTER(slapi_csn_counter_exist);
	return (CSN*)slapi_ch_calloc(sizeof(CSN),1);
}

CSN *csn_new_by_string(const char *s)
{
	CSN *newcsn= NULL;
	if(s!=NULL)
	{
	    if(_csnIsValidString(s))
		{
			newcsn= csn_new();
			csn_init_by_string(newcsn,s);
		}
	}
	return newcsn;
}

void csn_init(CSN *csn)
{
	if(csn!=NULL)
	{
		memset(csn,0,sizeof(CSN));
	}
}

void csn_init_by_csn(CSN *csn1,const CSN *csn2)
{
	if(csn2!=NULL)
	{
		memcpy(csn1,csn2,sizeof(CSN));
	}
	else
	{
		csn_init(csn1);
	}
}

void csn_init_by_string(CSN *csn, const char *s)
{
	time_t csnTime= 0;
	PRUint16 csnSeqNum= 0;
	ReplicaId rid= 0;
	PRUint16 csnSubSeqNum= 0;

    if(_csnIsValidString(s))
    {
		/* JCM - char2hex faster */
		sscanf((s+_CSN_TSORDER_TSTAMP_OFFSET), _CSN_TSTAMP_SCANSTR, &csnTime); /* JCM - scanf is very slow */
		sscanf((s+_CSN_TSORDER_SEQNUM_OFFSET), _CSN_SEQNUM_SCANSTR, &csnSeqNum);/* JCM - scanf is very slow */
		sscanf((s+_CSN_TSORDER_REPLID_OFFSET), _CSN_REPLID_SCANSTR, &rid);/* JCM - scanf is very slow */
	    sscanf((s+_CSN_TSORDER_SUBSEQNUM_OFFSET), _CSN_SUBSEQNUM_SCANSTR, &csnSubSeqNum);/* JCM - scanf is very slow */
	    csn->tstamp= csnTime;
	    csn->seqnum= csnSeqNum;
	    csn->rid= rid;
		csn->subseqnum= csnSubSeqNum;
	}
}

CSN *csn_dup(const CSN *csn)
{
	CSN *newcsn= NULL;
    if(csn!=NULL)
    {
		newcsn= csn_new();
		csn_init_by_csn(newcsn,csn);
    }
	return newcsn;
}

void csn_done(CSN *csn)
{
}

void csn_free(CSN **csn)
{
	if(csn!=NULL && *csn!=NULL)
	{
		if(!counters_created)
		{
			csn_create_counters();
		}
		PR_INCREMENT_COUNTER(slapi_csn_counter_deleted);
		PR_DECREMENT_COUNTER(slapi_csn_counter_exist);
	    slapi_ch_free((void **)csn);
	}
    return;
}

void csn_set_replicaid(CSN *csn, ReplicaId rid)
{
    csn->rid= rid;
}

void csn_set_time(CSN *csn, time_t csntime)
{
    csn->tstamp= csntime;
}

void csn_set_seqnum(CSN *csn, PRUint16 seqnum)
{
    csn->seqnum= seqnum;
}

ReplicaId csn_get_replicaid(const CSN *csn)
{
    return csn->rid;
}

PRUint16 csn_get_seqnum(const CSN *csn)
{
    return csn->seqnum;
}

time_t csn_get_time(const CSN *csn)
{
	if(csn==NULL)
	{
		return 0;
	}
	else
	{
		return csn->tstamp;
	}
}


/*
 * WARNING: ss must point to memory at least CSN_STRSIZE bytes long, 
 * WARNING: or be NULL, which means this function will allocate the 
 * WARNING: memory, which must be free'd by the caller.
 */
char *
csn_as_string(const CSN *csn, PRBool replicaIdOrder, char *ss)
{
	char *s= ss;
	if(s==NULL)
	{
		s= slapi_ch_malloc(CSN_STRSIZE);
	}
	if(csn==NULL)
	{
		s[0]= '\0';
	}
	else
	{
		/* JCM - hex2char would be quicker */
		sprintf(s,"%08lx%04x%04x%04x",
				csn->tstamp,csn->seqnum,csn->rid, csn->subseqnum);
	}
	return s;
}


/*
 * WARNING: ss must point to memory at least (7+CSN_STRSIZE) bytes long, 
 * WARNING: or be NULL, which means this function will allocate the 
 * WARNING: memory, which must be free'd by the caller.
 */
char *
csn_as_attr_option_string(CSNType t,const CSN *csn,char *ss)
{
	char *s= ss;
	if(csn!=NULL)
	{
		if(s==NULL)
		{
			s= slapi_ch_malloc(8+CSN_STRSIZE);
		}
		s[0]= ';';
		switch(t)
		{
		case CSN_TYPE_UNKNOWN:
			s[1]= 'x';
			s[2]= '1';
			break;
		case CSN_TYPE_NONE:
			s[1]= 'x';
			s[2]= '2';
			break;
		case CSN_TYPE_ATTRIBUTE_DELETED:
			s[1]= 'a';
			s[2]= 'd';
			break;
		case CSN_TYPE_VALUE_UPDATED:
			s[1]= 'v';
			s[2]= 'u';
			break;
		case CSN_TYPE_VALUE_DELETED:
			s[1]= 'v';
			s[2]= 'd';
			break;
		case CSN_TYPE_VALUE_DISTINGUISHED:
			s[1]= 'm';
			s[2]= 'd';
			break;
		}
		s[3]= 'c';
		s[4]= 's';
		s[5]= 'n';
		s[6]= '-';
		csn_as_string(csn,PR_FALSE,s+7);
	}
	return s;
}

int 
csn_compare(const CSN *csn1, const CSN *csn2)
{
    PRInt32 retVal;
	if(csn1!=NULL && csn2!=NULL)
	{
        /* csns can't be compared via memcmp (previuos version of the code) 
           because, on NT, bytes are reversed */
        if (csn1->tstamp < csn2->tstamp)
            retVal = -1;
        else if (csn1->tstamp > csn2->tstamp)
            retVal = 1;
        else
        {
            if (csn1->seqnum < csn2->seqnum)
                retVal = -1;
            else if (csn1->seqnum > csn2->seqnum)
                retVal = 1;
            else
            {
                if (csn1->rid < csn2->rid)
                    retVal = -1;
                else if (csn1->rid > csn2->rid)
                    retVal = 1;
                else
                {
                    if (csn1->subseqnum < csn2->subseqnum)
                        retVal = -1;
                    else if (csn1->subseqnum > csn2->subseqnum)
                        retVal = 1;
                    else
                        retVal = 0;
                }
            }
        }
		
	}
	else if(csn1!=NULL && csn2==NULL)
	{
		retVal= 1; /* csn1>csn2 */
	}
	else if (csn1==NULL && csn2!=NULL)
	{
		retVal= -1; /* csn1<csn2 */
	}
	else /* (csn1==NULL && csn2==NULL) */
	{
		retVal= 0; /* The same */
	}

    return(retVal);
}

time_t csn_time_difference(const CSN *csn1, const CSN *csn2)
{
	return csn_get_time(csn1) - csn_get_time(csn2);
}

const CSN *
csn_max(const CSN *csn1,const CSN *csn2)
{
	if(csn_compare(csn1, csn2)>0)
	{
		return csn1;
	}
	else
	{
		return csn2;
	}
}

int csn_increment_subsequence (CSN *csn)
{
	if (csn == NULL)
	{
		return -1;
	}
	else if (csn->subseqnum == 0xFFFFFFFF)
	{
		slapi_log_error(SLAPI_LOG_FATAL, NULL, 
						"csn_increment_subsequence: subsequence overflow\n");

		return -1;
	}
	else 
	{
		csn->subseqnum ++;
		return 0;	
	}
}

/*
 * sizeof(vucsn-011111111222233334444)
 * Does not include the trailing '\0'
 */
size_t
csn_string_size()
{
	return LDIF_CSNPREFIX_MAXLENGTH + _CSN_VALIDCSN_STRLEN;
}

static PRBool
_csnIsValidString(const char *s)
{
    if(NULL == s) {
		return(PR_FALSE);
    }
    if(strlen(s) < _CSN_VALIDCSN_STRLEN) {
		return(PR_FALSE);
    }

    /* some more checks on validity of tstamp portion? */
    return(PR_TRUE);
}