コード例 #1
0
ファイル: DGNode.cs プロジェクト: zendbit/DBreeze
        ///// <summary>
        ///// Any content bound to node from outside
        ///// </summary>
        //public byte[] Content
        //{
        //    get
        //    {
        //        return content;
        //    }
        //    set
        //    {
        //        if (content._ByteArrayEquals(value))
        //            return;
        //        content = value;
        //        contentWasModified = true;
        //    }
        //}

        /// <summary>
        /// Sets content and returns this node back
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="content"></param>
        /// <returns></returns>
        public DGNode SetContent <T>(T content)
        {
            byte[] c = DataTypesConvertor.ConvertValue <T>(content);
            if (c._ByteArrayEquals(this.content))
            {
                return(this);
            }
            this.content       = c;
            contentWasModified = true;
            return(this);
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="indexNumber">Must be unique for every index, will be used for searches</param>
        /// <param name="props">chain of data types forming byte[] index. if null is supplied existing index entry will be removed</param>
        public DBreezeIndex(byte indexNumber, params object[] props)
        {
            AddPrimaryToTheEnd = true;
            PrimaryIndex       = false;

            if (IndexNumber == 0)
            {
                throw new Exception("DBreezeIndex: zero as index number is not allowed!");
            }

            //if(props.Count() == 0)
            //    throw new Exception($"DBreeze.Transaction.InsertObject: index { indexNumber } is incorrectly defined");

            IndexNumber = indexNumber;

            if (props != null)
            {
                foreach (var prop in props)
                {
                    if (prop == null)
                    {
                        AddPrimaryToTheEnd = false;
                        bts.Clear();
                        break;
                    }

                    //throw new Exception($"DBreeze.Transaction.InsertObject: index { indexNumber } is incorrectly defined");

                    bts.Add(DataTypesConvertor.ConvertValue(prop, prop.GetType()));
                }
            }
            else
            {
                AddPrimaryToTheEnd = false;
            }
        }
コード例 #3
0
 /// <summary>
 /// Batch insert of resources where value is a defined DBreeze or DBreeze.CustomSerializer type
 /// </summary>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="resources"></param>
 /// <param name="resourceSettings"></param>
 public void Insert <TValue>(IDictionary <string, TValue> resources, Settings resourceSettings = null)
 {
     this.Insert(resources.ToDictionary(r => r.Key, r => DataTypesConvertor.ConvertValue <TValue>(r.Value)), resourceSettings);
 }
コード例 #4
0
        /// <summary>
        /// Insert resource
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="resourceName"></param>
        /// <param name="resourceObject"></param>
        /// <param name="resourceSettings">resource extra behaviour</param>
        public void Insert <TValue>(string resourceName, TValue resourceObject, Settings resourceSettings = null)
        {
            if (String.IsNullOrEmpty(resourceName))
            {
                return;
            }

            if (resourceSettings == null)
            {
                resourceSettings = _defaultSetting;
            }

            string rn = _urp + resourceName;

            byte[] btKey   = DataTypesConvertor.ConvertKey <string>(rn);
            byte[] btValue = DataTypesConvertor.ConvertValue <TValue>(resourceObject);

            _sync.EnterWriteLock();
            try
            {
                //------- Verification, to prevent storing of the identical value
                if (resourceSettings.InsertWithVerification)
                {
                    byte[] btExVal = null;
                    if (_d.TryGetValue(rn, out btExVal))
                    {
                        if (btExVal._ByteArrayEquals(btValue))
                        {
                            return;
                        }
                    }
                    else
                    {
                        //Grabbing from disk
                        if (resourceSettings.HoldOnDisk)
                        {
                            var row = LTrie.GetKey(btKey, false, false);
                            if (row.Exists)
                            {
                                btExVal = row.GetFullValue(false);
                                if (btExVal._ByteArrayEquals(btValue))
                                {
                                    if (resourceSettings.HoldInMemory)
                                    {
                                        _d[rn] = btValue;
                                    }

                                    return;
                                }
                            }
                        }
                    }
                }
                //-------


                if (resourceSettings.HoldOnDisk)
                {
                    bool cov = LTrie.OverWriteIsAllowed;
                    if (resourceSettings.FastUpdates)
                    {
                        LTrie.OverWriteIsAllowed = false;
                    }

                    LTrie.Add(btKey, btValue);
                    LTrie.Commit();

                    if (resourceSettings.FastUpdates)
                    {
                        LTrie.OverWriteIsAllowed = cov;
                    }
                }

                if (resourceSettings.HoldInMemory)
                {
                    _d[rn] = btValue;
                }
            }
            catch (Exception ex)
            {
                throw DBreezeException.Throw(DBreezeException.eDBreezeExceptions.DBREEZE_RESOURCES_CONCERNING, "in Insert", ex);
            }
            finally
            {
                _sync.ExitWriteLock();
            }


            #region "remark"
            //            if (holdInMemory)
            //            {
            //                _sync.EnterWriteLock();
            //                try
            //                {
            //                    _d[resourceName] = btValue;
            //                }
            //                catch (Exception ex)
            //                {
            //                    throw ex;
            //                }
            //                finally
            //                {
            //                    _sync.ExitWriteLock();
            //                }
            //            }

            //            Action a = () =>
            //            {

            //                _sync.EnterWriteLock();
            //                try
            //                {
            //                    LTrie.Add(btKey, btValue);
            //                    LTrie.Commit();
            //                }
            //                catch (Exception ex)
            //                {
            //                    throw ex;
            //                }
            //                finally
            //                {
            //                    _sync.ExitWriteLock();
            //                }
            //            };

            //#if NET35 || NETr40   //The same must be use for .NET 4.0

            //            new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            //            {
            //                a();
            //            })).Start();
            //#else
            //            System.Threading.Tasks.Task.Run(() => {
            //                a();
            //            });
            //#endif
            #endregion
        }