1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.xdbm.search.impl;
21
22
23 import org.apache.directory.shared.ldap.filter.PresenceNode;
24 import org.apache.directory.shared.ldap.schema.AttributeType;
25 import org.apache.directory.server.xdbm.IndexEntry;
26 import org.apache.directory.server.xdbm.Store;
27 import org.apache.directory.server.xdbm.Index;
28 import org.apache.directory.server.xdbm.search.Evaluator;
29 import org.apache.directory.server.schema.registries.Registries;
30 import org.apache.directory.server.core.entry.ServerEntry;
31 import org.apache.directory.server.core.entry.ServerAttribute;
32
33 import java.util.Iterator;
34
35
36
37
38
39
40
41
42
43 public class PresenceEvaluator implements Evaluator<PresenceNode, ServerEntry>
44 {
45 private final PresenceNode node;
46 private final Store<ServerEntry> db;
47 private final Registries registries;
48 private final AttributeType type;
49 private final Index<String,ServerEntry> idx;
50
51
52 public PresenceEvaluator( PresenceNode node, Store<ServerEntry> db, Registries registries )
53 throws Exception
54 {
55 this.db = db;
56 this.node = node;
57 this.registries = registries;
58 this.type = registries.getAttributeTypeRegistry().lookup( node.getAttribute() );
59
60 if ( db.hasUserIndexOn( node.getAttribute() ) )
61 {
62 idx = db.getPresenceIndex();
63 }
64 else
65 {
66 idx = null;
67 }
68 }
69
70
71 public PresenceNode getExpression()
72 {
73 return node;
74 }
75
76
77 public AttributeType getAttributeType()
78 {
79 return type;
80 }
81
82
83
84
85 public boolean evaluate( IndexEntry<?,ServerEntry> indexEntry ) throws Exception
86 {
87 if ( idx != null )
88 {
89 return idx.forward( type.getOid(), indexEntry.getId() );
90 }
91
92 ServerEntry entry = indexEntry.getObject();
93
94
95 if ( null == entry )
96 {
97 entry = db.lookup( indexEntry.getId() );
98 indexEntry.setObject( entry );
99 }
100
101 return evaluate( entry );
102 }
103
104
105
106
107 public boolean evaluate( Long id ) throws Exception
108 {
109 if ( idx != null )
110 {
111 return idx.forward( type.getOid(), id );
112 }
113
114 return evaluate( db.lookup( id ) );
115 }
116
117
118
119
120 public boolean evaluate( ServerEntry entry ) throws Exception
121 {
122
123 ServerAttribute attr = ( ServerAttribute ) entry.get( type );
124
125
126 if ( attr != null )
127 {
128 return true;
129 }
130
131
132
133
134 if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
135 {
136
137
138
139 Iterator<AttributeType> descendants =
140 registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
141
142 do
143 {
144 AttributeType descendant = descendants.next();
145
146 attr = ( ServerAttribute ) entry.get( descendant );
147
148 if ( attr != null )
149 {
150 return true;
151 }
152 }
153 while ( descendants.hasNext() );
154 }
155
156
157 return false;
158 }
159 }