示例#1
0
文件: DGNode.cs 项目: zendbit/DBreeze
 /// <summary>
 /// Returns content back
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public T GetContent <T>()
 {
     return(DataTypesConvertor.ConvertBack <T>(this.content));
 }
示例#2
0
文件: DGNode.cs 项目: zendbit/DBreeze
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public T ExternalId <T>()
 {
     return(DataTypesConvertor.ConvertBack <T>(externalId));
 }
示例#3
0
        /// <summary>
        /// Gets resources of the same type as a batch from memory or database (if not yet loaded).
        /// Value instance, when byte[], must stay immutable, please use Dbreeze.Utils.CloneArray
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="resourcesNames"></param>
        /// <param name="resourceSettings">resource extra behaviour</param>
        /// <returns></returns>
        public IDictionary <string, TValue> Select <TValue>(IList <string> resourcesNames, Settings resourceSettings = null)
        {
            Dictionary <string, TValue> ret = new Dictionary <string, TValue>();

            if (resourcesNames == null || resourcesNames.Count < 1)
            {
                return(ret);
            }
            if (resourceSettings == null)
            {
                resourceSettings = _defaultSetting;
            }

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

            //bool ba = typeof(TValue) == typeof(byte[]);

            _sync.EnterUpgradeableReadLock();
            try
            {
                foreach (var rsn in resourcesNames.OrderBy(r => r))
                {
                    if (String.IsNullOrEmpty(rsn))
                    {
                        continue;
                    }
                    rn = _urp + rsn;

                    if (!_d.TryGetValue(rn, out val))
                    {
                        //Value is not found
                        _sync.EnterWriteLock();
                        try
                        {
                            //At this moment value appeared
                            if (_d.TryGetValue(rn, out val))
                            {
                                if (val != null)
                                {
                                    ret[rsn] = DataTypesConvertor.ConvertBack <TValue>(val);
                                }
                                else
                                {
                                    ret[rsn] = default(TValue);
                                }

                                continue;
                            }

                            //trying to get from database
                            byte[] btKey = DataTypesConvertor.ConvertKey <string>(rn);
                            var    row   = LTrie.GetKey(btKey, false, false);
                            if (row.Exists)
                            {
                                val = row.GetFullValue(false);
                                if (val == null)
                                {
                                    if (resourceSettings.HoldInMemory)
                                    {
                                        _d[rn] = null;
                                    }

                                    ret[rsn] = default(TValue);
                                }
                                else
                                {
                                    if (resourceSettings.HoldInMemory)
                                    {
                                        _d[rn] = val;
                                    }
                                    ret[rsn] = DataTypesConvertor.ConvertBack <TValue>(val);
                                }
                            }
                            else
                            {
                                if (resourceSettings.HoldInMemory)
                                {
                                    _d[rn] = null;
                                }

                                ret[rsn] = default(TValue);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.DBREEZE_RESOURCES_CONCERNING, "in Select 1", ex);
                        }
                        finally
                        {
                            _sync.ExitWriteLock();
                        }
                    }
                    else
                    {
                        if (val == null)
                        {
                            ret[rsn] = default(TValue);
                        }
                        else
                        {
                            ret[rsn] = DataTypesConvertor.ConvertBack <TValue>(val);
                        }
                    }
                }//eo foreach
            }
            catch (System.Exception ex)
            {
                throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.DBREEZE_RESOURCES_CONCERNING, "in Select 2", ex);
            }
            finally
            {
                _sync.ExitUpgradeableReadLock();
            }

            return(ret);
        }
示例#4
0
        /// <summary>
        /// Gets resource from memory or database (if not yet loaded)
        /// Value instance, when byte[], must stay immutable, please use Dbreeze.Utils.CloneArray
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="resourceName"></param>
        /// <param name="resourceSettings">resource extra behaviour</param>
        /// <returns></returns>
        public TValue Select <TValue>(string resourceName, Settings resourceSettings = null)
        {
            if (String.IsNullOrEmpty(resourceName))
            {
                return(default(TValue));
            }
            if (resourceSettings == null)
            {
                resourceSettings = _defaultSetting;
            }

            byte[] val = null;
            string rn  = _urp + resourceName;

            _sync.EnterUpgradeableReadLock();
            try
            {
                if (!_d.TryGetValue(rn, out val))
                {
                    //Value is not found
                    _sync.EnterWriteLock();
                    try
                    {
                        //At this moment value appeared
                        if (_d.TryGetValue(rn, out val))
                        {
                            return(val == null ? default(TValue) : DataTypesConvertor.ConvertBack <TValue>(val));
                        }

                        //trying to get from database
                        byte[] btKey = DataTypesConvertor.ConvertKey <string>(rn);
                        var    row   = LTrie.GetKey(btKey, false, false);
                        if (row.Exists)
                        {
                            val = row.GetFullValue(false);
                            if (val == null)
                            {
                                if (resourceSettings.HoldInMemory)
                                {
                                    _d[rn] = null;
                                }

                                return(default(TValue));
                            }
                            else
                            {
                                if (resourceSettings.HoldInMemory)
                                {
                                    _d[rn] = val;
                                }

                                return(DataTypesConvertor.ConvertBack <TValue>(val));
                            }
                        }
                        else
                        {
                            if (resourceSettings.HoldInMemory)
                            {
                                _d[rn] = null;
                            }

                            return(default(TValue));
                        }
                    }
                    catch (Exception ex)
                    {
                        throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.DBREEZE_RESOURCES_CONCERNING, "in Select 1", ex);
                    }
                    finally
                    {
                        _sync.ExitWriteLock();
                    }
                }
                else
                {
                    return(val == null ? default(TValue) : DataTypesConvertor.ConvertBack <TValue>(val));
                }
            }
            catch (System.Exception ex)
            {
                throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.DBREEZE_RESOURCES_CONCERNING, "in Select 2", ex);
            }
            finally
            {
                _sync.ExitUpgradeableReadLock();
            }
        }
示例#5
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
        }