[elephant-devel] Re: blog example w/ hunchentoot

Ian Eslick eslick at csail.mit.edu
Sun Apr 22 10:37:14 UTC 2007


You can access the class index cursor using:
'make-class-cursor' for all instances and
'make-inverted-cursor' on a specific slot index

You can also use the convenience macros:
'with-inverted-cursor' and 'with-class-cursor'

These will be documented in the new manual, but for now you can look  
at them
in src/elephant/classindex.lisp.

You can rely on get-instances-by-range returning ascending orders,  
but you shouldn't
rely on the ordering of duplicates.

You do make the good point that I should add an option to the map  
operators to return
objects in reverse order.  I'll think about this.

Ian

On Apr 22, 2007, at 5:34 AM, Mac Chan wrote:

> Hi Ian,
>
> On 4/21/07, Ian Eslick <eslick at csail.mit.edu> wrote:
>> This example could be done even more cleanly using the class indexing
>> facilities.  If you feel inclined to experiment with them, this would
>> be a good contribution to improve the example.
>
> Actually it turned out to be an interesting exercise. Please see the
> attached file.
>
> I have some questions, though.
>
>
> * Displaying all the blog entries in descending order
>
> Using the b-tree index, one could iterate through the entries with a
> cursor as shown in the original example code:
>
> (with-btree-cursor (curs *my-blog*)
>  (loop
>   for (m k v) = (multiple-value-list (cursor-first curs))
>   then (multiple-value-list (cursor-next curs))
>   while m
>   collect v))
>
> Now after I replace the explicit index with a class index, I couldn't
> figure out how to use cursor on the class index. Maybe there's an
> undocumented way to do this?
>
> (loop
> for v in
> (sort (get-instances-by-class 'blog-entry) #'> :key #'entry-date)
> collect v)
>
> Now I have to get all the instances from that class. I assumed the
> result returned by get-instances-by-class is just a list of OID's?
> Elephant will only fetch the values when I use the accessor functions
> on the OID handle, right?
>
> Another downside with this approach is that I need to manually sort
> the entries (I guess if my assumption from the previous paragraph is
> true, then I'm really paying the penalty here if there are a lot of
> entries to sort) whereas in the original code it's taken care of by
> the cursor API.
>
>
>
>
> * Displaying all the blog entries for a particular month in descending
> order
>
> (with-btree-cursor (curs (get-index *my-blog* 'month))
>  (loop
>   for (m k v) = (multiple-value-list
>                  (cursor-set curs month-year))
>   then (multiple-value-list (cursor-next-dup curs))
>   while m
>   collect v))
>
> (loop
> for v in
> (multiple-value-bind (start end)
>     (the-month-range month-year)
>   (nreverse (get-instances-by-range 'blog-entry 'date start end)))
> collect v)
>
> In this case both the cursor and the get-instances-by-range API are
> rather easy to use. Unlike get-instances-by-class,
> get-instances-by-range seems to always return the result sorted, is
> that correct?
>
> When I first read the documenation, I couldn't think of a use case for
> cursor-next-dup.  It is really handy as shown in the original example.
>
>
>
>
> * Displaying href links to previous blog entries (month/year)
>
> (loop
> for (m k v) = (multiple-value-list (cursor-first curs))
> then (multiple-value-list (cursor-next-nodup curs))
> while m
> collect v)))
>
> (let ((month-year))
>  (loop
>   for entry in (get-instances-by-class 'blog-entry) do
>   (multiple-value-bind (second minute hour date month year)
>       (decode-universal-time (entry-date entry))
>     (declare (ignore second minute hour date))
>     (pushnew (encode-universal-time 0 0 0 1 month year) month-year)))
>  (sort month-year #'>))
>
> Now the cursor API is much easier to use in this case. Again, since I
> can't figure out a way to use cursor on the class index, I need to go
> through _all_ the entries, collect and sort the date myself.
>
> I believe I'm not using this stuff right, so please show us the  
> way :-)
>
> Regards,
> -- Mac
> <hunchentoot.blog.lisp>
> _______________________________________________
> 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