[elephant-devel] secondary indeces

Gabor Melis mega at hotpop.com
Tue Nov 23 10:47:36 UTC 2004


Somewhat naïvely I set up a secondary index on a slot on expecting it to 
behave as in sql, i.e. change automatically when the slot value changes. 
Well, it didn't. In hindsight, it is really a stretch to expect elephant to 
that, but it would be very useful, because secondary indeces are often 
mutable.

The way I'd go about it to write an after method for the slot writer that 
forces an update of the secondary index of the object, but 
(setf get-value) (value key (bt btree-index)) signals an error.

Gábor


(use-package :ele)

(defclass x ()
  ((key :accessor key :initarg :key)
   (index :accessor index :initarg :index))
  (:metaclass persistent-metaclass))

(defun index-it (secondary-db primary-key x)
  (declare (ignore secondary-db primary-key))
  (values t (index x)))

(defparameter *x*
  (make-instance 'x :key 1 :index "a"))

(defparameter *primary*
  (add-to-root "primary" (make-instance 'indexed-btree)))

(defparameter *secondary*
  (add-to-root "secondary" (add-index *primary* :index-name 'index
                                      :key-form 'index-it
                                      :populate t)))

(defun find-by-key (key)
  (get-value key *primary*))

(defun find-by-index (index)
  (get-value index *secondary*))

(setf (get-value 1 *primary*) *x*)
(assert (eq *x* (find-by-key 1)))
(assert (eq *x* (find-by-index "a")))
(setf (index *x*) "b")
;; These fail:
(assert (eq *x* (find-by-index "b")))
(assert (null (find-by-index "a")))




More information about the elephant-devel mailing list