Ejemplo n.º 1
0
        /// <summary>
        /// Loads data view (loader and transforms) from <paramref name="rep"/> if <paramref name="loadTransforms"/> is set to true,
        /// otherwise loads loader only.
        /// </summary>
        public static ILegacyDataLoader LoadLoader(IHostEnvironment env, RepositoryReader rep, IMultiStreamSource files, bool loadTransforms)
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(rep, nameof(rep));
            env.CheckValue(files, nameof(files));

            ILegacyDataLoader loader;

            // If loadTransforms is false, load the loader only, not the transforms.
            Repository.Entry ent = null;
            string           dir = "";

            if (!loadTransforms)
            {
                ent = rep.OpenEntryOrNull(dir = Path.Combine(DirDataLoaderModel, "Loader"), ModelLoadContext.ModelStreamName);
            }

            if (ent == null) // either loadTransforms is true, or it's not a composite loader
            {
                ent = rep.OpenEntry(dir = DirDataLoaderModel, ModelLoadContext.ModelStreamName);
            }

            env.CheckDecode(ent != null, "Loader is not found.");
            env.AssertNonEmpty(dir);
            using (ent)
            {
                env.Assert(ent.Stream.Position == 0);
                ModelLoadContext.LoadModel <ILegacyDataLoader, SignatureLoadDataLoader>(env, out loader, rep, ent, dir, files);
            }
            return(loader);
        }
Ejemplo n.º 2
0
        /// <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;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a <see cref="ModelSaveContext"/> supporting saving to a single-stream, for implementors of <see cref="ICanSaveInBinaryFormat"/>.
        /// </summary>
        internal ModelSaveContext(BinaryWriter writer, IExceptionContext ectx = null)
        {
            Contracts.AssertValueOrNull(ectx);
            _ectx = ectx;
            _ectx.CheckValue(writer, nameof(writer));

            Repository = null;
            Directory  = null;
            _ent       = null;

            Strings = new NormStr.Pool();
            Writer  = writer;
            ModelHeader.BeginWrite(Writer, out FpMin, out Header);
        }
        /// <summary>
        /// Create a ModelLoadContext supporting loading from a repository, for implementors of ICanSaveModel.
        /// </summary>
        internal ModelLoadContext(RepositoryReader rep, Repository.Entry ent, string dir)
        {
            Contracts.CheckValue(rep, nameof(rep));
            Repository = rep;
            _ectx      = rep.ExceptionContext;

            _ectx.CheckValue(ent, nameof(ent));
            _ectx.CheckValueOrNull(dir);

            Directory = dir;

            Reader = new BinaryReader(ent.Stream, Encoding.UTF8, leaveOpen: true);
            try
            {
                ModelHeader.BeginRead(out FpMin, out Header, out Strings, out LoaderAssemblyName, Reader);
            }
            catch
            {
                Reader.Dispose();
                throw;
            }
        }
Ejemplo n.º 5
0
 public EntryStream(Repository.Entry entry)
 {
     Contracts.CheckValue(entry, nameof(entry));
     Contracts.CheckValue(entry.Stream, nameof(entry.Stream));
     _entry = entry;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Load from the given repository entry using the default loader(s) specified in the header.
 /// </summary>
 public static void LoadModel <TRes, TSig>(IHostEnvironment env, out TRes result, RepositoryReader rep, Repository.Entry ent, string dir, params object[] extra)
     where TRes : class
 {
     Contracts.CheckValue(env, nameof(env));
     env.CheckValue(rep, nameof(rep));
     if (!TryLoadModel <TRes, TSig>(env, out result, rep, ent, dir, extra))
     {
         throw env.ExceptDecode("Couldn't load model: '{0}'", dir);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Try to load from the given repository entry using the default loader(s) specified in the header.
        /// Returns false iff the default loader(s) could not be bound to a compatible loadable class.
        /// </summary>
        private static bool TryLoadModel <TRes, TSig>(IHostEnvironment env, out TRes result, RepositoryReader rep, Repository.Entry ent, string dir, params object[] extra)
            where TRes : class
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckValue(rep, nameof(rep));
            long fp = ent.Stream.Position;

            using (var ctx = new ModelLoadContext(rep, ent, dir))
            {
                env.Assert(fp == ctx.FpMin);
                if (ctx.TryLoadModelCore <TRes, TSig>(env, out result, extra))
                {
                    return(true);
                }
            }

            // TryLoadModelCore should rewind on failure.
            Contracts.Assert(fp == ent.Stream.Position);
            return(false);
        }