001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2015-2016 ForgeRock AS.
016 */
017package org.opends.server.replication.plugin;
018
019import java.util.zip.Checksum;
020
021/**
022 * This class computes the generation id used for a replication domain.
023 * It is a checksum based on some special entries/attributes of the domain.
024 * The written stream to this class is the LDIF representation of the entries
025 * we are interested in for computing the generation id. The current
026 * implementation simply does the sum of each written byte and stores the value
027 * in a long. We do not care about the cycling long as the probability of 2
028 * data sets having the same checksum is very low.
029 */
030public class GenerationIdChecksum implements Checksum
031{
032  /** Checksum to be returned. */
033  private long checksum;
034
035  /** This is the generation id for an empty backend. */
036  public static final long EMPTY_BACKEND_GENERATION_ID = 48L;
037
038  /** Update the checksum with one added byte. */
039  private void updateWithOneByte(byte b)
040  {
041    /*
042     * The "end of line" code is CRLF under windows but LF on UNIX. So to get
043     * the same checksum value on every platforms, we always exclude the CR and
044     * LF characters from the computation.
045     */
046    if (b != 0x0D && b != 0x0A) // CR=0D and LF=0A
047    {
048      checksum += b;
049    }
050  }
051
052  @Override
053  public void update(int b)
054  {
055    updateWithOneByte((byte) b);
056  }
057
058  @Override
059  public void update(byte[] b, int off, int len)
060  {
061    for (int i = off; i < off + len; i++)
062    {
063      updateWithOneByte(b[i]);
064    }
065  }
066
067  @Override
068  public long getValue()
069  {
070    if (checksum != 0L)
071    {
072      return checksum;
073    } else
074    {
075      /*
076       * Computing an empty backend writes the number of entries (0) only,
077       * will not be added to the checksum as no entries will follow. To treat
078       * this special case, and to keep consistency with old versions, in that
079       * case we hardcode and return the generation id value for an empty
080       * backend.
081       */
082      return EMPTY_BACKEND_GENERATION_ID;
083    }
084  }
085
086  @Override
087  public void reset()
088  {
089    checksum = 0L;
090  }
091}