diff options
author | Björn Stenberg <bjorn@haxx.se> | 2007-01-08 23:53:00 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2007-01-08 23:53:00 +0000 |
commit | 7039a05147b8bbfc829babea1c65bd436450b505 (patch) | |
tree | 4ba555eb84ed97b72b0575034d5b0530a393713e /songdbj/TagDatabase.java | |
parent | 6d4c19707ef95942e323cbdc89fbbfdbe45e7cc5 (diff) | |
download | rockbox-7039a05147b8bbfc829babea1c65bd436450b505.tar.gz rockbox-7039a05147b8bbfc829babea1c65bd436450b505.zip |
Splitting out songdbj
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@11953 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'songdbj/TagDatabase.java')
-rw-r--r-- | songdbj/TagDatabase.java | 377 |
1 files changed, 0 insertions, 377 deletions
diff --git a/songdbj/TagDatabase.java b/songdbj/TagDatabase.java deleted file mode 100644 index 36c2c09f37..0000000000 --- a/songdbj/TagDatabase.java +++ /dev/null | |||
@@ -1,377 +0,0 @@ | |||
1 | import java.util.*; | ||
2 | import java.io.*; | ||
3 | import java.lang.reflect.Array; | ||
4 | |||
5 | /* | ||
6 | TreeSet for runtimedatabase with entry hash used in compareto | ||
7 | fix commandline interface. | ||
8 | */ | ||
9 | |||
10 | public class TagDatabase { | ||
11 | protected static TagDatabase instance=null; | ||
12 | protected TreeMap songs; | ||
13 | protected TreeMap files; | ||
14 | protected TreeMap filehashes; | ||
15 | protected TreeMap albums; | ||
16 | protected TreeMap artists; | ||
17 | protected int artiststart,albumstart,songstart,filestart; | ||
18 | protected int artistcount,albumcount,songcount,filecount; | ||
19 | public int artistlen,albumlen,songlen,genrelen,filelen,songarraylen,albumarraylen; | ||
20 | public String strip,add; | ||
21 | public boolean haveOldDatabase,dirisalbum,dirisalbumname,showduplicates; | ||
22 | protected Vector sortedsongs,sortedfiles,sortedalbums,sortedartists; | ||
23 | |||
24 | protected TagDatabase() { | ||
25 | songs=new TreeMap(); | ||
26 | files=new TreeMap(); | ||
27 | filehashes=new TreeMap(); | ||
28 | albums=new TreeMap(); | ||
29 | artists=new TreeMap(); | ||
30 | strip=null; | ||
31 | add=null; | ||
32 | haveOldDatabase=false; | ||
33 | dirisalbum=false; | ||
34 | dirisalbumname=true; | ||
35 | showduplicates=true; | ||
36 | } | ||
37 | |||
38 | public static TagDatabase getInstance() { | ||
39 | if(instance==null) | ||
40 | instance=new TagDatabase(); | ||
41 | return instance; | ||
42 | } | ||
43 | |||
44 | public void removeFileEntry(File file) { | ||
45 | String key = file.getAbsolutePath(); | ||
46 | files.remove(key); | ||
47 | } | ||
48 | |||
49 | public FileEntry getFileEntry(File file) throws FileNotFoundException, IOException { | ||
50 | String key = file.getAbsolutePath(); | ||
51 | if(!files.containsKey(key)) { | ||
52 | FileEntry f = new FileEntry(file); | ||
53 | files.put(key,f); | ||
54 | return f; | ||
55 | } | ||
56 | else | ||
57 | return (FileEntry)files.get(key); | ||
58 | } | ||
59 | |||
60 | public ArtistEntry getArtistEntry(String name) { | ||
61 | String key = name.toLowerCase(); | ||
62 | if(!artists.containsKey(key)) { | ||
63 | ArtistEntry a = new ArtistEntry(name); | ||
64 | artists.put(key,a); | ||
65 | return a; | ||
66 | } | ||
67 | else | ||
68 | return (ArtistEntry)artists.get(key); | ||
69 | } | ||
70 | |||
71 | public String getAlbumKey(String name, String directory) { | ||
72 | if(dirisalbum) | ||
73 | return directory; | ||
74 | else | ||
75 | return name.toLowerCase()+"___"+directory; | ||
76 | } | ||
77 | |||
78 | public AlbumEntry getAlbumEntry(String name,String directory) { | ||
79 | String key = getAlbumKey(name,directory); | ||
80 | if(!albums.containsKey(key)) { | ||
81 | AlbumEntry a = new AlbumEntry(name); | ||
82 | albums.put(key,a); | ||
83 | return a; | ||
84 | } | ||
85 | else | ||
86 | return (AlbumEntry)albums.get(key); | ||
87 | } | ||
88 | |||
89 | public void removeSongEntry(FileEntry file) { | ||
90 | String key = file.getFilename(); | ||
91 | songs.remove(key); | ||
92 | file.setSongEntry(null); | ||
93 | } | ||
94 | |||
95 | public SongEntry getSongEntry(FileEntry file) { | ||
96 | String key = file.getFilename(); | ||
97 | if(!songs.containsKey(key)) { | ||
98 | SongEntry s = new SongEntry(file); | ||
99 | songs.put(key,s); | ||
100 | return s; | ||
101 | } | ||
102 | else | ||
103 | return (SongEntry)songs.get(key); | ||
104 | } | ||
105 | |||
106 | private class SongFilter implements FileFilter { | ||
107 | public boolean accept(File f) { | ||
108 | if(f.isDirectory()) // always accept directories. | ||
109 | return true; | ||
110 | String name=f.getName(); | ||
111 | return name.endsWith(".mp3")||name.endsWith(".ogg"); | ||
112 | } | ||
113 | } | ||
114 | |||
115 | public void add(File f) { | ||
116 | if(!f.isDirectory()) { | ||
117 | if(f.isFile()) { | ||
118 | addSong(f); | ||
119 | } | ||
120 | } | ||
121 | else { | ||
122 | File[] files = f.listFiles(new SongFilter()); | ||
123 | int length=Array.getLength(files); | ||
124 | System.out.println(FileEntry.convertPath(f.getAbsolutePath())); | ||
125 | for(int i=0;i<length;i++) { | ||
126 | add(files[i]); | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | |||
131 | protected FileEntry addSong(File f) { | ||
132 | FileEntry file = null; | ||
133 | try { | ||
134 | file = getFileEntry(f); | ||
135 | } | ||
136 | catch(Exception e) { | ||
137 | return null; | ||
138 | } | ||
139 | SongEntry song = getSongEntry(file); | ||
140 | if(!song.gotTagInfo()) { | ||
141 | removeSongEntry(file); | ||
142 | return null; | ||
143 | } | ||
144 | ArtistEntry artist = getArtistEntry(song.getArtistTag()); | ||
145 | AlbumEntry album = getAlbumEntry(song.getAlbumTag(),f.getParent()); | ||
146 | album.setArtist(artist); | ||
147 | album.addSong(song); | ||
148 | return file; | ||
149 | } | ||
150 | |||
151 | protected int align(int len) { | ||
152 | while((len&3)!=0) len++; | ||
153 | return len; | ||
154 | } | ||
155 | |||
156 | protected void calcLimits() { | ||
157 | ArtistEntry longartist=null,longalbumarray=null; | ||
158 | AlbumEntry longalbum=null, longsongarray=null; | ||
159 | SongEntry longsong=null,longgenre=null; | ||
160 | FileEntry longfile=null; | ||
161 | Iterator i; | ||
162 | artistlen=0; | ||
163 | albumarraylen=0; | ||
164 | i=sortedartists.iterator(); | ||
165 | while(i.hasNext()) { | ||
166 | ArtistEntry artist = (ArtistEntry) i.next(); | ||
167 | int length=artist.getName().length(); | ||
168 | int albumcount=artist.size(); | ||
169 | if(length > artistlen) { | ||
170 | artistlen=align(length); | ||
171 | longartist=artist; | ||
172 | } | ||
173 | if(albumcount> albumarraylen) { | ||
174 | albumarraylen=albumcount; | ||
175 | longalbumarray=artist; | ||
176 | } | ||
177 | } | ||
178 | artistcount=sortedartists.size(); | ||
179 | if(longartist!=null) | ||
180 | System.out.println("Artist with longest name ("+artistlen+") :"+longartist.getName()); | ||
181 | if(longalbumarray!=null) | ||
182 | System.out.println("Artist with most albums ("+albumarraylen+") :"+longalbumarray.getName()); | ||
183 | albumlen=0; | ||
184 | songarraylen=0; | ||
185 | i=sortedalbums.iterator(); | ||
186 | while(i.hasNext()) { | ||
187 | AlbumEntry album = (AlbumEntry) i.next(); | ||
188 | int length=album.getName().length(); | ||
189 | int songcount=album.size(); | ||
190 | if(length > albumlen) { | ||
191 | albumlen=align(length); | ||
192 | longalbum=album; | ||
193 | } | ||
194 | if(songcount> songarraylen) { | ||
195 | songarraylen=songcount; | ||
196 | longsongarray=album; | ||
197 | } | ||
198 | } | ||
199 | albumcount=sortedalbums.size(); | ||
200 | if(longalbum!=null) | ||
201 | System.out.println("Album with longest name ("+albumlen+") :"+longalbum.getName()); | ||
202 | if(longsongarray!=null) | ||
203 | System.out.println("Album with most songs ("+songarraylen+") :"+longsongarray.getName()); | ||
204 | filelen=0; | ||
205 | i=sortedfiles.iterator(); | ||
206 | while(i.hasNext()) { | ||
207 | FileEntry file = (FileEntry) i.next(); | ||
208 | int length=file.getFilename().length(); | ||
209 | if(length> filelen) { | ||
210 | filelen=align(length); | ||
211 | longfile=file; | ||
212 | } | ||
213 | } | ||
214 | filecount=sortedfiles.size(); | ||
215 | if(longfile!=null) | ||
216 | System.out.println("File with longest filename ("+filelen+") :"+longfile.getFilename()); | ||
217 | songlen=0; | ||
218 | genrelen=0; | ||
219 | i=sortedsongs.iterator(); | ||
220 | while(i.hasNext()) { | ||
221 | SongEntry song = (SongEntry) i.next(); | ||
222 | int tlength=song.getName().length(); | ||
223 | int glength=song.getGenreTag().length(); | ||
224 | if(tlength> songlen) { | ||
225 | songlen=align(tlength); | ||
226 | longsong=song; | ||
227 | } | ||
228 | if(glength> genrelen) { | ||
229 | genrelen=align(glength); | ||
230 | longgenre=song; | ||
231 | } | ||
232 | } | ||
233 | songcount=sortedsongs.size(); | ||
234 | if(longsong!=null) | ||
235 | System.out.println("Song with longest name ("+songlen+") :"+longsong.getName()); | ||
236 | if(longsong!=null) | ||
237 | System.out.println("Song with longest genre ("+genrelen+") :"+longgenre.getGenreTag()); | ||
238 | System.out.println("Artistcount: "+artistcount); | ||
239 | System.out.println("Albumcount : "+albumcount); | ||
240 | System.out.println("Songcount : "+songcount); | ||
241 | System.out.println("Filecount : "+filecount); | ||
242 | artiststart=68; | ||
243 | albumstart=artiststart+artistcount*ArtistEntry.entrySize(); | ||
244 | songstart=albumstart+albumcount*AlbumEntry.entrySize(); | ||
245 | filestart=songstart+songcount*SongEntry.entrySize(); | ||
246 | } | ||
247 | |||
248 | protected void calcOffsets() { | ||
249 | Iterator i; | ||
250 | int offset=artiststart; | ||
251 | i=sortedartists.iterator(); | ||
252 | while(i.hasNext()) { | ||
253 | Entry e = (Entry) i.next(); | ||
254 | e.setOffset(offset); | ||
255 | offset+=ArtistEntry.entrySize(); | ||
256 | } | ||
257 | // assert(offset==albumstart); | ||
258 | i=sortedalbums.iterator(); | ||
259 | while(i.hasNext()) { | ||
260 | Entry e = (Entry) i.next(); | ||
261 | e.setOffset(offset); | ||
262 | offset+=AlbumEntry.entrySize(); | ||
263 | } | ||
264 | // assert(offset==songstart); | ||
265 | i=sortedsongs.iterator(); | ||
266 | while(i.hasNext()) { | ||
267 | Entry e = (Entry) i.next(); | ||
268 | e.setOffset(offset); | ||
269 | offset+=SongEntry.entrySize(); | ||
270 | } | ||
271 | // assert(offset==filestart); | ||
272 | i=sortedfiles.iterator(); | ||
273 | while(i.hasNext()) { | ||
274 | Entry e = (Entry) i.next(); | ||
275 | e.setOffset(offset); | ||
276 | offset+=FileEntry.entrySize(); | ||
277 | } | ||
278 | } | ||
279 | |||
280 | protected void calcHashes() { | ||
281 | Iterator i; | ||
282 | i=sortedfiles.iterator(); | ||
283 | while(i.hasNext()) { | ||
284 | FileEntry file = (FileEntry) i.next(); | ||
285 | Integer key = new Integer(file.getHash()); | ||
286 | if(!filehashes.containsKey(key)) | ||
287 | filehashes.put(key,file); | ||
288 | else { | ||
289 | System.out.println("Duplicate hash:"); | ||
290 | System.out.println(((FileEntry)filehashes.get(key)).getFilename()); | ||
291 | System.out.println(file.getFilename()); | ||
292 | } | ||
293 | } | ||
294 | } | ||
295 | |||
296 | protected void writeHeader(DataOutputStream w) throws IOException { | ||
297 | w.write('R'); | ||
298 | w.write('D'); | ||
299 | w.write('B'); | ||
300 | w.write(0x3); | ||
301 | w.writeInt(artiststart); | ||
302 | w.writeInt(albumstart); | ||
303 | w.writeInt(songstart); | ||
304 | w.writeInt(filestart); | ||
305 | w.writeInt(artistcount); | ||
306 | w.writeInt(albumcount); | ||
307 | w.writeInt(songcount); | ||
308 | w.writeInt(filecount); | ||
309 | w.writeInt(artistlen); | ||
310 | w.writeInt(albumlen); | ||
311 | w.writeInt(songlen); | ||
312 | w.writeInt(genrelen); | ||
313 | w.writeInt(filelen); | ||
314 | w.writeInt(songarraylen); | ||
315 | w.writeInt(albumarraylen); | ||
316 | w.writeInt(RuntimeDatabase.getInstance().isDirty()); | ||
317 | } | ||
318 | |||
319 | public void prepareWrite() { | ||
320 | System.out.println("Sorting artists.."); | ||
321 | sortedartists=new Vector(); | ||
322 | sortedartists.addAll(artists.values()); | ||
323 | Collections.sort(sortedartists); | ||
324 | System.out.println("Sorting albums.."); | ||
325 | sortedalbums=new Vector(); | ||
326 | sortedalbums.addAll(albums.values()); | ||
327 | Collections.sort(sortedalbums); | ||
328 | System.out.println("Sorting songs.."); | ||
329 | sortedsongs=new Vector(); | ||
330 | sortedsongs.addAll(songs.values()); | ||
331 | Collections.sort(sortedsongs); | ||
332 | System.out.println("Sorting files.."); | ||
333 | sortedfiles=new Vector(); | ||
334 | sortedfiles.addAll(files.values()); | ||
335 | Collections.sort(sortedfiles); | ||
336 | System.out.println("Calculating tag database limits.."); | ||
337 | calcLimits(); | ||
338 | System.out.println("Calculating tag database offsets.."); | ||
339 | calcOffsets(); | ||
340 | if(showduplicates) { | ||
341 | System.out.println("Comparing file hashes.."); | ||
342 | calcHashes(); | ||
343 | } | ||
344 | } | ||
345 | |||
346 | public void writeDatabase(File f) throws IOException { | ||
347 | int x; | ||
348 | Iterator i; | ||
349 | DataOutputStream w = new DataOutputStream(new FileOutputStream(f)); | ||
350 | System.out.println("Writing tag database.."); | ||
351 | writeHeader(w); | ||
352 | |||
353 | i=sortedartists.iterator(); | ||
354 | while(i.hasNext()) { | ||
355 | Entry e = (Entry) i.next(); | ||
356 | e.write(w); | ||
357 | } | ||
358 | i=sortedalbums.iterator(); | ||
359 | while(i.hasNext()) { | ||
360 | Entry e = (Entry) i.next(); | ||
361 | e.write(w); | ||
362 | } | ||
363 | i=sortedsongs.iterator(); | ||
364 | while(i.hasNext()) { | ||
365 | Entry e = (Entry) i.next(); | ||
366 | e.write(w); | ||
367 | } | ||
368 | i=sortedfiles.iterator(); | ||
369 | while(i.hasNext()) { | ||
370 | Entry e = (Entry) i.next(); | ||
371 | e.write(w); | ||
372 | } | ||
373 | // done... | ||
374 | w.flush(); | ||
375 | w.close(); | ||
376 | } | ||
377 | } \ No newline at end of file | ||