[elephant-devel] secondary indeces

Gábor Melis mega at hotpop.com
Tue Feb 15 11:35:22 UTC 2005


On Tuesday 23 November 2004 18:29, Ben wrote:
> 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!

For the record, I now use this to manage my indices from 'user space':

(defmacro with-reindexing ((key btree) &body body)
  "Force update of indices by removing and readding KEY."
  (with-gensyms (value k b)
    `(with-transaction ()
       (let* ((,k ,key)
              (,b ,btree)
              (,value (get-value ,k ,b)))
         (remove-kv ,k ,b)
         (unwind-protect (progn , at body)
           (setf (get-value ,k ,b) ,value))))))

In the scenario with a user class having an activation-code slot that is the 
key of the secondary index it works like this:

(defmethod (setf user-activation-code) :around (value (user user))
  (with-reindexed-user (user)
    (call-next-method)))

With-reindexed-user is just a convenience macro on top of with-reindexing that 
looks up the key and btree for the user object.

Not a pretty sight, but it is the best I could come up with and it is buried 
in the bowels of the business model where no client code ventures.

Gábor



More information about the elephant-devel mailing list