Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / beagled / Lucene.Net / Store / Lock.cs
blob15f97b87a2197fb02e52c07b05570366324e2dec
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using IndexWriter = Lucene.Net.Index.IndexWriter;
18 namespace Lucene.Net.Store
21 /// <summary>An interprocess mutex lock.
22 /// <p>Typical use might look like:<pre>
23 /// new Lock.With(directory.makeLock("my.lock")) {
24 /// public Object doBody() {
25 /// <i>... code to execute while locked ...</i>
26 /// }
27 /// }.Run();
28 /// </pre>
29 ///
30 /// </summary>
31 /// <author> Doug Cutting
32 /// </author>
33 /// <version> $Id: Lock.cs,v 1.6 2005/10/31 00:37:06 dsd Exp $
34 /// </version>
35 /// <seealso cref="Directory#MakeLock(String)">
36 /// </seealso>
37 public abstract class Lock
39 public static long LOCK_POLL_INTERVAL = 1000;
41 /// <summary>Attempts to obtain exclusive access and immediately return
42 /// upon success or failure.
43 /// </summary>
44 /// <returns> true iff exclusive access is obtained
45 /// </returns>
46 public abstract bool Obtain();
48 /// <summary>Attempts to obtain an exclusive lock within amount
49 /// of time given. Currently polls once per second until
50 /// lockWaitTimeout is passed.
51 /// </summary>
52 /// <param name="lockWaitTimeout">length of time to wait in ms
53 /// </param>
54 /// <returns> true if lock was obtained
55 /// </returns>
56 /// <throws> IOException if lock wait times out or obtain() throws an IOException </throws>
57 public virtual bool Obtain(long lockWaitTimeout)
59 int maxSleepCount = (int) (lockWaitTimeout / LOCK_POLL_INTERVAL);
60 int sleepCount = 0;
61 maxSleepCount = System.Math.Max (maxSleepCount, 1);
62 FSDirectory.Log ("Lock.Obtain timeout={0} maxsleepcount={1}", lockWaitTimeout, maxSleepCount);
63 bool locked = Obtain();
65 while (!locked)
67 if (sleepCount == maxSleepCount)
69 // Try and be a little more verbose on failure
70 string lockpath = this.ToString ();
71 System.Text.StringBuilder ex = new System.Text.StringBuilder();
72 ex.Append ("Lock obain timed out: ");
73 ex.Append (lockpath);
74 if (System.IO.File.Exists (lockpath)) {
75 System.IO.FileStream fs = System.IO.File.Open (lockpath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
76 System.IO.StreamReader sr = new System.IO.StreamReader (fs);
77 string pid = sr.ReadToEnd ().Trim ();
78 sr.Close ();
79 fs.Close ();
80 ex.Append (" -- pid ").Append (pid);
82 if (System.IO.Directory.Exists ("/proc/" + pid))
83 ex.Append (" -- process exists");
84 else
85 ex.Append (" -- process does not exist, stale lockfile?");
86 } else {
87 ex.Append (" -- lock file doesn't exist!?");
89 throw new System.IO.IOException(ex.ToString ());
91 ++sleepCount;
93 System.Threading.Thread.Sleep((int) LOCK_POLL_INTERVAL);
94 locked = Obtain();
96 return locked;
99 /// <summary>Releases exclusive access. </summary>
100 public abstract void Release();
102 /// <summary>Returns true if the resource is currently locked. Note that one must
103 /// still call {@link #Obtain()} before using the resource.
104 /// </summary>
105 public abstract bool IsLocked();
108 /// <summary>Utility class for executing code with exclusive access. </summary>
109 public abstract class With
111 private Lock lock_Renamed;
112 private long lockWaitTimeout;
114 /// <summary>Constructs an executor that will grab the named lock.
115 /// Defaults lockWaitTimeout to Lock.COMMIT_LOCK_TIMEOUT.
116 /// </summary>
117 /// <deprecated> Kept only to avoid breaking existing code.
118 /// </deprecated>
119 public With(Lock lock_Renamed):this(lock_Renamed, IndexWriter.COMMIT_LOCK_TIMEOUT)
123 /// <summary>Constructs an executor that will grab the named lock. </summary>
124 public With(Lock lock_Renamed, long lockWaitTimeout)
126 this.lock_Renamed = lock_Renamed;
127 this.lockWaitTimeout = lockWaitTimeout;
130 /// <summary>Code to execute with exclusive access. </summary>
131 public abstract System.Object DoBody();
133 /// <summary>Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
134 /// cannot be obtained immediately. Retries to obtain lock once per second
135 /// until it is obtained, or until it has tried ten times. Lock is released when
136 /// {@link #doBody} exits.
137 /// </summary>
138 public virtual System.Object Run()
140 bool locked = false;
143 locked = lock_Renamed.Obtain(lockWaitTimeout);
144 return DoBody();
146 finally
148 if (locked)
149 lock_Renamed.Release();