Files
bim-dogma-poc/ExcelKvPoC/Program.cs
2026-01-08 18:09:21 +09:00

135 lines
3.8 KiB
C#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Garnet;
using MiniExcelLibs;
using ExcelKv.Core; // Use Shared Library
namespace ExcelKvPoC;
// --- 1. Storage Layer (Garnet) Adapter ---
// Adapter to implement IStorageWrapper for Core Loader
public class GarnetClientAdapter : IStorageWrapper, IDisposable
{
private readonly StackExchange.Redis.ConnectionMultiplexer _redis;
private readonly StackExchange.Redis.IDatabase _db;
private readonly StackExchange.Redis.IBatch _batch;
private readonly List<Task> _tasks = new();
public GarnetClientAdapter(string connectionString = "localhost:3278")
{
_redis = StackExchange.Redis.ConnectionMultiplexer.Connect(connectionString);
_db = _redis.GetDatabase();
_batch = _db.CreateBatch();
}
public async Task SetAsync(string key, string value)
{
_tasks.Add(_batch.StringSetAsync(key, value));
await Task.CompletedTask; // Fire and forget in batch context mainly
}
public async Task SetAsync(string key, string value, int row, int col)
{
// For this adapter we store only key/value; row/col metadata can be added later if needed.
_tasks.Add(_batch.StringSetAsync(key, value));
await Task.CompletedTask;
}
public async Task IncrementAsync(string key, double value)
{
_tasks.Add(_batch.StringIncrementAsync(key, value));
await Task.CompletedTask;
}
public void ExecuteBatch()
{
_batch.Execute();
Task.WaitAll(_tasks.ToArray());
_tasks.Clear();
}
public void Dispose()
{
_redis.Dispose();
}
}
public class GarnetServerWrapper : IDisposable
{
private readonly GarnetServer _server;
public GarnetServerWrapper()
{
try
{
var serverArgs = new string[] { "--port", "3278" };
_server = new GarnetServer(serverArgs);
_server.Start();
Console.WriteLine("[Garnet] Server started on port 3278");
}
catch(Exception ex)
{
Console.WriteLine($"[Garnet] Failed to start: {ex.Message}");
throw;
}
}
public void SaveCheckpoint()
{
Console.WriteLine("[Garnet] Saving checkpoint...");
Console.WriteLine("[Garnet] Checkpoint saved (Simulated).");
}
public void Dispose()
{
_server.Dispose();
Console.WriteLine("[Garnet] Server stopped");
}
}
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("=== Excel KV Middleware PoC (Using Core) ===");
using var server = new GarnetServerWrapper();
var registry = new SchemaRegistry();
string excelPath = "/home/lectom/repos/design-bim-dogma/DB작업_U형측구.xlsx";
string sheetName = "U형측구현황DB작업";
if (File.Exists(excelPath))
{
// Define Region Config (Interactive Area Simulation)
var rangeConfig = new RegionConfig
{
TopHeaderStartRow = 0,
TopHeaderDepth = 3,
LeftHeaderStartCol = 0,
LeftHeaderWidth = 4
};
using var client = new GarnetClientAdapter();
// Process
await ExcelLoader.ProcessFileAsync(excelPath, sheetName, rangeConfig, client, registry);
client.ExecuteBatch(); // Commit
}
else
{
Console.WriteLine($"File not found: {excelPath}");
}
server.SaveCheckpoint();
Console.WriteLine("Done.");
}
}