ravendb /Raven.Database/Server/RavenFS/Search/IndexStorage.cs

Language C# Lines 237
MD5 Hash cda9dc2def939b16fc0830658abdc194
Repository https://github.com/nwendel/ravendb.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Raven.Database.Indexing;
using Raven.Json.Linq;
using Raven.Database.Server.RavenFS.Extensions;
using Lucene.Net.QueryParsers;

namespace Raven.Database.Server.RavenFS.Search
{
	public class IndexStorage : IDisposable
	{
		private const string DateIndexFormat = "yyyy-MM-dd_HH-mm-ss";
		private static readonly string[] NumericIndexFields = new[] { "__size_numeric" };

		private readonly string path;
		private FSDirectory directory;
		private LowerCaseKeywordAnalyzer analyzer;
		private IndexWriter writer;
		private readonly object writerLock = new object();
		private readonly IndexSearcherHolder currentIndexSearcherHolder = new IndexSearcherHolder();

		public IndexStorage(string path, NameValueCollection _)
		{
			this.path = path;
		}

		public void Initialize()
		{
			if (System.IO.Directory.Exists(path) == false)
				System.IO.Directory.CreateDirectory(path);
			directory = FSDirectory.Open(new DirectoryInfo(path));
			if (IndexWriter.IsLocked(directory))
				IndexWriter.Unlock(directory);

			analyzer = new LowerCaseKeywordAnalyzer();
			writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
			writer.SetMergeScheduler(new ErrorLoggingConcurrentMergeScheduler());
			currentIndexSearcherHolder.SetIndexSearcher(new IndexSearcher(directory, true));
		}

		public string[] Query(string query, string[] sortFields, int start, int pageSize, out int totalResults)
		{
			IndexSearcher searcher;
			using (GetSearcher(out searcher))
			{
				Query q;
				if (string.IsNullOrEmpty(query))
				{
					q = new MatchAllDocsQuery();
				}
				else
				{
					var queryParser = new RavenQueryParser(analyzer, NumericIndexFields);
                    q = queryParser.Parse(query);
				}

				var topDocs = ExecuteQuery(searcher, sortFields, q, pageSize + start);

				var results = new List<string>();

				for (var i = start; i < pageSize + start && i < topDocs.TotalHits; i++)
				{
					var document = searcher.Doc(topDocs.ScoreDocs[i].Doc);
					results.Add(document.Get("__key"));
				}
				totalResults = topDocs.TotalHits;
				return results.ToArray();
			}
		}

		private TopDocs ExecuteQuery(IndexSearcher searcher, string[] sortFields, Query q, int size)
		{
			TopDocs topDocs;
			if (sortFields != null && sortFields.Length > 0)
			{
				var sort = new Sort(sortFields.Select(field =>
				{
					var desc = field.StartsWith("-");
					if (desc)
						field = field.Substring(1);
					return new SortField(field, SortField.STRING, desc);
				}).ToArray());
				topDocs = searcher.Search(q, null, size, sort);
			}
			else
			{
				topDocs = searcher.Search(q, null, size);
			}
			return topDocs;
		}

        public virtual void Index(string key, RavenJObject metadata)
        {
            lock (writerLock)
            {
                var lowerKey = key.ToLowerInvariant();
                var doc = CreateDocument(lowerKey, metadata);

                // REVIEW: Check if there is more straight-forward/efficient pattern out there to work with RavenJObjects.
                var lookup = metadata.ToLookup(x => x.Key);
                foreach ( var metadataKey in lookup )
                {
                    foreach ( var metadataHolder in metadataKey )
                    {                        
                        doc.Add(new Field(metadataHolder.Key, metadataHolder.Value.ToString(), Field.Store.NO, Field.Index.ANALYZED_NO_NORMS));
                    }
                }

                writer.DeleteDocuments(new Term("__key", lowerKey));
                writer.AddDocument(doc);
                // yes, this is slow, but we aren't expecting high writes count
                writer.Commit();
                ReplaceSearcher();
            }
        }

        private static Document CreateDocument(string lowerKey, RavenJObject metadata)
        {
            var doc = new Document();
            doc.Add(new Field("__key", lowerKey, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

            var fileName = Path.GetFileName(lowerKey);
            Debug.Assert(fileName != null);
            doc.Add(new Field("__fileName", fileName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            // the reversed version of the file name is used to allow searches that start with wildcards
            char[] revFileName = fileName.ToCharArray();
            Array.Reverse(revFileName);
            doc.Add(new Field("__rfileName", new string(revFileName), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));

            int level = 0;
            var directoryName = Path.GetDirectoryName(lowerKey);
            do
            {
                level += 1;
                directoryName = (string.IsNullOrEmpty(directoryName) ? "" : directoryName.Replace("\\", "/"));
                if (directoryName.StartsWith("/") == false)
                    directoryName = "/" + directoryName;
                doc.Add(new Field("__directory", directoryName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
                directoryName = Path.GetDirectoryName(directoryName);
            } while (directoryName != null);
            doc.Add(new Field("__modified", DateTime.UtcNow.ToString(DateIndexFormat, CultureInfo.InvariantCulture), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("__level", level.ToString(CultureInfo.InvariantCulture), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
            
            RavenJToken contentLen;
            if ( metadata.TryGetValue("Content-Length", out contentLen))
            {
                long len;
                if (long.TryParse(contentLen.Value<string>(), out len))
                {
                    doc.Add(new Field("__size", len.ToString("D20"), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
                    doc.Add(new NumericField("__size_numeric", Field.Store.NO, true).SetLongValue(len));
                }
            }

            return doc;
        }

		internal IDisposable GetSearcher(out IndexSearcher searcher)
		{
			return currentIndexSearcherHolder.GetSearcher(out searcher);
		}

		public void Dispose()
		{
			analyzer.Close();
			if (currentIndexSearcherHolder != null)
			{
				currentIndexSearcherHolder.SetIndexSearcher(null);
			}
			writer.Dispose();
			directory.Dispose();
		}

		public void Delete(string key)
		{
			var lowerKey = key.ToLowerInvariant();

			lock (writerLock)
			{
				writer.DeleteDocuments(new Term("__key", lowerKey));
				writer.Optimize();
				writer.Commit();
				ReplaceSearcher();
			}
		}

		private void ReplaceSearcher()
		{
			currentIndexSearcherHolder.SetIndexSearcher(new IndexSearcher(writer.GetReader()));
		}

		public IEnumerable<string> GetTermsFor(string field, string fromValue)
		{
			IndexSearcher searcher;
			using (GetSearcher(out searcher))
			{
				var termEnum = searcher.IndexReader.Terms(new Term(field, fromValue ?? string.Empty));
				try
				{
					if (string.IsNullOrEmpty(fromValue) == false) // need to skip this value
					{
						while (termEnum.Term == null || fromValue.Equals(termEnum.Term.Text))
						{
							if (termEnum.Next() == false)
								yield break;
						}
					}
					while (termEnum.Term == null ||
						field.Equals(termEnum.Term.Field))
					{
						if (termEnum.Term != null)
						{
							var item = termEnum.Term.Text;
							yield return item;
						}

						if (termEnum.Next() == false)
							break;
					}
				}
				finally
				{
					termEnum.Dispose();
				}
			}
		}
	}
}
Back to Top