|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.IO; |
| 4 | +using Lucene.Net.Analysis; |
| 5 | +using Lucene.Net.Index; |
| 6 | +using Lucene.Net.Documents; |
| 7 | +using Lucene.Net.QueryParsers; |
| 8 | +using Lucene.Net.Search; |
| 9 | +using System.Runtime.CompilerServices; |
| 10 | + |
| 11 | +namespace moNotationalVelocity |
| 12 | +{ |
| 13 | + public class FilesystemNotesStore : NotesStore |
| 14 | + { |
| 15 | + |
| 16 | + ArrayList notes = new ArrayList (); |
| 17 | + string noteStorePath; |
| 18 | + Lucene.Net.Store.Directory lucIdx; |
| 19 | + Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer (); |
| 20 | + |
| 21 | + public FilesystemNotesStore (string path) |
| 22 | + { |
| 23 | + this.noteStorePath = path; |
| 24 | + |
| 25 | + |
| 26 | + lucIdx = new Lucene.Net.Store.RAMDirectory (); |
| 27 | + IndexWriter writer = new IndexWriter (lucIdx, analyzer, true); |
| 28 | + |
| 29 | + |
| 30 | + DirectoryInfo di = new DirectoryInfo (noteStorePath); |
| 31 | + FileInfo[] rgFiles = di.GetFiles ("*.txt"); |
| 32 | + foreach (FileInfo fi in rgFiles) { |
| 33 | + |
| 34 | + string noteTitle = Path.GetFileNameWithoutExtension (fi.FullName); |
| 35 | + string noteLastMod = fi.LastAccessTime.ToShortDateString (); |
| 36 | + |
| 37 | + notes.Add (new Note (noteTitle, noteLastMod)); |
| 38 | + Document doc = new Document (); |
| 39 | + doc.Add (new Field ("title", noteTitle, true, true, true)); |
| 40 | + doc.Add (new Field ("lastmod", noteLastMod, true, true, true)); |
| 41 | + string content = getNoteContent (noteTitle); |
| 42 | + if (content != null && content.Length > 0) |
| 43 | + doc.Add (new Field ("content", content, false, true, true, true)); |
| 44 | + writer.AddDocument (doc); |
| 45 | + } |
| 46 | + |
| 47 | + writer.Optimize (); |
| 48 | + writer.Close (); |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + public ArrayList getAllNotes () |
| 53 | + { |
| 54 | + return notes; |
| 55 | + } |
| 56 | + |
| 57 | + public ArrayList getNotesMatchingTitle (string search) |
| 58 | + { |
| 59 | + |
| 60 | + ArrayList snotes = new ArrayList (); |
| 61 | + |
| 62 | + try { |
| 63 | + QueryParser parser = new QueryParser ("title", analyzer); |
| 64 | + |
| 65 | + string lucsearch = search + "*^4" + " content:" + search + "*"; |
| 66 | + |
| 67 | + Query query = parser.Parse (lucsearch); |
| 68 | + IndexSearcher searcher = new IndexSearcher (lucIdx); |
| 69 | + Hits hits = searcher.Search (query); |
| 70 | + |
| 71 | + int results = hits.Length (); |
| 72 | + Console.WriteLine ("Found {0} results", results); |
| 73 | + for (int i = 0; i < results; i++) { |
| 74 | + Document doc = hits.Doc (i); |
| 75 | + //float score = hits.Score (i); |
| 76 | + snotes.Add (new Note (doc.Get ("title"), doc.Get ("lastmod"))); |
| 77 | + } |
| 78 | + } catch (Exception e) { |
| 79 | + Console.WriteLine ("ERROR Search: " + e.Message); |
| 80 | + } |
| 81 | + |
| 82 | + return snotes; |
| 83 | + } |
| 84 | + |
| 85 | + public bool doesNoteExist (string title) |
| 86 | + { |
| 87 | + if (new FileInfo (noteStorePath + "/" + title + ".txt").Exists) |
| 88 | + return true; |
| 89 | + |
| 90 | + return false; |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + public void createNote (string title) |
| 95 | + { |
| 96 | + TextWriter textWriter = new StreamWriter (noteStorePath + "/" + title + ".txt"); |
| 97 | + textWriter.Write (""); |
| 98 | + textWriter.Close (); |
| 99 | + getAllNotes (); |
| 100 | + } |
| 101 | + |
| 102 | + public string getNoteContent (string title) |
| 103 | + { |
| 104 | + StreamReader streamReader = new StreamReader (noteStorePath + "/" + title + ".txt"); |
| 105 | + string text = streamReader.ReadToEnd (); |
| 106 | + streamReader.Close (); |
| 107 | + return text; |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + [MethodImpl(MethodImplOptions.Synchronized)] |
| 112 | + public void storeNoteContent (string title, string content) |
| 113 | + { |
| 114 | + TextWriter textWriter = new StreamWriter (noteStorePath + "/" + title + ".txt"); |
| 115 | + textWriter.Write (content); |
| 116 | + textWriter.Close (); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | +} |
| 121 | + |
0 commit comments