Ejemplo n.º 1
0
 public void Begin <T>(UnityEngine.Object obj, T asset) where T : IRefCountable
 {
     if (IsFinish || IsDisposed)
     {
         return;
     }
     m_data = asset;
     m_data?.AddRef();
     m_content = obj;
     DoBegin();
 }
Ejemplo n.º 2
0
        public IBucket CreateBucket(string bucketName, string password)
        {
            var exceptions = new List <Exception>();

            lock (_syncObject)
            {
                //shortcircuit in case lock was waited upon because another thread bootstraped same bucket
                if (_buckets.ContainsKey(bucketName))
                {
                    Log.DebugFormat("Bootstraping was already done, returning existing bucket {0}", bucketName);
                    return(_buckets[bucketName]);
                }
                //otherwise bootstrap a new bucket
                var     success = false;
                IBucket bucket  = null;
                foreach (var provider in _configProviders)
                {
                    try
                    {
                        Log.DebugFormat("Trying to bootstrap with {0}.", provider);
                        var           config       = provider.GetConfig(bucketName, password);
                        IRefCountable refCountable = null;
                        switch (config.NodeLocator)
                        {
                        case NodeLocatorEnum.VBucket:
                            bucket = _buckets.GetOrAdd(bucketName,
                                                       name => new CouchbaseBucket(this, bucketName, Converter, Transcoder));
                            refCountable = bucket as IRefCountable;
                            if (refCountable != null)
                            {
                                refCountable.AddRef();
                            }
                            break;

                        case NodeLocatorEnum.Ketama:
                            bucket = _buckets.GetOrAdd(bucketName,
                                                       name => new MemcachedBucket(this, bucketName, Converter, Transcoder));
                            refCountable = bucket as IRefCountable;
                            if (refCountable != null)
                            {
                                refCountable.AddRef();
                            }
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        var configObserver = (IConfigObserver)bucket;
                        if (provider.ObserverExists(configObserver))
                        {
                            Log.DebugFormat("Using existing bootstrap {0}.", provider);
                            _clientConfig.UpdateBootstrapList(config.BucketConfig);

                            configObserver.NotifyConfigChanged(config);
                            success = true;
                            break;
                        }

                        if (provider.RegisterObserver(configObserver) &&
                            _buckets.TryAdd(bucket.Name, bucket))
                        {
                            Log.DebugFormat("Successfully bootstrapped using {0}.", provider);
                            _clientConfig.UpdateBootstrapList(config.BucketConfig);
                            configObserver.NotifyConfigChanged(config);
                            success = true;
                            break;
                        }
                        _clientConfig.UpdateBootstrapList(config.BucketConfig);
                        configObserver.NotifyConfigChanged(config);
                        success = true;
                        break;
                    }
                    catch (Exception e)
                    {
                        Log.Warn(e);
                        exceptions.Add(e);
                    }
                }

                if (!success)
                {
                    throw new AggregateException("Could not bootstrap - check inner exceptions for details.", exceptions);
                }
                return(bucket);
            }
        }
        private IBucket CreateBucketImpl(string bucketName, string password, IAuthenticator authenticator)
        {
            var exceptions = new List <Exception>();

            //shortcircuit in case lock was waited upon because another thread bootstraped same bucket
            if (_buckets.ContainsKey(bucketName))
            {
                IBucket existingBucket = _buckets[bucketName];
                if ((existingBucket as IRefCountable).AddRef() != -1)
                {
                    Log.Debug("Bootstraping was already done, returning existing bucket {0}", bucketName);
                    return(existingBucket); // This is the only short circuit. All other cases fall through to bootstrapping.
                }
                Log.Debug("Bucket dictionary contained disposed bucket. Bootstrapping {0}.", bucketName);
                DestroyBucket(existingBucket);
            }
            //otherwise bootstrap a new bucket
            var     success     = false;
            var     credentials = ResolveCredentials(bucketName, password, authenticator);
            IBucket bucket      = null;

            foreach (var provider in _configProviders)
            {
                try
                {
                    Log.Debug("Trying to bootstrap with {0}.", provider);
                    var           config       = provider.GetConfig(bucketName, credentials.Key, credentials.Value);
                    IRefCountable refCountable = null;
                    switch (config.NodeLocator)
                    {
                    case NodeLocatorEnum.VBucket:
                        bucket = _buckets.GetOrAdd(bucketName,
                                                   name => new CouchbaseBucket(this, bucketName, Converter, Transcoder, authenticator));
                        refCountable = bucket as IRefCountable;
                        if (refCountable != null)
                        {
                            refCountable.AddRef();
                        }
                        break;

                    case NodeLocatorEnum.Ketama:
                        bucket = _buckets.GetOrAdd(bucketName,
                                                   name => new MemcachedBucket(this, bucketName, Converter, Transcoder, authenticator));
                        refCountable = bucket as IRefCountable;
                        if (refCountable != null)
                        {
                            refCountable.AddRef();
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    var configObserver = (IConfigObserver)bucket;
                    if (provider.ObserverExists(configObserver))
                    {
                        Log.Debug("Using existing bootstrap {0}.", provider);
                        _clientConfig.UpdateBootstrapList(config.BucketConfig);

                        configObserver.NotifyConfigChanged(config);
                        success = true;
                        break;
                    }

                    if (provider.RegisterObserver(configObserver) &&
                        _buckets.TryAdd(bucket.Name, bucket))
                    {
                        Log.Debug("Successfully bootstrapped using {0}.", provider);
                        _clientConfig.UpdateBootstrapList(config.BucketConfig);
                        configObserver.NotifyConfigChanged(config);
                        success = true;
                        break;
                    }
                    _clientConfig.UpdateBootstrapList(config.BucketConfig);
                    configObserver.NotifyConfigChanged(config);
                    success = true;
                    break;
                }
                catch (Exception e)
                {
                    Log.Warn(e);

                    if (e is AggregateException aggExp)
                    {
                        exceptions.AddRange(aggExp.InnerExceptions);
                    }
                    exceptions.Add(e);
                }
            }

            if (!success)
            {
                throw new BootstrapException("Could not bootstrap - check inner exceptions for details.", exceptions);
            }
            return(bucket);
        }
        public IBucket CreateBucket(string bucketName, string password)
        {
            var exceptions = new List <Exception>();

            lock (SyncObject)
            {
                var     success = false;
                IBucket bucket  = null;
                foreach (var provider in _configProviders)
                {
                    try
                    {
                        Log.DebugFormat("Trying to boostrap with {0}.", provider);
                        var           config       = provider.GetConfig(bucketName, password);
                        IRefCountable refCountable = null;
                        switch (config.NodeLocator)
                        {
                        case NodeLocatorEnum.VBucket:
                            bucket = _buckets.GetOrAdd(bucketName,
                                                       name => new CouchbaseBucket(this, bucketName, _converter, _serializer));
                            refCountable = bucket as IRefCountable;
                            if (refCountable != null)
                            {
                                refCountable.AddRef();
                            }
                            break;

                        case NodeLocatorEnum.Ketama:
                            bucket = _buckets.GetOrAdd(bucketName,
                                                       name => new MemcachedBucket(this, bucketName, _converter, _serializer));
                            refCountable = bucket as IRefCountable;
                            if (refCountable != null)
                            {
                                refCountable.AddRef();
                            }
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        var configObserver = (IConfigObserver)bucket;
                        if (provider.ObserverExists(configObserver))
                        {
                            Log.DebugFormat("Using existing bootstrap {0}.", provider);
                            UpdateBootstrapList(config.BucketConfig);
                            configObserver.NotifyConfigChanged(config);
                            success = true;
                            break;
                        }

                        if (provider.RegisterObserver(configObserver) &&
                            _buckets.TryAdd(bucket.Name, bucket))
                        {
                            Log.DebugFormat("Successfully boostrap using {0}.", provider);
                            UpdateBootstrapList(config.BucketConfig);
                            configObserver.NotifyConfigChanged(config);
                            success = true;
                            break;
                        }
                        UpdateBootstrapList(config.BucketConfig);
                        configObserver.NotifyConfigChanged(config);
                        success = true;
                        break;
                    }
                    catch (Exception e)
                    {
                        Log.Warn(e);
                        exceptions.Add(e);
                    }
                }

                if (!success)
                {
                    throw new AggregateException("Could not bootstrap - check inner exceptions for details.", exceptions);
                }
                return(bucket);
            }
        }
Ejemplo n.º 5
0
 public BaseObjectRef(IRefCountable baseObject)
 {
     this.baseObject = baseObject?.AddRef() == true ? baseObject : null;
 }
Ejemplo n.º 6
0
 public RefKeeper(IRefCountable counter)
 {
     m_counter = counter;
     m_counter?.AddRef();
     RefCount = 1;
 }