Permalink
Browse files

Added tag caching - still need to come up with some sort of cache policy

on it so file doesn't grow ad-infinitum
  • Loading branch information...
1 parent bbdd640 commit 17e9b8b4a98c66b0228e7343f66189214d80674b @paulcbetts committed Nov 23, 2007
Showing with 70 additions and 5 deletions.
  1. +25 −0 .autotest
  2. +40 −0 lib/library.rb
  3. +1 −2 lib/platform.rb
  4. +3 −2 lib/song.rb
  5. +1 −1 lib/utility.rb
View
@@ -0,0 +1,25 @@
+# -*- ruby -*-
+
+require 'autotest/redgreen'
+require 'autotest/timestamp'
+#require 'autotest/autoupdate'
+#require 'autotest/emacs'
+#require 'autotest/fixtures'
+require 'autotest/growl'
+require 'autotest/heckle'
+#require 'autotest/html_report'
+#require 'autotest/kdenotify'
+#require 'autotest/menu'
+#require 'autotest/migrate'
+#require 'autotest/notify'
+#require 'autotest/pretty'
+#require 'autotest/screen'
+#require 'autotest/shame'
+#require 'autotest/snarl'
+
+#Autotest::AutoUpdate.sleep_time = 60
+#Autotest::AutoUpdate.update_cmd = 'git pull'
+#Autotest::Emacs.client_cmd = 'emacsclient -e'
+Autotest::Heckle.flags << '-t test/**/*.rb'
+#Autotest::Heckle.klasses << 'MyClass'
+#Autotest::Shame.chat_app = :adium
View
@@ -25,6 +25,8 @@
require 'logger'
require 'gettext'
require 'pathname'
+require 'digest/md5'
+require 'zlib'
# Estelle
require 'platform'
@@ -37,6 +39,7 @@ class MusicLibrary < Logger::Application
@taggers = nil
@tag_info = nil
@album_info = nil
+ @md5_index = nil
public
attr_writer :is_soundtrack
@@ -59,13 +62,22 @@ def load(files, progress_rate = 0.05)
load_taggers(DefaultTaggerPath) unless @taggers
return unless @taggers
+ load_cache
+
@tag_info = {}
log INFO, "Processing #{files.size} files..."
count = 0
yield_every = (files.size * progress_rate).to_i
files.each do |current|
count += 1
#log DEBUG, 'Reading %s..' % current
+
+ # First, see if we've already scanned this file before
+ md5sum = Digest::MD5.hexdigest(File.read(current))
+ if (@tag_info[current] = @md5_index[md5sum])
+ #log DEBUG, "Cache hit!"
+ next
+ end
# Check to see if we can load this file
loader = nil
@@ -76,17 +88,45 @@ def load(files, progress_rate = 0.05)
if (@tag_info[current] = loader.song_info(current))
@tag_info[current][:path] = current
+ @tag_info[current][:md5sum] = md5sum
next
end
# Can't be loaded, delete it and move on
@tag_info.delete current
end
+ #save_cache
+
log INFO, "Loaded #{@tag_info.size} files"
log DEBUG, "Exiting load"
end
+ def load_cache(target = nil)
+ infile = (target || File.join(Platform.home_dir, "libcache.yaml.gz"))
+ begin
+ a = nil; Zlib::GzipReader.open(infile) {|x| a = YAML::load(x.read)}
+ throw "Failed!" unless a.is_a? Hash and a.values.all? {|x| x.is_a? Hash}
+ @tag_info = {}; a.keys.each {|x| @tag_info[x] = Song.new(a[x])}
+ rescue
+ # TODO: Worst debugging message evar.
+ log DEBUG, "Couldn't load cache"
+ @tag_info = {}
+ end
+
+ @md5_index = {}
+ @tag_info.values.each do |x|
+ next unless x[:md5sum]
+ @md5_index[x[:md5sum]] = x
+ end
+ end
+
+ def save_cache(target = nil)
+ outfile = (target || File.join(Platform.home_dir, "libcache.yaml.gz"))
+ t = {}; @tag_info.keys.each {|x| t[x] = @tag_info[x].to_hash}
+ Zlib::GzipWriter.open(outfile) { |x| x.puts t.to_yaml() }
+ end
+
def find_soundtracks
@is_soundtrack ||= {}
log DEBUG, "Entering find_soundtracks"
View
@@ -55,8 +55,7 @@ def which(program)
end
def settings_file_path
- # FIXME: Clearly, wrong.
- return home_dir
+ return File.join(home_dir, "settings.yaml")
end
def binary_dir
View
@@ -25,14 +25,15 @@
require 'logger'
require 'gettext'
require 'pathname'
+require 'digest/md5'
include GetText
FeaturingList = ['Featuring', 'Feat.', 'featuring', 'feat.', 'FEATURING', 'FEAT.']
class Song
- def initialize
- @data = {}
+ def initialize(hash = {})
+ @data = hash.clone
end
def get_canonical_artist
View
@@ -118,7 +118,7 @@ def escaped_path(path)
def filelist_from_root(path)
list = []
d = Pathname.new path
- d.find { |x| list << x.to_s }
+ d.find { |x| list << x.to_s if x.file? }
list
end

0 comments on commit 17e9b8b

Please sign in to comment.