forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameIntCacheLRU.cs
More file actions
253 lines (219 loc) · 9.24 KB
/
Copy pathNameIntCacheLRU.cs
File metadata and controls
253 lines (219 loc) · 9.24 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Lucene version compatibility level 4.8.1
using J2N.Collections.Concurrent;
using Lucene.Net.Support.Threading;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Facet.Taxonomy.WriterCache
{
/*
* 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.
*/
/// <summary>
/// An an LRU cache of mapping from name to int.
/// Used to cache Ordinals of category paths.
/// <para/>
/// NOTE: This was NameIntCacheLRU in Lucene
/// <para/>
/// @lucene.experimental
/// </summary>
/// <remarks>
/// Note: Nothing in this class is synchronized. The caller is assumed to be
/// synchronized so that no two methods of this class are called concurrently.
/// </remarks>
public class NameInt32CacheLru : IInternalNameInt32CacheLru // LUCENENET specific - added interface
{
private readonly IInternalNameInt32CacheLru cache;
internal NameInt32CacheLru(int limit)
{
this.cache = new NameCacheLru<FacetLabel>(
limit,
(name) => name,
(name, prefixLength) => name.Subpath(prefixLength));
}
/// <inheritdoc/>
public int Count => cache.Count;
/// <inheritdoc/>
public int Limit => cache.Limit;
/// <inheritdoc/>
string IInternalNameInt32CacheLru.Stats => cache.Stats;
/// <inheritdoc/>
void IInternalNameInt32CacheLru.Clear() => cache.Clear();
/// <inheritdoc/>
bool IInternalNameInt32CacheLru.MakeRoomLRU() => cache.MakeRoomLRU();
/// <inheritdoc/>
bool IInternalNameInt32CacheLru.Put(FacetLabel name, int val) => cache.Put(name, val);
/// <inheritdoc/>
bool IInternalNameInt32CacheLru.Put(FacetLabel name, int prefixLen, int val) => cache.Put(name, prefixLen, val);
/// <inheritdoc/>
bool IInternalNameInt32CacheLru.TryGetValue(FacetLabel name, out int value) => cache.TryGetValue(name, out value);
}
/// <summary>
/// Public members of the <see cref="NameInt32CacheLru"/> that are shared between instances.
/// </summary>
public interface INameInt32CacheLru
{
/// <summary>
/// Number of entries currently in the cache.
/// </summary>
int Count { get; }
/// <summary>
/// Maximum number of cache entries before eviction.
/// </summary>
int Limit { get; }
}
// LUCENENET specific - defined interface so we can share public and internal members between cache
// instances.
internal interface IInternalNameInt32CacheLru : INameInt32CacheLru
{
string Stats { get; }
void Clear();
/// <summary>
/// If cache is full remove least recently used entries from cache. Return <c>true</c>
/// if anything was removed, <c>false</c> otherwise.
/// <para/>
/// See comment in <see cref="Directory.DirectoryTaxonomyWriter.AddToCache(FacetLabel, int)"/> for an
/// explanation why we clean 2/3rds of the cache, and not just one entry.
/// </summary>
bool MakeRoomLRU();
/// <summary>
/// Add a new value to cache.
/// Return true if cache became full and some room need to be made.
/// </summary>
bool Put(FacetLabel name, int val);
bool Put(FacetLabel name, int prefixLen, int val);
bool TryGetValue(FacetLabel name, out int value);
}
/// <summary>
/// Class for name LRU caches. Users can specify
/// a type that can be used as the key of the cache. The
/// key may be either a value type or a reference type.
/// </summary>
/// <typeparam name="TName">The type of key for the cache.</typeparam>
// LUCENENET specific - extracted the logic out of NameInt32CacheLru to make
// it generic so boxing/unboxing can be avoided during lookups, while also
// keeping the generic closing type from being exposed on the public API.
internal sealed class NameCacheLru<TName> : IInternalNameInt32CacheLru
{
private IDictionary<TName, int> cache;
internal long nMisses = 0; // for debug
internal long nHits = 0; // for debug
private readonly int maxCacheSize;
private readonly object syncLock = new object(); // LUCENENET specific so we don't lock this
private readonly Func<FacetLabel, TName> getKey;
private readonly Func<FacetLabel, int, TName> getKeyWithPrefixLength;
internal NameCacheLru(int limit, Func<FacetLabel, TName> getKey, Func<FacetLabel, int, TName> getKeyWithPrefixLength)
{
this.getKey = getKey ?? throw new ArgumentNullException(nameof(getKey)); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention)
this.getKeyWithPrefixLength = getKeyWithPrefixLength ?? throw new ArgumentNullException(nameof(getKeyWithPrefixLength)); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention)
this.maxCacheSize = limit;
CreateCache(limit);
}
/// <summary>
/// Maximum number of cache entries before eviction.
/// </summary>
public int Limit => maxCacheSize;
/// <summary>
/// Number of entries currently in the cache.
/// </summary>
public int Count => cache.Count;
private void CreateCache(int maxSize)
{
if (maxSize < int.MaxValue)
{
cache = new LurchTable<TName, int>(capacity: 1000, ordering: LurchTableOrder.Access); //for LRU
}
else
{
cache = new JCG.Dictionary<TName, int>(capacity: 1000);
}
}
bool IInternalNameInt32CacheLru.TryGetValue(FacetLabel name, out int value) // LUCENENET specific - use TryGetValue() instead of Get()
{
if (!cache.TryGetValue(getKey(name), out value))
{
nMisses++;
return false;
}
else
{
nHits++;
return true;
}
}
// LUCENENET: No need for Key() functions, they are passed in as delegates through the constructor.
/// <summary>
/// Add a new value to cache.
/// Return true if cache became full and some room need to be made.
/// </summary>
bool IInternalNameInt32CacheLru.Put(FacetLabel name, int val)
{
cache[getKey(name)] = val;
return IsCacheFull;
}
bool IInternalNameInt32CacheLru.Put(FacetLabel name, int prefixLen, int val)
{
cache[getKeyWithPrefixLength(name, prefixLen)] = val;
return IsCacheFull;
}
private bool IsCacheFull => cache.Count > maxCacheSize;
void IInternalNameInt32CacheLru.Clear() => cache.Clear();
string IInternalNameInt32CacheLru.Stats => "#miss=" + nMisses + " #hit=" + nHits;
/// <summary>
/// If cache is full remove least recently used entries from cache. Return <c>true</c>
/// if anything was removed, <c>false</c> otherwise.
/// <para/>
/// See comment in <see cref="Directory.DirectoryTaxonomyWriter.AddToCache(FacetLabel, int)"/> for an
/// explanation why we clean 2/3rds of the cache, and not just one entry.
/// </summary>
bool IInternalNameInt32CacheLru.MakeRoomLRU()
{
if (!IsCacheFull)
{
return false;
}
int n = cache.Count - (2 * maxCacheSize) / 3;
if (n <= 0)
{
return false;
}
UninterruptableMonitor.Enter(syncLock);
try
{
// Double-check that another thread didn't beat us to the operation
n = cache.Count - (2 * maxCacheSize) / 3;
if (n <= 0)
{
return false;
}
//System.Diagnostics.Debug.WriteLine("Removing cache entries in MakeRoomLRU");
using var it = cache.GetEnumerator();
int i = 0;
while (i < n && it.MoveNext())
{
cache.Remove(it.Current.Key);
i++;
}
}
finally
{
UninterruptableMonitor.Exit(syncLock);
}
return true;
}
}
}