mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

boli
03.09.2006 83dd61651cb5d73c1a15dfcb7d217c0f272722d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006 Sun Microsystems, Inc.
 */
package org.opends.server.backends.jeb;
 
import com.sleepycat.je.Transaction;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.LockMode;
import org.opends.server.protocols.asn1.ASN1OctetString;
 
import java.util.HashMap;
import java.util.Map;
import java.util.LinkedHashMap;
 
/**
 * A buffered index is used to buffer multiple reads or writes to the
 * same index key into a single read or write.
 * It can only be used to buffer multiple reads and writes under
 * the same transaction. The transaction may be null if it is known
 * that there are no other concurrent updates to the index.
 */
public class BufferedIndex
{
  /**
   * The index that is being buffered.
   */
  private Index index;
 
  /**
   * The database transaction used for all reads and writes, or null
   * if there is no transaction.
   */
  private Transaction txn;
 
  /**
   * The buffered records stored as a map from the record key to the
   * buffered value for that key.
   */
  private HashMap<ASN1OctetString, BufferedValue> values;
 
  /**
   * Inner class representing a buffered value.
   */
  private class BufferedValue
  {
    /**
     * The current value.
     */
    EntryIDSet value;
 
    /**
     * A flag to indicate whether the buffered value is different to the
     * value stored in the database, and therefore needs to be written
     * to the database.
     */
    boolean isDirty;
  }
 
  /**
   * Construct a buffered index.
   *
   * @param index The index to be buffered
   * @param txn The transaction to be used for all reads and writes
   *            through this buffered index, or null for no transaction.
   */
  public BufferedIndex(Index index, Transaction txn)
  {
    this.index = index;
    this.txn = txn;
    values = new LinkedHashMap<ASN1OctetString, BufferedValue>();
  }
 
  /**
   * Get the list of IDs for a key.
   * The value may be returned from a buffered value in memory.
   *
   * @param key The desired key
   * @return The list of IDs.
   */
  public EntryIDSet get(byte[] key)
  {
    return getCachedValue(key).value;
  }
 
  /**
   * Get the buffered value for a key, creating it if necessary.
   *
   * @param key The desired key.
   * @return The buffered value.
   */
  private BufferedValue getCachedValue(byte[] key)
  {
    BufferedValue bufferedValue = values.get(new ASN1OctetString(key));
    if (bufferedValue == null)
    {
      bufferedValue = new BufferedValue();
      bufferedValue.value = index.readKey(new DatabaseEntry(key), txn,
                                          LockMode.RMW);
      bufferedValue.isDirty = false;
      values.put(new ASN1OctetString(key), bufferedValue);
    }
    return bufferedValue;
  }
 
  /**
   * Add an ID to the given key.
   * The update may be buffered in memory.
   *
   * @param entryLimit The index entry limit
   * @param key The key to which the ID is to be inserted.
   * @param entryID The ID to be inserted.
   */
  public void insertID(int entryLimit, byte[] key, EntryID entryID)
  {
    BufferedValue bufferedValue = getCachedValue(key);
    EntryIDSet entryIDList = bufferedValue.value;
    if (entryIDList.isDefined())
    {
      if (entryLimit > 0 && entryIDList.size() >= entryLimit)
      {
        bufferedValue.value = new EntryIDSet();
        bufferedValue.isDirty = true;
      }
      else
      {
        bufferedValue.isDirty = entryIDList.add(entryID);
      }
    }
  }
 
  /**
   * Remove an ID from the given key.
   * The update may be buffered in memory.
   *
   * @param key The key from which the ID is to be removed.
   * @param entryID The ID to be removed.
   */
  public void removeID(byte[] key, EntryID entryID)
  {
    BufferedValue bufferedValue = getCachedValue(key);
    bufferedValue.isDirty = bufferedValue.value.remove(entryID);
  }
 
  /**
   * Remove a key from the index.
   * The update may be buffered in memory.
   *
   * @param key The key to be removed.
   */
  public void remove(byte[] key)
  {
    BufferedValue bufferedValue = getCachedValue(key);
    bufferedValue.value = new EntryIDSet(key, null);
    bufferedValue.isDirty = true;
  }
 
  /**
   * Flush all the dirty data out to the index.
   *
   * @throws DatabaseException If an error occurs while writing out the
   * dirty data to the index.
   */
  public void flush() throws DatabaseException
  {
    for (Map.Entry<ASN1OctetString,BufferedValue> hashEntry : values.entrySet())
    {
      BufferedValue bufferedValue = hashEntry.getValue();
      if (bufferedValue.isDirty)
      {
        index.writeKey(txn, new DatabaseEntry(hashEntry.getKey().value()),
                       bufferedValue.value);
        bufferedValue.isDirty = false;
      }
    }
  }
}