Oops, fix a scale problem with the RSS size calculation
[beagle.git] / beagled / LabyrinthQueryable / LabyrinthQueryable.cs
blob1175c6a90ec41d0b15e863b2d8d82e9289ae920e
1 //
2 // LabyrithQueryable.cs
3 //
4 // Copyright (C) 2006 Kevin Kubasik <kevin@kubasik.net>
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all 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
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
30 using System.Threading;
31 using System.Text;
33 using Beagle.Daemon;
34 using Beagle.Util;
36 namespace Beagle.Daemon.LabyrinthQueryable {
38 [QueryableFlavor (Name="Labyrinth", Domain=QueryDomain.Local, RequireInotify=false)]
39 public class LabyrinthQueryable : LuceneFileQueryable, IIndexableGenerator {
41 string lab_dir;
43 public LabyrinthQueryable () : base ("LabyrinthIndex")
45 lab_dir = Path.Combine (PathFinder.HomeDir, ".gnome2");
46 lab_dir = Path.Combine (lab_dir, "labyrinth");
49 /////////////////////////////////////////////////
51 private void StartWorker()
53 if (! Directory.Exists (lab_dir)) {
54 GLib.Timeout.Add (60000, new GLib.TimeoutHandler (CheckForExistence));
55 return;
58 Log.Info ("Starting Labyrinth backend");
60 Stopwatch stopwatch = new Stopwatch ();
61 stopwatch.Start ();
65 if (Inotify.Enabled)
66 Inotify.Subscribe (lab_dir, OnInotifyNewNote, Inotify.EventType.CloseWrite | Inotify.EventType.Modify);
68 Scheduler.Task task;
69 task = NewAddTask (this);
70 task.Tag = "Crawling Labyrinth Notes";
71 task.Source = this;
73 ThisScheduler.Add (task);
75 stopwatch.Stop ();
77 Log.Info ("labyrinth backend worker thread done in {0}", stopwatch);
80 public override void Start ()
82 base.Start ();
84 ExceptionHandlingThread.Start (new ThreadStart (StartWorker));
87 /////////////////////////////////////////////////
90 public string StatusName {
91 get { return "LabyrinthQueryable"; }
94 private IEnumerator map_files = null;
96 public void PostFlushHook () { }
98 public bool HasNextIndexable ()
100 if (map_files == null)
101 map_files = DirectoryWalker.GetFileInfos (lab_dir).GetEnumerator ();
103 return map_files.MoveNext ();
106 public Indexable GetNextIndexable ()
108 FileInfo file = (FileInfo) map_files.Current;
110 if (! file.Exists)
111 return null;
113 if (IsUpToDate (file.FullName))
114 return null;
116 Indexable indexable = NoteToIndexable (file);
118 return indexable;
120 private bool CheckForExistence ()
122 if (!Directory.Exists (lab_dir))
123 return true;
125 this.Start ();
127 return false;
129 /////////////////////////////////////////////////
131 private static Indexable NoteToIndexable (FileInfo file)
133 Indexable indexable = new Indexable (UriFu.PathToFileUri(file.FullName));
134 indexable.Timestamp = file.LastWriteTimeUtc;
135 indexable.HitType = "Note";
136 indexable.MimeType = "x-beagle/x-labyrinth-note";
137 indexable.AddProperty (Property.NewUnsearched ("fixme:application","labyrinth"));
139 return indexable;
142 private void IndexNote (FileInfo file, Scheduler.Priority priority)
145 if (! File.Exists (file.FullName))
146 return;
148 if (IsUpToDate (file.FullName))
149 return;
151 Indexable indexable = NoteToIndexable (file);
152 Scheduler.Task task = NewAddTask (indexable);
153 task.Priority = priority;
154 task.SubPriority = 0;
155 ThisScheduler.Add (task);
160 /////////////////////////////////////////////////
163 private void OnInotifyNewNote(Inotify.Watch watch,
164 string path, string subitem, string srcpath,
165 Inotify.EventType type)
167 if (subitem.Length == 0 || (type & Inotify.EventType.IsDirectory) != 0)
168 return;
170 if ( subitem.EndsWith("map"))
171 IndexNote (new FileInfo(Path.Combine (path, subitem)), Scheduler.Priority.Immediate);
174 /////////////////////////////////////////////////