Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 
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 
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 
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 }