;;; registry.el --- Track and remember data items by various fields
-;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
+;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
;; Author: Teodor Zlatanov <tzz@lifelogs.com>
;; Keywords: data
;; version: a float
-;; max-size: an integer, default 5000000
+;; max-size: an integer, default most-positive-fixnum
;; prune-factor: a float between 0 and 1, default 0.1
(error
"eieio not found in `load-path' or gnus-fallback-lib/ directory.")))
+(eval-when-compile
+ (unless (fboundp 'cl-remf)
+ (defalias 'cl-remf 'remf)
+ (defalias 'cl-loop 'loop)
+ (defalias 'cl-subseq 'subseq)))
+
;; The version number needs to be kept outside of the class definition
;; itself. The persistent-save process does *not* write to file any
;; slot values that are equal to the default :initform value. If a
(defvar registry-db-version 0.2
"The current version of the registry format.")
+(eval `
(defclass registry-db (eieio-persistent)
((version :initarg :version
:initform nil
:type (or null float)
:documentation "The registry version.")
(max-size :initarg :max-size
- :initform 5000000
+ ;; EIEIO's :initform is not 100% compatible with CLOS in
+ ;; that if the form is an atom, it assumes it's constant
+ ;; value rather than an expression, so in order to get the value
+ ;; of `most-positive-fixnum', we need to use an
+ ;; expression that's not just a symbol.
+ :initform ,(symbol-value 'most-positive-fixnum)
:type integer
:custom integer
:documentation "The maximum number of registry entries.")
(data :initarg :data
:type hash-table
:documentation "The data hashtable.")))
+)
(defmethod initialize-instance :BEFORE ((this registry-db) slots)
"Check whether a registry object needs to be upgraded."
(defmethod registry-lookup ((db registry-db) keys)
"Search for KEYS in the registry-db THIS.
Returns an alist of the key followed by the entry in a list, not a cons cell."
- (let ((data (oref db :data)))
+ (let ((data (oref db data)))
(delq nil
(mapcar
(lambda (k)
(defmethod registry-lookup-breaks-before-lexbind ((db registry-db) keys)
"Search for KEYS in the registry-db THIS.
Returns an alist of the key followed by the entry in a list, not a cons cell."
- (let ((data (oref db :data)))
+ (let ((data (oref db data)))
(delq nil
(loop for key in keys
when (gethash key data)
(when create
(puthash tracksym
(make-hash-table :size 800 :rehash-size 2.0 :test 'equal)
- (oref db :tracker))
- (gethash tracksym (oref db :tracker))))))
+ (oref db tracker))
+ (gethash tracksym (oref db tracker))))))
(defmethod registry-lookup-secondary-value ((db registry-db) tracksym val
&optional set)
(defmethod registry-search ((db registry-db) &rest spec)
"Search for SPEC across the registry-db THIS.
-For example calling with :member '(a 1 2) will match entry '((a 3 1)).
+For example calling with :member \\='(a 1 2) will match entry \\='((a 3 1)).
Calling with :all t (any non-nil value) will match all.
-Calling with :regex '\(a \"h.llo\") will match entry '((a \"hullo\" \"bye\").
+Calling with :regex \\='\(a \"h.llo\") will match entry \\='((a \"hullo\" \"bye\").
The test order is to check :all first, then :member, then :regex."
(when db
(let ((all (plist-get spec :all))
(member (plist-get spec :member))
(regex (plist-get spec :regex)))
- (loop for k being the hash-keys of (oref db :data)
+ (loop for k being the hash-keys of (oref db data)
using (hash-values v)
when (or
;; :all non-nil returns all
If KEYS is nil, use SPEC to do a search.
Updates the secondary ('tracked') indices as well.
With assert non-nil, errors out if the key does not exist already."
- (let* ((data (oref db :data))
+ (let* ((data (oref db data))
(keys (or keys
(apply 'registry-search db spec)))
- (tracked (oref db :tracked)))
+ (tracked (oref db tracked)))
(dolist (key keys)
(let ((entry (gethash key data)))
(defmethod registry-size ((db registry-db))
"Returns the size of the registry-db object THIS.
-This is the key count of the :data slot."
- (hash-table-count (oref db :data)))
+This is the key count of the `data' slot."
+ (hash-table-count (oref db data)))
(defmethod registry-full ((db registry-db))
"Checks if registry-db THIS is full."
(>= (registry-size db)
- (oref db :max-size)))
+ (oref db max-size)))
(defmethod registry-insert ((db registry-db) key entry)
"Insert ENTRY under KEY into the registry-db THIS.
Updates the secondary ('tracked') indices as well.
Errors out if the key exists already."
- (assert (not (gethash key (oref db :data))) nil
+ (assert (not (gethash key (oref db data))) nil
"Key already exists in database")
(assert (not (registry-full db))
"registry max-size limit reached")
;; store the entry
- (puthash key entry (oref db :data))
+ (puthash key entry (oref db data))
;; store the secondary indices
- (dolist (tr (oref db :tracked))
+ (dolist (tr (oref db tracked))
;; for every value in the entry under that key...
(dolist (val (cdr-safe (assq tr entry)))
(let* ((value-keys (registry-lookup-secondary-value db tr val)))
(defmethod registry-reindex ((db registry-db))
"Rebuild the secondary indices of registry-db THIS."
(let ((count 0)
- (expected (* (length (oref db :tracked)) (registry-size db))))
- (dolist (tr (oref db :tracked))
+ (expected (* (length (oref db tracked)) (registry-size db))))
+ (dolist (tr (oref db tracked))
(let (values)
(maphash
(lambda (key v)
(when (and (< 0 expected)
(= 0 (mod count 1000)))
(message "reindexing: %d of %d (%.2f%%)"
- count expected (/ (* 100 count) expected)))
+ count expected (/ (* 100.0 count) expected)))
(dolist (val (cdr-safe (assq tr v)))
(let* ((value-keys (registry-lookup-secondary-value db tr val)))
(push key value-keys)
(registry-lookup-secondary-value db tr val value-keys))))
- (oref db :data))))))
+ (oref db data))))))
(defmethod registry-prune ((db registry-db) &optional sortfunc)
"Prunes the registry-db object DB.
Returns the number of deleted entries."
(let ((size (registry-size db))
- (target-size (- (oref db :max-size)
- (* (oref db :max-size)
- (oref db :prune-factor))))
+ (target-size
+ (floor (- (oref db max-size)
+ (* (oref db max-size)
+ (oref db prune-factor)))))
candidates)
- (if (> size target-size)
+ (if (registry-full db)
(progn
(setq candidates
(registry-collect-prune-candidates
Proposes only entries without the :precious keys, and attempts to
return LIMIT such candidates. If SORTFUNC is provided, sort
entries first and return candidates from beginning of list."
- (let* ((precious (oref db :precious))
+ (let* ((precious (oref db precious))
(precious-p (lambda (entry-key)
(cdr (memq (car entry-key) precious))))
- (data (oref db :data))
+ (data (oref db data))
(candidates (cl-loop for k being the hash-keys of data
using (hash-values v)
when (notany precious-p v)
;; list of entry keys.
(when sortfunc
(setq candidates (sort candidates sortfunc)))
- (delq nil (cl-subseq (mapcar #'car candidates) 0 limit))))
+ (cl-subseq (mapcar #'car candidates) 0 (min limit (length candidates)))))
(provide 'registry)
;;; registry.el ends here