internal void SaveTextStream(string name, Action <TextWriter> action)
        {
            _ectx.Check(InRepository, "Can't save a text stream when writing to a single stream");
            _ectx.CheckNonEmpty(name, nameof(name));
            _ectx.CheckValue(action, nameof(action));

            // I verified in the CLR source that the default buffer size is 1024. It's unfortunate
            // that to set leaveOpen to true, we have to specify the buffer size....
            using (var ent = Repository.CreateEntry(Directory, name))
                using (var writer = Utils.OpenWriter(ent.Stream))
                {
                    action(writer);
                }
        }
        /// <summary>
        /// Create a <see cref="ModelSaveContext"/> supporting saving to a repository, for implementors of <see cref="ICanSaveModel"/>.
        /// </summary>
        internal ModelSaveContext(RepositoryWriter rep, string dir, string name)
        {
            Contracts.CheckValue(rep, nameof(rep));
            Repository = rep;
            _ectx      = rep.ExceptionContext;

            _ectx.CheckValueOrNull(dir);
            _ectx.CheckNonEmpty(name, nameof(name));

            Directory = dir;
            Strings   = new NormStr.Pool();

            _ent = rep.CreateEntry(dir, name);
            try
            {
                Writer = new BinaryWriter(_ent.Stream, Encoding.UTF8, leaveOpen: true);
                try
                {
                    ModelHeader.BeginWrite(Writer, out FpMin, out Header);
                }
                catch
                {
                    Writer.Dispose();
                    throw;
                }
            }
            catch
            {
                _ent.Dispose();
                throw;
            }
        }
Beispiel #3
0
        internal static void SaveModel <T>(RepositoryWriter rep, T value, string path)
            where T : class
        {
            if (value == null)
            {
                return;
            }

            var sm = value as ICanSaveModel;

            if (sm != null)
            {
                using (var ctx = new ModelSaveContext(rep, path, ModelLoadContext.ModelStreamName))
                {
                    sm.Save(ctx);
                    ctx.Done();
                }
                return;
            }

            var sb = value as ICanSaveInBinaryFormat;

            if (sb != null)
            {
                using (var ent = rep.CreateEntry(path, ModelLoadContext.NameBinary))
                    using (var writer = new BinaryWriter(ent.Stream, Encoding.UTF8, leaveOpen: true))
                    {
                        sb.SaveAsBinary(writer);
                    }
            }
        }
        public static RepositoryWriter CreateNew(Stream stream, IExceptionContext ectx = null, bool useFileSystem = true)
        {
            Contracts.CheckValueOrNull(ectx);
            ectx.CheckValue(stream, nameof(stream));
            var rep = new RepositoryWriter(stream, ectx, useFileSystem);

            using (var ent = rep.CreateEntry(DirTrainingInfo, "Version.txt"))
                using (var writer = Utils.OpenWriter(ent.Stream))
                    writer.WriteLine(GetProductVersion());
            return(rep);
        }
Beispiel #5
0
        private void SaveInputSchema(DataViewSchema inputSchema, RepositoryWriter rep)
        {
            _env.AssertValueOrNull(inputSchema);
            _env.AssertValue(rep);

            if (inputSchema == null)
            {
                return;
            }

            using (var ch = _env.Start("Saving Schema"))
            {
                var entry = rep.CreateEntry(SchemaEntryName);
                var saver = new BinarySaver(_env, new BinarySaver.Arguments {
                    Silent = true
                });
                DataSaverUtils.SaveDataView(ch, saver, new EmptyDataView(_env, inputSchema), entry.Stream, keepHidden: true);
            }
        }