当前位置: 首页>>代码示例>>C#>>正文


C# SortedSet.HeadSet方法代码示例

本文整理汇总了C#中SortedSet.HeadSet方法的典型用法代码示例。如果您正苦于以下问题:C# SortedSet.HeadSet方法的具体用法?C# SortedSet.HeadSet怎么用?C# SortedSet.HeadSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SortedSet的用法示例。


在下文中一共展示了SortedSet.HeadSet方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: LockOperation

        private object LockOperation()
        {
            do
            {
                if (id == null)
                {
                    long sessionId = Zookeeper.SessionId;
                    string prefix = "x-" + sessionId + "-";
                    FindPrefixInChildren(prefix, Zookeeper, dir);
                    idName = new ZNodeName(id);
                }

                if (id == null) continue;

                List<string> names = Zookeeper.GetChildren(dir, false);
                if (names.IsEmpty())
                {
                    LOG.Warn("No children in: " + dir + " when we've just " +
                             "created one! Lets recreate it...");
                    // lets force the recreation of the id
                    id = null;
                }
                else
                {
                    // lets sort them explicitly (though they do seem to come back in order ususally :)
                    var sortedNames = new SortedSet<ZNodeName>();
                    foreach (string name in names)
                    {
                        sortedNames.Add(new ZNodeName(dir.Combine(name)));
                    }
                    ownerId = sortedNames.First().Name;
                    SortedSet<ZNodeName> lessThanMe = sortedNames.HeadSet(idName);
                    if (!lessThanMe.IsEmpty())
                    {
                        ZNodeName lastChildName = lessThanMe.Last();
                        lastChildId = lastChildName.Name;
                        if (LOG.IsDebugEnabled)
                        {
                            LOG.Debug("watching less than me node: " + lastChildId);
                        }
                        Stat stat = Zookeeper.Exists(lastChildId, new LockWatcher(this));
                        if (stat != null)
                        {
                            return false;
                        }

                        LOG.Warn("Could not find the stats for less than me: " + lastChildName.Name);
                    }
                    else
                    {
                        if (Owner)
                        {
                            OnLockAcquired();
                            return true;
                        }
                    }
                }
            } while (id == null);
            return false;
        }
开发者ID:chriseldredge,项目名称:zookeeper,代码行数:60,代码来源:WriteLock.cs

示例2: RunForLeader

        public bool RunForLeader()
        {
            long sessionId = Zookeeper.SessionId;
            string prefix = "election-" + sessionId + "-";
            var names = Zookeeper.GetChildren(_path, false);
            // See whether we have already run for election in this process
            foreach (string name in names)
            {
                if (name.StartsWith(prefix))
                {
                    _id = name;
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("Found id created last time: {0}", _id);
                    }
                }
            }

            if (_id == null)
            {
                _id = Zookeeper.Create(_path.Combine(prefix), _data, Acl, CreateMode.EphemeralSequential);

                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("Created id: {0}", _id);
                }
            }

            _idName = new ZNodeName(_id);

            names = Zookeeper.GetChildren(_path, false);
            var sortedNames = new SortedSet<ZNodeName>();
            foreach (var name in names)
            {
                sortedNames.Add(new ZNodeName(name));
            }

            var priors = sortedNames.HeadSet(_idName);

            if (priors.Count == 0)
            {
                throw new InvalidOperationException("Count of priors is 0, but should at least include this node.");
            }

            if (priors.Count == 1)
            {
                IsOwner = true;
                _watcher.TakeLeadership();
                return true;
            }
            // only watch the node directly before us
            ZNodeName penultimate = null, last = null;
            foreach (var name in sortedNames)
            {
                penultimate = last;
                last = name;
            }
            if (penultimate == null)
            {
                throw new InvalidOperationException("Penultimate value in priors is null, but count shoudl have been at least 2.");
            }
            var watchPath = _path.Combine(penultimate.Name);
            if (Zookeeper.Exists(watchPath, new LeaderWatcher(this, watchPath, _watcher)) == null)
            {
                IsOwner = true;
                _watcher.TakeLeadership();
                return true;
            }
            return false;
        }
开发者ID:chang892886597,项目名称:ZooKeeperNetClient,代码行数:70,代码来源:LeaderElection.cs


注:本文中的SortedSet.HeadSet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。