aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeodor Zlatanov2011-05-10 22:21:39 +0000
committerKatsumi Yamaoka2011-05-10 22:21:39 +0000
commit15cc1ab1f0607f32ac76fa689df140cb1b3e27bd (patch)
treeec0ce489cc58d8b5a9331040fac7786d7ed5a33d
parent8b3402409798b2399150796ff6dfb2ffb62e0cbb (diff)
downloademacs-15cc1ab1f0607f32ac76fa689df140cb1b3e27bd.tar.gz
emacs-15cc1ab1f0607f32ac76fa689df140cb1b3e27bd.zip
registry.el (registry-prune-hard-candidates, registry-prune-soft-candidates): Helper methods for registry pruning.
(registry-prune): Use them. Make the sort function optional.
-rw-r--r--lisp/gnus/ChangeLog6
-rw-r--r--lisp/gnus/registry.el54
2 files changed, 47 insertions, 13 deletions
diff --git a/lisp/gnus/ChangeLog b/lisp/gnus/ChangeLog
index 657700a6343..ad69b292a7f 100644
--- a/lisp/gnus/ChangeLog
+++ b/lisp/gnus/ChangeLog
@@ -1,3 +1,9 @@
12011-05-10 Teodor Zlatanov <tzz@lifelogs.com>
2
3 * registry.el (registry-prune-hard-candidates)
4 (registry-prune-soft-candidates): Helper methods for registry pruning.
5 (registry-prune): Use them. Make the sort function optional.
6
12011-05-10 Julien Danjou <julien@danjou.info> 72011-05-10 Julien Danjou <julien@danjou.info>
2 8
3 * shr.el (shr-put-color-1): Do not bug out when old-props is a face 9 * shr.el (shr-put-color-1): Do not bug out when old-props is a face
diff --git a/lisp/gnus/registry.el b/lisp/gnus/registry.el
index 4beafd4b845..ffba2e10e6a 100644
--- a/lisp/gnus/registry.el
+++ b/lisp/gnus/registry.el
@@ -314,29 +314,57 @@ Errors out if the key exists already."
314This is the key count of the :data slot." 314This is the key count of the :data slot."
315 (hash-table-count (oref db :data))) 315 (hash-table-count (oref db :data)))
316 316
317 (defmethod registry-prune ((db registry-db)) 317 (defmethod registry-prune ((db registry-db) &optional sortfun)
318 "Prunes the registry-db object THIS. 318 "Prunes the registry-db object THIS.
319Removes only entries without the :precious keys." 319Removes only entries without the :precious keys if it can,
320then removes oldest entries first.
321Returns the number of deleted entries.
322If SORTFUN is given, tries to keep entries that sort *higher*.
323SORTFUN is passed only the two keys so it must look them up directly."
324 (dolist (collector '(registry-prune-soft-candidates
325 registry-prune-hard-candidates))
326 (let* ((size (registry-size db))
327 (collected (funcall collector db))
328 (limit (nth 0 collected))
329 (candidates (nth 1 collected))
330 ;; sort the candidates if SORTFUN was given
331 (candidates (if sortfun (sort candidates sortfun) candidates))
332 (candidates-count (length candidates))
333 ;; are we over max-soft?
334 (prune-needed (> size limit)))
335
336 ;; while we have more candidates than we need to remove...
337 (while (and (> candidates-count (- size limit)) candidates)
338 (decf candidates-count)
339 (setq candidates (cdr candidates)))
340
341 (registry-delete db candidates nil)
342 (length candidates))))
343
344 (defmethod registry-prune-soft-candidates ((db registry-db))
345 "Collects pruning candidates from the registry-db object THIS.
346Proposes only entries without the :precious keys."
320 (let* ((precious (oref db :precious)) 347 (let* ((precious (oref db :precious))
321 (precious-p (lambda (entry-key) 348 (precious-p (lambda (entry-key)
322 (cdr (memq (car entry-key) precious)))) 349 (cdr (memq (car entry-key) precious))))
323 (data (oref db :data)) 350 (data (oref db :data))
324 (limit (oref db :max-soft)) 351 (limit (oref db :max-soft))
325 (size (registry-size db))
326 (candidates (loop for k being the hash-keys of data 352 (candidates (loop for k being the hash-keys of data
327 using (hash-values v) 353 using (hash-values v)
328 when (notany precious-p v) 354 when (notany precious-p v)
329 collect k)) 355 collect k)))
330 (candidates-count (length candidates)) 356 (list limit candidates)))
331 ;; are we over max-soft?
332 (prune-needed (> size limit)))
333 357
334 ;; while we have more candidates than we need to remove... 358 (defmethod registry-prune-hard-candidates ((db registry-db))
335 (while (and (> candidates-count (- size limit)) candidates) 359 "Collects pruning candidates from the registry-db object THIS.
336 (decf candidates-count) 360Proposes any entries over the max-hard limit minus 10."
337 (setq candidates (cdr candidates))) 361 (let* ((data (oref db :data))
338 362 ;; prune to 10 below the max-hard limit so we're not
339 (registry-delete db candidates nil)))) 363 ;; pruning all the time
364 (limit (- (oref db :max-hard) 10))
365 (candidates (loop for k being the hash-keys of data
366 collect k)))
367 (list limit candidates))))
340 368
341(ert-deftest registry-instantiation-test () 369(ert-deftest registry-instantiation-test ()
342 (should (registry-db "Testing"))) 370 (should (registry-db "Testing")))