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
SnapshotDeletionPolicy.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.Collections.Generic;
20 using Directory = Lucene.Net.Store.Directory;
21 
22 namespace Lucene.Net.Index
23 {
24 
25  /// <summary>A <see cref="IndexDeletionPolicy" /> that wraps around any other
26  /// <see cref="IndexDeletionPolicy" /> and adds the ability to hold and
27  /// later release a single "snapshot" of an index. While
28  /// the snapshot is held, the <see cref="IndexWriter" /> will not
29  /// remove any files associated with it even if the index is
30  /// otherwise being actively, arbitrarily changed. Because
31  /// we wrap another arbitrary <see cref="IndexDeletionPolicy" />, this
32  /// gives you the freedom to continue using whatever <see cref="IndexDeletionPolicy" />
33  /// you would normally want to use with your
34  /// index. Note that you can re-use a single instance of
35  /// SnapshotDeletionPolicy across multiple writers as long
36  /// as they are against the same index Directory. Any
37  /// snapshot held when a writer is closed will "survive"
38  /// when the next writer is opened.
39  ///
40  /// <p/><b>WARNING</b>: This API is a new and experimental and
41  /// may suddenly change.<p/>
42  /// </summary>
43 
45  {
46 
47  private IndexCommit lastCommit;
48  private IndexDeletionPolicy primary;
49  private System.String snapshot;
50 
52  {
53  this.primary = primary;
54  }
55 
56  public virtual void OnInit<T>(IList<T> commits) where T : IndexCommit
57  {
58  lock (this)
59  {
60  primary.OnInit(WrapCommits(commits));
61  lastCommit = commits[commits.Count - 1];
62  }
63  }
64 
65  public virtual void OnCommit<T>(IList<T> commits) where T : IndexCommit
66  {
67  lock (this)
68  {
69  primary.OnCommit(WrapCommits(commits));
70  lastCommit = commits[commits.Count - 1];
71  }
72  }
73 
74  /// <summary>Take a snapshot of the most recent commit to the
75  /// index. You must call release() to free this snapshot.
76  /// Note that while the snapshot is held, the files it
77  /// references will not be deleted, which will consume
78  /// additional disk space in your index. If you take a
79  /// snapshot at a particularly bad time (say just before
80  /// you call optimize()) then in the worst case this could
81  /// consume an extra 1X of your total index size, until
82  /// you release the snapshot.
83  /// </summary>
84  public virtual IndexCommit Snapshot()
85  {
86  lock (this)
87  {
88  if (lastCommit == null)
89  {
90  throw new System.SystemException("no index commits to snapshot !");
91  }
92 
93  if (snapshot == null)
94  snapshot = lastCommit.SegmentsFileName;
95  else
96  throw new System.SystemException("snapshot is already set; please call release() first");
97  return lastCommit;
98  }
99  }
100 
101  /// <summary>Release the currently held snapshot. </summary>
102  public virtual void Release()
103  {
104  lock (this)
105  {
106  if (snapshot != null)
107  snapshot = null;
108  else
109  throw new System.SystemException("snapshot was not set; please call snapshot() first");
110  }
111  }
112 
113  private class MyCommitPoint : IndexCommit
114  {
115  private void InitBlock(SnapshotDeletionPolicy enclosingInstance)
116  {
117  this.enclosingInstance = enclosingInstance;
118  }
119  private SnapshotDeletionPolicy enclosingInstance;
120  public SnapshotDeletionPolicy Enclosing_Instance
121  {
122  get
123  {
124  return enclosingInstance;
125  }
126 
127  }
128  internal IndexCommit cp;
129  internal MyCommitPoint(SnapshotDeletionPolicy enclosingInstance, IndexCommit cp)
130  {
131  InitBlock(enclosingInstance);
132  this.cp = cp;
133  }
134 
135  public override string ToString()
136  {
137  return "SnapshotDeletionPolicy.SnapshotCommitPoint(" + cp + ")";
138  }
139 
140  public override string SegmentsFileName
141  {
142  get { return cp.SegmentsFileName; }
143  }
144 
145  public override ICollection<string> FileNames
146  {
147  get { return cp.FileNames; }
148  }
149 
150  public override Directory Directory
151  {
152  get { return cp.Directory; }
153  }
154 
155  public override void Delete()
156  {
157  lock (Enclosing_Instance)
158  {
159  // Suppress the delete request if this commit point is
160  // our current snapshot.
161  if (Enclosing_Instance.snapshot == null || !Enclosing_Instance.snapshot.Equals(SegmentsFileName))
162  cp.Delete();
163  }
164  }
165 
166  public override bool IsDeleted
167  {
168  get { return cp.IsDeleted; }
169  }
170 
171  public override long Version
172  {
173  get { return cp.Version; }
174  }
175 
176  public override long Generation
177  {
178  get { return cp.Generation; }
179  }
180 
181  public override IDictionary<string, string> UserData
182  {
183  get { return cp.UserData; }
184  }
185 
186  public override bool IsOptimized
187  {
188  get { return cp.IsOptimized; }
189  }
190  }
191 
192  private IList<IndexCommit> WrapCommits<T>(IList<T> commits) where T : IndexCommit
193  {
194  int count = commits.Count;
195  var myCommits = new List<IndexCommit>(count);
196  for (int i = 0; i < count; i++)
197  {
198  myCommits.Add(new MyCommitPoint(this, commits[i]));
199  }
200  return myCommits;
201  }
202  }
203 }