using Apache.IoTDB;
using Apache.IoTDB.DataStructure;
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.Common.Helpers;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace JiShe.CollectBus.IoTDBProvider
{
///
/// IoTDB数据源
///
public class IoTDBProvider : IIoTDBProvider, IDisposable
{
private readonly IoTDBOptions _options;
private readonly TableSessionPool _sessionPool;
private static readonly ConcurrentDictionary _metadataCache = new();
private readonly ILogger _logger;
public IoTDBProvider(IOptions options, ILogger logger)
{
_options = options.Value;
_sessionPool = new TableSessionPool.Builder()
.SetNodeUrls(_options.ClusterList)
.SetUsername(_options.UserName)
.SetPassword(_options.Password)
.SetDatabase(_options.DataBaseName)
.SetFetchSize(_options.PoolSize)
.Build();
_sessionPool.Open(false).Wait();
if (_options.OpenDebugMode)
{
_sessionPool.OpenDebugMode(builder =>
{
builder.AddConsole();
});
}
_logger = logger;
}
///
/// 插入数据
///
///
///
///
public async Task InsertAsync(T entity, int buildTabletMode) where T : IoTEntity
{
var metadata = GetMetadata();
var tablet = BuildTablet(new[] { entity }, metadata, buildTabletMode);
var result = await _sessionPool.InsertAsync(tablet);
if (result <=0)
{
_logger.LogWarning($"{typeof(T).Name}插入数据没有成功");
}
}
///
/// 批量插入数据
///
///
///
///
public async Task BatchInsertAsync(IEnumerable entities, int buildTabletMode) where T : IoTEntity
{
var metadata = GetMetadata();
var batchSize = 1000;
var batches = entities.Chunk(batchSize);
foreach (var batch in batches)
{
var tablet = BuildTablet(batch, metadata, buildTabletMode);
var result = await _sessionPool.InsertAsync(tablet);
if (result <= 0)
{
_logger.LogWarning($"{typeof(T).Name} 批量插入数据第{batch}批次没有成功,共{batches}批次。");
}
}
}
///
/// 删除数据
///
///
///
///
public async Task