1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 module hunt.pool.PooledObject; 18 19 import hunt.pool.PooledObjectState; 20 21 // import java.io.PrintWriter; 22 // import java.util.Deque; 23 24 import hunt.collection.Deque; 25 import hunt.util.Comparator; 26 import hunt.util.Common; 27 28 interface IPooledObject { 29 30 // Object getObject(); 31 TypeInfo objectType(); 32 33 string objectToString(); 34 35 /** 36 * Obtains the time (using the same basis as 37 * {@link DateTimeHelper.currentTimeMillis()}) that this object was created. 38 * 39 * @return The creation time for the wrapped object 40 */ 41 long getCreateTime(); 42 43 /** 44 * Obtains the time in milliseconds that this object last spent in the 45 * active state (it may still be active in which case subsequent calls will 46 * return an increased value). 47 * 48 * @return The time in milliseconds last spent in the active state 49 */ 50 long getActiveTimeMillis(); 51 52 /** 53 * Obtains the time in milliseconds that this object last spend in the 54 * idle state (it may still be idle in which case subsequent calls will 55 * return an increased value). 56 * 57 * @return The time in milliseconds last spent in the idle state 58 */ 59 long getIdleTimeMillis(); 60 61 /** 62 * Obtains the time the wrapped object was last borrowed. 63 * 64 * @return The time the object was last borrowed 65 */ 66 long getLastBorrowTime(); 67 68 /** 69 * Obtains the time the wrapped object was last returned. 70 * 71 * @return The time the object was last returned 72 */ 73 long getLastReturnTime(); 74 75 /** 76 * Returns an estimate of the last time this object was used. If the class 77 * of the pooled object implements {@link TrackedUse}, what is returned is 78 * the maximum of {@link TrackedUse#getLastUsed()} and 79 * {@link #getLastBorrowTime()}; otherwise this method gives the same 80 * value as {@link #getLastBorrowTime()}. 81 * 82 * @return the last time this object was used 83 */ 84 long getLastUsedTime(); 85 86 bool opEquals(IPooledObject obj); 87 88 // bool opEquals(Object obj); 89 90 size_t toHash() @trusted nothrow; 91 92 /** 93 * Provides a string form of the wrapper for debug purposes. The format is 94 * not fixed and may change at any time. 95 * <p> 96 * {@inheritDoc} 97 */ 98 string toString(); 99 100 /** 101 * Attempts to place the pooled object in the 102 * {@link PooledObjectState#EVICTION} state. 103 * 104 * @return <code>true</code> if the object was placed in the 105 * {@link PooledObjectState#EVICTION} state otherwise 106 * <code>false</code> 107 */ 108 bool startEvictionTest(); 109 110 /** 111 * Allocates the object. 112 * 113 * @return {@code true} if the original state was {@link PooledObjectState#IDLE IDLE} 114 */ 115 bool allocate(); 116 117 /** 118 * Deallocates the object and sets it {@link PooledObjectState#IDLE IDLE} 119 * if it is currently {@link PooledObjectState#ALLOCATED ALLOCATED}. 120 * 121 * @return {@code true} if the state was {@link PooledObjectState#ALLOCATED ALLOCATED} 122 */ 123 bool deallocate(); 124 125 /** 126 * Sets the state to {@link PooledObjectState#INVALID INVALID} 127 */ 128 void invalidate(); 129 130 /** 131 * Is abandoned object tracking being used? If this is true the 132 * implementation will need to record the stack trace of the last caller to 133 * borrow this object. 134 * 135 * @param logAbandoned The new configuration setting for abandoned 136 * object tracking 137 */ 138 void setLogAbandoned(bool logAbandoned); 139 140 // TODO: uncomment in 3.0 (API compatibility) 141 // /** 142 // * Configures the stack trace generation strategy based on whether or not fully 143 // * detailed stack traces are required. When set to false, abandoned logs may 144 // * only include caller class information rather than method names, line numbers, 145 // * and other normal metadata available in a full stack trace. 146 // * 147 // * @param requireFullStackTrace the new configuration setting for abandoned object 148 // * logging 149 // */ 150 // void setRequireFullStackTrace(bool requireFullStackTrace); 151 152 /** 153 * Record the current stack trace as the last time the object was used. 154 */ 155 void use(); 156 157 /** 158 * Prints the stack trace of the code that borrowed this pooled object and 159 * the stack trace of the last code to use this object (if available) to 160 * the supplied writer. 161 * 162 * @param writer The destination for the debug output 163 */ 164 // void printStackTrace(PrintWriter writer); 165 166 /** 167 * Returns the state of this object. 168 * @return state 169 */ 170 PooledObjectState getState(); 171 172 /** 173 * Marks the pooled object as abandoned. 174 */ 175 void markAbandoned(); 176 177 /** 178 * Marks the object as returning to the pool. 179 */ 180 void markReturning(); 181 182 // TODO: Uncomment this for version 3 (can't add it to 2.x as it will break 183 // API compatibility) 184 ///** 185 // * Get the number of times this object has been borrowed. 186 // */ 187 //long getBorrowedCount(); 188 } 189 190 /** 191 * Defines the wrapper that is used to track the additional information, such as 192 * state, for the pooled objects. 193 * <p> 194 * Implementations of this class are required to be thread-safe. 195 * 196 * @param <T> the type of object in the pool 197 * 198 */ 199 interface PooledObject(T) : IPooledObject, Comparable!(IPooledObject) { 200 201 /** 202 * Obtains the underlying object that is wrapped by this instance of 203 * {@link PooledObject}. 204 * 205 * @return The wrapped object 206 */ 207 T getObject(); 208 209 /** 210 * Orders instances based on idle time - i.e. the length of time since the 211 * instance was returned to the pool. Used by the GKOP idle object evictor. 212 *<p> 213 * Note: This class has a natural ordering that is inconsistent with 214 * equals if distinct objects have the same identity hash code. 215 * </p> 216 * <p> 217 * {@inheritDoc} 218 * </p> 219 */ 220 int opCmp(IPooledObject other); 221 222 /** 223 * Called to inform the object that the eviction test has ended. 224 * 225 * @param idleQueue The queue of idle objects to which the object should be 226 * returned 227 * 228 * @return Currently not used 229 */ 230 bool endEvictionTest(Deque!(IPooledObject) idleQueue); 231 232 }