82 lines
2.4 KiB
C#
Raw Normal View History

2025-04-17 20:28:50 +08:00
using Apache.IoTDB;
using Apache.IoTDB.DataStructure;
using JiShe.CollectBus.IoTDB.Interface;
using JiShe.CollectBus.IoTDB.Options;
2025-04-07 16:44:25 +08:00
using Microsoft.Extensions.Logging;
2025-04-17 20:28:50 +08:00
namespace JiShe.CollectBus.IoTDB.Provider
2025-04-07 16:44:25 +08:00
{
/// <summary>
/// 树模型连接池
/// </summary>
2025-04-21 10:17:40 +08:00
public class SessionPoolAdapter : IIoTDbSessionPool
2025-04-07 16:44:25 +08:00
{
private readonly SessionPool _sessionPool;
2025-04-21 10:17:40 +08:00
private readonly IoTDbOptions _options;
2025-04-07 16:44:25 +08:00
2025-04-21 10:17:40 +08:00
/// <summary>
/// SessionPoolAdapter
/// </summary>
/// <param name="options"></param>
public SessionPoolAdapter(IoTDbOptions options)
2025-04-07 16:44:25 +08:00
{
_options = options;
_sessionPool = new SessionPool.Builder()
.SetNodeUrl(options.ClusterList)
.SetUsername(options.UserName)
.SetPassword(options.Password)
.SetZoneId(options.ZoneId)
2025-04-07 16:44:25 +08:00
.SetFetchSize(options.FetchSize)
.SetPoolSize(options.PoolSize)
.Build();
}
/// <summary>
/// 打开连接池
/// </summary>
/// <returns></returns>
public async Task OpenAsync()
{
await _sessionPool.Open(false);
if (_options.OpenDebugMode)
{
_sessionPool.OpenDebugMode(builder =>
{
builder.AddConsole();
});
}
}
/// <summary>
/// 批量插入对齐时间序列数据
/// </summary>
/// <param name="tablet"></param>
/// <returns></returns>
public async Task<int> InsertAsync(Tablet tablet)
{
2025-04-11 14:50:46 +08:00
var result = await _sessionPool.InsertAlignedTabletAsync(tablet);
if (result != 0)
{
2025-04-21 14:20:49 +08:00
throw new Exception($"{nameof(SessionPoolAdapter)} Tree模型数据入库没有成功返回结果为{result}");
}
2025-04-11 14:50:46 +08:00
return result;
2025-04-07 16:44:25 +08:00
}
/// <summary>
/// 查询数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public async Task<SessionDataSet> ExecuteQueryStatementAsync(string sql)
{
2025-04-22 17:58:14 +08:00
return await _sessionPool.ExecuteQueryStatementAsync(sql, _options.Timeout);
2025-04-07 16:44:25 +08:00
}
public void Dispose()
{
_sessionPool?.Close().ConfigureAwait(false).GetAwaiter().GetResult();
}
}
}