Skip to content
This repository was archived by the owner on May 10, 2020. It is now read-only.

Commit b82bff8

Browse files
committed
Add Max Id to metadata
1 parent c6ce23c commit b82bff8

File tree

3 files changed

+80
-33
lines changed

3 files changed

+80
-33
lines changed

src/BlazorDB/Storage/Metadata.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ internal class Metadata
88
public List<Guid> Guids { get; set; }
99
public string ModelName { get; set; }
1010
public string ContextName { get; set; }
11+
public int MaxId { get; set; }
1112
}
1213
}

src/BlazorDB/Storage/StorageManagerSave.cs

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,90 +15,131 @@ public int SaveContextToLocalStorage(StorageContext context)
1515
var contextType = context.GetType();
1616
//Logger.ContextSaved(contextType);
1717
var storageSets = StorageManagerUtil.GetStorageSets(contextType);
18-
total = SaveStorageSets(context, total, contextType, storageSets);
18+
var metadataMap = LoadMetadataList(context, storageSets, contextType);
19+
total = SaveStorageSets(context, total, contextType, storageSets, metadataMap);
1920
//Logger.EndGroup();
2021
return total;
2122
}
2223

24+
private static IReadOnlyDictionary<string, Metadata> LoadMetadataList(StorageContext context, IEnumerable<PropertyInfo> storageSets, Type contextType)
25+
{
26+
var map = new Dictionary<string, Metadata>();
27+
foreach (var prop in storageSets)
28+
{
29+
var modelType = prop.PropertyType.GetGenericArguments()[0];
30+
var storageTableName = Util.GetStorageTableName(contextType, modelType);
31+
var metadata = StorageManagerUtil.LoadMetadata(storageTableName) ?? new Metadata
32+
{
33+
Guids = new List<Guid>(),
34+
ContextName = Util.GetFullyQualifiedTypeName(context.GetType()),
35+
ModelName = Util.GetFullyQualifiedTypeName(modelType)
36+
};
37+
map.Add(Util.GetFullyQualifiedTypeName(modelType), metadata);
38+
}
39+
40+
return map;
41+
}
42+
2343
private static int SaveStorageSets(StorageContext context, int total, Type contextType,
24-
List<PropertyInfo> storageSets)
44+
List<PropertyInfo> storageSets, IReadOnlyDictionary<string, Metadata> metadataMap)
2545
{
2646
foreach (var prop in storageSets)
2747
{
2848
var storageSetValue = prop.GetValue(context);
2949
var modelType = prop.PropertyType.GetGenericArguments()[0];
3050
var storageTableName = Util.GetStorageTableName(contextType, modelType);
31-
EnsureAllModelsHaveIds(storageSetValue, modelType);
32-
EnsureAllAssociationsHaveIds(context, storageSetValue, modelType, storageSets);
33-
51+
52+
EnsureAllModelsHaveIds(storageSetValue, modelType, metadataMap);
53+
EnsureAllAssociationsHaveIds(context, storageSetValue, modelType, storageSets, metadataMap);
3454

35-
var guids = SaveModels(context, storageSetValue, modelType, storageTableName, storageSets);
55+
var guids = SaveModels(storageSetValue, modelType, storageTableName, storageSets);
3656
total += guids.Count;
37-
var oldMetadata = StorageManagerUtil.LoadMetadata(storageTableName);
38-
SaveMetadata(storageTableName, guids, contextType, modelType);
39-
if (oldMetadata != null) DeleteOldModelsFromStorage(oldMetadata, storageTableName);
57+
var oldMetadata = metadataMap[Util.GetFullyQualifiedTypeName(modelType)];
58+
SaveMetadata(storageTableName, guids, contextType, modelType, oldMetadata);
59+
DeleteOldModelsFromStorage(oldMetadata, storageTableName);
4060
Logger.StorageSetSaved(modelType, guids.Count);
4161
}
4262

4363
return total;
4464
}
4565

46-
private static void EnsureAllAssociationsHaveIds(StorageContext context, object storageSetValue, Type modelType, List<PropertyInfo> storageSets)
66+
private static void EnsureAllAssociationsHaveIds(StorageContext context, object storageSetValue, Type modelType, List<PropertyInfo> storageSets, IReadOnlyDictionary<string, Metadata> metadataMap)
4767
{
4868
var storageSetType = StorageManagerUtil.GenericStorageSetType.MakeGenericType(modelType);
4969
var method = storageSetType.GetMethod(StorageManagerUtil.GetEnumerator);
5070
var enumerator = (IEnumerator)method.Invoke(storageSetValue, new object[] { });
51-
enumerator.Reset();
5271
while (enumerator.MoveNext())
5372
{
5473
var model = enumerator.Current;
5574
foreach (var prop in model.GetType().GetProperties())
5675
{
5776
if (prop.GetValue(model) == null || (!StorageManagerUtil.IsInContext(storageSets, prop) &&
5877
!StorageManagerUtil.IsListInContext(storageSets, prop))) continue;
59-
if (StorageManagerUtil.IsInContext(storageSets, prop)) EnsureOneAssociationHasId(context, model, prop, storageSets);
60-
61-
78+
if (StorageManagerUtil.IsInContext(storageSets, prop)) EnsureOneAssociationHasId(context, prop.GetValue(model), prop.PropertyType, storageSets, metadataMap);
79+
if (StorageManagerUtil.IsListInContext(storageSets, prop)) EnsureManyAssociationHasId(context, prop.GetValue(model), prop, storageSets, metadataMap);
6280
}
6381
}
6482
}
6583

66-
private static void EnsureOneAssociationHasId(StorageContext context, object model, PropertyInfo prop, List<PropertyInfo> storageSets)
84+
private static void EnsureManyAssociationHasId(StorageContext context, object listObject, PropertyInfo prop, List<PropertyInfo> storageSets, IReadOnlyDictionary<string, Metadata> metadataMap)
85+
{
86+
Console.WriteLine("listObject: {0}", listObject);
87+
var method = listObject.GetType().GetMethod(StorageManagerUtil.GetEnumerator);
88+
var enumerator = (IEnumerator)method.Invoke(listObject, new object[] { });
89+
while (enumerator.MoveNext())
90+
{
91+
var model = enumerator.Current;
92+
EnsureOneAssociationHasId(context, model, prop.PropertyType.GetGenericArguments()[0], storageSets, metadataMap);
93+
}
94+
}
95+
96+
private static void EnsureOneAssociationHasId(StorageContext context, object associatedModel, Type propType, List<PropertyInfo> storageSets, IReadOnlyDictionary<string, Metadata> metadataMap)
6797
{
68-
var associatedModel = prop.GetValue(model);
6998
var idProp = GetIdProperty(associatedModel);
7099
var id = Convert.ToString(idProp.GetValue(associatedModel));
100+
var metadata = metadataMap[Util.GetFullyQualifiedTypeName(propType)];
101+
Console.WriteLine("metadata: {0}", metadata.ModelName);
102+
Console.WriteLine("maxId: {0}", metadata.MaxId);
71103
Console.WriteLine("id: {0}", id);
72-
if (id == "0") SaveAssociationModel(context, associatedModel, prop, storageSets);
104+
if (id == "0")
105+
{
106+
metadata.MaxId = metadata.MaxId + 1;
107+
SaveAssociationModel(context, associatedModel, propType, storageSets, metadata.MaxId);
108+
}
109+
Console.WriteLine("maxId: {0}", metadata.MaxId);
73110
}
74111

75-
private static void EnsureAllModelsHaveIds(object storageSetValue, Type modelType)
112+
private static void EnsureAllModelsHaveIds(object storageSetValue, Type modelType, IReadOnlyDictionary<string, Metadata> metadataMap)
76113
{
77114
var storageSetType = StorageManagerUtil.GenericStorageSetType.MakeGenericType(modelType);
78115
var method = storageSetType.GetMethod(StorageManagerUtil.GetEnumerator);
116+
var metadata = metadataMap[Util.GetFullyQualifiedTypeName(modelType)];
79117
var enumerator = (IEnumerator)method.Invoke(storageSetValue, new object[] { });
80-
var maxId = GetMaxId(enumerator);
81-
enumerator.Reset();
82118
while (enumerator.MoveNext())
83119
{
84120
var model = enumerator.Current;
85-
if (GetId(model) == 0) SetId(model, ++maxId);
121+
if (GetId(model) == 0)
122+
{
123+
metadata.MaxId = metadata.MaxId + 1;
124+
SetId(model, metadata.MaxId);
125+
}
86126
}
87127
}
88128

89-
private static void SaveMetadata(string storageTableName, List<Guid> guids, Type context, Type modelType)
129+
private static void SaveMetadata(string storageTableName, List<Guid> guids, Type context, Type modelType, Metadata oldMetadata)
90130
{
91131
var metadata = new Metadata
92132
{
93133
Guids = guids,
94134
ContextName = Util.GetFullyQualifiedTypeName(context),
95-
ModelName = Util.GetFullyQualifiedTypeName(modelType)
135+
ModelName = Util.GetFullyQualifiedTypeName(modelType),
136+
MaxId = oldMetadata.MaxId
96137
};
97138
var name = $"{storageTableName}-{StorageManagerUtil.Metadata}";
98139
BlazorDBInterop.SetItem(name, JsonUtil.Serialize(metadata), false);
99140
}
100141

101-
private static List<Guid> SaveModels(StorageContext context, object storageSetValue, Type modelType, string storageTableName,
142+
private static List<Guid> SaveModels(object storageSetValue, Type modelType, string storageTableName,
102143
List<PropertyInfo> storageSets)
103144
{
104145
var guids = new List<Guid>();
@@ -111,7 +152,7 @@ private static List<Guid> SaveModels(StorageContext context, object storageSetVa
111152
guids.Add(guid);
112153
var model = enumerator.Current;
113154
var name = $"{storageTableName}-{guid}";
114-
var serializedModel = ScanModelForAssociations(context, storageSetValue, model, storageSets, JsonUtil.Serialize(model));
155+
var serializedModel = ScanModelForAssociations(model, storageSets, JsonUtil.Serialize(model));
115156
BlazorDBInterop.SetItem(name, serializedModel, false);
116157
}
117158

@@ -140,15 +181,15 @@ private static void DeleteOldModelsFromStorage(Metadata metadata, string storage
140181
}
141182
}
142183

143-
private static string ScanModelForAssociations(StorageContext context, object storageSetValue, object model, List<PropertyInfo> storageSets,
184+
private static string ScanModelForAssociations(object model, List<PropertyInfo> storageSets,
144185
string serializedModel)
145186
{
146187
var result = serializedModel;
147188
foreach (var prop in model.GetType().GetProperties())
148189
{
149190
if (prop.GetValue(model) == null || (!StorageManagerUtil.IsInContext(storageSets, prop) &&
150191
!StorageManagerUtil.IsListInContext(storageSets, prop))) continue;
151-
if (StorageManagerUtil.IsInContext(storageSets, prop)) result = FixOneAssociation(context, storageSetValue, model, prop, result, storageSets);
192+
if (StorageManagerUtil.IsInContext(storageSets, prop)) result = FixOneAssociation(model, prop, result);
152193
if (StorageManagerUtil.IsListInContext(storageSets, prop))
153194
result = FixManyAssociation(model, prop, result);
154195
}
@@ -170,7 +211,7 @@ private static string FixManyAssociation(object model, PropertyInfo prop, string
170211
return result;
171212
}
172213

173-
private static string FixOneAssociation(StorageContext context, object storageSetValue, object model, PropertyInfo prop, string result, List<PropertyInfo> storageSets)
214+
private static string FixOneAssociation(object model, PropertyInfo prop, string result)
174215
{
175216
var associatedModel = prop.GetValue(model);
176217
var idProp = GetIdProperty(associatedModel);
@@ -182,22 +223,24 @@ private static string FixOneAssociation(StorageContext context, object storageSe
182223
return result;
183224
}
184225

185-
private static string SaveAssociationModel(StorageContext context, object associatedModel, PropertyInfo prop, List<PropertyInfo> storageSets)
226+
private static int SaveAssociationModel(StorageContext context, object associatedModel, Type propType, IEnumerable<PropertyInfo> storageSets, int id)
186227
{
228+
Console.WriteLine("SaveAssociationModel id: {0}", id);
187229
Console.WriteLine("associatedModel: {0}", associatedModel);
230+
Console.WriteLine("propType: {0}", propType);
188231
var q = from p in storageSets
189-
where p.PropertyType.GetGenericArguments()[0] == prop.PropertyType
190-
select p;
232+
where p.PropertyType.GetGenericArguments()[0] == propType
233+
select p;
191234
var storeageSetProp = q.Single();
192235
Console.WriteLine("storeageSetProp: {0}", storeageSetProp);
193236
var storeageSet = storeageSetProp.GetValue(context);
194237
Console.WriteLine("storeageSet: {0}", storeageSet);
195238
var listProp = storeageSet.GetType().GetProperty(StorageManagerUtil.List, StorageManagerUtil.Flags);
196239
var list = listProp.GetValue(storeageSet);
197240
var addMethod = list.GetType().GetMethod(StorageManagerUtil.Add);
198-
SetId(associatedModel, 999);
241+
SetId(associatedModel, id);
199242
addMethod.Invoke(list, new[] { associatedModel });
200-
return "999";
243+
return id;
201244
}
202245

203246
private static string ReplaceModelWithId(string result, string serializedItem, string id)

src/Sample/Pages/Index.cshtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
{
2727
//In this case, address gets serailized as part of the object because Address is not in the Context
2828
var person = new Person { FirstName = "John", LastName = "Smith", HomeAddress = new Address { Street = "221 Baker Street", City = "This should be part of the json, since address is not in context (Very long city)" } };
29+
var address1 = new Address { Street = "Many test 1", City = "Saved as a reference" };
30+
var address2 = new Address { Street = "Many test 2", City = "Saved as a reference" };
31+
person.OtherAddresses = new List<Address> { address1, address2 };
2932
Context.People.Add(person);
3033
}
3134

0 commit comments

Comments
 (0)