1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::core::store::NativeFSLock;
use crate::core::store::no_lock_factory::NoLock;
use crate::core::store::simple_fs_lock_factory::SimpleFSLock;
use crate::core::store::single_instance_lock_factory::SingleInstanceLock;
use crate::core::util::close::CloseableRef;
use crate::core::util::error::lucene_error::Result;
use std::fmt::{Display, Formatter};
/// An interprocess mutex lock.
///
/// # Example
/// Typical use might look like:
///
/// ```text
/// let lock = directory.obtain_lock("my.lock")?;
/// // ... code to execute while locked ...
/// ```
///
/// # See Also
/// [`Directory::obtain_lock`](crate::core::store::directory::Directory::obtain_lock)
///
/// # Note
/// This is an internal API.
pub trait Lock: Display + CloseableRef + Send + Sync {
/// Best effort check that this lock is still valid. Locks could become
/// invalidated externally for a number of reasons, such as if a user
/// deletes the lock file manually or when a network filesystem is in
/// use.
///
/// # Errors
/// Returns an [`LuceneError`](crate::core::util::error::lucene_error::LuceneError) if the lock is no longer valid.
fn ensure_valid(&self) -> Result<()>;
}
pub type DynLock = dyn Lock + Send + Sync;
pub type CustomLock = Box<DynLock>;
pub enum LockEnum {
Single(SingleInstanceLock),
Simple(SimpleFSLock),
Native(NativeFSLock),
Custom(CustomLock),
NoLock(NoLock),
}
impl LockEnum {
pub fn custom<L>(lock: L) -> Self
where
L: Lock + Send + Sync + 'static,
{
Self::Custom(Box::new(lock))
}
}
impl Display for LockEnum {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Single(inner) => inner.fmt(f),
Self::Simple(inner) => inner.fmt(f),
Self::Native(inner) => inner.fmt(f),
Self::Custom(inner) => inner.fmt(f),
Self::NoLock(inner) => inner.fmt(f),
}
}
}
impl CloseableRef for LockEnum {
fn close(&self) -> Result<()> {
match self {
Self::Single(inner) => inner.close(),
Self::Simple(inner) => inner.close(),
Self::Native(inner) => inner.close(),
Self::Custom(inner) => inner.close(),
Self::NoLock(inner) => inner.close(),
}
}
}
impl Lock for LockEnum {
fn ensure_valid(&self) -> Result<()> {
match self {
Self::Single(inner) => inner.ensure_valid(),
Self::Simple(inner) => inner.ensure_valid(),
Self::Native(inner) => inner.ensure_valid(),
Self::Custom(inner) => inner.ensure_valid(),
Self::NoLock(inner) => inner.ensure_valid(),
}
}
}
macro_rules! either_lock {
($vis:vis $name:ident { $( $Variant:ident : $T:ident ),+ $(,)? }) => {
$vis enum $name<$( $T ),+> {
$( $Variant($T), )+
}
impl<$( $T ),+> Display for $name<$( $T ),+>
where
$( $T: Display ),+
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
$( Self::$Variant(inner) => inner.fmt(f), )+
}
}
}
impl<$( $T ),+> CloseableRef for $name<$( $T ),+>
where
$( $T: CloseableRef ),+
{
fn close(&self) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.close(), )+
}
}
}
impl<$( $T ),+> Lock for $name<$( $T ),+>
where
$( $T: Lock ),+
{
fn ensure_valid(&self) -> Result<()> {
match self {
$( Self::$Variant(inner) => inner.ensure_valid(), )+
}
}
}
};
}
either_lock!(pub LockEnum2 { A: A, B: B });
either_lock!(pub LockEnum3 { A: A, B: B, C: C });