[elephant-devel] Re: blog example w/ hunchentoot
Mac Chan
emailmac at gmail.com
Sun Apr 22 09:34:07 UTC 2007
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hunchentoot.blog.lisp
Type: application/octet-stream
Size: 15077 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/elephant-devel/attachments/20070422/11176af4/attachment.obj>
More information about the elephant-devel
mailing list