/* * 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 2006-2010 Sun Microsystems, Inc. * Portions Copyright 2014 ForgeRock AS */ package org.opends.server.backends.jeb; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Set; import org.forgerock.i18n.slf4j.LocalizedLogger; import org.forgerock.opendj.ldap.DecodeException; import org.opends.server.api.ApproximateMatchingRule; import org.opends.server.types.*; /** * An implementation of an Indexer for attribute approximate matching. */ public class ApproximateIndexer extends Indexer { private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); /** * The comparator for index keys generated by this class. */ private static final Comparator comparator = new AttributeIndex.KeyComparator(); /** * The attribute type approximate matching rule. */ private ApproximateMatchingRule approximateRule; /** * The attribute type for which this instance will * generate index keys. */ private AttributeType attributeType; /** * Create a new attribute approximate indexer for the given index * configuration. * @param attributeType The attribute type for which an indexer is * required. */ public ApproximateIndexer(AttributeType attributeType) { this.attributeType = attributeType; this.approximateRule = attributeType.getApproximateMatchingRule(); } /** * Get a string representation of this object. The returned value is * used to name an index created using this object. * @return A string representation of this object. */ @Override public String toString() { return attributeType.getNameOrOID() + ".approximate"; } /** * Get the comparator that must be used to compare index keys * generated by this class. * * @return A byte array comparator. */ @Override public Comparator getComparator() { return comparator; } /** * Generate the set of index keys for an entry. * * @param entry The entry. * @param keys The set into which the generated keys will be inserted. */ @Override public void indexEntry(Entry entry, Set keys) { List attrList = entry.getAttribute(attributeType); if (attrList != null) { indexAttribute(attrList, keys); } } /** * Generate the set of index keys to be added and the set of index keys * to be deleted for an entry that has been replaced. * * @param oldEntry The original entry contents. * @param newEntry The new entry contents. * @param modifiedKeys The map into which the modified keys will be inserted. */ @Override public void replaceEntry(Entry oldEntry, Entry newEntry, Map modifiedKeys) { List newAttributes = newEntry.getAttribute(attributeType, true); List oldAttributes = oldEntry.getAttribute(attributeType, true); indexAttribute(oldAttributes, modifiedKeys, false); indexAttribute(newAttributes, modifiedKeys, true); } /** * Generate the set of index keys to be added and the set of index keys * to be deleted for an entry that was modified. * * @param oldEntry The original entry contents. * @param newEntry The new entry contents. * @param mods The set of modifications that were applied to the entry. * @param modifiedKeys The map into which the modified keys will be inserted. */ @Override public void modifyEntry(Entry oldEntry, Entry newEntry, List mods, Map modifiedKeys) { List newAttributes = newEntry.getAttribute(attributeType, true); List oldAttributes = oldEntry.getAttribute(attributeType, true); indexAttribute(oldAttributes, modifiedKeys, false); indexAttribute(newAttributes, modifiedKeys, true); } /** * Generate the set of index keys for an attribute. * @param attrList The attribute to be indexed. * @param keys The set into which the keys will be inserted. */ private void indexAttribute(List attrList, Set keys) { if (attrList == null) return; for (Attribute attr : attrList) { if (attr.isVirtual()) { continue; } for (AttributeValue value : attr) { try { byte[] keyBytes = approximateRule.normalizeAttributeValue(value.getValue()).toByteArray(); keys.add(keyBytes); } catch (DecodeException e) { logger.traceException(e); } } } } /** * Generate the set of index keys for an attribute. * @param attrList The attribute to be indexed. * @param modifiedKeys The map into which the modified * keys will be inserted. * @param insert true if generated keys should * be inserted or false otherwise. */ private void indexAttribute(List attrList, Map modifiedKeys, Boolean insert) { if (attrList == null) return; for (Attribute attr : attrList) { if (attr.isVirtual()) { continue; } for (AttributeValue value : attr) { try { byte[] keyBytes = approximateRule .normalizeAttributeValue(value.getValue()).toByteArray(); Boolean cInsert = modifiedKeys.get(keyBytes); if(cInsert == null) { modifiedKeys.put(keyBytes, insert); } else if(!cInsert.equals(insert)) { modifiedKeys.remove(keyBytes); } } catch (DecodeException e) { logger.traceException(e); } } } } }