Index: branches/concurrency/includes/ConcurrencyCheck.php |
— | — | @@ -32,6 +32,7 @@ |
33 | 33 | $this->dbw = wfGetDb( DB_MASTER ); |
34 | 34 | |
35 | 35 | $this->user = $user; |
| 36 | + // TODO: create a registry of all valid resourceTypes that client app can add to. |
36 | 37 | $this->resourceType = $resourceType; |
37 | 38 | $this->setExpirationTime( $expirationTime ); |
38 | 39 | } |
— | — | @@ -85,6 +86,7 @@ |
86 | 87 | return true; |
87 | 88 | } |
88 | 89 | |
| 90 | + // if the insert failed, it's necessary to check the expiration. |
89 | 91 | $dbw->begin(); |
90 | 92 | $row = $dbw->selectRow( |
91 | 93 | 'concurrencycheck', |
— | — | @@ -123,24 +125,10 @@ |
124 | 126 | ); |
125 | 127 | |
126 | 128 | // cache the result. |
127 | | - $memc->set( $cacheKey, array( 'userId' => $userId, 'expiration' => $expiration ), $expiration - time() ); |
| 129 | + $memc->set( $cacheKey, array( 'userId' => $userId, 'expiration' => $expiration ), $this->expirationTime ); |
128 | 130 | |
129 | 131 | $dbw->commit(); |
130 | 132 | return true; |
131 | | - |
132 | | - // if insert succeeds, delete the cache key. don't make a new one since they have to be created atomically. |
133 | | - // |
134 | | - // if insert fails: |
135 | | - // begin transaction |
136 | | - // select where key=key and expiration > now() |
137 | | - // if row is missing or user matches: |
138 | | - // execute a replace() |
139 | | - // overwrite the cache key (might as well, since this is inside a transaction) |
140 | | - // commit |
141 | | - // if select returned an unexpired row owned by someone else, return failure. |
142 | | - |
143 | | - // optional: check to see if the current user already has the resource checked out, and if so, |
144 | | - // return that checkout information instead. (does anyone want that?) |
145 | 133 | } |
146 | 134 | |
147 | 135 | /** |
— | — | @@ -173,15 +161,7 @@ |
174 | 162 | return true; |
175 | 163 | } |
176 | 164 | |
177 | | - return false; |
178 | | - |
179 | | - // delete the row, specifying the username in the where clause (keeps users from checking in stuff that's not theirs). |
180 | | - // if a row was deleted: |
181 | | - // remove the record from memcache. (removing cache key doesn't require atomicity) |
182 | | - // return true |
183 | | - // else |
184 | | - // return false |
185 | | - |
| 165 | + return false; |
186 | 166 | } |
187 | 167 | |
188 | 168 | /** |
— | — | @@ -229,19 +209,6 @@ |
230 | 210 | |
231 | 211 | // return the number of rows removed. |
232 | 212 | return $dbw->affectedRows(); |
233 | | - |
234 | | - // grab a unixtime. |
235 | | - // select all rows where expiration < time |
236 | | - // delete all rows where expiration < time |
237 | | - // remove selected rows from memcache |
238 | | - // |
239 | | - // previous idea, probably wrong: |
240 | | - // select all expired rows. |
241 | | - // foreach( expired ) |
242 | | - // delete row where id=id and expiration < now() (accounts for updates) |
243 | | - // if delete succeeded, remove cache key (txn not required, since removing cache key doesn't require atomicity) |
244 | | - // (sadly, this must be many deletes to coordinate removal from memcache) |
245 | | - // (is it necessary to remove expired cache entries?) |
246 | 213 | } |
247 | 214 | |
248 | 215 | public function status( $keys ) { |
— | — | @@ -251,9 +218,6 @@ |
252 | 219 | |
253 | 220 | $checkouts = array(); |
254 | 221 | $toSelect = array(); |
255 | | - |
256 | | - // maybe run this here? |
257 | | - //$this->expire(); |
258 | 222 | |
259 | 223 | // validate keys, attempt to retrieve from cache. |
260 | 224 | foreach( $keys as $key ) { |
— | — | @@ -276,6 +240,9 @@ |
277 | 241 | |
278 | 242 | // if there were cache misses... |
279 | 243 | if( $toSelect ) { |
| 244 | + // If it's time to go to the database, go ahead and expire old rows. |
| 245 | + $this->expire(); |
| 246 | + |
280 | 247 | // the transaction seems incongruous, I know, but it's to keep the cache update atomic. |
281 | 248 | $dbw->begin(); |
282 | 249 | $res = $dbw->select( |
— | — | @@ -315,23 +282,10 @@ |
316 | 283 | } |
317 | 284 | |
318 | 285 | return $checkouts; |
319 | | - |
320 | | - // fetch keys from cache or db (keys are an array) |
321 | | - // |
322 | | - // for all unexpired keys present in cache, store cached return value for returning later. |
323 | | - // |
324 | | - // if some keys remain (missing from cache or expired): |
325 | | - // execute expire() to make sure db records are cleared |
326 | | - // for all remaining keys: |
327 | | - // begin transaction |
328 | | - // select rows where key in (keys) and expiration > now() |
329 | | - // overwrite any memcache entry |
330 | | - // commit |
331 | | - // return values that were added to cache, plus values pulled from cache |
332 | 286 | } |
333 | 287 | |
334 | 288 | public function listCheckouts() { |
335 | | - // fill in the function that lets you get the complete set of checkouts for a given application. |
| 289 | + // TODO: fill in the function that lets you get the complete set of checkouts for a given application. |
336 | 290 | } |
337 | 291 | |
338 | 292 | public function setUser ( $user ) { |
— | — | @@ -367,6 +321,8 @@ |
368 | 322 | if( ! preg_match('/^\d+$/', $record) ) { |
369 | 323 | throw new ConcurrencyCheckBadRecordIdException( 'Record ID ' . $record . ' must be a positive integer' ); |
370 | 324 | } |
| 325 | + |
| 326 | + // TODO: add a hook here for client-side validation. |
371 | 327 | return true; |
372 | 328 | } |
373 | 329 | } |