80 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>
/// 表模型Session连接池
/// </summary>
2025-04-21 10:17:40 +08:00
public class TableSessionPoolAdapter : IIoTDbSessionPool
2025-04-07 16:44:25 +08:00
{
private readonly TableSessionPool _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>
/// TableSessionPoolAdapter
/// </summary>
/// <param name="options"></param>
public TableSessionPoolAdapter(IoTDbOptions options)
2025-04-07 16:44:25 +08:00
{
_options = options;
_sessionPool = new TableSessionPool.Builder()
.SetNodeUrls(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)
.SetDatabase(options.DataBaseName)
.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.InsertAsync(tablet);
if (result != 0)
{
2025-04-21 14:20:49 +08:00
throw new Exception($"{nameof(TableSessionPoolAdapter)} table模型数据入库没有成功返回结果为{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();
}
}
}