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     */
020    package org.apache.directory.shared.ldap.csn;
021    
022    import java.util.concurrent.atomic.AtomicInteger;
023    
024    
025    /**
026     * Generates a new {@link Csn}.
027     * 
028     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
029     */
030    public class CsnFactory
031    {
032        /** The last timestamp */
033        private static volatile long lastTimestamp;
034        
035        /** The integer used to disambiguate CSN generated at the same time */
036        private AtomicInteger changeCount;
037        
038        /** The replicaId to use for every CSN created by this factory */
039        private int replicaId;
040    
041        /** A special instance ID for a purge CSN */
042        private static final int PURGE_INSTANCEID = 0x0FFF;
043    
044    
045        public CsnFactory( int replicaId )
046        {
047            changeCount = new AtomicInteger( 0 );
048            this.replicaId = replicaId;
049        }
050    
051    
052        /**
053         * Returns a new {@link Csn}.
054         * Generated CSN can be duplicate if user generates CSNs more than 2G 
055         * times a milliseconds.
056         */
057        public Csn newInstance()
058        {
059            synchronized ( changeCount )
060            {
061                long newTimestamp = System.currentTimeMillis();
062                
063                // We will be able to generate 2 147 483 647 CSNs each 10 ms max
064                if ( lastTimestamp == newTimestamp )
065                {
066                    changeCount.incrementAndGet();
067                }
068                else
069                {
070                    lastTimestamp = newTimestamp;
071                    changeCount.set( 0 );
072                }
073            }
074    
075            return new Csn( lastTimestamp, changeCount.get(), replicaId, 0 );
076        }
077    
078    
079        /**
080         * Returns a new {@link Csn} created from the given values.
081         * 
082         * This method is <b>not</b> to be used except for test purposes.
083         * 
084         * @param timestamp The timestamp to use
085         * @param replicaId Replica ID.  ReplicaID must be 1-3 digit value
086         * @param changeCount The change count to use
087         */
088        public Csn newInstance( long timestamp, int changeCount )
089        {
090            return new Csn( timestamp, changeCount, replicaId, 0 );
091        }
092        
093        
094        /**
095         * Generates a CSN used to purge data. Its replicaID is not associated
096         * to a server. 
097         * 
098         * @param expirationDate The time up to the first CSN we want to keep 
099         */
100        public Csn newPurgeCsn( long expirationDate )
101        {
102            return new Csn( expirationDate, Integer.MAX_VALUE, PURGE_INSTANCEID, Integer.MAX_VALUE );
103        }
104    }