4 using System.Collections.Generic;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.IO;
9 using SiliconStudio.Core.Serialization.Serializers;
10 using SiliconStudio.Core.Storage;
12 namespace SiliconStudio.BuildEngine
17 [DataSerializerGlobal(typeof(KeyValuePairSerializer<FileVersionKey, ObjectId>))]
33 public static bool Compact(
string storagePath)
35 FileStream fileStreamExclusive;
38 fileStreamExclusive =
new FileStream(storagePath,
FileMode.Open, FileAccess.ReadWrite, FileShare.None);
47 using (var localTracker =
new FileVersionStorage(fileStreamExclusive) { UseTransaction =
true, AutoLoadNewValues =
false })
49 localTracker.LoadNewValues();
50 var latestVersion =
new Dictionary<string, KeyValuePair<FileVersionKey, ObjectId>>(StringComparer.OrdinalIgnoreCase);
51 foreach (var keyValue
in localTracker.GetValues())
53 var filePath = keyValue.Key.Path;
54 KeyValuePair<FileVersionKey, ObjectId> previousKeyValue;
55 if (!latestVersion.TryGetValue(filePath, out previousKeyValue) || keyValue.Key.LastModifiedDate > previousKeyValue.Key.LastModifiedDate)
57 latestVersion[filePath] = keyValue;
61 localTracker.AddValues(latestVersion.Values);
73 protected override List<KeyValuePair<FileVersionKey, ObjectId>>
ReadEntries(System.IO.Stream localStream)
79 var entries =
new List<KeyValuePair<FileVersionKey, ObjectId>>();
80 while ((line = reader.ReadLine()) != null)
82 var values = line.Split(
'\t');
83 if (values.Length != 4)
91 if (!
long.TryParse(values[1], out dateTime))
93 throw new InvalidOperationException(
"Unable to decode datetime [{0}] when reading file version index".ToFormat(values[1]));
95 key.LastModifiedDate =
new DateTime(dateTime);
97 if (!
long.TryParse(values[2], out key.FileSize))
99 throw new InvalidOperationException(
"Unable to decode filesize [{0}] when reading file version index".ToFormat(values[2]));
101 var objectIdStr = values[3];
106 throw new InvalidOperationException(
"Unable to decode objectid [{0}] when reading file version index".ToFormat(objectIdStr));
109 var entry =
new KeyValuePair<FileVersionKey, ObjectId>(key, objectId);
115 protected override void WriteEntry(
Stream localStream, KeyValuePair<FileVersionKey, ObjectId> value)
118 var line = string.Format(
"{0}\t{1}\t{2}\t{3}\n", key.Path, key.LastModifiedDate.Ticks, key.FileSize, value.Value);
119 var bytes = Encoding.UTF8.GetBytes(line);
120 localStream.Write(bytes, 0, bytes.Length);
Storage used for FileVersionKey associated with an ObjectId.
FileVersionStorage(Stream stream)
Initializes a new instance of the FileVersionStorage class.
System.Text.Encoding Encoding
System.IO.FileMode FileMode
override void WriteEntry(Stream localStream, KeyValuePair< FileVersionKey, ObjectId > value)
A hash to uniquely identify data.
static bool Compact(string storagePath)
Compacts the specified storage path.
override List< KeyValuePair< FileVersionKey, ObjectId > > ReadEntries(System.IO.Stream localStream)
static bool TryParse(string input, out ObjectId result)
Tries to parse an ObjectId from a string.