예제 #1
0
        /// <summary>
        /// SelectStartsWith.
        /// Value instance, when byte[], must stay immutable, please use Dbreeze.Utils.CloneArray
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="resourceNameStartsWith"></param>
        /// <param name="resourceSettings"></param>
        /// <returns></returns>
        public IEnumerable <KeyValuePair <string, TValue> > SelectStartsWith <TValue>(string resourceNameStartsWith, Settings resourceSettings = null)
        {
            if (!String.IsNullOrEmpty(resourceNameStartsWith))
            {
                if (resourceSettings == null)
                {
                    resourceSettings = _defaultSetting;
                }

                byte[] val   = null;
                string rn    = String.Empty;
                byte[] btKey = null;



                _sync.EnterUpgradeableReadLock();

                btKey = DataTypesConvertor.ConvertKey <string>(_urp + resourceNameStartsWith);
                var q = LTrie.IterateForwardStartsWith(btKey, true, false);
                if (!resourceSettings.SortingAscending)
                {
                    q = LTrie.IterateBackwardStartsWith(btKey, true, false);
                }

                foreach (var el in q)
                {
                    rn = el.Key.UTF8_GetString();

                    if (!_d.TryGetValue(rn, out val))
                    {
                        val = el.GetFullValue(false);

                        if (resourceSettings.HoldInMemory)
                        {
                            _sync.EnterWriteLock();
                            try
                            {
                                _d[rn] = val;
                            }
                            catch (Exception)
                            { }
                            finally
                            {
                                _sync.ExitWriteLock();
                            }
                        }
                    }

                    //no try..catch for yield return
                    yield return(new KeyValuePair <string, TValue>(rn.Substring(1), val == null ? default(TValue) : DataTypesConvertor.ConvertBack <TValue>(val)));
                }

                _sync.ExitUpgradeableReadLock();
            }//if is null or
        }