示例#1
0
        /// <summary>
        /// Creates a new instance of <see cref="CmdStore"/> that
        /// represents a brand new command store.
        /// </summary>
        /// <param name="storeName">The name for the new store (could be a directory
        /// path if <paramref name="storeType"/> is <see cref="StoreType.File"/>)</param>
        /// <param name="storeType">The type of store to create.</param>
        /// <returns>The newly created command store.</returns>
        public static CmdStore Create(string storeName, StoreType storeType)
        {
            Guid storeId = Guid.NewGuid();

            var c = new CmdData(nameof(ICreateStore), 0, DateTime.UtcNow);

            c.Add(nameof(ICreateStore.StoreId), storeId);
            c.Add(nameof(ICreateStore.Name), storeName);
            c.Add(nameof(ICreateStore.Type), storeType);

            var handler = new CreateStoreHandler(c);
            var ec      = new ExecutionContext();

            handler.Process(ec);
            return(ec.Store);
        }
示例#2
0
        /// <summary>
        /// Creates a local branch from this remote branch.
        /// </summary>
        /// <param name="ec">The current execution context</param>
        /// <returns>The newly created branch which the execution context
        /// now refers to.</returns>
        /// <exception cref="ApplicationException">This is not a remote branch,
        /// or the branch already has at least one local child branch.</exception>
        public Branch CreateLocal(ExecutionContext ec)
        {
            if (!IsRemote)
            {
                throw new ApplicationException("Branch is already local");
            }

            if (!CanBranch)
            {
                throw new ApplicationException("A branch received from a clone cannot be branched");
            }

            // Search for a local child
            if (Children.Any(x => !x.IsRemote))
            {
                throw new ApplicationException("Remote branch already has at least one child");
            }

            // Create a new child branch
            var data = new CmdData(
                cmdName: nameof(ICreateBranch),
                sequence: 0,
                createdAt: DateTime.UtcNow);

            // + is a valid folder name, but may well not work in other types of store
            string childName = "+";

            data.Add(nameof(ICreateBranch.Name), childName);
            data.Add(nameof(ICreateBranch.CommandCount), Info.CommandCount);

            var cb = new CreateBranchHandler(data);

            cb.Process(ec);
            Branch result = GetChild(childName);

            // And switch to it
            ec.Store.SwitchTo(result);
            Log.Info($"Created (and switched to) {result}");

            return(result);
        }