Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IndexDocument.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Configuration;
20 using System.IO;
21 using System.Reflection;
22 using System.Runtime.Serialization;
23 using System.Runtime.Serialization.Formatters.Binary;
24 using Lucene.Net.Analysis;
25 using Lucene.Net.Documents;
26 using Lucene.Net.Distributed;
27 
28 namespace Lucene.Net.Distributed.Indexing
29 {
30 
31  /// <summary>
32  /// Base class representing a record to be added to a Lucene index.
33  /// <para>
34  /// IndexDocument contains a RecordId and a Lucene.Net.Document. The RecordId
35  /// is interrogated to determine which index to add the associated
36  /// Lucene.Net.Document.
37  /// </para>
38  /// </summary>
39  [Serializable]
40  public abstract class IndexDocument
41  {
42  #region Variables
43  protected Document _oDocument;
44  protected int _intRecordId;
45  public static BinaryFormatter Formatter = new BinaryFormatter();
46  private static string filepath = (ConfigurationManager.AppSettings["IndexDocumentPath"] != null ? ConfigurationManager.AppSettings["IndexDocumentPath"] : "");
47  private static string endwhack = (filepath.EndsWith(@"\") ? "" : @"\");
48  private DateTime _eDateTime;
49  #endregion
50 
51  #region Constructors
52  /// <summary>
53  /// Empty public constructor.
54  /// </summary>
55  public IndexDocument()
56  {
57  }
58 
59  /// <summary>
60  /// Base constructor accepting only a RecordId. Useful for classes that
61  /// will have no associated Document, i.e. deletes.
62  /// </summary>
63  /// <param name="iRecordId">The source recordId (see also <seealso cref="#">IndexSet.IdColumn</seealso>) </param>
64  public IndexDocument(int iRecordId)
65  {
66  this._intRecordId = iRecordId;
67  this._oDocument = new Document();
68  this._eDateTime = DateTime.Now;
69  }
70 
71  public IndexDocument(Document oDocument, int iRecordId)
72  {
73  this._oDocument = oDocument;
74  this._intRecordId = iRecordId;
75  this._eDateTime = DateTime.Now;
76  }
77 
78  #endregion
79 
80  #region Properties
81  public Document Document
82  {
83  get {return this._oDocument;}
84  }
85 
86  public int RecordId
87  {
88  get {return this._intRecordId;}
89  }
90 
91  public virtual Analyzer GetAnalyzer()
92  {
93  return null;
94  }
95 
96  public string FileName
97  {
98  get { return Environment.MachineName + "_" + this.GetType().ToString() + "_" + this.RecordId.ToString() + "_" + this.DateTime.Ticks.ToString() + ".bin"; }
99  }
100  private DateTime DateTime
101  {
102  get { return this._eDateTime; }
103  }
104 
105  #endregion
106 
107  #region Methods
108  public void Save()
109  {
110  try
111  {
112  FileStream fs = File.Open(filepath + endwhack + this.FileName, FileMode.Create, FileAccess.ReadWrite);
113  IndexDocument.Formatter.Serialize(fs, this);
114  fs.Close();
115  }
116  catch (SerializationException se)
117  {
118  throw (se);
119  }
120  catch (NullReferenceException nre)
121  {
122  throw (nre);
123  }
124  }
125  public void Save(string filePath)
126  {
127  try
128  {
129  FileStream fs = File.Open(filePath + endwhack + this.FileName, FileMode.Create, FileAccess.ReadWrite);
130  IndexDocument.Formatter.Serialize(fs, this);
131  fs.Close();
132  }
133  catch (SerializationException se)
134  {
135  throw (se);
136  }
137  catch (NullReferenceException nre)
138  {
139  throw (nre);
140  }
141  }
142  #endregion
143 
144 
145  }
146 }