Commits

Paul Ruane committed c509b67

Improved 'untag' performance. (Still scope to improve perforamnce of 'delete' command.)

  • Participants
  • Parent commits 548433e

Comments (0)

Files changed (3)

src/tmsu/storage/file.go

 	return storage.Db.DeleteFile(fileId)
 }
 
+// Deletes a file if it is untagged
+func (storage *Storage) DeleteFileIfUntagged(fileId uint) error {
+	count, err := storage.FileTagCountByFileId(fileId, true)
+	if err != nil {
+		return err
+	}
+	if count == 0 {
+		if err := storage.DeleteFile(fileId); err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
 // Deletes all untagged files from the database.
 func (storage *Storage) DeleteUntaggedFiles() error {
 	return storage.Db.DeleteUntaggedFiles()

src/tmsu/storage/filetag.go

 }
 
 // Retrieves the count of file tags for the specified file.
-func (storage *Storage) FileTagCountByFileId(fileId uint) (uint, error) {
-	return storage.Db.FileTagCountByFileId(fileId)
+func (storage *Storage) FileTagCountByFileId(fileId uint, explicitOnly bool) (uint, error) {
+	if explicitOnly {
+		return storage.Db.FileTagCountByFileId(fileId)
+	}
+
+	fileTags, err := storage.FileTagsByFileId(fileId, false)
+	if err != nil {
+		return 0, err
+	}
+
+	return uint(len(fileTags)), err
 }
 
 // Retrieves the count of file tags for the specified tag.
 func (storage *Storage) FileTagCountByTagId(tagId uint) (uint, error) {
+	//TODO add explicit only
 	return storage.Db.FileTagCountByTagId(tagId)
 }
 
 // Retrieves the file tags with the specified tag ID.
 func (storage *Storage) FileTagsByTagId(tagId uint) (entities.FileTags, error) {
+	//TODO add explicit only
 	return storage.Db.FileTagsByTagId(tagId)
 }
 
 		return err
 	}
 
-	if err := storage.DeleteUntaggedFiles(); err != nil {
+	if err := storage.DeleteFileIfUntagged(fileId); err != nil {
 		return err
 	}
 
-	if err := storage.DeleteUnusedValues(); err != nil {
+	if err := storage.DeleteValueIfUnused(valueId); err != nil {
 		return err
 	}
 
 		return err
 	}
 
-	if err := storage.DeleteUntaggedFiles(); err != nil {
+	if err := storage.DeleteFileIfUntagged(fileId); err != nil {
 		return err
 	}
 
+	//TODO look only at the values that were in the filetags removed
 	if err := storage.DeleteUnusedValues(); err != nil {
 		return err
 	}
 	return storage.Db.CopyFileTags(sourceTagId, destTagId)
 }
 
-// helpers
+// unexported
 
 func containsFileTag(fileTags entities.FileTags, fileTag entities.FileTag) bool {
 	for _, ft := range fileTags {

src/tmsu/storage/value.go

 
 // Deletes a value.
 func (storage *Storage) DeleteValue(valueId uint) error {
-	if err := storage.Db.DeleteValue(valueId); err != nil {
+	//TODO delete file tags that use the value
+
+	return storage.Db.DeleteValue(valueId)
+}
+
+// Deletes the value if it is unused.
+func (storage *Storage) DeleteValueIfUnused(valueId uint) error {
+	if valueId == 0 {
+		return nil
+	}
+
+	count, err := storage.FileTagCountByValueId(valueId)
+	if err != nil {
 		return err
 	}
+	if count == 0 {
+		if err := storage.DeleteValue(valueId); err != nil {
+			return err
+		}
+	}
 
-	return storage.DeleteUnusedValues()
+	return nil
 }
 
 // Deletes unused values.