View Javadoc

1   /**
2    * Copyright (c) 2011, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  
28  package org.treetank.bucket;
29  
30  import static com.google.common.base.Objects.toStringHelper;
31  
32  import java.io.DataOutput;
33  import java.io.IOException;
34  import java.util.Arrays;
35  
36  import org.treetank.access.conf.StandardSettings;
37  import org.treetank.bucket.interfaces.IReferenceBucket;
38  import org.treetank.exception.TTIOException;
39  
40  import com.google.common.hash.HashCode;
41  import com.google.common.hash.Hasher;
42  
43  /**
44   * <h1>IndirectBucket</h1>
45   * 
46   * <p>
47   * Indirect bucket holds a set of references to build a reference tree.
48   * </p>
49   * 
50   * @author Sebastian Graf, University of Konstanz
51   * @author Marc Kramis, University of Konstanz
52   */
53  public final class IndirectBucket implements IReferenceBucket {
54  
55      /** Reference keys. */
56      private final long[] mReferenceKeys;
57  
58      private final byte[][] mReferenceHashs;
59  
60      /** Key of this bucket. */
61      private final long mBucketKey;
62  
63      /**
64       * Create indirect bucket.
65       * 
66       * @param pBucketKey
67       *            Key of the bucket
68       */
69      public IndirectBucket(final long pBucketKey) {
70          mBucketKey = pBucketKey;
71          mReferenceKeys = new long[IConstants.CONTENT_COUNT];
72          mReferenceHashs = new byte[IConstants.CONTENT_COUNT][];
73          Arrays.fill(mReferenceHashs, new byte[0]);
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public void serialize(final DataOutput pOutput) throws TTIOException {
81          try {
82              pOutput.writeInt(IConstants.INDIRCTBUCKET);
83              pOutput.writeLong(mBucketKey);
84              for (long key : mReferenceKeys) {
85                  pOutput.writeLong(key);
86              }
87              for (byte[] hash : mReferenceHashs) {
88                  pOutput.writeInt(hash.length);
89                  pOutput.write(hash);
90              }
91          } catch (final IOException exc) {
92              throw new TTIOException(exc);
93          }
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public long getBucketKey() {
101         return mBucketKey;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public long[] getReferenceKeys() {
109         return mReferenceKeys;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public void setReferenceKey(int pIndex, long pKey) {
117         mReferenceKeys[pIndex] = pKey;
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public String toString() {
125         return toStringHelper(this).add("mBucketKey", mBucketKey).add("mReferenceKeys",
126             Arrays.toString(mReferenceKeys)).add("mReferenceHashs", Arrays.toString(mReferenceHashs))
127             .toString();
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public byte[][] getReferenceHashs() {
135         return mReferenceHashs;
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public void setReferenceHash(int pIndex, byte[] pHash) {
143         mReferenceHashs[pIndex] = pHash;
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public int hashCode() {
151         final int prime = 81551;
152         int result = 1;
153         result = prime * result + (int)(mBucketKey ^ (mBucketKey >>> 32));
154         result = prime * result + Arrays.hashCode(mReferenceKeys);
155         for (byte[] hash : mReferenceHashs) {
156             result = prime * result + Arrays.hashCode(hash);
157         }
158         return result;
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public boolean equals(Object obj) {
166         return obj.hashCode() == this.hashCode();
167     }
168 
169     /**
170      * {@inheritDoc}
171      */
172     @Override
173     public HashCode  secureHash() {
174         final Hasher code = StandardSettings.HASHFUNC.newHasher().putLong(mBucketKey);
175         for (int i = 0; i < mReferenceKeys.length; i++) {
176             code.putLong(mReferenceKeys[i]);
177             code.putBytes(mReferenceHashs[i]);
178         }
179         return code.hash();
180     }
181 
182 }