View Javadoc

1   package org.musicontroller.repair;
2   
3   import java.io.File;
4   import java.util.List;
5   
6   import org.apache.log4j.Logger;
7   import org.musicontroller.core.Link;
8   import org.musicontroller.core.Song;
9   import org.musicontroller.dao.Dao;
10  import org.varienaja.util.DenseSet;
11  
12  public class ConsistencyChecker {
13  	private static final Logger log = Logger.getLogger(ConsistencyChecker.class);
14  	private static DenseSet _brokenSongIDs = new DenseSet();
15  
16  	/**
17  	 * Checks whether all Songs in the Dao have a corresponding
18  	 * working link on the filesystem.  
19  	 * @param dao
20  	 * @return A DenseSet, containing all song-ids in error.
21  	 */
22  	public static synchronized DenseSet checkLinks(Dao dao) {
23  		DenseSet errors = new DenseSet();
24  		List<Long> songIDs = dao.getSongIds();
25  		int count = songIDs.size();
26  		for (Long songid : songIDs) {
27  			Song song = dao.getSongById(songid);
28  			Link link = song.getLink();
29  			if (link==null || !(link.getFile()).exists()) {
30  				errors.add(songid);
31  				log.debug("Incorrect filesystem link for song: "+song.getName());
32  			}
33  			dao.evict(link); //We won't use this objects anymore in this session, so we get rid of them
34  			dao.evict(song); //in order to free memory and avoid out-of-memory exceptions.
35  			if (log.isDebugEnabled()) {
36  				count--;
37  				if (count % 250 == 0) {
38  					log.debug("Songlinks to check: "+count);
39  				}
40  			}
41  		}
42  		_brokenSongIDs = errors;
43  		return errors;
44  	}
45  	
46  	/**
47  	 * This getter only returns correct data after checkLinks(Dao dao) has been run.
48  	 * @return All song-ids that have no correct filesystem-link.
49  	 */
50  	public static DenseSet getBrokenSongIDs() {
51  		return _brokenSongIDs;
52  	}
53  }