[elephant-devel] secondary indeces

Ben ben at medianstrip.net
Tue Nov 23 17:29:51 UTC 2004


i don't know how to handle this in any general fashion, because
objects don't know when they are being stored in a btree with a
secondary index.  it's great you bring up this point, i'll definitely
think about how to do it.  one might consider registering mutation
functions on object insert which would update secondary indices.
however it seems nigh impossible to do this right.  a persisted object
may be loaded without the container btree loaded, in which case
elephant has no idea that some (not loaded) secondary index needs to
be updated.  i will look into this issue.

if you want to mutate the secondary index by hand, i think on the
store-controller object there are two slots which are handles to the
secondary index.  one is opened as a secondary index, the other opened
as a raw btree.  you can update the raw one manually, but be careful!

B

On Tue, 23 Nov 2004, Gabor Melis wrote:

> 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")))
>
> _______________________________________________
> elephant-devel site list
> elephant-devel at common-lisp.net
> http://common-lisp.net/mailman/listinfo/elephant-devel
>


More information about the elephant-devel mailing list