mirror of
https://github.com/netfun2000/ip2region.git
synced 2026-02-27 09:44:31 +08:00
feat:support for concurrent file-based queries
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
// Copyright 2023 The Ip2Region Authors. All rights reserved.
|
||||
// Use of this source code is governed by a Apache2.0-style
|
||||
// license that can be found in the LICENSE file.
|
||||
// @Author Alan <lzh.shap@gmail.com>
|
||||
// @Date 2023/07/25
|
||||
|
||||
using System.Buffers;
|
||||
|
||||
namespace IP2Region.Net.Internal.Abstractions;
|
||||
|
||||
internal abstract class AbstractCacheStrategy
|
||||
{
|
||||
protected const int HeaderInfoLength = 256;
|
||||
protected const int VectorIndexRows = 256;
|
||||
protected const int VectorIndexCols = 256;
|
||||
protected const int VectorIndexSize = 8;
|
||||
|
||||
protected readonly FileStream XdbFileStream;
|
||||
private const int BufferSize = 4096;
|
||||
|
||||
internal int IoCount { get; private set; }
|
||||
|
||||
protected AbstractCacheStrategy(string xdbPath)
|
||||
{
|
||||
XdbFileStream = new FileStream(xdbPath, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize,
|
||||
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<byte> GetVectorIndex(uint ip);
|
||||
|
||||
internal virtual ReadOnlyMemory<byte> GetData(int offset, int length)
|
||||
{
|
||||
byte[] buffer = ArrayPool<byte>.Shared.Rent(length);
|
||||
int totalBytesRead = 0;
|
||||
try
|
||||
{
|
||||
XdbFileStream.Seek(offset, SeekOrigin.Begin);
|
||||
|
||||
int bytesRead;
|
||||
do
|
||||
{
|
||||
int bytesToRead = Math.Min(BufferSize, length - totalBytesRead);
|
||||
bytesRead = XdbFileStream.Read(buffer, totalBytesRead, bytesToRead);
|
||||
totalBytesRead += bytesRead;
|
||||
|
||||
IoCount++;
|
||||
} while (bytesRead > 0 && totalBytesRead < length);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(buffer);
|
||||
}
|
||||
|
||||
return new ReadOnlyMemory<byte>(buffer, 0, totalBytesRead);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2023 The Ip2Region Authors. All rights reserved.
|
||||
// Use of this source code is governed by a Apache2.0-style
|
||||
// license that can be found in the LICENSE file.
|
||||
// @Author Alan <lzh.shap@gmail.com>
|
||||
// @Date 2023/07/25
|
||||
|
||||
using IP2Region.Net.Internal.Abstractions;
|
||||
using IP2Region.Net.XDB;
|
||||
|
||||
namespace IP2Region.Net.Internal;
|
||||
|
||||
internal class CacheStrategyFactory
|
||||
{
|
||||
private readonly string _xdbPath;
|
||||
|
||||
public CacheStrategyFactory(string xdbPath)
|
||||
{
|
||||
_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))
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2023 The Ip2Region Authors. All rights reserved.
|
||||
// Use of this source code is governed by a Apache2.0-style
|
||||
// license that can be found in the LICENSE file.
|
||||
// @Author Alan <lzh.shap@gmail.com>
|
||||
// @Date 2023/07/25
|
||||
|
||||
using IP2Region.Net.Internal.Abstractions;
|
||||
|
||||
namespace IP2Region.Net.Internal;
|
||||
|
||||
internal class ContentCacheStrategy : AbstractCacheStrategy
|
||||
{
|
||||
private readonly ReadOnlyMemory<byte> _cacheData;
|
||||
|
||||
public ContentCacheStrategy(string xdbPath) : base(xdbPath)
|
||||
{
|
||||
_cacheData = base.GetData(0, (int)XdbFileStream.Length);
|
||||
XdbFileStream.Close();
|
||||
XdbFileStream.Dispose();
|
||||
}
|
||||
|
||||
internal override ReadOnlyMemory<byte> GetVectorIndex(uint ip)
|
||||
{
|
||||
int idx = GetVectorIndexStartPos(ip);
|
||||
return _cacheData.Slice(HeaderInfoLength + idx, VectorIndexSize);
|
||||
}
|
||||
|
||||
internal override ReadOnlyMemory<byte> GetData(int offset, int length)
|
||||
{
|
||||
return _cacheData.Slice(offset, length);
|
||||
}
|
||||
}
|
||||
22
binding/csharp/IP2Region.Net/Internal/FileCacheStrategy.cs
Normal file
22
binding/csharp/IP2Region.Net/Internal/FileCacheStrategy.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2023 The Ip2Region Authors. All rights reserved.
|
||||
// Use of this source code is governed by a Apache2.0-style
|
||||
// license that can be found in the LICENSE file.
|
||||
// @Author Alan <lzh.shap@gmail.com>
|
||||
// @Date 2023/07/25
|
||||
|
||||
using IP2Region.Net.Internal.Abstractions;
|
||||
|
||||
namespace IP2Region.Net.Internal;
|
||||
|
||||
internal class FileCacheStrategy : AbstractCacheStrategy
|
||||
{
|
||||
public FileCacheStrategy(string xdbPath) : base(xdbPath)
|
||||
{
|
||||
}
|
||||
|
||||
internal override ReadOnlyMemory<byte> GetVectorIndex(uint ip)
|
||||
{
|
||||
var idx = GetVectorIndexStartPos(ip);
|
||||
return GetData(HeaderInfoLength + idx, VectorIndexSize);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2023 The Ip2Region Authors. All rights reserved.
|
||||
// Use of this source code is governed by a Apache2.0-style
|
||||
// license that can be found in the LICENSE file.
|
||||
// @Author Alan <lzh.shap@gmail.com>
|
||||
// @Date 2023/07/25
|
||||
|
||||
using IP2Region.Net.Internal.Abstractions;
|
||||
|
||||
namespace IP2Region.Net.Internal;
|
||||
|
||||
internal class VectorIndexCacheStrategy : AbstractCacheStrategy
|
||||
{
|
||||
private readonly ReadOnlyMemory<byte> _vectorIndex;
|
||||
|
||||
public VectorIndexCacheStrategy(string xdbPath) : base(xdbPath)
|
||||
{
|
||||
var vectorLength = VectorIndexRows * VectorIndexCols * VectorIndexSize;
|
||||
_vectorIndex = base.GetData(HeaderInfoLength, vectorLength);
|
||||
}
|
||||
|
||||
internal override ReadOnlyMemory<byte> GetVectorIndex(uint ip)
|
||||
{
|
||||
var idx = GetVectorIndexStartPos(ip);
|
||||
return _vectorIndex.Slice(idx, VectorIndexSize);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user