1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.directory.api.ldap.model.csn; 21 22 23 /** 24 * Generates a new {@link Csn}. 25 * 26 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 27 */ 28 public class CsnFactory 29 { 30 /** The last timestamp */ 31 private static volatile long lastTimestamp; 32 33 /** The integer used to disambiguate CSN generated at the same time */ 34 private int changeCount; 35 36 /** The replicaId to use for every CSN created by this factory */ 37 private int replicaId; 38 39 /** A special instance ID for a purge CSN */ 40 private static final int PURGE_INSTANCEID = 0x0FFF; 41 42 /** A lock used during the instance creation */ 43 private Object lock = new Object(); 44 45 46 public CsnFactory( int replicaId ) 47 { 48 changeCount = 0; 49 this.replicaId = replicaId; 50 } 51 52 53 /** 54 * Returns a new {@link Csn}. 55 * Generated CSN can be duplicate if user generates CSNs more than 2G 56 * times a milliseconds. 57 */ 58 public Csn newInstance() 59 { 60 int changeCount = 0; 61 62 synchronized ( lock ) 63 { 64 long newTimestamp = System.currentTimeMillis(); 65 66 // We will be able to generate 2 147 483 647 CSNs each 10 ms max 67 if ( lastTimestamp == newTimestamp ) 68 { 69 this.changeCount++; 70 } 71 else 72 { 73 lastTimestamp = newTimestamp; 74 this.changeCount = 0; 75 } 76 77 changeCount = this.changeCount; 78 } 79 80 return new Csn( lastTimestamp, changeCount, replicaId, 0 ); 81 } 82 83 84 /** 85 * Returns a new {@link Csn} created from the given values. 86 * 87 * This method is <b>not</b> to be used except for test purposes. 88 * 89 * @param timestamp The timestamp to use 90 * @param changeCount The change count to use 91 */ 92 public Csn newInstance( long timestamp, int changeCount ) 93 { 94 return new Csn( timestamp, changeCount, replicaId, 0 ); 95 } 96 97 98 /** 99 * Generates a CSN used to purge data. Its replicaID is not associated 100 * to a server. 101 * 102 * @param expirationDate The time up to the first CSN we want to keep 103 */ 104 public Csn newPurgeCsn( long expirationDate ) 105 { 106 return new Csn( expirationDate, Integer.MAX_VALUE, PURGE_INSTANCEID, Integer.MAX_VALUE ); 107 } 108 109 110 public void setReplicaId( int replicaId ) 111 { 112 this.replicaId = replicaId; 113 } 114 }