public static IEnumerable <ManifestImmutable> ReadAllManifests(string baseFileName) { string manifestFile = Config.ManifestFile(baseFileName); if (!File.Exists(manifestFile)) { throw new FileNotFoundException("Could not find the manifest file.", manifestFile); } FileStream fs = new FileStream(manifestFile, FileMode.Open, FileAccess.Read, FileShare.None, 1024, false); BinaryReader reader = new BinaryReader(fs); try { do { var m = new ManifestImmutable(null); m.ReadManifestContents(reader); yield return(m); reader.ReadInt32(); // Consume the size encoded at the end of each manifest } while (true); } finally { reader.Close(); } }
void Read() { string manifestFile = Config.ManifestFile(_baseFileName); if (!File.Exists(manifestFile)) { _manifests.AddLast(new ManifestImmutable(this)); return; } FileStream fs = new FileStream(manifestFile, FileMode.Open, FileAccess.Read, FileShare.None, 1024, false); BinaryReader reader = new BinaryReader(fs); try { // Get the size of the last manifest block reader.BaseStream.Seek(-4, SeekOrigin.End); int size = reader.ReadInt32(); // Now seek to that position and read it reader.BaseStream.Seek(-size - 4, SeekOrigin.End); var m = new ManifestImmutable(this); m.ReadManifestContents(reader); _manifests.AddLast(m); } catch (Exception ex) { LogMessage("Error reading manifest file: {0}", _baseFileName); ex.GetBaseException(); } finally { reader.Close(); } }
void CommitManifest(ManifestImmutable manifest) { lock (manifestLock) { Write(manifest); _manifests.AddLast(manifest); } }
ManifestImmutable Clone() { // Clone this copy of the manifest. var clone = new ManifestImmutable(_manifest); for (int v = 0; v < _versions.Length; v++) { clone._versions [v] = _versions [v]; } for (int p = 0; p < _pages.Length; p++) { clone._pages [p] = new List <PageRecord> (); foreach (var page in _pages[p]) { clone._pages [p].Add(page); } } return(clone); }
void Write(ManifestImmutable m) { // Get an in-memory copy of the all the bytes that will be written to the manifest var ms = new MemoryStream(); var writer = new BinaryWriter(ms); m.WriteManifestContents(writer); writer.Close(); var manifestBytes = ms.ToArray(); // Increment manifest versions, we're getting ready to write another copy _manifestVersion++; _altManifestVersion++; // Write primary manifest FileMode fileMode = FileMode.Append; if (_manifestVersion > Config.ManifestVersionCount) { fileMode = FileMode.Create; _manifestVersion = 0; } using (FileStream mfs = new FileStream(Config.ManifestFile(BaseFileName), fileMode, FileAccess.Write, FileShare.None, 8, FileOptions.WriteThrough)) { IAsyncResult manifestWrite = mfs.BeginWrite(manifestBytes, 0, manifestBytes.Length, null, null); // Write alternative manifest fileMode = FileMode.Append; if (_altManifestVersion > Config.ManifestVersionCount) { fileMode = FileMode.Create; _altManifestVersion = 0; } using (FileStream afs = new FileStream(Config.AltManifestFile(BaseFileName), fileMode, FileAccess.Write, FileShare.None, 8, FileOptions.WriteThrough)) { IAsyncResult altManifestWrite = afs.BeginWrite(manifestBytes, 0, manifestBytes.Length, null, null); // Wait for i/o's to complete mfs.EndWrite(manifestWrite); afs.EndWrite(altManifestWrite); } } }
void ReleaseManifest(ManifestImmutable manifest) { lock (manifestLock) _manifests.Remove(manifest); }