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 org.opends.server.loggers.debug.DebugTracer;
030 import static org.opends.server.loggers.debug.DebugLogger.getTracer;
031 import org.opends.server.util.StaticUtils;
032 import com.sleepycat.je.*;
033
034 import java.util.Arrays;
035
036 /**
037 * This class is responsible for storing the configuration state of
038 * the JE backend for a particular suffix.
039 */
040 public class State extends DatabaseContainer
041 {
042 /**
043 * The tracer object for the debug logger.
044 */
045 private static final DebugTracer TRACER = getTracer();
046
047 private static final byte[] falseBytes = new byte[]{0x00};
048 private static final byte[] trueBytes = new byte[]{0x01};
049
050 /**
051 * Create a new State object.
052 *
053 * @param name The name of the entry database.
054 * @param env The JE Environment.
055 * @param entryContainer The entryContainer of the entry database.
056 * @throws com.sleepycat.je.DatabaseException If an error occurs in the
057 * JE database.
058 *
059 */
060 State(String name, Environment env, EntryContainer entryContainer)
061 throws DatabaseException
062 {
063 super(name, env, entryContainer);
064
065 DatabaseConfig dbNodupsConfig = new DatabaseConfig();
066
067 if(env.getConfig().getReadOnly())
068 {
069 dbNodupsConfig.setReadOnly(true);
070 dbNodupsConfig.setAllowCreate(false);
071 dbNodupsConfig.setTransactional(false);
072 }
073 else if(!env.getConfig().getTransactional())
074 {
075 dbNodupsConfig.setAllowCreate(true);
076 dbNodupsConfig.setTransactional(false);
077 dbNodupsConfig.setDeferredWrite(true);
078 }
079 else
080 {
081 dbNodupsConfig.setAllowCreate(true);
082 dbNodupsConfig.setTransactional(true);
083 }
084
085 this.dbConfig = dbNodupsConfig;
086 }
087
088 /**
089 * Remove a record from the entry database.
090 *
091 * @param txn The database transaction or null if none.
092 * @param index The index storing the trusted state info.
093 * @return true if the entry was removed, false if it was not.
094 * @throws DatabaseException If an error occurs in the JE database.
095 */
096 public boolean removeIndexTrustState(Transaction txn, Index index)
097 throws DatabaseException
098 {
099 DatabaseEntry key =
100 new DatabaseEntry(StaticUtils.getBytes(index.getName()));
101
102 OperationStatus status = delete(txn, key);
103 if (status != OperationStatus.SUCCESS)
104 {
105 return false;
106 }
107 return true;
108 }
109
110 /**
111 * Fetch index state from the database.
112 * @param txn The database transaction or null if none.
113 * @param index The index storing the trusted state info.
114 * @return The trusted state of the index in the database.
115 * @throws DatabaseException If an error occurs in the JE database.
116 */
117 public boolean getIndexTrustState(Transaction txn, Index index)
118 throws DatabaseException
119 {
120 String sortName =
121 index.getName().replace(entryContainer.getDatabasePrefix(), "");
122 DatabaseEntry key =
123 new DatabaseEntry(StaticUtils.getBytes(sortName));
124 DatabaseEntry data = new DatabaseEntry();
125
126 OperationStatus status;
127 status = read(txn, key, data, LockMode.DEFAULT);
128
129 if (status != OperationStatus.SUCCESS)
130 {
131 return false;
132 }
133
134 byte[] bytes = data.getData();
135 return Arrays.equals(bytes, trueBytes);
136 }
137
138 /**
139 * Fetch index state from the database.
140 * @param txn The database transaction or null if none.
141 * @param vlvIndex The index storing the trusted state info.
142 * @return The trusted state of the index in the database.
143 * @throws DatabaseException If an error occurs in the JE database.
144 */
145 public boolean getIndexTrustState(Transaction txn, VLVIndex vlvIndex)
146 throws DatabaseException
147 {
148 String shortName =
149 vlvIndex.getName().replace(entryContainer.getDatabasePrefix(), "");
150 DatabaseEntry key =
151 new DatabaseEntry(StaticUtils.getBytes(shortName));
152 DatabaseEntry data = new DatabaseEntry();
153
154 OperationStatus status;
155 status = read(txn, key, data, LockMode.DEFAULT);
156
157 if (status != OperationStatus.SUCCESS)
158 {
159 return false;
160 }
161
162 byte[] bytes = data.getData();
163 return Arrays.equals(bytes, trueBytes);
164 }
165
166 /**
167 * Put index state to database.
168 * @param txn The database transaction or null if none.
169 * @param index The index storing the trusted state info.
170 * @param trusted The state value to put into the database.
171 * @return true if the entry was written, false if it was not.
172 * @throws DatabaseException If an error occurs in the JE database.
173 */
174 public boolean putIndexTrustState(Transaction txn, Index index,
175 boolean trusted)
176 throws DatabaseException
177 {
178 String shortName =
179 index.getName().replace(entryContainer.getDatabasePrefix(), "");
180 DatabaseEntry key =
181 new DatabaseEntry(StaticUtils.getBytes(shortName));
182 DatabaseEntry data = new DatabaseEntry();
183
184 if(trusted)
185 data.setData(trueBytes);
186 else
187 data.setData(falseBytes);
188
189 OperationStatus status;
190 status = put(txn, key, data);
191 if (status != OperationStatus.SUCCESS)
192 {
193 return false;
194 }
195 return true;
196 }
197
198 /**
199 * Put VLV index state to database.
200 * @param txn The database transaction or null if none.
201 * @param vlvIndex The VLV index storing the trusted state info.
202 * @param trusted The state value to put into the database.
203 * @return true if the entry was written, false if it was not.
204 * @throws DatabaseException If an error occurs in the JE database.
205 */
206 public boolean putIndexTrustState(Transaction txn, VLVIndex vlvIndex,
207 boolean trusted)
208 throws DatabaseException
209 {
210 String shortName =
211 vlvIndex.getName().replace(entryContainer.getDatabasePrefix(), "");
212 DatabaseEntry key =
213 new DatabaseEntry(StaticUtils.getBytes(shortName));
214 DatabaseEntry data = new DatabaseEntry();
215
216 if(trusted)
217 data.setData(trueBytes);
218 else
219 data.setData(falseBytes);
220
221 OperationStatus status;
222 status = put(txn, key, data);
223 if (status != OperationStatus.SUCCESS)
224 {
225 return false;
226 }
227 return true;
228 }
229 }