From 63e1053b6cad1b874563400fbe83a708d5c0fa17 Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Mon, 11 Mar 2024 23:37:01 +0700 Subject: [PATCH] SWEEP: Reviewed usage of atomic numeric type methods (#927) * SWEEP: Reviewed all applications of .AddAndGet() methods from atomic numeric classes * SWEEP: Reviewed all applications of .GetAndAdd() methods from atomic numeric classes * SWEEP: Reviewed all applications of .IncrementAndGet(), .GetAndIncrement(), .DecrementAndGet(), and .GetAndDecrement() from atomic numeric classes. Closes #917. --- src/Lucene.Net.Replicator/LocalReplicator.cs | 5 +++-- .../Codecs/RAMOnly/RAMOnlyPostingsFormat.cs | 6 +++--- src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs | 4 ++-- src/Lucene.Net.Tests/Search/TestBooleanOr.cs | 2 +- src/Lucene.Net/Store/RAMFile.cs | 4 ++-- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Lucene.Net.Replicator/LocalReplicator.cs b/src/Lucene.Net.Replicator/LocalReplicator.cs index 7566570666..a7c06b7dfb 100644 --- a/src/Lucene.Net.Replicator/LocalReplicator.cs +++ b/src/Lucene.Net.Replicator/LocalReplicator.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.IO; using System.Linq; @@ -189,7 +190,7 @@ public virtual SessionToken CheckForUpdate(string currentVersion) // currentVersion is either null or older than latest published revision currentRevision.IncRef(); - string sessionID = sessionToken.IncrementAndGet().ToString(); + string sessionID = sessionToken.IncrementAndGet().ToString(CultureInfo.InvariantCulture); SessionToken token = new SessionToken(sessionID, currentRevision.Revision); sessions[sessionID] = new ReplicationSession(token, currentRevision); return token; @@ -329,4 +330,4 @@ public virtual void Release(string sessionId) } } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.TestFramework/Codecs/RAMOnly/RAMOnlyPostingsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/RAMOnly/RAMOnlyPostingsFormat.cs index badefe6a65..6f198afc48 100644 --- a/src/Lucene.Net.TestFramework/Codecs/RAMOnly/RAMOnlyPostingsFormat.cs +++ b/src/Lucene.Net.TestFramework/Codecs/RAMOnly/RAMOnlyPostingsFormat.cs @@ -572,7 +572,7 @@ public override long GetCost() // Holds all indexes created, keyed by the ID assigned in fieldsConsumer private readonly IDictionary state = new Dictionary(); - private readonly AtomicInt64 nextID = new AtomicInt64(); + private readonly AtomicInt32 nextID = new AtomicInt32(); private readonly string RAM_ONLY_NAME = "RAMOnly"; private const int VERSION_START = 0; @@ -582,7 +582,7 @@ public override long GetCost() public override FieldsConsumer FieldsConsumer(SegmentWriteState writeState) { - int id = (int)nextID.GetAndIncrement(); + int id = nextID.GetAndIncrement(); // TODO -- ok to do this up front instead of // on close....? should be ok? @@ -659,4 +659,4 @@ public override FieldsProducer FieldsProducer(SegmentReadState readState) } } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs index dc226fa691..0c756b4b7c 100644 --- a/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs +++ b/src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs @@ -704,7 +704,7 @@ public override IndexOutput CreateOutput(string name, IOContext context) { if (existing != null) { - ramdir.m_sizeInBytes.AddAndGet(-existing.GetSizeInBytes()); // LUCENENET: GetAndAdd in Lucene, but we are not using the value + ramdir.m_sizeInBytes.GetAndAdd(-existing.GetSizeInBytes()); existing.directory = null; } ramdir.m_fileMap[name] = file; @@ -1558,4 +1558,4 @@ protected FakeIOException(SerializationInfo info, StreamingContext context) } #endif } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Tests/Search/TestBooleanOr.cs b/src/Lucene.Net.Tests/Search/TestBooleanOr.cs index 74971e4288..243be1e397 100644 --- a/src/Lucene.Net.Tests/Search/TestBooleanOr.cs +++ b/src/Lucene.Net.Tests/Search/TestBooleanOr.cs @@ -205,7 +205,7 @@ public virtual void TestBooleanScorerMax() while (end.Value < docCount) { int inc = TestUtil.NextInt32(Random, 1, 1000); - end.AddAndGet(inc); + end.GetAndAdd(inc); scorer.Score(c, end); } diff --git a/src/Lucene.Net/Store/RAMFile.cs b/src/Lucene.Net/Store/RAMFile.cs index 11b512df62..ddebcd6b27 100644 --- a/src/Lucene.Net/Store/RAMFile.cs +++ b/src/Lucene.Net/Store/RAMFile.cs @@ -90,7 +90,7 @@ protected internal byte[] AddBuffer(int size) UninterruptableMonitor.Exit(this); } - directory?.m_sizeInBytes.AddAndGet(size); + directory?.m_sizeInBytes.GetAndAdd(size); return buffer; } @@ -146,4 +146,4 @@ public virtual long GetSizeInBytes() } } } -} \ No newline at end of file +}