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

Jean-Noel Rouvignac
03.12.2014 acb89043d88330d28394c2be0e689632fe1fe8ea
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * 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 legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * 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 legal-notices/CDDLv1_0.txt.
 * 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
 *
 *      Copyright 2014 ForgeRock AS
 */
package org.opends.server.backends.persistit;
 
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.opends.server.api.EntryCache;
import org.opends.server.backends.jeb.EntryID;
import org.opends.server.backends.pluggable.SuffixContainer;
import org.opends.server.core.DirectoryServer;
import org.opends.server.types.DN;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.Entry;
 
import com.persistit.Exchange;
import com.persistit.Persistit;
import com.persistit.Volume;
import com.persistit.exception.PersistitException;
import com.sleepycat.je.DatabaseException;
 
import static org.opends.messages.JebMessages.*;
 
/**
 * Persistit implementation of a {@link SuffixContainer}.
 */
class PersistitSuffixContainer implements SuffixContainer
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
  /** The baseDN handled by this suffix container. */
  private final DN baseDN;
  /**
   * Prevents name clashes for common indexes (like id2entry) across multiple
   * suffixes. For example when a root container contains multiple suffixes.
   */
  private final String indexPrefix;
  private final PersistitRootContainer rootContainer;
  private final Persistit db;
  private final Volume volume;
 
  private PersistitID2Entry id2entry;
  private PersistitDN2ID dn2id;
 
  /**
   * Constructor for this class.
   *
   * @param baseDN
   *          The baseDN handled by this suffix container
   * @param indexPrefix
   *          the prefix to use for indexes name
   * @param rootContainer
   *          the persisit root container
   * @param db
   *          the persisit database
   * @param volume
   *          the volume where indexes will be created
   */
  PersistitSuffixContainer(DN baseDN, String indexPrefix, PersistitRootContainer rootContainer,
      Persistit db, Volume volume)
  {
    this.baseDN = baseDN;
    this.indexPrefix = indexPrefix;
    this.rootContainer = rootContainer;
    this.db = db;
    this.volume = volume;
  }
 
  /**
   * Opens the entryContainer for reading and writing.
   *
   * @throws DirectoryException
   *           If an error occurs while opening the suffix container.
   */
  void open() throws DirectoryException
  {
    id2entry = new PersistitID2Entry(this);
    id2entry.open();
    dn2id = new PersistitDN2ID(this);
    dn2id.open();
  }
 
  /** {@inheritDoc} */
  @Override
  public void close()
  {
    dn2id.close();
    id2entry.close();
  }
 
  /** {@inheritDoc} */
  @Override
  public DN getBaseDN()
  {
    return baseDN;
  }
 
  /**
   * Returns the index name prefix.
   *
   * @return the index name prefix
   */
  public String getIndexPrefix()
  {
    return indexPrefix;
  }
 
  /** {@inheritDoc} */
  @Override
  public long getEntryCount()
  {
    // FIXME To be implemented
    // JNR: I have not found a suitable API to return this value
    return -1;
  }
 
  /**
   * Returns the id2entry index.
   *
   * @return the id2entry index
   */
  public PersistitID2Entry getID2Entry()
  {
    return id2entry;
  }
 
  /**
   * Creates a {@link Tree} for the fully qualified index name.
   *
   * @param fullyQualifiedIndexName
   *          The fully qualified index name
   * @throws PersistitException
   *           if a database exception happens
   */
  void createTree(String fullyQualifiedIndexName) throws PersistitException
  {
    volume.getTree(fullyQualifiedIndexName, true);
  }
 
  /**
   * Returns a new {@link Exchange} working on the index provided via its fully
   * qualified name.
   * <p>
   * Note: exchanges obtained with this method must be released by calling
   * {@link #releaseExchange(Exchange)}.
   *
   * @param fullyQualifiedIndexName
   *          The fully qualified index name
   * @return the exchange
   * @throws PersistitException
   *           if a database exception happens
   */
  final Exchange getExchange(String fullyQualifiedIndexName) throws PersistitException
  {
    return db.getExchange(volume, fullyQualifiedIndexName, false);
  }
 
  /**
   * Returns the fully qualified index name for this simple index name.
   * <p>
   * e.g.
   *
   * <pre>
   * PersistitSuffixContainer sc = ...; // initialize the suffix container
   * assertEquals(sc.getIndexPrefix(), "dccom");
   * assertEquals(sc.getFullyQualifiedIndexName("id2entry"), "dccom_id2entry");
   * </pre>
   *
   * @param simpleIndexName
   *          the simple index name to convert to a fully qualified index name
   * @return the fully qualified index name
   */
  public String getFullyQualifiedIndexName(String simpleIndexName)
  {
    return indexPrefix + "_" + simpleIndexName;
  }
 
  /**
   * Releases the provided exchange.
   *
   * @param exchange
   *          the exchange to release
   */
  final void releaseExchange(Exchange exchange)
  {
    if (exchange != null)
    {
      db.releaseExchange(exchange);
    }
  }
 
  /**
   * Indicates whether an entry with the specified DN exists.
   *
   * @param entryDN
   *          The DN of the entry for which to determine existence.
   * @return <CODE>true</CODE> if the specified entry exists, or
   *         <CODE>false</CODE> if it does not.
   * @throws DirectoryException
   *           If a problem occurs while trying to make the determination.
   */
  public boolean entryExists(DN entryDN) throws DirectoryException
  {
    // Try the entry cache first.
    EntryCache<?> entryCache = DirectoryServer.getEntryCache();
    if (entryCache != null && entryCache.containsEntry(entryDN))
    {
      return true;
    }
 
    try
    {
      // Read the ID from dn2id.
      EntryID id = dn2id.get(null, entryDN, null);
      return id != null;
    }
    catch (DatabaseException e)
    {
      logger.traceException(e);
      return false;
    }
  }
 
  /**
   * Retrieves and returns the entry associated to the provided DN.
   *
   * @param entryDN
   *          the DN of the entry to return
   * @return the entry associated to the provided DN
   * @throws DirectoryException
   *           if a directory exception happens
   * @throws PersistitException
   *           if a database exception happens
   */
  Entry getEntry(DN entryDN) throws DirectoryException, PersistitException
  {
    EntryCache<?> entryCache = DirectoryServer.getEntryCache();
 
    // Try the entry cache first.
    Entry entry = null;
    if (entryCache != null)
    {
      entry = entryCache.getEntry(entryDN);
    }
 
    if (entry == null)
    {
      // Read dn2id.
      EntryID entryID = dn2id.get(null, entryDN, null);
      if (entryID == null)
      {
        // The entryDN does not exist.
 
        // Check for referral entries above the target entry.
        // TODO JNR uncomment next line
        // dn2uri.targetEntryReferrals(entryDN, null);
 
        return null;
      }
 
      // Read id2entry.
      entry = id2entry.get(null, entryID, null);
      if (entry == null)
      {
        // The entryID does not exist.
        throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), ERR_JEB_MISSING_ID2ENTRY_RECORD
            .get(entryID));
      }
 
      // Put the entry in the cache making sure not to overwrite
      // a newer copy that may have been inserted since the time
      // we read the cache.
      if (entryCache != null)
      {
        entryCache.putEntryIfAbsent(entry, rootContainer.getBackend(), entryID.longValue());
      }
    }
 
    return entry;
  }
 
  /** {@inheritDoc} */
  @Override
  public String toString()
  {
    return getClass().getSimpleName() + " baseDN=" + baseDN;
  }
}