-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiteDBGraphRepository.cs
More file actions
174 lines (146 loc) · 6.04 KB
/
LiteDBGraphRepository.cs
File metadata and controls
174 lines (146 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using LiteDB;
using LiteGraph.GraphRepositories.Interfaces;
using WebNet.LiteGraphExtensions.GraphRepositories.Implementations.LiteDB;
namespace WebNet.LiteGraphExtensions.GraphRepositories
{
/// <summary>
/// LiteDB-based graph repository implementation.
/// Provides document-oriented NoSQL storage with BSON serialization and LINQ query support.
/// </summary>
public class LiteDBGraphRepository : LiteGraph.GraphRepositories.GraphRepositoryBase
{
private readonly LiteDatabase _database;
private readonly string _connectionString;
private bool _disposed;
/// <summary>
/// Initialize a new LiteDB graph repository.
/// </summary>
/// <param name="connectionString">LiteDB connection string (e.g., "mydata.db" or "Filename=mydata.db;Connection=shared")</param>
public LiteDBGraphRepository(string connectionString)
{
if (string.IsNullOrWhiteSpace(connectionString))
throw new ArgumentException("Connection string cannot be null or empty", nameof(connectionString));
_connectionString = connectionString;
_database = new LiteDatabase(_connectionString);
// Initialize all interface implementations
Admin = new AdminMethods(this);
Tenant = new TenantMethods(this);
User = new UserMethods(this);
Credential = new CredentialMethods(this);
Label = new LabelMethods(this);
Tag = new TagMethods(this);
Vector = new VectorMethods(this);
Graph = new GraphMethods(this);
Node = new NodeMethods(this);
Edge = new EdgeMethods(this);
Batch = new BatchMethods(this);
VectorIndex = new VectorIndexMethods(this);
}
/// <summary>
/// Admin methods interface implementation.
/// </summary>
public override IAdminMethods Admin { get; }
/// <summary>
/// Tenant methods interface implementation.
/// </summary>
public override ITenantMethods Tenant { get; }
/// <summary>
/// User methods interface implementation.
/// </summary>
public override IUserMethods User { get; }
/// <summary>
/// Credential methods interface implementation.
/// </summary>
public override ICredentialMethods Credential { get; }
/// <summary>
/// Label methods interface implementation.
/// </summary>
public override ILabelMethods Label { get; }
/// <summary>
/// Tag methods interface implementation.
/// </summary>
public override ITagMethods Tag { get; }
/// <summary>
/// Vector methods interface implementation.
/// </summary>
public override IVectorMethods Vector { get; }
/// <summary>
/// Graph methods interface implementation.
/// </summary>
public override IGraphMethods Graph { get; }
/// <summary>
/// Node methods interface implementation.
/// </summary>
public override INodeMethods Node { get; }
/// <summary>
/// Edge methods interface implementation.
/// </summary>
public override IEdgeMethods Edge { get; }
/// <summary>
/// Batch methods interface implementation.
/// </summary>
public override IBatchMethods Batch { get; }
/// <summary>
/// Vector index methods interface implementation.
/// </summary>
public override IVectorIndexMethods VectorIndex { get; }
/// <summary>
/// Initialize the repository.
/// Collections are automatically created when first accessed.
/// Creates indexes for commonly queried fields.
/// </summary>
public override void InitializeRepository()
{
// Create indexes for performance
var tenants = _database.GetCollection<LiteGraph.TenantMetadata>("tenants");
tenants.EnsureIndex(x => x.GUID, true);
tenants.EnsureIndex(x => x.Name);
var graphs = _database.GetCollection<LiteGraph.Graph>("graphs");
graphs.EnsureIndex(x => x.GUID, true);
graphs.EnsureIndex(x => x.TenantGUID);
var nodes = _database.GetCollection<LiteGraph.Node>("nodes");
nodes.EnsureIndex(x => x.GUID, true);
nodes.EnsureIndex(x => x.GraphGUID);
nodes.EnsureIndex(x => x.TenantGUID);
var edges = _database.GetCollection<LiteGraph.Edge>("edges");
edges.EnsureIndex(x => x.GUID, true);
edges.EnsureIndex(x => x.GraphGUID);
edges.EnsureIndex(x => x.From);
edges.EnsureIndex(x => x.To);
var users = _database.GetCollection<LiteGraph.UserMaster>("users");
users.EnsureIndex(x => x.GUID, true);
users.EnsureIndex(x => x.Email);
var credentials = _database.GetCollection<LiteGraph.Credential>("credentials");
credentials.EnsureIndex(x => x.GUID, true);
var labels = _database.GetCollection<LiteGraph.LabelMetadata>("labels");
labels.EnsureIndex(x => x.GUID, true);
var tags = _database.GetCollection<LiteGraph.TagMetadata>("tags");
tags.EnsureIndex(x => x.GUID, true);
var vectors = _database.GetCollection<LiteGraph.VectorMetadata>("vectors");
vectors.EnsureIndex(x => x.GUID, true);
}
/// <summary>
/// Flush database changes to disk.
/// </summary>
public override void Flush()
{
_database.Checkpoint();
}
/// <summary>
/// Get the LiteDB database instance.
/// </summary>
public LiteDatabase GetDatabase() => _database;
/// <summary>
/// Dispose the repository.
/// </summary>
public void Dispose()
{
if (!_disposed)
{
_database?.Dispose();
_disposed = true;
}
}
}
}