View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.server.core.api;
21  
22  import org.apache.directory.api.ldap.model.cursor.Cursor;
23  import org.apache.directory.api.ldap.model.entry.Entry;
24  import org.apache.directory.api.ldap.model.message.SearchRequest;
25  
26  /**
27   * A container storing a SearchRequest being processed, and the associated 
28   * size and time limit.
29   * <br>
30   * We use an instance of this class for each new searchRequest, and we discard
31   * it when the SearchRequest is completed or abandonned.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class SearchRequestContainer
36  {
37      /** The SearchRequest */
38      private SearchRequest searchRequest;
39      
40      /** The maximum number of entries we can send */
41      private long sizeLimit;
42      
43      /** The number of entries already sent */
44      private int count;
45      
46      /** The time limit */
47      private long timeLimit;
48      
49      /** The SearchRequest reception date */
50      private long initialTime;
51      
52      /** The Cursor associated with the searchRequest */
53      private Cursor<Entry> cursor;
54      
55      
56      /**
57       * Create an instance of the container with the SearchRequest and its limit.
58       * 
59       * @param searchRequest The SearchRequest instance
60       * @param cursor The cursor to process
61       */
62      public SearchRequestContainer( SearchRequest searchRequest, Cursor<Entry> cursor )
63      {
64          this.searchRequest = searchRequest;
65          this.cursor = cursor;
66          this.sizeLimit = searchRequest.getSizeLimit();
67          this.timeLimit = searchRequest.getTimeLimit() * 1000L; // Time limit is in seconds. Translate that to milliseconds
68          
69          // Initialize the count and current time
70          count = 0;
71          initialTime = System.currentTimeMillis();
72      }
73      
74  
75      /**
76       * @return the searchRequest
77       */
78      public SearchRequest getSearchRequest()
79      {
80          return searchRequest;
81      }
82      
83  
84      /**
85       * @return the sizeLimit
86       */
87      public long getSizeLimit()
88      {
89          return sizeLimit;
90      }
91      
92  
93      /**
94       * @return the count
95       */
96      public int getCount()
97      {
98          return count;
99      }
100     
101 
102     /**
103      * Check the size limit
104      * 
105      * @return true if we have reached the size limit
106      */
107     public boolean isSizeLimitReached()
108     {
109         return ( sizeLimit > 0 ) && ( count >= sizeLimit );
110     }
111     
112 
113     /**
114      * Increment the count
115      */
116     public void increment()
117     {
118         this.count++;
119     }
120     
121 
122     /**
123      * @return the timeLimit
124      */
125     public int getTimeLimit()
126     {
127         return ( int ) ( timeLimit / 1000L ); // Convert it back to seconds
128     }
129     
130     
131     /**
132      * Check if we have reached the time limit
133      * 
134      * @return true if we have reached the time limit
135      */
136     public boolean isTimeLimitReached()
137     {
138         long currentTime = System.currentTimeMillis();
139         
140         return ( timeLimit > 0 ) && ( initialTime + timeLimit < currentTime );
141     }
142     
143 
144     /**
145      * @return the initialTime
146      */
147     public long getInitialTime()
148     {
149         return initialTime;
150     }
151     
152     
153     /**
154      * @return The cursor associated with the SearchRequest
155      */
156     public Cursor<Entry> getCursor()
157     {
158         return cursor;
159     }
160 
161 
162     /**
163      * @see Object#toString()
164      */
165     public String toString()
166     {
167         long elapsed = System.currentTimeMillis() - initialTime;
168         
169         return "SRContainer : <" + searchRequest.getMessageId() + ", nbSent : " + count + ", elapsed : " + elapsed + ">";
170     }
171 }