Пример #1
0
        /// <summary>
        /// Clears all entries in the <see cref="FlatMapKeyValueStore{T}"/> and loads all entries
        /// from the database archive file, if it exists.
        /// </summary>
        /// <returns>The <see cref="Result"/> of the operation.</returns>
        public Result Load()
        {
            // Clear any existing entries.
            _index.Clear();

            var buffer = new AutoBuffer();

            try
            {
                Result rc = ReadArchive(ref buffer);
                if (rc.IsFailure())
                {
                    // If the file is not found, we don't have any entries to load.
                    if (ResultFs.PathNotFound.Includes(rc))
                    {
                        return(Result.Success.LogConverted(rc));
                    }

                    return(rc);
                }

                rc = LoadFrom(buffer.Get());
                if (rc.IsFailure())
                {
                    return(rc);
                }

                return(Result.Success);
            }
            finally
            {
                buffer.Dispose();
            }
        }
Пример #2
0
        /// <summary>
        /// Writes all entries in the <see cref="FlatMapKeyValueStore{T}"/> to a database archive file.
        /// </summary>
        /// <returns>The <see cref="Result"/> of the operation.</returns>
        public Result Save()
        {
            // Create a buffer to hold the archive.
            var    buffer = new AutoBuffer();
            Result rc     = buffer.Initialize(CalculateArchiveSize(), _memoryResourceForAutoBuffers);

            if (rc.IsFailure())
            {
                return(rc);
            }

            try
            {
                // Write the archive to the buffer.
                Span <byte> span   = buffer.Get();
                var         writer = new KeyValueArchiveBufferWriter(span);
                SaveTo(ref writer);

                // Save the buffer to disk.
                return(CommitArchive(span));
            }
            finally
            {
                buffer.Dispose();
            }
        }