001package com.nimbusds.infinispan.persistence.ldap;
002
003
004import java.util.Date;
005
006import com.nimbusds.infinispan.persistence.common.InfinispanEntry;
007import net.jcip.annotations.Immutable;
008
009
010/**
011 * LDAP write strategy resolver based on the created timestamp found in the
012 * metadata of Infinispan entries. Recently created Infinispan entries are
013 * given {@link LDAPWriteStrategy#TRY_LDAP_ADD_FIRST}, else
014 * {@link LDAPWriteStrategy#TRY_LDAP_MODIFY_FIRST}. Note that entries without
015 * expiration are not assigned a created timestamp (-1) in which case the
016 * resolver returns {@link LDAPWriteStrategy#TRY_LDAP_ADD_FIRST} (the default
017 * strategy).
018 */
019@Immutable
020public class CreatedTimestampLDAPWriteStrategyResolver<K,V> implements LDAPWriteStrategyResolver<K,V> {
021
022
023        /**
024         * The default age (50 milliseconds) of entries considered recent.
025         */
026        public static final long DEFAULT_RECENT_AGE_MS = 50;
027
028
029        /**
030         * The age (in milliseconds) of entries considered recent.
031         */
032        private final long recentAgeMs;
033
034
035        /**
036         * Creates a new LDAP write strategy resolver based on the created
037         * timestamp of Infinispan entries where entries aged younger than
038         * 50 milliseconds are given
039         * {@link LDAPWriteStrategy#TRY_LDAP_ADD_FIRST}.
040         */
041        public CreatedTimestampLDAPWriteStrategyResolver() {
042                this(DEFAULT_RECENT_AGE_MS);
043        }
044
045
046        /**
047         * Creates a new LDAP write strategy resolver based on the created
048         * timestamp of Infinispan entries where entries aged younger than
049         * the specified age are given
050         * {@link LDAPWriteStrategy#TRY_LDAP_ADD_FIRST}.
051         *
052         * @param recentAgeMs The entry age (in milliseconds) that is
053         *                    considered recent. Must be non-negative.
054         */
055        public CreatedTimestampLDAPWriteStrategyResolver(long recentAgeMs) {
056                assert recentAgeMs >= 0;
057                this.recentAgeMs = recentAgeMs;
058        }
059        
060
061        @Override
062        public LDAPWriteStrategy resolveLDAPWriteStrategy(final InfinispanEntry<K,V> infinispanEntry) {
063
064                if (infinispanEntry.getMetadata() == null) {
065                        return LDAPWriteStrategy.getDefault();
066                }
067
068                if (infinispanEntry.getMetadata().created() < 0L) {
069                        return LDAPWriteStrategy.getDefault();
070                }
071
072                final Date now = new Date();
073
074                if (infinispanEntry.getMetadata().created() + recentAgeMs > now.getTime()) {
075                        // New entry
076                        return LDAPWriteStrategy.TRY_LDAP_ADD_FIRST;
077                } else {
078                        // Entry which has been around for some time
079                        return LDAPWriteStrategy.TRY_LDAP_MODIFY_FIRST;
080                }
081        }
082}