[elephant-devel] Newbie question: Index on collections
Ian Eslick
eslick at media.mit.edu
Mon Mar 17 09:47:42 UTC 2008
The pset is probably your best bet. Other collection objects have
more performance impact. With a pset you can ask:
is X a friend of Y? (find-item X (friends Y)) => X | nil
all people that X is friends of:
(remove-nulls
(map-class 'people (lambda (y)
(when (find-item X (friends Y))
Y))
:collect t))
This requires walking all person objects. For larger DB's, you can
build your own 'many-to-many' relation table or waiting for the next
release which should do this for you.
(defpclass friend-relation ()
((first :accessor first :initarg :first :index t)
(second :accessor second :initarg :second :index t)))
(defmethod add-friend-relation (x y)
(make-instance 'friend-relation :first x :second y))
(defmethod friends-of (x)
(union (mapcar #'second (get-instances-by-slot 'friend-relation
'first x))
(mapcar #'first (get-instances-by-slot 'friend-relation
'second x))))
Of course there are ways to do this more efficiently, but I think this
is the idea.
Ian
On Mar 16, 2008, at 11:06 PM, Marc wrote:
> Hello!
>
> I fear that this may be a rather stupid newbie question, but neither
> from the documentation nor from the mailing list posts I got an idea
> as to the best practice for the following type of searches. Let's
> use an example akin to the one in the tutorial:
>
> (defpclass person ()
> ((name :accessor name :index t)
> (friends :accessor friends)) ;collection with names (say as strings)
> of the friends of this person
> )
>
> Given a similar structure, what is the recommended way to handle
> queries of the type "find all persons that X is a friend of?" (i.e.
> queries on individual entries in slots that contain collections)?
> What would be the best data structure for the friends slot, assuming
> that the collection remains fairly stable over time (psets, lists or
> other collections)?
>
> Thanks in advance for any hint!
>
> Best regards,
>
> Marc
>
>
>
>
> _______________________________________________
> 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