Querying nested MagmaCollections

Brent Pinkney brent at zamail.co.za
Thu Jan 7 15:28:37 UTC 2010


Hi Amir,

Tyically one refactors the design a bit. This is not neccessary, but MagmaCollections
are not light-wight constructs (they support millions of elements and ofer indexing),
so it is good practice to avoid hundreds (or many tens) or MagmaCollections.

Here is how I would implement this: 

	Object
		Author ( name, library )    "#books is a selector, not an ivar"
		Book ( author, title )

I would make the root of the repository a Library:

	Object
		Library ( authors, books )

I would initialize Library as:

	Library >> initialize
		authors := MagmaCollection new 
					addIndex: (MagmaStringIndex attribute: #name );
					yourself.
		books := MagmaCollection new 
					addIndex: (MagmaStringIndex attribute: #authorName );
					addIndex: (MagmaKeywordIndex attribute: #titleWords );
					yourself.
	!

Ok, so now we implement #authorName and #titleWords to satsify the indices:

	Book >> authorName
		^ authorName
	!
	Book >> titleWords
		^ title findTokens: ' '.
	!

So, to add authors and books (this is the 'relational' bit :) ):

	Library >> addAuthor: anAuthor
			anAuthor library: self.		"NB - used by #addBook: later"
			authors add: anAuthor
	!
	Author >> addBook: aBook
			aBook author: self.			"for the #authorName index"
			library books add: aBook	"store in the shared books collection"
	!

> 1.	All books with titles containing the word 'wind'

	library books books where: [ :b | b titleWords = 'wind' ]

> 2.	All writers who have books with titles containing the word 'dragon'

There is no simple query in Magma to perform this classically relational query, 
but is a common solution:

	Author >> = other
		^ name = other name
	!
	Author >> hash
		^ name hash
	!

Then:

	looners := Set new.
	(library books books where: [ :b | b titleWords = 'dragon' ])
		do: [ :b | looners add: b author ],

> Thanks for any help!
Have a look at Lava and Lava-tester for a relational overlay to Magma that provides full SQL query support.

(PS. I am not at my laptop, so this pseudo-code may have the odd syntax error).

Let me know if you need help or advice on other solutions to this problem.

Brent


More information about the Magma mailing list