Ejemplo n.º 1
0
        private void Read()
        {
            string manifestFile = Config.ManifestFile(_baseFileName);

            if (!File.Exists(manifestFile))
            {
                AddLastManifest(new ManifestImmutable(this));
                return;
            }

            FileStream   fs     = new FileStream(manifestFile, FileMode.Open, FileAccess.Read, FileShare.None, 1024, false);
            BinaryReader reader = new BinaryReader(fs);

            try {
                var m = new ManifestImmutable(this);

                // 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);

                m.ReadManifestContents(reader);
                AddLastManifest(m);
            } catch (Exception ex) {
                LogMessage("Error reading manifest file: {0} - {1}", _baseFileName, ex.Message);
            } finally {
                reader.Close();
            }
        }
Ejemplo n.º 2
0
        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();
            }
        }
Ejemplo n.º 3
0
 private void AddLastManifest(ManifestImmutable m)
 {
     lock (manifestLock) {
         _manifests.AddLast(m);
         _razorFormatVersion = null;
     }
 }
Ejemplo n.º 4
0
 private void ReleaseManifest(ManifestImmutable manifest)
 {
     lock (manifestLock) {
         _manifests.Remove(manifest);
         _razorFormatVersion = null;
     }
 }
Ejemplo n.º 5
0
 private void CommitManifest(ManifestImmutable manifest)
 {
     lock (manifestLock) {
         Write(manifest);
         AddLastManifest(manifest);
     }
 }
Ejemplo n.º 6
0
        internal 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);
        }
Ejemplo n.º 7
0
        private 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);
                }
            }
        }
Ejemplo n.º 8
0
        public TableEnumerator(int level, RazorCache rzrCache, string baseFileName, ManifestImmutable mft, Key key)
        {
            Cache           = rzrCache;
            BaseFileName    = baseFileName;
            ActiveManifest  = mft;
            StartKey        = key;
            Level           = level;
            StopEnumerating = false;

            var firstPage = ActiveManifest.FindPageForIndex(level, 0);

            if (firstPage != null && key.CompareTo(firstPage.FirstKey) < 0)
            {
                StartPageIndex = 0;
            }
            else
            {
                StartPageIndex = ActiveManifest.FindPageIndexForKey(Level, StartKey);
            }

            InitEnumerator();
        }
Ejemplo n.º 9
0
        public static IEnumerable <KeyValuePair <Key, Value> > Enumerate(int level, RazorCache rzrCache, string baseFileName, ManifestImmutable mft, Key key)
        {
            var enumerator = new TableEnumerator(level, rzrCache, baseFileName, mft, key);

            while (enumerator.MoveNext())
            {
                yield return(enumerator.Current);
            }
        }