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 2008-2010 Sun Microsystems, Inc. 025 * Portions Copyright 2015 ForgeRock AS 026 */ 027package org.opends.server.replication.plugin; 028 029import java.util.zip.Checksum; 030 031/** 032 * This class computes the generation id used for a replication domain. 033 * It is a checksum based on some special entries/attributes of the domain. 034 * The written stream to this class is the LDIF representation of the entries 035 * we are interested in for computing the generation id. The current 036 * implementation simply does the sum of each written byte and stores the value 037 * in a long. We do not care about the cycling long as the probability of 2 038 * data sets having the same checksum is very low. 039 */ 040public class GenerationIdChecksum implements Checksum 041{ 042 /** Checksum to be returned. */ 043 private long checksum; 044 045 /** This is the generation id for an empty backend. */ 046 public static final long EMPTY_BACKEND_GENERATION_ID = 48L; 047 048 /** Update the checksum with one added byte. */ 049 private void updateWithOneByte(byte b) 050 { 051 /* 052 * The "end of line" code is CRLF under windows but LF on UNIX. So to get 053 * the same checksum value on every platforms, we always exclude the CR and 054 * LF characters from the computation. 055 */ 056 if (b != 0x0D && b != 0x0A) // CR=0D and LF=0A 057 { 058 checksum += b; 059 } 060 } 061 062 /** {@inheritDoc} */ 063 public void update(int b) 064 { 065 updateWithOneByte((byte) b); 066 } 067 068 /** {@inheritDoc} */ 069 public void update(byte[] b, int off, int len) 070 { 071 for (int i = off; i < off + len; i++) 072 { 073 updateWithOneByte(b[i]); 074 } 075 } 076 077 /** {@inheritDoc} */ 078 public long getValue() 079 { 080 if (checksum != 0L) 081 { 082 return checksum; 083 } else 084 { 085 /* 086 * Computing an empty backend writes the number of entries (0) only, 087 * will not be added to the checksum as no entries will follow. To treat 088 * this special case, and to keep consistency with old versions, in that 089 * case we hardcode and return the generation id value for an empty 090 * backend. 091 */ 092 return EMPTY_BACKEND_GENERATION_ID; 093 } 094 } 095 096 /** {@inheritDoc} */ 097 public void reset() 098 { 099 checksum = 0L; 100 } 101}