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 legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2008-2010 Sun Microsystems, Inc. 025 * Portions Copyright 2015 ForgeRock AS 026 */ 027 028package org.opends.guitools.controlpanel.datamodel; 029 030import java.util.Collections; 031import java.util.List; 032 033import org.forgerock.opendj.ldap.SearchScope; 034import org.opends.server.admin.std.meta.BackendVLVIndexCfgDefn; 035import org.opends.server.types.DN; 036 037/** 038 * The class used to describe the VLV index configuration. 039 */ 040public class VLVIndexDescriptor extends AbstractIndexDescriptor 041{ 042 private final DN baseDN; 043 private final SearchScope scope; 044 private final String filter; 045 private List<VLVSortOrder> sortOrder = Collections.emptyList(); 046 private int hashCode; 047 048 /** 049 * Constructor for the VLVIndexDescriptor. 050 * 051 * @param name 052 * the name of the index. 053 * @param backend 054 * the backend where the index is defined. 055 * @param baseDN 056 * the baseDN of the search indexed by the VLV index. 057 * @param scope 058 * the scope of the search indexed by the VLV index. 059 * @param filter 060 * the filter or the search indexed by the VLV index. 061 * @param sortOrder 062 * the sort order list of the VLV index. 063 */ 064 public VLVIndexDescriptor(String name, BackendDescriptor backend, DN baseDN, SearchScope scope, String filter, 065 List<VLVSortOrder> sortOrder) 066 { 067 super(name, backend); 068 this.baseDN = baseDN; 069 this.scope = scope; 070 this.filter = filter; 071 this.sortOrder = Collections.unmodifiableList(sortOrder); 072 073 recalculateHashCode(); 074 } 075 076 @Override 077 public int compareTo(AbstractIndexDescriptor o) 078 { 079 return getName().toLowerCase().compareTo(o.getName().toLowerCase()); 080 } 081 082 @Override 083 public int hashCode() 084 { 085 return hashCode; 086 } 087 088 /** 089 * Returns the baseDN of the search indexed by the VLV index. 090 * 091 * @return the baseDN of the search indexed by the VLV index. 092 */ 093 public DN getBaseDN() 094 { 095 return baseDN; 096 } 097 098 /** 099 * Returns the filter of the search indexed by the VLV index. 100 * 101 * @return the filter of the search indexed by the VLV index. 102 */ 103 public String getFilter() 104 { 105 return filter; 106 } 107 108 /** 109 * Returns the scope of the search indexed by the VLV index. 110 * 111 * @return the scope of the search indexed by the VLV index. 112 */ 113 public SearchScope getScope() 114 { 115 return scope; 116 } 117 118 /** 119 * Returns the sort order list of the VLV index. 120 * 121 * @return the sort order list of the VLV index. 122 */ 123 public List<VLVSortOrder> getSortOrder() 124 { 125 return sortOrder; 126 } 127 128 @Override 129 public boolean equals(Object o) 130 { 131 if (o == this) 132 { 133 return true; 134 } 135 if (!(o instanceof VLVIndexDescriptor)) 136 { 137 return false; 138 } 139 140 final VLVIndexDescriptor index = (VLVIndexDescriptor) o; 141 return index.getName().equalsIgnoreCase(getName()) 142 && index.getBaseDN().equals(getBaseDN()) 143 && index.getFilter().equals(getFilter()) 144 && index.getScope() == getScope() 145 && index.getSortOrder().equals(getSortOrder()) 146 && backendIdEqual(index); 147 } 148 149 private boolean backendIdEqual(VLVIndexDescriptor index) 150 { 151 return getBackend() != null 152 && index.getBackend() != null 153 // Only compare the backend IDs. In this context is better to 154 // do this since the backend object contains some state (like 155 // number entries) that can change. 156 && getBackend().getBackendID().equals(index.getBackend().getBackendID()); 157 } 158 159 @Override 160 protected void recalculateHashCode() 161 { 162 final StringBuilder sb = new StringBuilder(); 163 for (final VLVSortOrder s : sortOrder) 164 { 165 sb.append(s.getAttributeName()).append(s.isAscending()).append(","); 166 } 167 if (getBackend() != null) 168 { 169 sb.append(getBackend().getBackendID()); 170 } 171 hashCode = (getName()+baseDN+scope+filter+sb).hashCode(); 172 } 173 174 /** 175 * Returns the equivalent {@code BackendVLVIndexCfgDefn.Scope} to the provided 176 * search scope. 177 * 178 * @param scope 179 * The {@code SearchScope} to convert. 180 * @return the equivalent {@code BackendVLVIndexCfgDefn.Scope} to the provided 181 * search scope. 182 */ 183 public static BackendVLVIndexCfgDefn.Scope getBackendVLVIndexScope(final SearchScope scope) 184 { 185 switch (scope.asEnum()) 186 { 187 case BASE_OBJECT: 188 return BackendVLVIndexCfgDefn.Scope.BASE_OBJECT; 189 case SINGLE_LEVEL: 190 return BackendVLVIndexCfgDefn.Scope.SINGLE_LEVEL; 191 case SUBORDINATES: 192 return BackendVLVIndexCfgDefn.Scope.SUBORDINATE_SUBTREE; 193 case WHOLE_SUBTREE: 194 return BackendVLVIndexCfgDefn.Scope.WHOLE_SUBTREE; 195 case UNKNOWN: 196 default: 197 throw new IllegalArgumentException("Unsupported SearchScope: " + scope); 198 } 199 } 200 201 /** 202 * Convert the provided {@code BackendVLVIndexCfgDefn.Scope} to 203 * {@code SearchScope}. 204 * 205 * @param scope 206 * The scope to convert. 207 * @return the provided {@code BackendVLVIndexCfgDefn.Scope} to 208 * {@code SearchScope} 209 */ 210 public static SearchScope toSearchScope(final BackendVLVIndexCfgDefn.Scope scope) 211 { 212 switch (scope) 213 { 214 case BASE_OBJECT: 215 return SearchScope.BASE_OBJECT; 216 case SINGLE_LEVEL: 217 return SearchScope.SINGLE_LEVEL; 218 case SUBORDINATE_SUBTREE: 219 return SearchScope.SUBORDINATES; 220 case WHOLE_SUBTREE: 221 return SearchScope.WHOLE_SUBTREE; 222 default: 223 throw new IllegalArgumentException("Unsupported BackendVLVIndexCfgDefn.Scope: " + scope); 224 } 225 } 226}