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.backends.jeb;
028
029 import java.util.ArrayList;
030 import java.util.BitSet;
031 import java.util.List;
032
033 /**
034 * This is a class used by the index merge thread. It merges the data
035 * for one index key from multiple intermediate files.
036 */
037 public class MergeValue
038 {
039 /**
040 * The value of the index key.
041 */
042 byte[] key;
043
044 /**
045 * The entry IDs to be added, where each set comes from a different file.
046 */
047 List<Longs> addData;
048
049 /**
050 * The entry IDs to be deleted, where each set comes from a different file.
051 */
052 ArrayList<Longs> delData;
053
054 /**
055 * A bit set indicating which files have contributed data for this key.
056 * Each file reader is identified by an array index. If bit n is set,
057 * it means that the reader with index n contributed data.
058 */
059 BitSet readers;
060
061 /**
062 * The index entry limit.
063 */
064 int entryLimit;
065
066 /**
067 * Create a new merge value.
068 * @param numReaders The total number of file readers that could be
069 * contributing to this value. Reader identifiers are in the range
070 * 0 .. numReaders-1.
071 * @param entryLimit The configured index entry limit.
072 */
073 public MergeValue(int numReaders, int entryLimit)
074 {
075 this.key = null;
076 addData = new ArrayList<Longs>(numReaders);
077 delData = new ArrayList<Longs>(numReaders);
078 readers = new BitSet(numReaders);
079 this.entryLimit = entryLimit;
080 }
081
082
083 /**
084 * Get the value of the key.
085 * @return The key value.
086 */
087 public byte[] getKey()
088 {
089 return key;
090 }
091
092
093 /**
094 * Set the value of the key.
095 * @param key The key value .
096 */
097 public void setKey(byte[] key)
098 {
099 this.key = key;
100 }
101
102
103
104 /**
105 * Provide data for the key from one of the file readers.
106 * @param reader The reader providing the data.
107 * @param addData A set of entry IDs to be added.
108 * @param delData A set of entry IDs to be deleted.
109 */
110 public void mergeData(int reader, Longs addData, Longs delData)
111 {
112 this.addData.add(addData);
113 if (delData.size() > 0)
114 {
115 this.delData.add(delData);
116 }
117 readers.set(reader);
118 }
119
120
121 /**
122 * Get the readers that provided data to be merged.
123 * @return An array of identifiers of readers that provided data.
124 */
125 public int[] getReaders()
126 {
127 int[] ret = new int[readers.cardinality()];
128
129 for (int i = readers.nextSetBit(0), j = 0; i != -1;
130 i = readers.nextSetBit(i+1))
131 {
132 ret[j++] = i;
133 }
134 return ret;
135 }
136
137
138 /**
139 * Get the list of arrays of IDs to be added.
140 * @return The list of arrays of IDs to be added.
141 */
142 public List<Longs> getAddValues()
143 {
144 return addData;
145 }
146
147
148 /**
149 * Get the list of arrays of IDs to be deleted.
150 * @return The list of arrays of IDs to be deleted.
151 */
152 public List<Longs> getDelValues()
153 {
154 return delData;
155 }
156
157 }