01: package org.apache.lucene.store;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import java.io.IOException;
21: import java.util.HashSet;
22:
23: /**
24: * Implements {@link LockFactory} for a single in-process instance,
25: * meaning all locking will take place through this one instance.
26: * Only use this {@link LockFactory} when you are certain all
27: * IndexReaders and IndexWriters for a given index are running
28: * against a single shared in-process Directory instance. This is
29: * currently the default locking for RAMDirectory.
30: *
31: * @see LockFactory
32: */
33:
34: public class SingleInstanceLockFactory extends LockFactory {
35:
36: private HashSet locks = new HashSet();
37:
38: public Lock makeLock(String lockName) {
39: // We do not use the LockPrefix at all, because the private
40: // HashSet instance effectively scopes the locking to this
41: // single Directory instance.
42: return new SingleInstanceLock(locks, lockName);
43: }
44:
45: public void clearLock(String lockName) throws IOException {
46: synchronized (locks) {
47: if (locks.contains(lockName)) {
48: locks.remove(lockName);
49: }
50: }
51: }
52: };
53:
54: class SingleInstanceLock extends Lock {
55:
56: String lockName;
57: private HashSet locks;
58:
59: public SingleInstanceLock(HashSet locks, String lockName) {
60: this .locks = locks;
61: this .lockName = lockName;
62: }
63:
64: public boolean obtain() throws IOException {
65: synchronized (locks) {
66: return locks.add(lockName);
67: }
68: }
69:
70: public void release() {
71: synchronized (locks) {
72: locks.remove(lockName);
73: }
74: }
75:
76: public boolean isLocked() {
77: synchronized (locks) {
78: return locks.contains(lockName);
79: }
80: }
81:
82: public String toString() {
83: return "SingleInstanceLock: " + lockName;
84: }
85: }
|