【发布时间】:2015-09-02 04:24:31
【问题描述】:
谁能帮我改进java中的ldap递归搜索?下面是我的代码。我通过的过滤器是filter=(&(IMSI=404201234500021))。目前从 100000 条记录中搜索一条记录需要 19 秒以上。
public List<Map<String, String>> searchRecursive(List<String> sColumns, Map<String, String> searchFilters, int startIndex,
int amount)
{
String searchRDN = "";
List<Map<String, String>> items = new ArrayList<Map<String, String>>();
Attributes searchAttributes = new BasicAttributes();
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = null;
try
{
String filter = "";
if (searchFilters != null)
{
for (Map.Entry<String, String> entry : searchFilters.entrySet())
{
filter += "(" + entry.getKey() + "=" + entry.getValue() + ")";
}
}
filter = "(&" + filter + ")";
_log.debug("filter="+filter);
byte[] cookie = null;
ctx.setRequestControls(new Control[]
{new PagedResultsControl(amount, cookie, Control.NONCRITICAL)});
//results = ctx.search(searchRDN, searchAttributes, null);
results= ctx.search("", filter, ctls);
if (results != null)
{
while (results.hasMore())
{
Map<String, String> rowSet = new CaseInsensitiveMap();
SearchResult searchResult = results.next();
String rdn = searchResult.getNameInNamespace();
rowSet.put("baseRDN", rdn);
NamingEnumeration<? extends Attribute> all = searchResult.getAttributes().getAll();
while (all.hasMoreElements())
{
Attribute attr = all.nextElement();
// attr.getID()
NamingEnumeration<?> all2 = attr.getAll();
String value = "";
while (all2.hasMoreElements())
{
if (!value.equals(""))
{
value += ", " + all2.nextElement();
}
else
{
value = "" + all2.nextElement();
}
}
rowSet.put(attr.getID(), value);
}
items.add(rowSet);
}
}
}
catch (Throwable e)
{
_log.error(e.getMessage());
_log.debug(e);
}
finally
{
if (results != null)
{
try
{
results.close();
}
catch (Exception e)
{
}
}
}
return items;
}
【问题讨论】:
-
您能解释一下您的代码在做什么以及为什么要这样做吗?
-
我想在所有对象类中搜索一个记录条目。共有三个对象类