001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.ldap.model.csn; 021 022 023/** 024 * Generates a new {@link Csn}. 025 * 026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 027 */ 028public class CsnFactory 029{ 030 /** The last timestamp */ 031 private static volatile long lastTimestamp; 032 033 /** The integer used to disambiguate CSN generated at the same time */ 034 private int changeCount; 035 036 /** The replicaId to use for every CSN created by this factory */ 037 private int replicaId; 038 039 /** A special instance ID for a purge CSN */ 040 private static final int PURGE_INSTANCEID = 0x0FFF; 041 042 /** A lock used during the instance creation */ 043 private Object lock = new Object(); 044 045 046 /** 047 * Creates a new CsnFactory instance 048 * @param replicaId The replica ID 049 */ 050 public CsnFactory( int replicaId ) 051 { 052 changeCount = 0; 053 this.replicaId = replicaId; 054 } 055 056 057 /** 058 * Returns a new {@link Csn}. 059 * Generated CSN can be duplicate if user generates CSNs more than 2G 060 * times a milliseconds. 061 * 062 * @return The new generated CSN 063 */ 064 public Csn newInstance() 065 { 066 int tmpChangeCount; 067 068 synchronized ( lock ) 069 { 070 long newTimestamp = System.currentTimeMillis(); 071 072 // We will be able to generate 2 147 483 647 CSNs each 10 ms max 073 if ( lastTimestamp == newTimestamp ) 074 { 075 changeCount++; 076 } 077 else 078 { 079 lastTimestamp = newTimestamp; 080 changeCount = 0; 081 } 082 083 tmpChangeCount = changeCount; 084 } 085 086 return new Csn( lastTimestamp, tmpChangeCount, replicaId, 0 ); 087 } 088 089 090 /** 091 * Returns a new {@link Csn} created from the given values. 092 * 093 * This method is <b>not</b> to be used except for test purposes. 094 * 095 * @param timestamp The timestamp to use 096 * @param changeCount The change count to use 097 * @return The new generated CSN 098 */ 099 public Csn newInstance( long timestamp, int changeCount ) 100 { 101 return new Csn( timestamp, changeCount, replicaId, 0 ); 102 } 103 104 105 /** 106 * Generates a CSN used to purge data. Its replicaID is not associated 107 * to a server. 108 * 109 * @param expirationDate The time up to the first CSN we want to keep 110 * @return The new generated CSN 111 */ 112 public Csn newPurgeCsn( long expirationDate ) 113 { 114 return new Csn( expirationDate, Integer.MAX_VALUE, PURGE_INSTANCEID, Integer.MAX_VALUE ); 115 } 116 117 118 /** 119 * Sets the replica ID 120 * @param replicaId The replica ID 121 */ 122 public void setReplicaId( int replicaId ) 123 { 124 this.replicaId = replicaId; 125 } 126}