This is important because STRING<
and STRING=
will signal an error if called on NIL
; it's better to catch bad values before they go into the table rather than when you try to use them.[293]
Another column type you'll need for the MP3 database is an interned-string
whose values are interned as discussed previously. Since you need a hash table in which to intern values, you should define a subclass of column
, interned-values-column
, that adds a slot whose value is the hash table you use to intern.
To implement the actual interning, you'll also need to provide an :initform
for value- normalizer
of a function that interns the value in the column's interned-values
hash table. And because one of the main reasons to intern values is to allow you to use EQL
as the equality predicate, you should also add an :initform
for the equality-predicate
of #'eql
.
(defclass interned-values-column (column)
((interned-values
:reader interned-values
:initform (make-hash-table :test #'equal))
(equality-predicate :initform #'eql)
(value-normalizer :initform #'intern-for-column)))
(defun intern-for-column (value column)
(let ((hash (interned-values column)))
(or (gethash (not-nullable value column) hash)
(setf (gethash value hash) value))))
You can then define a make-column
method specialized on the name interned- string
that returns an instance of interned-values-column
.
(defmethod make-column (name (type (eql 'interned-string)) &optional default-value)
(make-instance
'interned-values-column
:name name
:comparator #'string<
:default-value default-value))
With these methods defined on make-column
, you can now define a function, make- schema
, that builds a list of column
objects from a list of column specifications consisting of a column name, a column type name, and, optionally, a default value.
(defun make-schema (spec)
(mapcar #'(lambda (column-spec) (apply #'make-column column-spec)) spec))
For instance, you can define the schema for the table you'll use to store data extracted from MP3s like this:
(defparameter *mp3-schema*
(make-schema
'((:file string)
(:genre interned-string 'Unknown')
(:artist interned-string 'Unknown')
(:album interned-string 'Unknown')
(:song string)
(:track number 0)
(:year number 0)
(:id3-size number))))
To make an actual table for holding information about MP3s, you pass *mp3-schema*
as the :schema
initarg to MAKE-INSTANCE
.
(defparameter *mp3s* (make-instance 'table :schema *mp3-schema*))
Now you're ready to define your first table operation, insert-row
, which takes a plist of names and values and a table and adds a row to the table containing the given values. The bulk of the work is done in a helper function, normalize-row
, that builds a plist with a defaulted, normalized value for each column, using the values from names-and-values
if available and the default- value
for the column if not.
(defun insert-row (names-and-values table)
(vector-push-extend (normalize-row names-and-values (schema table)) (rows table)))
(defun normalize-row (names-and-values schema)
(loop
for column in schema
for name = (name column)
for value = (or (getf names-and-values name) (default-value column))
collect name
collect (normalize-for-column value column)))
It's worth defining a separate helper function, normalize-for-column
, that takes a value and a column
object and returns the normalized value because you'll need to perform the same normalization on query arguments.
(defun normalize-for-column (value column)
(funcall (value-normalizer column) value column))
Now you're ready to combine this database code with code from previous chapters to build a database of data extracted from MP3 files. You can define a function, file->row
, that uses read- id3
from the ID3v2 library to extract an ID3 tag from a file and turns it into a plist that you can pass to insert-row
.
(defun file->row (file)
(let ((id3 (read-id3 file)))
(list
:file (namestring (truename file))
:genre (translated-genre id3)
:artist (artist id3)
:album (album id3)
:song (song id3)
:track (parse-track (track id3))
:year (parse-year (year id3))
:id3-size (size id3))))
You don't have to worry about normalizing the values since insert-row
takes care of that for you. You do, however, have to convert the string values returned by the track
and year
into numbers. The track number in an ID3 tag is sometimes stored as the ASCII representation of the track number and sometimes as a number followed by a slash followed by the total number of tracks on the album. Since you care only about the actual track number, you should use the :end
argument to PARSE-INTEGER
to specify that it should