001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2012-2015 ForgeRock AS.
026 */
027package org.opends.server.replication.plugin;
028
029import java.util.Collection;
030import java.util.Collections;
031import java.util.List;
032
033import org.forgerock.opendj.ldap.Assertion;
034import org.forgerock.opendj.ldap.ByteSequence;
035import org.forgerock.opendj.ldap.ByteString;
036import org.forgerock.opendj.ldap.ByteStringBuilder;
037import org.forgerock.opendj.ldap.ConditionResult;
038import org.forgerock.opendj.ldap.DecodeException;
039import org.forgerock.opendj.ldap.schema.MatchingRuleImpl;
040import org.forgerock.opendj.ldap.schema.Schema;
041import org.forgerock.opendj.ldap.spi.IndexQueryFactory;
042import org.forgerock.opendj.ldap.spi.Indexer;
043import org.forgerock.opendj.ldap.spi.IndexingOptions;
044
045import static org.forgerock.opendj.ldap.Assertion.*;
046import static org.opends.messages.ReplicationMessages.*;
047import static org.opends.server.util.StaticUtils.*;
048
049/**
050 * Matching rule used to establish an order between historical information and index them.
051 */
052public final class HistoricalCsnOrderingMatchingRuleImpl implements MatchingRuleImpl
053{
054  private static final String ORDERING_ID = "changeSequenceNumberOrderingMatch";
055
056  private final Collection<? extends Indexer> indexers = Collections.singleton(new HistoricalIndexer());
057
058  /** Indexer for the matching rule. */
059  private final class HistoricalIndexer implements Indexer
060  {
061    @Override
062    public void createKeys(Schema schema, ByteSequence value, Collection<ByteString> keys) throws DecodeException
063    {
064      keys.add(normalizeAttributeValue(schema, value));
065    }
066
067    @Override
068    public String getIndexID()
069    {
070      return ORDERING_ID;
071    }
072  }
073
074  /** {@inheritDoc} */
075  @Override
076  public ByteString normalizeAttributeValue(Schema schema, ByteSequence value) throws DecodeException
077  {
078    /*
079     * Change the format of the value to index and start with the serverId. In
080     * that manner, the search response time is optimized for a particular
081     * serverId. The format of the key is now : serverId + timestamp + seqNum
082     */
083    try
084    {
085      int csnIndex = value.toString().indexOf(':') + 1;
086      String csn = value.subSequence(csnIndex, csnIndex + 28).toString();
087      ByteStringBuilder builder = new ByteStringBuilder(14);
088      builder.append(hexStringToByteArray(csn.substring(16, 20)));
089      builder.append(hexStringToByteArray(csn.substring(0, 16)));
090      builder.append(hexStringToByteArray(csn.substring(20, 28)));
091      return builder.toByteString();
092    }
093    catch (Exception e)
094    {
095      // This should never occur in practice since these attributes are managed
096      // internally.
097      throw DecodeException.error(WARN_INVALID_SYNC_HIST_VALUE.get(value), e);
098    }
099  }
100
101  /** {@inheritDoc} */
102  @Override
103  public Assertion getAssertion(final Schema schema, final ByteSequence value) throws DecodeException
104  {
105    final ByteString normAssertion = normalizeAttributeValue(schema, value);
106    return new Assertion()
107    {
108      @Override
109      public ConditionResult matches(final ByteSequence attributeValue)
110      {
111        return ConditionResult.valueOf(attributeValue.compareTo(normAssertion) < 0);
112      }
113
114      @Override
115      public <T> T createIndexQuery(IndexQueryFactory<T> factory) throws DecodeException
116      {
117        return factory.createRangeMatchQuery(ORDERING_ID, ByteString.empty(), normAssertion, false, false);
118      }
119    };
120  }
121
122  /** {@inheritDoc} */
123  @Override
124  public Assertion getSubstringAssertion(Schema schema, ByteSequence subInitial,
125      List<? extends ByteSequence> subAnyElements, ByteSequence subFinal) throws DecodeException
126  {
127    return UNDEFINED_ASSERTION;
128  }
129
130  /** {@inheritDoc} */
131  @Override
132  public Assertion getGreaterOrEqualAssertion(Schema schema, ByteSequence value) throws DecodeException
133  {
134    final ByteString normAssertion = normalizeAttributeValue(schema, value);
135    return new Assertion()
136    {
137      @Override
138      public ConditionResult matches(final ByteSequence normalizedAttributeValue)
139      {
140        return ConditionResult.valueOf(normalizedAttributeValue.compareTo(normAssertion) >= 0);
141      }
142
143      @Override
144      public <T> T createIndexQuery(IndexQueryFactory<T> factory) throws DecodeException
145      {
146        return factory.createRangeMatchQuery(ORDERING_ID, normAssertion, ByteString.empty(), true, false);
147      }
148    };
149  }
150
151  /** {@inheritDoc} */
152  @Override
153  public Assertion getLessOrEqualAssertion(Schema schema, ByteSequence value) throws DecodeException
154  {
155    final ByteString normAssertion = normalizeAttributeValue(schema, value);
156    return new Assertion()
157    {
158      @Override
159      public ConditionResult matches(final ByteSequence normalizedAttributeValue)
160      {
161        return ConditionResult.valueOf(normalizedAttributeValue.compareTo(normAssertion) <= 0);
162      }
163
164      @Override
165      public <T> T createIndexQuery(IndexQueryFactory<T> factory) throws DecodeException
166      {
167        return factory.createRangeMatchQuery(ORDERING_ID, ByteString.empty(), normAssertion, false, true);
168      }
169    };
170  }
171
172  /** {@inheritDoc} */
173  @Override
174  public Collection<? extends Indexer> createIndexers(IndexingOptions options)
175  {
176    return indexers;
177  }
178
179}