mirror of
https://github.com/NecroticBamboo/DeepTrace.git
synced 2025-12-21 11:21:51 +00:00
58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
using DeepTrace.Data;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
|
|
namespace DeepTrace.Services;
|
|
|
|
public class TrainedModelStorageService: ITrainedModelStorageService
|
|
{
|
|
private const string MongoDBDatabaseName = "DeepTrace";
|
|
private const string MongoDBCollection = "TrainedModels";
|
|
|
|
private readonly IMongoClient _client;
|
|
|
|
public TrainedModelStorageService(IMongoClient client)
|
|
{
|
|
_client = client;
|
|
}
|
|
|
|
public async Task<List<TrainedModelDefinition>> Load()
|
|
{
|
|
var db = _client.GetDatabase(MongoDBDatabaseName);
|
|
var collection = db.GetCollection<TrainedModelDefinition>(MongoDBCollection);
|
|
|
|
var res = await (await collection.FindAsync("{}")).ToListAsync();
|
|
return res;
|
|
}
|
|
public async Task Store(TrainedModelDefinition source)
|
|
{
|
|
var db = _client.GetDatabase(MongoDBDatabaseName);
|
|
var collection = db.GetCollection<TrainedModelDefinition>(MongoDBCollection);
|
|
|
|
if (source.Id == null)
|
|
source.Id = ObjectId.GenerateNewId();
|
|
|
|
// use upsert (insert or update) to automatically handle subsequent updates
|
|
await collection.ReplaceOneAsync(
|
|
filter: new BsonDocument("_id", source.Id),
|
|
options: new ReplaceOptions { IsUpsert = true },
|
|
replacement: source
|
|
);
|
|
}
|
|
|
|
public async Task Delete(TrainedModelDefinition source, bool ignoreNotStored = false)
|
|
{
|
|
if (source.Id == null)
|
|
{
|
|
if (!ignoreNotStored)
|
|
throw new InvalidDataException("Source was not stored yet. There is nothing to delete");
|
|
return;
|
|
}
|
|
|
|
var db = _client.GetDatabase(MongoDBDatabaseName);
|
|
var collection = db.GetCollection<TrainedModelDefinition>(MongoDBCollection);
|
|
|
|
await collection.DeleteOneAsync(filter: new BsonDocument("_id", source.Id));
|
|
}
|
|
}
|