Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / FileSystemQueryable / FileCrawlTask.cs
blob2f7176bcb07bc0ef0345f92681444c81835efb7c
1 //
2 // FileCrawlTask.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
31 using Beagle.Daemon;
32 using Beagle.Util;
34 namespace Beagle.Daemon.FileSystemQueryable {
36 public class FileCrawlTask : Scheduler.Task {
38 object big_lock = new object ();
39 FileSystemQueryable queryable;
40 DirectoryModel current_dir = null;
41 IIndexableGenerator current_generator = null;
42 bool is_active = false;
44 Scheduler.Hook our_post_hook;
46 public FileCrawlTask (FileSystemQueryable queryable)
48 this.queryable = queryable;
49 this.Tag = "File Crawler";
50 this.Priority = Scheduler.Priority.Generator;
52 this.our_post_hook = new Scheduler.Hook (PostCrawlHook);
55 public bool IsActive {
56 get { lock (big_lock) return is_active; }
59 private void PostCrawlHook ()
61 Logger.Log.Debug ("Done crawling '{0}'", current_dir.FullName);
63 queryable.DoneCrawlingOneDirectory (current_dir);
65 current_generator = null;
66 current_dir = null;
69 override protected void DoTaskReal ()
71 // If our last generator is still doing stuff, just reschedule
72 // and return. This keeps us from generating more tasks until
73 // the last one we started runs to completion.
74 if ((current_generator != null && current_generator.HasNextIndexable ())
75 || current_dir != null) {
76 Reschedule = true;
77 return;
80 lock (big_lock) {
81 is_active = true;
82 current_dir = queryable.GetNextDirectoryToCrawl ();
83 if (current_dir == null) {
84 Logger.Log.Debug ("Done crawling!!!!");
85 is_active = false;
86 return;
90 if (!current_dir.IsAttached) {
91 Reschedule = true;
92 return;
95 if (FileSystemQueryable.Debug)
96 Logger.Log.Debug ("Starting crawl of '{0}'", current_dir.FullName);
98 // Schedule a DirectoryIndexableGenerator
99 // for that directory, and then reschedule ourselves.
100 try {
101 current_generator = new DirectoryIndexableGenerator (queryable, current_dir);
102 } catch (DirectoryNotFoundException ex) {
103 Logger.Log.Debug ("Couldn't crawl '{0}'", current_dir.FullName);
104 current_dir = null;
107 if (current_generator != null) {
108 Scheduler.TaskGroup group;
109 group = Scheduler.NewTaskGroup ("Crawl task group", null, our_post_hook);
111 Scheduler.Task task;
112 task = queryable.NewAddTask (current_generator);
113 task.AddTaskGroup (group);
114 ThisScheduler.Add (task);
117 Reschedule = true;