コード例 #1
0
ファイル: NamespaceName.cs プロジェクト: eaba/SharpPulsar
 /// <summary>
 /// Compose the topic name from namespace + topic.
 /// </summary>
 /// <param name="domain"> </param>
 /// <param name="topic">
 /// @return </param>
 internal string GetTopicName(TopicDomain domain, string topic)
 {
     try
     {
         Condition.CheckNotNull(domain);
         NamedEntity.CheckName(topic);
         return($"{domain.ToString()}://{_namespace}/{topic}");
     }
     catch (NullReferenceException e)
     {
         throw new ArgumentException("Null pointer is invalid as domain for topic.", e);
     }
 }
コード例 #2
0
        private TopicName(string completeTopicName)
        {
            try
            {
                // The topic name can be in two different forms, one is fully qualified topic name,
                // the other one is short topic name
                if (!completeTopicName.Contains("://"))
                {
                    // The short topic name can be:
                    // - <topic>
                    // - <property>/<namespace>/<topic>
                    var prts = completeTopicName.Split('/');
                    if (prts.Length == 3)
                    {
                        completeTopicName = TopicDomain.Persistent.Value() + "://" + completeTopicName;
                    }
                    else if (prts.Length == 1)
                    {
                        completeTopicName = TopicDomain.Persistent.Value() + "://" + PublicTenant + "/" + DefaultNamespace + "/" + prts[0];
                    }
                    else
                    {
                        throw new ArgumentException("Invalid short topic name '" + completeTopicName + "', it should be in the format of " + "<tenant>/<namespace>/<topic> or <topic>");
                    }
                }

                // The fully qualified topic name can be in two different forms:
                // new:    persistent://tenant/namespace/topic
                // legacy: persistent://tenant/cluster/namespace/topic

                IList <string> parts = completeTopicName.Split("://").Take(2).ToList();
                _domain = TopicDomain.GetEnum(parts[0]);

                string rest = parts[1];

                // The rest of the name can be in different forms:
                // new:    tenant/namespace/<localName>
                // legacy: tenant/cluster/namespace/<localName>
                // Examples of localName:
                // 1. some/name/xyz//
                // 2. /xyz-123/feeder-2


                parts = rest.Split("/").Take(4).ToList();
                if (parts.Count == 3)
                {
                    // New topic name without cluster name
                    _tenant           = parts[0];
                    _cluster          = null;
                    _namespacePortion = parts[1];
                    _localName        = parts[2];
                    _partitionIndex   = GetPartitionIndex(completeTopicName);
                    _namespaceName    = NamespaceName.Get(_tenant, _namespacePortion);
                }
                else if (parts.Count == 4)
                {
                    // Legacy topic name that includes cluster name
                    _tenant           = parts[0];
                    _cluster          = parts[1];
                    _namespacePortion = parts[2];
                    _localName        = parts[3];
                    _partitionIndex   = GetPartitionIndex(completeTopicName);
                    _namespaceName    = NamespaceName.Get(_tenant, _cluster, _namespacePortion);
                }
                else
                {
                    throw new ArgumentException("Invalid topic name: " + completeTopicName);
                }


                if (ReferenceEquals(_localName, null) || _localName.Length == 0)
                {
                    throw new ArgumentException("Invalid topic name: " + completeTopicName);
                }
            }
            catch (NullReferenceException e)
            {
                throw new ArgumentException("Invalid topic name: " + completeTopicName, e);
            }
            if (V2)
            {
                _completeTopicName = $"{_domain}://{_tenant}/{_namespacePortion}/{_localName}";
            }
            else
            {
                _completeTopicName = $"{_domain}://{_tenant}/{_cluster}/{_namespacePortion}/{_localName}";
            }
        }