[elephant-devel] Persistent class slot

Alex Mizrahi killerstorm at newmail.ru
Thu Jun 17 22:19:06 UTC 2010


> The issue I have is: I am using Weblocks + Elephant + Postmodern to 
> display the instances of a class in
> paginated web pages. Weblocks needs to know the total count of the 
> instances to plan the pagination (so it > can display something like "1 of 
> xxx pages" ). It turns out just counting (by map-class) the instances 
> (about
> 5000) will take about 8 seconds (my laptop is not that powerful, but quite 
> decent).

I guess it would be faster with BDB, but it still needs to go through all 
records.

> Counting instances of a class seems to be a very common use case.
> So I hope the above is convincing enough for the need of persistent class 
> slots.
> But I am too inexperienced to know how much work this will require.

 You can do that without persistent class slots -- make a counter class and 
update its value.
Something like this:

(defpclass counter ()
  ((classname :index t :initarg :classname)
   (counter :initform 0)))

Then you can increment it automatically:

(defmethod initialize-instance ((foo bar) &key &allow-other-keys)
  (let ((counter (get-instance-from-value 'counter 'classname 'bar)))
    (incf (slot-value counter 'counter))))

It isn't pretty but it will work.
Moreover, you need to implement this only once, so it doesn't matter how it 
is implemented.

If we implement class slots, they will work in a similar way under the hood.








More information about the elephant-devel mailing list