GeoPubby  Version 0.1.0.0
AutocompleteEngine< T extends Indexable > Class Template Reference

Facade for indexing and searching Indexable elements. More...

Inheritance diagram for AutocompleteEngine< T extends Indexable >:
Collaboration diagram for AutocompleteEngine< T extends Indexable >:

Classes

class  Builder
 Builder for constructing AutocompleteEngine instances. More...
 

Public Member Functions

boolean add (T element)
 Indexes a single element. More...
 
boolean addAll (Collection< T > elements)
 Indexes a collection of elements. More...
 
boolean remove (T element)
 Removes a single element. More...
 
boolean removeAll (Collection< T > elements)
 Removes a collection of elements. More...
 
Collection< T > getAll ()
 Gets all elements. More...
 
List< T > search (String query)
 Returns a List of all elements that match a query, sorted according to the default comparator. More...
 
List< T > search (String query, int limit)
 Returns a List of the top elements that match a query, sorted according to the default comparator. More...
 

Private Member Functions

 AutocompleteEngine (Builder< T > builder)
 

Private Attributes

final Analyzer analyzer
 
final Comparator< ScoredObject< T > > comparator
 
final IndexAdapter< T > index
 
final Lock read
 
final Lock write
 

Detailed Description

Facade for indexing and searching Indexable elements.

Constructor & Destructor Documentation

◆ AutocompleteEngine()

AutocompleteEngine ( Builder< T >  builder)
private
36  {
37  assert builder != null;
38  this.analyzer = builder.analyzer;
39  this.comparator = builder.comparator;
40  this.index = builder.index;
41  ReadWriteLock lock = new ReentrantReadWriteLock();
42  this.read = lock.readLock();
43  this.write = lock.writeLock();
44  }
final IndexAdapter< T > index
Definition: AutocompleteEngine.java:31
final Comparator< ScoredObject< T > > comparator
Definition: AutocompleteEngine.java:30
final Analyzer analyzer
Definition: AutocompleteEngine.java:29
final Lock read
Definition: AutocompleteEngine.java:32
final Lock write
Definition: AutocompleteEngine.java:33

References AutocompleteEngine< T extends Indexable >.Builder< T extends Indexable >.analyzer, AutocompleteEngine< T extends Indexable >.Builder< T extends Indexable >.comparator, and AutocompleteEngine< T extends Indexable >.Builder< T extends Indexable >.index.

Member Function Documentation

◆ add()

boolean add ( element)

Indexes a single element.

Exceptions
NullPointerExceptionif
element
is null;
52  {
53  return addAll(Arrays.asList(element));
54  }
boolean addAll(Collection< T > elements)
Indexes a collection of elements.
Definition: AutocompleteEngine.java:61

References AutocompleteEngine< T extends Indexable >.addAll().

◆ addAll()

boolean addAll ( Collection< T >  elements)

Indexes a collection of elements.

Exceptions
NullPointerExceptionif
elements
is null or contains a null element;
62  {
63  checkPointer(elements != null);
64  boolean result = false;
65  for (T element : elements)
66  {
67  checkPointer(element != null);
68  write.lock();
69  try
70  {
71  for (String field : element.getFields())
72  {
73  for (String token : analyzer.apply(field))
74  {
75  result |= index.put(token, element);
76  }
77  }
78  }
79  finally
80  {
81  write.unlock();
82  }
83  }
84  return result;
85  }

References AutocompleteEngine< T extends Indexable >.analyzer, AutocompleteEngine< T extends Indexable >.index, and AutocompleteEngine< T extends Indexable >.write.

Referenced by AutocompleteEngine< T extends Indexable >.add().

◆ getAll()

Collection<T> getAll ( )

Gets all elements.

Exceptions
NullPointerExceptionif
elements
is null or contains a null element;
128  {
129  return new LinkedList<T>();
130  }

◆ remove()

boolean remove ( element)

Removes a single element.

Exceptions
NullPointerExceptionif
element
is null;
93  {
94  return removeAll(Arrays.asList(element));
95  }
boolean removeAll(Collection< T > elements)
Removes a collection of elements.
Definition: AutocompleteEngine.java:102

References AutocompleteEngine< T extends Indexable >.removeAll().

◆ removeAll()

boolean removeAll ( Collection< T >  elements)

Removes a collection of elements.

Exceptions
NullPointerExceptionif
elements
is null or contains a null element;
103  {
104  checkPointer(elements != null);
105  boolean result = false;
106  for (T element : elements)
107  {
108  checkPointer(element != null);
109  write.lock();
110  try
111  {
112  result |= index.remove(element);
113  }
114  finally
115  {
116  write.unlock();
117  }
118  }
119  return result;
120  }

References AutocompleteEngine< T extends Indexable >.index, and AutocompleteEngine< T extends Indexable >.write.

Referenced by AutocompleteEngine< T extends Indexable >.remove().

◆ search() [1/2]

List<T> search ( String  query)

Returns a List of all elements that match a query, sorted according to the default comparator.

Exceptions
NullPointerExceptionif
query
is null;
139  {
140  checkPointer(query != null);
141  read.lock();
142  try
143  {
144  Aggregator<T> aggregator = new Aggregator<>(comparator);
145  Iterator<String> tokens = analyzer.apply(query).iterator();
146  if (tokens.hasNext())
147  {
148  aggregator.addAll(index.get(tokens.next()));
149  }
150  while (tokens.hasNext())
151  {
152  if (aggregator.isEmpty())
153  {
154  break;
155  }
156  aggregator.retainAll(index.get(tokens.next()));
157  }
158  return aggregator.values();
159  }
160  finally
161  {
162  read.unlock();
163  }
164  }

References AutocompleteEngine< T extends Indexable >.analyzer, AutocompleteEngine< T extends Indexable >.comparator, AutocompleteEngine< T extends Indexable >.index, and AutocompleteEngine< T extends Indexable >.read.

Referenced by AutocompleteEngine< T extends Indexable >.search().

◆ search() [2/2]

List<T> search ( String  query,
int  limit 
)

Returns a List of the top elements that match a query, sorted according to the default comparator.

Exceptions
NullPointerExceptionif
query
is null;
IllegalArgumentExceptionif
limit
is negative;
174  {
175  checkArgument(limit >= 0);
176  List<T> result = search(query);
177  if (result.size() > limit)
178  {
179  return result.subList(0, limit);
180  }
181  return result;
182  }
List< T > search(String query)
Returns a List of all elements that match a query, sorted according to the default comparator.
Definition: AutocompleteEngine.java:138

References AutocompleteEngine< T extends Indexable >.search().

Member Data Documentation

◆ analyzer

◆ comparator

final Comparator<ScoredObject<T> > comparator
private

◆ index

◆ read

final Lock read
private

◆ write