public virtual void AssertSelected(bool outcome, RegistryAdminService.NodeSelector
                                           selector)
        {
            bool select = selector.ShouldSelect("/", status, record);

            NUnit.Framework.Assert.AreEqual(selector.ToString(), outcome, select);
        }
 public AsyncPurge(RegistryAdminService _enclosing, string path, RegistryAdminService.NodeSelector
                   selector, RegistryAdminService.PurgePolicy purgePolicy, BackgroundCallback callback
                   )
 {
     this._enclosing  = _enclosing;
     this.callback    = callback;
     this.selector    = selector;
     this.path        = path;
     this.purgePolicy = purgePolicy;
 }
        public virtual int Purge(string path, RegistryAdminService.NodeSelector selector,
                                 RegistryAdminService.PurgePolicy purgePolicy, BackgroundCallback callback)
        {
            bool toDelete = false;
            // look at self to see if it has a service record
            IDictionary <string, RegistryPathStatus> childEntries;
            ICollection <RegistryPathStatus>         entries;

            try
            {
                // list this path's children
                childEntries = RegistryUtils.StatChildren(this, path);
                entries      = childEntries.Values;
            }
            catch (PathNotFoundException)
            {
                // there's no record here, it may have been deleted already.
                // exit
                return(0);
            }
            try
            {
                RegistryPathStatus registryPathStatus = Stat(path);
                ServiceRecord      serviceRecord      = Resolve(path);
                // there is now an entry here.
                toDelete = selector.ShouldSelect(path, registryPathStatus, serviceRecord);
            }
            catch (EOFException)
            {
            }
            catch (InvalidRecordException)
            {
            }
            catch (NoRecordException)
            {
            }
            catch (PathNotFoundException)
            {
                // ignore
                // ignore
                // ignore
                // there's no record here, it may have been deleted already.
                // exit
                return(0);
            }
            if (toDelete && !entries.IsEmpty())
            {
                if (Log.IsDebugEnabled())
                {
                    Log.Debug("Match on record @ {} with children ", path);
                }
                switch (purgePolicy)
                {
                case RegistryAdminService.PurgePolicy.SkipOnChildren:
                {
                    // there's children
                    // don't do the deletion... continue to next record
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("Skipping deletion");
                    }
                    toDelete = false;
                    break;
                }

                case RegistryAdminService.PurgePolicy.PurgeAll:
                {
                    // mark for deletion
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("Scheduling for deletion with children");
                    }
                    toDelete = true;
                    entries  = new AList <RegistryPathStatus>(0);
                    break;
                }

                case RegistryAdminService.PurgePolicy.FailOnChildren:
                {
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("Failing deletion operation");
                    }
                    throw new PathIsNotEmptyDirectoryException(path);
                }
                }
            }
            int deleteOps = 0;

            if (toDelete)
            {
                try
                {
                    ZkDelete(path, true, callback);
                }
                catch (PathNotFoundException)
                {
                    // sign that the path was deleted during the operation.
                    // this is a no-op, and all children can be skipped
                    return(deleteOps);
                }
                deleteOps++;
            }
            // now go through the children
            foreach (RegistryPathStatus status in entries)
            {
                string childname = status.path;
                string childpath = RegistryPathUtils.Join(path, childname);
                deleteOps += Purge(childpath, selector, purgePolicy, callback);
            }
            return(deleteOps);
        }