mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Nicolas Capponi
22.51.2014 db364b9d90deb67f8c9928d79299d68512853efb
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 *      Portions Copyright 2013-2014 ForgeRock AS.
 */
package org.opends.server.replication.common;
 
import java.io.Serializable;
import java.util.Date;
 
import org.forgerock.opendj.ldap.ByteSequence;
import org.forgerock.opendj.ldap.ByteSequenceReader;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ByteStringBuilder;
 
/**
 * Class used to represent Change Sequence Numbers.
 *
 * @see <a href="http://tools.ietf.org/html/draft-ietf-ldup-infomod-08"
 * >Inspiration for this class comes from LDAPChangeSequenceNumber</a>
 */
public class CSN implements Serializable, Comparable<CSN>
{
  /**
   * The number of bytes used by the byte string representation of a change
   * number.
   *
   * @see #valueOf(ByteSequence)
   * @see #toByteString()
   * @see #toByteString(ByteStringBuilder)
   */
  public static final int BYTE_ENCODING_LENGTH = 14;
 
  /**
   * The number of characters used by the string representation of a change
   * number.
   *
   * @see #valueOf(String)
   * @see #toString()
   */
  public static final int STRING_ENCODING_LENGTH = 28;
 
  /** The maximum possible value for a CSN. */
  public static final CSN MAX_CSN_VALUE = new CSN(Long.MAX_VALUE, Integer.MAX_VALUE, Short.MAX_VALUE);
 
  private static final long serialVersionUID = -8802722277749190740L;
  private final long timeStamp;
  /**
   * The sequence number is set to zero at the start of each millisecond, and
   * incremented by one for each update operation that occurs within that
   * millisecond. It allows to distinguish changes that have been done in the
   * same millisecond.
   */
  private final int seqnum;
  private final int serverId;
 
  /**
   * Parses the provided {@link #toString()} representation of a CSN.
   *
   * @param s
   *          The string to be parsed.
   * @return The parsed CSN.
   * @see #toString()
   */
  public static CSN valueOf(String s)
  {
    return new CSN(s);
  }
 
  /**
   * Decodes the provided {@link #toByteString()} representation of a change
   * number.
   *
   * @param bs
   *          The byte sequence to be parsed.
   * @return The decoded CSN.
   * @see #toByteString()
   */
  public static CSN valueOf(ByteSequence bs)
  {
    ByteSequenceReader reader = bs.asReader();
    long timeStamp = reader.getLong();
    int serverId = reader.getShort() & 0xffff;
    int seqnum = reader.getInt();
    return new CSN(timeStamp, seqnum, serverId);
  }
 
  /**
   * Create a new {@link CSN} from a String.
   *
   * @param str
   *          the string from which to create a {@link CSN}
   */
  public CSN(String str)
  {
    String temp = str.substring(0, 16);
    timeStamp = Long.parseLong(temp, 16);
 
    temp = str.substring(16, 20);
    serverId = Integer.parseInt(temp, 16);
 
    temp = str.substring(20, 28);
    seqnum = Integer.parseInt(temp, 16);
  }
 
  /**
   * Create a new {@link CSN}.
   *
   * @param timeStamp
   *          timeStamp for the {@link CSN}
   * @param seqNum
   *          sequence number
   * @param serverId
   *          identity of server
   */
  public CSN(long timeStamp, int seqNum, int serverId)
  {
    this.serverId = serverId;
    this.timeStamp = timeStamp;
    this.seqnum = seqNum;
  }
 
  /**
   * Getter for the time.
   *
   * @return the time
   */
  public long getTime()
  {
    return timeStamp;
  }
 
  /**
   * Get the timestamp associated to this {@link CSN} in seconds.
   *
   * @return timestamp associated to this {@link CSN} in seconds
   */
  public long getTimeSec()
  {
    return timeStamp / 1000;
  }
 
  /**
   * Getter for the sequence number.
   *
   * @return the sequence number
   */
  public int getSeqnum()
  {
    return seqnum;
  }
 
  /**
   * Getter for the server ID.
   *
   * @return the server ID
   */
  public int getServerId()
  {
    return serverId;
  }
 
  /**
   * {@inheritDoc}
   */
  @Override
  public boolean equals(Object obj)
  {
    if (this == obj)
    {
      return true;
    }
    else if (obj instanceof CSN)
    {
      final CSN csn = (CSN) obj;
      return this.seqnum == csn.seqnum && this.serverId == csn.serverId
          && this.timeStamp == csn.timeStamp;
    }
    else
    {
      return false;
    }
  }
 
  /**
   * {@inheritDoc}
   */
  @Override
  public int hashCode()
  {
    return this.seqnum + this.serverId + Long.valueOf(timeStamp).hashCode();
  }
 
  /**
   * Encodes this CSN as a byte string.
   * <p>
   * NOTE: this representation must not be modified otherwise interop with
   * earlier protocol versions will be broken.
   *
   * @return The encoded representation of this CSN.
   * @see #valueOf(ByteSequence)
   */
  public ByteString toByteString()
  {
    return toByteString(new ByteStringBuilder(BYTE_ENCODING_LENGTH))
        .toByteString();
  }
 
  /**
   * Encodes this CSN into the provided byte string builder.
   * <p>
   * NOTE: this representation must not be modified otherwise interop with
   * earlier protocol versions will be broken.
   *
   * @param builder
   *          The byte string builder.
   * @return The byte string builder containing the encoded CSN.
   * @see #valueOf(ByteSequence)
   */
  public ByteStringBuilder toByteString(ByteStringBuilder builder)
  {
    return builder.append(timeStamp).append((short) (serverId & 0xffff))
        .append(seqnum);
  }
 
  /**
   * Convert the {@link CSN} to a printable String.
   * <p>
   * NOTE: this representation must not be modified otherwise interop with
   * earlier protocol versions will be broken.
   *
   * @return the string
   */
  @Override
  public String toString()
  {
    return String.format("%016x%04x%08x", timeStamp, serverId, seqnum);
  }
 
  /**
   * Convert the {@link CSN} to a printable String with a user friendly format.
   *
   * @return the string
   */
  public String toStringUI()
  {
    Date date = new Date(timeStamp);
    return String.format(
        "%016x%04x%08x (sid=%d,tsd=%s,ts=%d,seqnum=%d)",
        timeStamp, serverId, seqnum,
        serverId, date.toString(), timeStamp, seqnum);
  }
 
  /**
   * Compares this CSN with the provided CSN for order and returns a negative
   * number if {@code csn1} is older than {@code csn2}, zero if they have the
   * same age, or a positive number if {@code csn1} is newer than {@code csn2}.
   *
   * @param csn1
   *          The first CSN to be compared, which may be {@code null}.
   * @param csn2
   *          The second CSN to be compared, which may be {@code null}.
   * @return A negative number if {@code csn1} is older than {@code csn2}, zero
   *         if they have the same age, or a positive number if {@code csn1} is
   *         newer than {@code csn2}.
   */
  public static int compare(CSN csn1, CSN csn2)
  {
    if (csn1 == null)
    {
      return csn2 == null ? 0 : -1;
    }
    else if (csn2 == null)
    {
      return 1;
    }
    else if (csn1.timeStamp != csn2.timeStamp)
    {
      return csn1.timeStamp < csn2.timeStamp ? -1 : 1;
    }
    else if (csn1.seqnum != csn2.seqnum)
    {
      return csn1.seqnum - csn2.seqnum;
    }
    else
    {
      return csn1.serverId - csn2.serverId;
    }
  }
 
  /**
   * Computes the difference in number of changes between 2 CSNs. First one is
   * expected to be newer than second one. If this is not the case, 0 will be
   * returned.
   *
   * @param csn1
   *          the first {@link CSN}
   * @param csn2
   *          the second {@link CSN}
   * @return the difference
   */
  public static int diffSeqNum(CSN csn1, CSN csn2)
  {
    if (csn1 == null)
    {
      return 0;
    }
    if (csn2 == null)
    {
      return csn1.getSeqnum();
    }
    if (csn2.isNewerThanOrEqualTo(csn1))
    {
      return 0;
    }
 
    int seqnum1 = csn1.getSeqnum();
    long time1 = csn1.getTime();
    int seqnum2 = csn2.getSeqnum();
    long time2 = csn2.getTime();
 
    if (time2 <= time1)
    {
      if (seqnum2 <= seqnum1)
      {
        return seqnum1 - seqnum2;
      }
      return Integer.MAX_VALUE - (seqnum2 - seqnum1) + 1;
    }
    return 0;
  }
 
  /**
   * Returns {@code true} if this CSN is older than the provided CSN.
   *
   * @param csn
   *          The CSN to be compared.
   * @return {@code true} if this CSN is older than the provided CSN.
   */
  public boolean isOlderThan(CSN csn)
  {
    return compare(this, csn) < 0;
  }
 
  /**
   * Returns {@code true} if this CSN is older than or equal to the provided
   * CSN.
   *
   * @param csn
   *          The CSN to be compared.
   * @return {@code true} if this CSN is older than or equal to the provided
   *         CSN.
   */
  public boolean isOlderThanOrEqualTo(CSN csn)
  {
    return compare(this, csn) <= 0;
  }
 
  /**
   * Returns {@code true} if this CSN is newer than or equal to the provided
   * CSN.
   *
   * @param csn
   *          The CSN to be compared.
   * @return {@code true} if this CSN is newer than or equal to the provided
   *         CSN.
   */
  public boolean isNewerThanOrEqualTo(CSN csn)
  {
    return compare(this, csn) >= 0;
  }
 
  /**
   * Returns {@code true} if this CSN is newer than the provided CSN.
   *
   * @param csn
   *          The CSN to be compared.
   * @return {@code true} if this CSN is newer than the provided CSN.
   */
  public boolean isNewerThan(CSN csn)
  {
    return compare(this, csn) > 0;
  }
 
  /**
   * Compares this CSN with the provided CSN for order and returns a negative
   * number if this CSN is older than {@code csn}, zero if they have the same
   * age, or a positive number if this CSN is newer than {@code csn}.
   *
   * @param csn
   *          The CSN to be compared.
   * @return A negative number if this CSN is older than {@code csn}, zero if
   *         they have the same age, or a positive number if this CSN is newer
   *         than {@code csn}.
   */
  @Override
  public int compareTo(CSN csn)
  {
    return compare(this, csn);
  }
}