~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/facet/src/java/org/apache/lucene/facet/search/TemporaryObjectAllocator.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.facet.search;
2
 
 
3
 
import java.util.concurrent.ConcurrentLinkedQueue;
4
 
 
5
 
/**
6
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
7
 
 * contributor license agreements.  See the NOTICE file distributed with
8
 
 * this work for additional information regarding copyright ownership.
9
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
10
 
 * (the "License"); you may not use this file except in compliance with
11
 
 * the License.  You may obtain a copy of the License at
12
 
 *
13
 
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 
 *
15
 
 * Unless required by applicable law or agreed to in writing, software
16
 
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 
 * See the License for the specific language governing permissions and
19
 
 * limitations under the License.
20
 
 */
21
 
 
22
 
/**
23
 
 * An TemporaryObjectAllocator is an object which manages large, reusable,
24
 
 * temporary objects needed during multiple concurrent computations. The idea
25
 
 * is to remember some of the previously allocated temporary objects, and
26
 
 * reuse them if possible to avoid constant allocation and garbage-collection
27
 
 * of these objects. 
28
 
 * <P>
29
 
 * This technique is useful for temporary counter arrays in faceted search
30
 
 * (see {@link FacetsAccumulator}), which can be reused across searches instead
31
 
 * of being allocated afresh on every search.
32
 
 * <P>
33
 
 * A TemporaryObjectAllocator is thread-safe.
34
 
 * 
35
 
 * @lucene.experimental
36
 
 */
37
 
public abstract class TemporaryObjectAllocator<T> {
38
 
 
39
 
  // In the "pool" we hold up to "maxObjects" old objects, and if the pool
40
 
  // is not empty, we return one of its objects rather than allocating a new
41
 
  // one.
42
 
  ConcurrentLinkedQueue<T> pool = new ConcurrentLinkedQueue<T>();  
43
 
  int maxObjects;
44
 
 
45
 
  /**
46
 
   * Construct an allocator for objects of a certain type, keeping around a
47
 
   * pool of up to <CODE>maxObjects</CODE> old objects.
48
 
   * <P>
49
 
   * Note that the pool size only restricts the number of objects that hang
50
 
   * around when not needed, but <I>not</I> the maximum number of objects
51
 
   * that are allocated when actually is use: If a number of concurrent
52
 
   * threads ask for an allocation, all of them will get an object, even if 
53
 
   * their number is greater than maxObjects. If an application wants to
54
 
   * limit the number of concurrent threads making allocations, it needs to
55
 
   * do so on its own - for example by blocking new threads until the
56
 
   * existing ones have finished. If more than maxObjects are freed, only
57
 
   * maxObjects of them will be kept in the pool - the rest will not and
58
 
   * will eventually be garbage-collected by Java.
59
 
   * <P>
60
 
   * In particular, when maxObjects=0, this object behaves as a trivial
61
 
   * allocator, always allocating a new array and never reusing an old one. 
62
 
   */
63
 
  public TemporaryObjectAllocator(int maxObjects) {
64
 
    this.maxObjects = maxObjects;
65
 
  }
66
 
 
67
 
  /**
68
 
   * Subclasses must override this method to actually create a new object
69
 
   * of the desired type.
70
 
   * 
71
 
   */
72
 
  protected abstract T create();
73
 
 
74
 
  /**
75
 
   * Subclasses must override this method to clear an existing object of
76
 
   * the desired type, to prepare it for reuse. Note that objects will be
77
 
   * cleared just before reuse (on allocation), not when freed.
78
 
   */
79
 
  protected abstract void clear(T object);
80
 
 
81
 
  /**
82
 
   * Allocate a new object. If there's a previously allocated object in our
83
 
   * pool, we return it immediately. Otherwise, a new object is allocated.
84
 
   * <P>
85
 
   * Don't forget to call {@link #free(Object)} when you're done with the object,
86
 
   * to return it to the pool. If you don't, memory is <I>not</I> leaked,
87
 
   * but the pool will remain empty and a new object will be allocated each
88
 
   * time (just like the maxArrays=0 case). 
89
 
   */
90
 
  public final T allocate() {
91
 
    T object = pool.poll();
92
 
    if (object==null) {
93
 
      return create();
94
 
    }
95
 
    clear(object);
96
 
    return object;
97
 
  }
98
 
 
99
 
  /**
100
 
   * Return a no-longer-needed object back to the pool. If we already have
101
 
   * enough objects in the pool (maxObjects as specified in the constructor),
102
 
   * the array will not be saved, and Java will eventually garbage collect
103
 
   * it.
104
 
   * <P>
105
 
   * In particular, when maxArrays=0, the given array is never saved and
106
 
   * free does nothing.
107
 
   */
108
 
  public final void free(T object) {
109
 
    if (pool.size() < maxObjects && object != null) {
110
 
      pool.add(object);
111
 
    }
112
 
  }
113
 
 
114
 
}