From 62ab1605722df915a6b69b9c197712a80b7b72dd Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 20 Nov 2025 17:30:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=B2=BE=E7=AE=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Abstractions/AbstractCacheStrategy.cs | 16 +++---------- .../Internal/CacheStrategyFactory.cs | 23 +++++-------------- .../Internal/ContentCacheStrategy.cs | 6 ----- .../Internal/FileCacheStrategy.cs | 11 +-------- .../Internal/VectorIndexCacheStrategy.cs | 6 ----- 5 files changed, 10 insertions(+), 52 deletions(-) diff --git a/binding/csharp/IP2Region.Net/Internal/Abstractions/AbstractCacheStrategy.cs b/binding/csharp/IP2Region.Net/Internal/Abstractions/AbstractCacheStrategy.cs index 13d19e5..8d5f06c 100644 --- a/binding/csharp/IP2Region.Net/Internal/Abstractions/AbstractCacheStrategy.cs +++ b/binding/csharp/IP2Region.Net/Internal/Abstractions/AbstractCacheStrategy.cs @@ -26,16 +26,6 @@ internal abstract class AbstractCacheStrategy useAsync: true); } - protected int GetVectorIndexStartPos(uint ip) - { - var il0 = ip >> 24 & 0xFF; - var il1 = ip >> 16 & 0xFF; - var idx = il0 * VectorIndexCols * VectorIndexSize + il1 * VectorIndexSize; - return (int)idx; - } - - internal abstract ReadOnlyMemory GetVectorIndex(uint ip); - internal virtual ReadOnlyMemory GetData(int offset, int length) { byte[] buffer = ArrayPool.Shared.Rent(length); @@ -45,14 +35,14 @@ internal abstract class AbstractCacheStrategy XdbFileStream.Seek(offset, SeekOrigin.Begin); int bytesRead; - do + while (totalBytesRead < length) { int bytesToRead = Math.Min(BufferSize, length - totalBytesRead); bytesRead = XdbFileStream.Read(buffer, totalBytesRead, bytesToRead); totalBytesRead += bytesRead; - + IoCount++; - } while (bytesRead > 0 && totalBytesRead < length); + } } finally { diff --git a/binding/csharp/IP2Region.Net/Internal/CacheStrategyFactory.cs b/binding/csharp/IP2Region.Net/Internal/CacheStrategyFactory.cs index 14e8eb0..4d9b3d5 100644 --- a/binding/csharp/IP2Region.Net/Internal/CacheStrategyFactory.cs +++ b/binding/csharp/IP2Region.Net/Internal/CacheStrategyFactory.cs @@ -9,23 +9,12 @@ using IP2Region.Net.XDB; namespace IP2Region.Net.Internal; -internal class CacheStrategyFactory +internal class CacheStrategyFactory(string xdbPath) { - private readonly string _xdbPath; - - public CacheStrategyFactory(string xdbPath) + public AbstractCacheStrategy CreateCacheStrategy(CachePolicy cachePolicy) => cachePolicy switch { - _xdbPath = xdbPath; - } - - public AbstractCacheStrategy CreateCacheStrategy(CachePolicy cachePolicy) - { - return cachePolicy switch - { - CachePolicy.Content => new ContentCacheStrategy(_xdbPath), - CachePolicy.VectorIndex => new VectorIndexCacheStrategy(_xdbPath), - CachePolicy.File => new FileCacheStrategy(_xdbPath), - _ => throw new ArgumentException(nameof(cachePolicy)) - }; - } + CachePolicy.Content => new ContentCacheStrategy(xdbPath), + CachePolicy.VectorIndex => new VectorIndexCacheStrategy(xdbPath), + _ => new FileCacheStrategy(xdbPath), + }; } \ No newline at end of file diff --git a/binding/csharp/IP2Region.Net/Internal/ContentCacheStrategy.cs b/binding/csharp/IP2Region.Net/Internal/ContentCacheStrategy.cs index e3094e1..614495a 100644 --- a/binding/csharp/IP2Region.Net/Internal/ContentCacheStrategy.cs +++ b/binding/csharp/IP2Region.Net/Internal/ContentCacheStrategy.cs @@ -19,12 +19,6 @@ internal class ContentCacheStrategy : AbstractCacheStrategy XdbFileStream.Dispose(); } - internal override ReadOnlyMemory GetVectorIndex(uint ip) - { - int idx = GetVectorIndexStartPos(ip); - return _cacheData.Slice(HeaderInfoLength + idx, VectorIndexSize); - } - internal override ReadOnlyMemory GetData(int offset, int length) { return _cacheData.Slice(offset, length); diff --git a/binding/csharp/IP2Region.Net/Internal/FileCacheStrategy.cs b/binding/csharp/IP2Region.Net/Internal/FileCacheStrategy.cs index 31f90f3..0f98cfe 100644 --- a/binding/csharp/IP2Region.Net/Internal/FileCacheStrategy.cs +++ b/binding/csharp/IP2Region.Net/Internal/FileCacheStrategy.cs @@ -8,15 +8,6 @@ using IP2Region.Net.Internal.Abstractions; namespace IP2Region.Net.Internal; -internal class FileCacheStrategy : AbstractCacheStrategy +internal class FileCacheStrategy(string xdbPath) : AbstractCacheStrategy(xdbPath) { - public FileCacheStrategy(string xdbPath) : base(xdbPath) - { - } - - internal override ReadOnlyMemory GetVectorIndex(uint ip) - { - var idx = GetVectorIndexStartPos(ip); - return GetData(HeaderInfoLength + idx, VectorIndexSize); - } } \ No newline at end of file diff --git a/binding/csharp/IP2Region.Net/Internal/VectorIndexCacheStrategy.cs b/binding/csharp/IP2Region.Net/Internal/VectorIndexCacheStrategy.cs index f1d8ae6..e9f5fac 100644 --- a/binding/csharp/IP2Region.Net/Internal/VectorIndexCacheStrategy.cs +++ b/binding/csharp/IP2Region.Net/Internal/VectorIndexCacheStrategy.cs @@ -17,10 +17,4 @@ internal class VectorIndexCacheStrategy : AbstractCacheStrategy var vectorLength = VectorIndexRows * VectorIndexCols * VectorIndexSize; _vectorIndex = base.GetData(HeaderInfoLength, vectorLength); } - - internal override ReadOnlyMemory GetVectorIndex(uint ip) - { - var idx = GetVectorIndexStartPos(ip); - return _vectorIndex.Slice(idx, VectorIndexSize); - } } \ No newline at end of file