csn.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * csn.c - CSN
  43. */
  44. #include <string.h>
  45. #include "slap.h"
  46. #include <prcountr.h>
  47. #define _CSN_TSTAMP_STRSIZE_STR "8"
  48. #define _CSN_SEQNUM_STRSIZE_STR "4"
  49. #define _CSN_REPLID_STRSIZE_STR "4"
  50. #define _CSN_SUBSEQNUM_STRSIZE_STR "4"
  51. #define _CSN_TSTAMP_SCANSTR "%"_CSN_TSTAMP_STRSIZE_STR"lx"
  52. #define _CSN_SEQNUM_SCANSTR "%"_CSN_SEQNUM_STRSIZE_STR"hx"
  53. #define _CSN_REPLID_SCANSTR "%"_CSN_REPLID_STRSIZE_STR"hx"
  54. #define _CSN_SUBSEQNUM_SCANSTR "%"_CSN_SUBSEQNUM_STRSIZE_STR"hx"
  55. #define _CSN_TSORDER_SPRINTSTR "%08x%04x%04x%04x"
  56. #define _CSN_TSORDER_TSTAMP_OFFSET 0
  57. #define _CSN_TSORDER_SEQNUM_OFFSET 8
  58. #define _CSN_TSORDER_REPLID_OFFSET 12
  59. #define _CSN_TSORDER_SUBSEQNUM_OFFSET 16
  60. static PRBool _csnIsValidString(const char *csnStr);
  61. /*
  62. * Debugging counters.
  63. */
  64. static int counters_created= 0;
  65. PR_DEFINE_COUNTER(slapi_csn_counter_created);
  66. PR_DEFINE_COUNTER(slapi_csn_counter_deleted);
  67. PR_DEFINE_COUNTER(slapi_csn_counter_exist);
  68. /*
  69. * **************************************************************************
  70. * CSN Functions
  71. * **************************************************************************
  72. */
  73. static void
  74. csn_create_counters()
  75. {
  76. PR_CREATE_COUNTER(slapi_csn_counter_created,"Slapi_CSN","created","");
  77. PR_CREATE_COUNTER(slapi_csn_counter_deleted,"Slapi_CSN","deleted","");
  78. PR_CREATE_COUNTER(slapi_csn_counter_exist,"Slapi_CSN","exist","");
  79. counters_created= 1;
  80. }
  81. CSN *csn_new()
  82. {
  83. if(!counters_created)
  84. {
  85. csn_create_counters();
  86. }
  87. PR_INCREMENT_COUNTER(slapi_csn_counter_created);
  88. PR_INCREMENT_COUNTER(slapi_csn_counter_exist);
  89. return (CSN*)slapi_ch_calloc(sizeof(CSN),1);
  90. }
  91. CSN *csn_new_by_string(const char *s)
  92. {
  93. CSN *newcsn= NULL;
  94. if(s!=NULL)
  95. {
  96. if(_csnIsValidString(s))
  97. {
  98. newcsn= csn_new();
  99. csn_init_by_string(newcsn,s);
  100. }
  101. }
  102. return newcsn;
  103. }
  104. void csn_init(CSN *csn)
  105. {
  106. if(csn!=NULL)
  107. {
  108. memset(csn,0,sizeof(CSN));
  109. }
  110. }
  111. void csn_init_by_csn(CSN *csn1,const CSN *csn2)
  112. {
  113. if(csn2!=NULL)
  114. {
  115. memcpy(csn1,csn2,sizeof(CSN));
  116. }
  117. else
  118. {
  119. csn_init(csn1);
  120. }
  121. }
  122. void csn_init_by_string(CSN *csn, const char *s)
  123. {
  124. time_t csnTime= 0;
  125. PRUint16 csnSeqNum= 0;
  126. ReplicaId rid= 0;
  127. PRUint16 csnSubSeqNum= 0;
  128. if(_csnIsValidString(s))
  129. {
  130. /* JCM - char2hex faster */
  131. sscanf((s+_CSN_TSORDER_TSTAMP_OFFSET), _CSN_TSTAMP_SCANSTR, &csnTime); /* JCM - scanf is very slow */
  132. sscanf((s+_CSN_TSORDER_SEQNUM_OFFSET), _CSN_SEQNUM_SCANSTR, &csnSeqNum);/* JCM - scanf is very slow */
  133. sscanf((s+_CSN_TSORDER_REPLID_OFFSET), _CSN_REPLID_SCANSTR, &rid);/* JCM - scanf is very slow */
  134. sscanf((s+_CSN_TSORDER_SUBSEQNUM_OFFSET), _CSN_SUBSEQNUM_SCANSTR, &csnSubSeqNum);/* JCM - scanf is very slow */
  135. csn->tstamp= csnTime;
  136. csn->seqnum= csnSeqNum;
  137. csn->rid= rid;
  138. csn->subseqnum= csnSubSeqNum;
  139. }
  140. }
  141. CSN *csn_dup(const CSN *csn)
  142. {
  143. CSN *newcsn= NULL;
  144. if(csn!=NULL)
  145. {
  146. newcsn= csn_new();
  147. csn_init_by_csn(newcsn,csn);
  148. }
  149. return newcsn;
  150. }
  151. void csn_done(CSN *csn)
  152. {
  153. }
  154. void csn_free(CSN **csn)
  155. {
  156. if(csn!=NULL && *csn!=NULL)
  157. {
  158. if(!counters_created)
  159. {
  160. csn_create_counters();
  161. }
  162. PR_INCREMENT_COUNTER(slapi_csn_counter_deleted);
  163. PR_DECREMENT_COUNTER(slapi_csn_counter_exist);
  164. slapi_ch_free((void **)csn);
  165. }
  166. return;
  167. }
  168. void csn_set_replicaid(CSN *csn, ReplicaId rid)
  169. {
  170. csn->rid= rid;
  171. }
  172. void csn_set_time(CSN *csn, time_t csntime)
  173. {
  174. csn->tstamp= csntime;
  175. }
  176. void csn_set_seqnum(CSN *csn, PRUint16 seqnum)
  177. {
  178. csn->seqnum= seqnum;
  179. }
  180. ReplicaId csn_get_replicaid(const CSN *csn)
  181. {
  182. return csn->rid;
  183. }
  184. PRUint16 csn_get_seqnum(const CSN *csn)
  185. {
  186. return csn->seqnum;
  187. }
  188. time_t csn_get_time(const CSN *csn)
  189. {
  190. if(csn==NULL)
  191. {
  192. return 0;
  193. }
  194. else
  195. {
  196. return csn->tstamp;
  197. }
  198. }
  199. /*
  200. * WARNING: ss must point to memory at least CSN_STRSIZE bytes long,
  201. * WARNING: or be NULL, which means this function will allocate the
  202. * WARNING: memory, which must be free'd by the caller.
  203. */
  204. char *
  205. csn_as_string(const CSN *csn, PRBool replicaIdOrder, char *ss)
  206. {
  207. char *s= ss;
  208. if(s==NULL)
  209. {
  210. s= slapi_ch_malloc(CSN_STRSIZE);
  211. }
  212. if(csn==NULL)
  213. {
  214. s[0]= '\0';
  215. }
  216. else
  217. {
  218. /* JCM - hex2char would be quicker */
  219. sprintf(s,"%08lx%04x%04x%04x",
  220. csn->tstamp,csn->seqnum,csn->rid, csn->subseqnum);
  221. }
  222. return s;
  223. }
  224. /*
  225. * WARNING: ss must point to memory at least (7+CSN_STRSIZE) bytes long,
  226. * WARNING: or be NULL, which means this function will allocate the
  227. * WARNING: memory, which must be free'd by the caller.
  228. */
  229. char *
  230. csn_as_attr_option_string(CSNType t,const CSN *csn,char *ss)
  231. {
  232. char *s= ss;
  233. if(csn!=NULL)
  234. {
  235. if(s==NULL)
  236. {
  237. s= slapi_ch_malloc(8+CSN_STRSIZE);
  238. }
  239. s[0]= ';';
  240. switch(t)
  241. {
  242. case CSN_TYPE_UNKNOWN:
  243. s[1]= 'x';
  244. s[2]= '1';
  245. break;
  246. case CSN_TYPE_NONE:
  247. s[1]= 'x';
  248. s[2]= '2';
  249. break;
  250. case CSN_TYPE_ATTRIBUTE_DELETED:
  251. s[1]= 'a';
  252. s[2]= 'd';
  253. break;
  254. case CSN_TYPE_VALUE_UPDATED:
  255. s[1]= 'v';
  256. s[2]= 'u';
  257. break;
  258. case CSN_TYPE_VALUE_DELETED:
  259. s[1]= 'v';
  260. s[2]= 'd';
  261. break;
  262. case CSN_TYPE_VALUE_DISTINGUISHED:
  263. s[1]= 'm';
  264. s[2]= 'd';
  265. break;
  266. }
  267. s[3]= 'c';
  268. s[4]= 's';
  269. s[5]= 'n';
  270. s[6]= '-';
  271. csn_as_string(csn,PR_FALSE,s+7);
  272. }
  273. return s;
  274. }
  275. int
  276. csn_compare(const CSN *csn1, const CSN *csn2)
  277. {
  278. PRInt32 retVal;
  279. if(csn1!=NULL && csn2!=NULL)
  280. {
  281. /* csns can't be compared via memcmp (previuos version of the code)
  282. because, on NT, bytes are reversed */
  283. if (csn1->tstamp < csn2->tstamp)
  284. retVal = -1;
  285. else if (csn1->tstamp > csn2->tstamp)
  286. retVal = 1;
  287. else
  288. {
  289. if (csn1->seqnum < csn2->seqnum)
  290. retVal = -1;
  291. else if (csn1->seqnum > csn2->seqnum)
  292. retVal = 1;
  293. else
  294. {
  295. if (csn1->rid < csn2->rid)
  296. retVal = -1;
  297. else if (csn1->rid > csn2->rid)
  298. retVal = 1;
  299. else
  300. {
  301. if (csn1->subseqnum < csn2->subseqnum)
  302. retVal = -1;
  303. else if (csn1->subseqnum > csn2->subseqnum)
  304. retVal = 1;
  305. else
  306. retVal = 0;
  307. }
  308. }
  309. }
  310. }
  311. else if(csn1!=NULL && csn2==NULL)
  312. {
  313. retVal= 1; /* csn1>csn2 */
  314. }
  315. else if (csn1==NULL && csn2!=NULL)
  316. {
  317. retVal= -1; /* csn1<csn2 */
  318. }
  319. else /* (csn1==NULL && csn2==NULL) */
  320. {
  321. retVal= 0; /* The same */
  322. }
  323. return(retVal);
  324. }
  325. time_t csn_time_difference(const CSN *csn1, const CSN *csn2)
  326. {
  327. return csn_get_time(csn1) - csn_get_time(csn2);
  328. }
  329. const CSN *
  330. csn_max(const CSN *csn1,const CSN *csn2)
  331. {
  332. if(csn_compare(csn1, csn2)>0)
  333. {
  334. return csn1;
  335. }
  336. else
  337. {
  338. return csn2;
  339. }
  340. }
  341. int csn_increment_subsequence (CSN *csn)
  342. {
  343. PRUint16 maxsubseq = (PRUint16)0xFFFFFFFF;
  344. if (csn == NULL)
  345. {
  346. return -1;
  347. }
  348. else if (csn->subseqnum == maxsubseq)
  349. {
  350. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  351. "csn_increment_subsequence: subsequence overflow\n");
  352. return -1;
  353. }
  354. else
  355. {
  356. csn->subseqnum ++;
  357. return 0;
  358. }
  359. }
  360. /*
  361. * sizeof(vucsn-011111111222233334444)
  362. * Does not include the trailing '\0'
  363. */
  364. size_t
  365. csn_string_size()
  366. {
  367. return LDIF_CSNPREFIX_MAXLENGTH + _CSN_VALIDCSN_STRLEN;
  368. }
  369. static PRBool
  370. _csnIsValidString(const char *s)
  371. {
  372. if(NULL == s) {
  373. return(PR_FALSE);
  374. }
  375. if(strlen(s) < _CSN_VALIDCSN_STRLEN) {
  376. return(PR_FALSE);
  377. }
  378. /* some more checks on validity of tstamp portion? */
  379. return(PR_TRUE);
  380. }