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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.replication.plugin;
028    
029    import org.opends.server.admin.std.server.OrderingMatchingRuleCfg;
030    import org.opends.server.api.OrderingMatchingRule;
031    import org.opends.server.protocols.asn1.ASN1OctetString;
032    import org.opends.server.types.ByteString;
033    
034    /**
035     * Used to establish an order between historical information and index them.
036     */
037    public class HistoricalCsnOrderingMatchingRule
038           extends OrderingMatchingRule
039    {
040      /**
041       * The serial version identifier required to satisfy the compiler because this
042       * class implements the <CODE>java.io.Serializable</CODE> interface.  This
043       * value was generated using the <CODE>serialver</CODE> command-line utility
044       * included with the Java SDK.
045       */
046      private static final long serialVersionUID = -3424403930225609943L;
047    
048    
049    
050      /**
051       * Construct a new  HistoricalCsnOrderingMatchingRule object.
052       *
053       */
054      public HistoricalCsnOrderingMatchingRule()
055      {
056        super();
057      }
058    
059      /**
060       * Compare two ByteString values containing historical information.
061       * @param value1 first value to compare
062       * @param value2 second value to compare
063       * @return 0 when equals, -1 or 1 to establish order
064       */
065      @Override
066      public int compareValues(ByteString value1, ByteString value2)
067      {
068        String[] token1 = value1.stringValue().split(":", 3);
069        String[] token2 = value2.stringValue().split(":", 3);
070    
071        if ((token1[1] == null) || (token2[1] == null))
072          return -1;
073    
074        return token1[1].compareTo(token2[1]);
075      }
076    
077      /**
078       * {@inheritDoc}
079       */
080      @Override
081      public void initializeMatchingRule(OrderingMatchingRuleCfg configuration)
082      {
083        // No implementation needed here.
084      }
085    
086      /**
087       * Get the name of this class.
088       * @return name of the class in String form
089       */
090      @Override
091      public String getName()
092      {
093        return "historicalCsnOrderingMatch";
094      }
095    
096      /**
097       * Get the OID of the class.
098       * @return the OID of the class in String form.
099       */
100      @Override
101      public String getOID()
102      {
103        return "1.3.6.1.4.1.26027.1.4.4";
104      }
105    
106      /**
107       * Get the description of this Class.
108       * @return the Class description in String form, currently not used.
109       */
110      @Override
111      public String getDescription()
112      {
113        return null;
114      }
115    
116      /**
117       * Get the Syntax OID for this class.
118       * @return the syntax OID in String form
119       */
120      @Override
121      public String getSyntaxOID()
122      {
123        return "1.3.6.1.4.1.26027.1.3.5";
124      }
125    
126      /**
127       * Normalize historical information representation.
128       * @param value the value that must be normalized
129       * @return The String form that must be used for historical information
130       * comparison
131       */
132      @Override
133      public ByteString normalizeValue(ByteString value)
134      {
135        String[] token = value.stringValue().split(":", 3);
136        return new ASN1OctetString(token[1]);
137      }
138    
139      /**
140       * Compares two normalized representation of historical information.
141       * @param b1 first value to compare
142       * @param b2 second value to compare
143       * @return 0, -1 or 1 depending on relative positions
144       */
145      public int compare(byte[] b1, byte[] b2)
146      {
147        int minLength = Math.min(b1.length, b2.length);
148    
149        for (int i=0; i < minLength; i++)
150        {
151          if (b1[i] == b2[i])
152          {
153            continue;
154          }
155          else if (b1[i] < b2[i])
156          {
157            return -1;
158          }
159          else if (b1[i] > b2[i])
160          {
161            return 1;
162          }
163        }
164    
165        if (b1.length == b2.length)
166        {
167          return 0;
168        }
169        else if (b1.length < b2.length)
170        {
171          return -1;
172        }
173        else
174        {
175          return 1;
176        }
177      }
178    
179    }