comparison implementations/meredith/import->db.lisp @ 286:d22c67dac97d

add minimal backend for Dave Meredith's data Ignore-this: 91608f727967a4c5709bd41634ab9ae2 darcs-hash:20090524193956-16a00-038e6f7cb235dea4e7efcc70c4d1a7bc7fd402a6.gz
author j.forth <j.forth@gold.ac.uk>
date Sun, 24 May 2009 20:39:56 +0100
parents
children c9573d61b1b9
comparison
equal deleted inserted replaced
285:c2e50459efab 286:d22c67dac97d
1 (cl:in-package #:amuse-meredith)
2
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ;; Importing
5
6 (defun make-path (base-path file-prefix file-id file-suffix)
7 (pathname (concatenate 'string
8 base-path
9 file-prefix
10 (princ-to-string file-id)
11 file-suffix)))
12
13 (defun file->list-of-lists (path)
14 (with-open-file (stream path)
15 (loop
16 for row-strings = (split-sequence:split-sequence
17 #\space (read-line stream nil)
18 :remove-empty-subseqs t)
19 while row-strings
20 collect (loop
21 for value in row-strings
22 collect (read-from-string value)
23 into row-objects
24 finally (return row-objects))
25 into results
26 finally (return (cdr results)))))
27
28 (defvar *import-file* (pathname "/tmp/raph-c.txt"))
29
30 (defun write-to-db-file (composition-id datapoints)
31 (with-open-file (stream *import-file*
32 :direction :output
33 :if-exists :supersede
34 :external-format :latin1)
35 (sequence:dosequence (datapoint datapoints)
36 (format stream
37 "~S \\N ~{~S ~}~%"
38 composition-id datapoint)))
39 t)
40
41 (defun import-meredith-composition (event-lists &key description
42 database)
43 "Import a composition into the database. A composition is a list of
44 lists (dataset in the SIA sense), where each internal list (datapoint)
45 represents an event. All datapoints are the same cardinality. The
46 description is a string of up to 255 characters."
47 #.(clsql:locally-enable-sql-reader-syntax)
48 (clsql:with-transaction
49 (:database database)
50 (let (composition-id)
51 (clsql:insert-records :into "meredith_compositions"
52 :attributes '([description])
53 :values (list description)
54 :database database)
55 #.(clsql:locally-disable-sql-reader-syntax)
56 (setf composition-id (clsql-mysql::mysql-insert-id
57 (clsql-mysql::database-mysql-ptr
58 database)))
59 (write-to-db-file composition-id event-lists)
60 (clsql:execute-command
61 (concatenate 'string
62 "LOAD DATA LOCAL INFILE '"
63 (namestring *import-file*)
64 "' INTO TABLE "
65 "meredith_events")
66 :database database)
67 (delete-file *import-file*)
68 (format t "composition added to db, id: ~A~%" composition-id)
69 (make-meredith-composition-identifier composition-id))))
70
71 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72 ;;; Import functions for specific datasets.
73
74 (defun import-bach-inv+sin (&optional (database *amuse-database*))
75 "Imports all of Dave's encodings of Bach's Inventions and Sinfonias,
76 and assigns them all so a unique dataset."
77 (clsql:with-transaction
78 (:database database)
79 (let ((dataset-identifier (make-new-dataset
80 "Bach Inventions and Sinfonias"
81 database)))
82 (import-bach-inventions database dataset-identifier)
83 (import-bach-sinfonias database dataset-identifier))))
84
85 (defun import-bach-inventions (database &optional dataset-identifier)
86 "Imports all of Dave's encodings of Bach's Inventions."
87 (let ((base-path "/home/jamie/Music/meredith-data/20060301raph-c/")
88 (file-prefix "bachrasmussinventio0")
89 (file-suffix "01.raph-c"))
90 (loop for file-id from 772 to 786
91 for composition-order from 1
92 for composition-identifier =
93 (import-meredith-composition
94 (file->list-of-lists
95 (make-path base-path file-prefix file-id file-suffix))
96 :description (concatenate 'string
97 "Bach Invention No."
98 (princ-to-string composition-order)
99 " BWV "
100 (princ-to-string file-id))
101 :database database)
102 do (when dataset-identifier
103 (assign-composition-to-dataset
104 composition-identifier dataset-identifier database)))))
105
106 (defun import-bach-sinfonias (database &optional dataset-identifier)
107 "Imports all of Dave's encodings of Bach's Sinfonias."
108 (let ((base-path "/home/jamie/Music/meredith-data/20060301raph-c/")
109 (file-prefix "bachrasmusssinfonie0")
110 (file-suffix "01.raph-c"))
111 (loop for file-id from 787 to 801
112 for composition-order from 1
113 for composition-identifier =
114 (import-meredith-composition
115 (file->list-of-lists
116 (make-path base-path file-prefix file-id file-suffix))
117 :description (concatenate 'string
118 "Bach Sinfonia No."
119 (princ-to-string composition-order)
120 " BWV "
121 (princ-to-string file-id))
122 :database database)
123 do (when dataset-identifier
124 (assign-composition-to-dataset
125 composition-identifier dataset-identifier database)))))