160 lines
4.6 KiB
C#
160 lines
4.6 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 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 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
|
|
|
|
// Verification Dump
|
|
DumpSample(sheetName);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"File not found: {excelPath}");
|
|
}
|
|
|
|
server.SaveCheckpoint();
|
|
Console.WriteLine("Done.");
|
|
}
|
|
|
|
static void DumpSample(string sheetName)
|
|
{
|
|
Console.WriteLine("\n--- Exporting to sample.txt ---");
|
|
using var redis = StackExchange.Redis.ConnectionMultiplexer.Connect("localhost:3278");
|
|
var serverEnd = redis.GetServer("localhost:3278");
|
|
var db = redis.GetDatabase();
|
|
var keys = serverEnd.Keys(pattern: $"{sheetName}:*");
|
|
|
|
using (var writer = new StreamWriter("sample.txt"))
|
|
{
|
|
int count = 0;
|
|
foreach(var key in keys)
|
|
{
|
|
string val = db.StringGet(key);
|
|
writer.WriteLine($"{key} = {val}");
|
|
count++;
|
|
}
|
|
Console.WriteLine($"[Export] Written {count} keys to sample.txt");
|
|
}
|
|
}
|
|
}
|