/// <summary> /// Skips <paramref name="amount"/> characters in this reader. Subsequent /// <see cref="Read()"/>s will not return these characters unless <see cref="Reset()"/> /// is used. Skipping characters may invalidate a mark if <see cref="markLimit"/> /// is surpassed. /// </summary> /// <param name="amount">the maximum number of characters to skip.</param> /// <returns>the number of characters actually skipped.</returns> /// <exception cref="ArgumentOutOfRangeException">if <c>amount < 0</c>.</exception> /// <exception cref="IOException">If this reader is disposed or some other I/O error occurs.</exception> /// <seealso cref="Mark(int)"/> /// <seealso cref="IsMarkSupported"/> /// <seealso cref="Reset()"/> public override long Skip(int amount) { if (amount < 0L) { throw new ArgumentOutOfRangeException(nameof(amount), "skip value is negative"); } UninterruptableMonitor.Enter(m_lock); try { EnsureOpen(); if (amount < 1) { return(0); } if (end - pos >= amount) { pos += amount; return(amount); } int read = end - pos; pos = end; while (read < amount) { if (FillBuf() == -1) { return(read); } if (end - pos >= amount - read) { pos += amount - read; return(amount); } // Couldn't get all the characters, skip what we read read += (end - pos); pos = end; } return(amount); } finally { UninterruptableMonitor.Exit(m_lock); } }
/// <summary> /// Closes files associated with this index. /// This method implements the disposable pattern. /// It may be overridden to dispose any managed or unmanaged resources, /// but be sure to call <c>base.Dispose(disposing)</c> to close files associated with the /// underlying <see cref="IndexReader"/>. /// </summary> /// <param name="disposing"><c>true</c> indicates to dispose all managed /// and unmanaged resources, <c>false</c> indicates dispose unmanaged /// resources only</param> protected virtual void Dispose(bool disposing) { if (disposing) { UninterruptableMonitor.Enter(this); try { if (!closed) { DecRef(); closed = true; } } finally { UninterruptableMonitor.Exit(this); } } }
internal void AbortFullFlushes(ISet <string> newFiles) { UninterruptableMonitor.Enter(this); try { try { AbortPendingFlushes(newFiles); } finally { fullFlush = false; } } finally { UninterruptableMonitor.Exit(this); } }
public override FieldsConsumer FieldsConsumer(SegmentWriteState writeState) { int id = (int)nextID.GetAndIncrement(); // TODO -- ok to do this up front instead of // on close....? should be ok? // Write our ID: string idFileName = IndexFileNames.SegmentFileName(writeState.SegmentInfo.Name, writeState.SegmentSuffix, ID_EXTENSION); IndexOutput @out = writeState.Directory.CreateOutput(idFileName, writeState.Context); bool success = false; try { CodecUtil.WriteHeader(@out, RAM_ONLY_NAME, VERSION_LATEST); @out.WriteVInt32(id); success = true; } finally { if (!success) { IOUtils.DisposeWhileHandlingException(@out); } else { IOUtils.Dispose(@out); } } RAMPostings postings = new RAMPostings(); RAMFieldsConsumer consumer = new RAMFieldsConsumer(postings); UninterruptableMonitor.Enter(state); try { state[id] = postings; } finally { UninterruptableMonitor.Exit(state); } return(consumer); }
public override Lock MakeLock(string lockName) { var path = GetCanonicalPathOfLockFile(lockName); Lock l; UninterruptableMonitor.Enter(_locks); try { if (!_locks.TryGetValue(path, out l)) { _locks.Add(path, l = NewLock(path)); } } finally { UninterruptableMonitor.Exit(_locks); } return(l); }
protected virtual void Dispose(bool disposing) { UninterruptableMonitor.Enter(syncLock); try { if (disposed || !disposing) { return; } StopUpdateThread(); infoStream.Dispose(); // LUCENENET specific disposed = true; } finally { UninterruptableMonitor.Exit(syncLock); } }
/// <summary> /// Returns total byte size used by cached filters. </summary> public virtual long GetSizeInBytes() { // Sync only to pull the current set of values: IList <DocIdSet> docIdSets; UninterruptableMonitor.Enter(cache); try { docIdSets = new JCG.List <DocIdSet>(); #if FEATURE_CONDITIONALWEAKTABLE_ENUMERATOR foreach (var pair in cache) { docIdSets.Add(pair.Value); } #else // LUCENENET specific - since .NET Standard 2.0 and .NET Framework don't have a CondtionalWeakTable enumerator, // we use a weak event to retrieve the DocIdSet instances. We look each of these up here to avoid the need // to attach events to the DocIdSet instances themselves (thus using the existing IndexReader.Dispose() // method to detach the events rather than using a finalizer in DocIdSet to ensure they are cleaned up). var e = new Events.GetCacheKeysEventArgs(); eventAggregator.GetEvent <Events.GetCacheKeysEvent>().Publish(e); foreach (var key in e.CacheKeys) { if (cache.TryGetValue(key, out DocIdSet value)) { docIdSets.Add(value); } } #endif } finally { UninterruptableMonitor.Exit(cache); } long total = 0; foreach (DocIdSet dis in docIdSets) { total += RamUsageEstimator.SizeOf(dis); } return(total); }
/// <summary> /// Expert: this method is called by <see cref="IndexReader"/>s which wrap other readers /// (e.g. <see cref="CompositeReader"/> or <see cref="FilterAtomicReader"/>) to register the parent /// at the child (this reader) on construction of the parent. When this reader is disposed, /// it will mark all registered parents as disposed, too. The references to parent readers /// are weak only, so they can be GCed once they are no longer in use. /// @lucene.experimental /// </summary> public void RegisterParentReader(IndexReader reader) { EnsureOpen(); // LUCENENET specific - since neither WeakDictionary nor ConditionalWeakTable synchronize // on the enumerator, we need to do external synchronization to make them threadsafe. UninterruptableMonitor.Enter(parentReadersLock); try { // LUCENENET: Since there is a set Add operation (unique) in Lucene, the equivalent // operation in .NET is AddOrUpdate, which effectively does nothing if the key exists. // Null is passed as a value, since it is not used anyway and .NET doesn't have a boolean // reference type. parentReaders.AddOrUpdate(key: reader, value: null); } finally { UninterruptableMonitor.Exit(parentReadersLock); } }
protected override void Dispose(bool disposing) { if (disposing) { UninterruptableMonitor.Enter(this); try { if (IsLocked()) { Verify((byte)0); @lock.Dispose(); } } finally { UninterruptableMonitor.Exit(this); } } }
/// <summary> /// Calls provided <see cref="IPruner"/> to prune entries. The /// entries are passed to the <see cref="IPruner"/> in sorted (newest to /// oldest <see cref="IndexSearcher"/>) order. /// /// <para/><b>NOTE</b>: you must peridiocally call this, ideally /// from the same background thread that opens new /// searchers. /// </summary> public virtual void Prune(IPruner pruner) { UninterruptableMonitor.Enter(this); try { // Cannot just pass searchers.values() to ArrayList ctor // (not thread-safe since the values can change while // ArrayList is init'ing itself); must instead iterate // ourselves: var trackers = _searchers.Values.Select(item => item.Value).ToList(); trackers.Sort(); var lastRecordTimeSec = 0.0; double now = Time.NanoTime() / NANOS_PER_SEC; foreach (var tracker in trackers) { double ageSec; if (lastRecordTimeSec == 0.0) { ageSec = 0.0; } else { ageSec = now - lastRecordTimeSec; } // First tracker is always age 0.0 sec, since it's // still "live"; second tracker's age (= seconds since // it was "live") is now minus first tracker's // recordTime, etc: if (pruner.DoPrune(ageSec, tracker.Searcher)) { //System.out.println("PRUNE version=" + tracker.version + " age=" + ageSec + " ms=" + Time.CurrentTimeMilliseconds()); Lazy <SearcherTracker> _; _searchers.TryRemove(tracker.Version, out _); tracker.Dispose(); } lastRecordTimeSec = tracker.RecordTimeSec; } } finally { UninterruptableMonitor.Exit(this); } }
protected override void Dispose(bool disposing) { if (disposing) { UninterruptableMonitor.Enter(this); try { // whether or not we have created a file, we need to remove // the lock instance from the dictionary that tracks them. try { UninterruptableMonitor.Enter(NativeFSLockFactory._locks); try { NativeFSLockFactory._locks.Remove(path); } finally { UninterruptableMonitor.Exit(NativeFSLockFactory._locks); } } finally { if (channel != null) { try { IOUtils.DisposeWhileHandlingException(channel); } finally { channel = null; } } } } finally { UninterruptableMonitor.Exit(this); } } }
public override DocData GetNextDocData(DocData docData) { string line; int myID; UninterruptableMonitor.Enter(this); try { line = reader.ReadLine(); if (line is null) { if (!m_forever) { throw new NoMoreDataException(); } // Reset the file OpenFile(); return(GetNextDocData(docData)); } if (docDataLineReader is null) { // first line ever, one time initialization, docDataLineReader = CreateDocDataLineReader(line); if (skipHeaderLine) { return(GetNextDocData(docData)); } } // increment IDS only once... myID = readCount++; } finally { UninterruptableMonitor.Exit(this); } // The date String was written in the format of DateTools.dateToString. docData.Clear(); docData.ID = myID; docDataLineReader.ParseLine(docData, line); return(docData); }
internal void AddFlushableState(ThreadState perThread) { if (infoStream.IsEnabled("DWFC")) { infoStream.Message("DWFC", "addFlushableState " + perThread.dwpt); } DocumentsWriterPerThread dwpt = perThread.dwpt; if (Debugging.AssertsEnabled) { Debugging.Assert(perThread.IsHeldByCurrentThread); Debugging.Assert(perThread.IsInitialized); Debugging.Assert(fullFlush); Debugging.Assert(dwpt.deleteQueue != documentsWriter.deleteQueue); } if (dwpt.NumDocsInRAM > 0) { UninterruptableMonitor.Enter(this); try { if (!perThread.flushPending) { SetFlushPending(perThread); } DocumentsWriterPerThread flushingDWPT = InternalTryCheckOutForFlush(perThread); if (Debugging.AssertsEnabled) { Debugging.Assert(flushingDWPT != null, "DWPT must never be null here since we hold the lock and it holds documents"); Debugging.Assert(dwpt == flushingDWPT, "flushControl returned different DWPT"); } fullFlushBuffer.Add(flushingDWPT); } finally { UninterruptableMonitor.Exit(this); } } else { DocumentsWriterPerThreadPool.Reset(perThread, closed); // make this state inactive // LUCENENET specific - made method static per CA1822 } }
public override void Run() { if (withTimeout) { outerInstance.DoTestTimeout(true, true); } else { outerInstance.DoTestSearch(); } UninterruptableMonitor.Enter(success); try { success.Set(num); } finally { UninterruptableMonitor.Exit(success); } }
public override void ClearLock(string lockName) { var path = GetCanonicalPathOfLockFile(lockName); // this is the reason why we can't use ConcurrentDictionary: we need the removal and disposal of the lock to be atomic // otherwise it may clash with MakeLock making a lock and ClearLock disposing of it in another thread. UninterruptableMonitor.Enter(_locks); try { if (_locks.TryGetValue(path, out Lock l)) { _locks.Remove(path); l.Dispose(); } } finally { UninterruptableMonitor.Exit(_locks); } }
protected internal byte[] AddBuffer(int size) { byte[] buffer = NewBuffer(size); UninterruptableMonitor.Enter(this); try { m_buffers.Add(buffer); m_sizeInBytes += size; } finally { UninterruptableMonitor.Exit(this); } if (directory != null) { directory.m_sizeInBytes.AddAndGet(size); } return(buffer); }
public override void Merge(IndexWriter writer, MergeTrigger trigger, bool newMergesFound) // LUCENENET NOTE: This was internal in the original, but the base class is public so there isn't much choice here { UninterruptableMonitor.Enter(this); try { while (true) { MergePolicy.OneMerge merge = writer.NextMerge(); if (merge == null) { break; } writer.Merge(merge); } } finally { UninterruptableMonitor.Exit(this); } }
public virtual void DropChanges() { UninterruptableMonitor.Enter(this); try { // Discard (don't save) changes when we are dropping // the reader; this is used only on the sub-readers // after a successful merge. If deletes had // accumulated on those sub-readers while the merge // is running, by now we have carried forward those // deletes onto the newly merged segment, so we can // discard them on the sub-readers: pendingDeleteCount = 0; DropMergingUpdates(); } finally { UninterruptableMonitor.Exit(this); } }
protected override void Dispose(bool disposing) { if (disposing) { UninterruptableMonitor.Enter(this); try { parser.Stop(); if (@is != null) { @is.Dispose(); @is = null; } } finally { UninterruptableMonitor.Exit(this); } } }
// Get reader for merging (does not load the terms // index): public virtual SegmentReader GetMergeReader(IOContext context) { UninterruptableMonitor.Enter(this); try { //System.out.println(" livedocs=" + rld.liveDocs); if (mergeReader == null) { if (reader != null) { // Just use the already opened non-merge reader // for merging. In the NRT case this saves us // pointless double-open: //System.out.println("PROMOTE non-merge reader seg=" + rld.info); // Ref for us: reader.IncRef(); mergeReader = reader; //System.out.println(Thread.currentThread().getName() + ": getMergeReader share seg=" + info.name); } else { //System.out.println(Thread.currentThread().getName() + ": getMergeReader seg=" + info.name); // We steal returned ref: mergeReader = new SegmentReader(Info, -1, context); if (liveDocs == null) { liveDocs = mergeReader.LiveDocs; } } } // Ref for caller mergeReader.IncRef(); return(mergeReader); } finally { UninterruptableMonitor.Exit(this); } }
public virtual IBits GetReadOnlyLiveDocs() { UninterruptableMonitor.Enter(this); try { //System.out.println("getROLiveDocs seg=" + info); if (Debugging.AssertsEnabled) { Debugging.Assert(UninterruptableMonitor.IsEntered(writer)); } liveDocsShared = true; //if (liveDocs != null) { //System.out.println(" liveCount=" + liveDocs.count()); //} return(liveDocs); } finally { UninterruptableMonitor.Exit(this); } }
public virtual Span[] SplitSentences(string line) { UninterruptableMonitor.Enter(this); try { if (sentenceSplitter != null) { return(sentenceSplitter.sentPosDetect(line)); } else { Span[] shorty = new Span[1]; shorty[0] = new Span(0, line.Length); return(shorty); } } finally { UninterruptableMonitor.Exit(this); } }
private void InitMergeThreadPriority() { UninterruptableMonitor.Enter(this); try { if (mergeThreadPriority == -1) { // Default to slightly higher priority than our // calling thread mergeThreadPriority = 1 + (int)ThreadJob.CurrentThread.Priority; if (mergeThreadPriority > (int)ThreadPriority.Highest) { mergeThreadPriority = (int)ThreadPriority.Highest; } } } finally { UninterruptableMonitor.Exit(this); } }
private void AcquireLocks(int fromInclusive, int toExclusive, ref int locksAcquired) { Debug.Assert(fromInclusive <= toExclusive); var locks = _tables.Locks; for (var i = fromInclusive; i < toExclusive; i++) { var lockTaken = false; try { UninterruptableMonitor.Enter(locks[i], ref lockTaken); } finally { if (lockTaken) { locksAcquired++; } } } }
private void Close() { UninterruptableMonitor.Enter(syncLock); try { if (reader != null) { reader.Dispose(); reader = null; } if (!string.IsNullOrEmpty(tempFilePath)) { DeleteAsync(tempFilePath); tempFilePath = null; } } finally { UninterruptableMonitor.Exit(syncLock); } }
/// <summary> /// Sets a different index as the spell checker index or re-open /// the existing index if <code>spellIndex</code> is the same value /// as given in the constructor. </summary> /// <param name="spellIndexDir"> the spell directory to use </param> /// <exception cref="ObjectDisposedException"> if the Spellchecker is already closed </exception> /// <exception cref="IOException"> if spellchecker can not open the directory </exception> // TODO: we should make this final as it is called in the constructor public virtual void SetSpellIndex(Directory spellIndexDir) { // this could be the same directory as the current spellIndex // modifications to the directory should be synchronized UninterruptableMonitor.Enter(modifyCurrentIndexLock); try { EnsureOpen(); if (!DirectoryReader.IndexExists(spellIndexDir)) { #pragma warning disable 612, 618 using var writer = new IndexWriter(spellIndexDir, new IndexWriterConfig(LuceneVersion.LUCENE_CURRENT, null)); #pragma warning restore 612, 618 } SwapSearcher(spellIndexDir); } finally { UninterruptableMonitor.Exit(modifyCurrentIndexLock); } }
public override long FileLength(string name) { UninterruptableMonitor.Enter(this); try { #pragma warning disable 612, 618 if (cache.FileExists(name)) #pragma warning restore 612, 618 { return(cache.FileLength(name)); } else { return(@delegate.FileLength(name)); } } finally { UninterruptableMonitor.Exit(this); } }
public override Similarity Get(string field) { UninterruptableMonitor.Enter(this); try { if (Debugging.AssertsEnabled) { Debugging.Assert(field != null); } if (!previousMappings.TryGetValue(field, out Similarity sim) || sim == null) { sim = knownSims[Math.Max(0, Math.Abs(perFieldSeed ^ field.GetHashCode())) % knownSims.Count]; previousMappings[field] = sim; } return(sim); } finally { UninterruptableMonitor.Exit(this); } }
public override string[] ListAll() { UninterruptableMonitor.Enter(this); try { ISet <string> files = new JCG.HashSet <string>(); foreach (string f in cache.ListAll()) { files.Add(f); } // LUCENE-1468: our NRTCachingDirectory will actually exist (RAMDir!), // but if the underlying delegate is an FSDir and mkdirs() has not // yet been called, because so far everything is a cached write, // in this case, we don't want to throw a NoSuchDirectoryException try { foreach (string f in @delegate.ListAll()) { // Cannot do this -- if lucene calls createOutput but // file already exists then this falsely trips: //assert !files.contains(f): "file \"" + f + "\" is in both dirs"; files.Add(f); } } catch (Exception ex) when(ex.IsNoSuchDirectoryException()) { // however, if there are no cached files, then the directory truly // does not "exist" if (files.Count == 0) { throw; // LUCENENET: CA2200: Rethrow to preserve stack details (https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200-rethrow-to-preserve-stack-details) } } return(files.ToArray()); } finally { UninterruptableMonitor.Exit(this); } }
public override SortedSetDocValues GetSortedSet(FieldInfo field) { var entry = fsts[field.Number]; if (entry.numOrds == 0) { return(DocValues.EMPTY_SORTED_SET); // empty FST! } FST <Int64> instance; UninterruptableMonitor.Enter(this); try { if (!fstInstances.TryGetValue(field.Number, out instance)) { data.Seek(entry.offset); instance = new FST <Int64>(data, PositiveInt32Outputs.Singleton); ramBytesUsed.AddAndGet(instance.GetSizeInBytes()); fstInstances[field.Number] = instance; } } finally { UninterruptableMonitor.Exit(this); } var docToOrds = GetBinary(field); var fst = instance; // per-thread resources var @in = fst.GetBytesReader(); var firstArc = new FST.Arc <Int64>(); var scratchArc = new FST.Arc <Int64>(); var scratchInts = new Int32sRef(); var fstEnum = new BytesRefFSTEnum <Int64>(fst); var @ref = new BytesRef(); var input = new ByteArrayDataInput(); return(new SortedSetDocValuesAnonymousClass(entry, docToOrds, fst, @in, firstArc, scratchArc, scratchInts, fstEnum, @ref, input)); }