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 * http://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 public CsnFactory( int replicaId ) 047 { 048 changeCount = 0; 049 this.replicaId = replicaId; 050 } 051 052 053 /** 054 * Returns a new {@link Csn}. 055 * Generated CSN can be duplicate if user generates CSNs more than 2G 056 * times a milliseconds. 057 */ 058 public Csn newInstance() 059 { 060 int changeCount = 0; 061 062 synchronized ( lock ) 063 { 064 long newTimestamp = System.currentTimeMillis(); 065 066 // We will be able to generate 2 147 483 647 CSNs each 10 ms max 067 if ( lastTimestamp == newTimestamp ) 068 { 069 this.changeCount++; 070 } 071 else 072 { 073 lastTimestamp = newTimestamp; 074 this.changeCount = 0; 075 } 076 077 changeCount = this.changeCount; 078 } 079 080 return new Csn( lastTimestamp, changeCount, replicaId, 0 ); 081 } 082 083 084 /** 085 * Returns a new {@link Csn} created from the given values. 086 * 087 * This method is <b>not</b> to be used except for test purposes. 088 * 089 * @param timestamp The timestamp to use 090 * @param changeCount The change count to use 091 */ 092 public Csn newInstance( long timestamp, int changeCount ) 093 { 094 return new Csn( timestamp, changeCount, replicaId, 0 ); 095 } 096 097 098 /** 099 * 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}