Ejemplo n.º 1
0
        public byte[] ToBytes(Dictionary <string, object> predata)
        {
            List <byte[]> Results     = new List <byte[]>();
            int           TotalLength = 0;

            foreach (var item in this.Fields)
            {
                byte[] result = null;

                object rightvalue = null;
                if (predata.ContainsKey(item.FieldName))
                {
                    var prevalue = predata[item.FieldName];
                    rightvalue = Dynamic.Accessor.ChangeType(prevalue, item.ClrType);
                }
                else
                {
                    rightvalue = IndexHelper.DefaultValue(item.ClrType);
                }

                if (rightvalue != null)
                {
                    result = item.ToBytes(rightvalue);
                }

                if (result == null || result.Length == 0)
                {
                    if (item.Length != int.MaxValue)
                    {
                        result = new byte[item.Length];
                    }
                    else
                    {
                        continue;
                    }
                }

                if (item.Length != int.MaxValue)
                {
                    result = Kooboo.IndexedDB.Helper.KeyHelper.AppendToKeyLength(result, true, item.Length);
                }

                Results.Add(BitConverter.GetBytes(item.FieldNameHash));
                TotalLength += 4;

                int bytelen = item.Length;
                if (bytelen == int.MaxValue)
                {
                    bytelen = result.Length;
                }

                Results.Add(BitConverter.GetBytes(bytelen));
                TotalLength += 4;

                Results.Add(result);
                TotalLength += result.Length;
            }

            byte[] BackValue       = new byte[TotalLength];
            int    currentposition = 0;

            foreach (var item in Results)
            {
                int len = item.Length;
                System.Buffer.BlockCopy(item, 0, BackValue, currentposition, len);
                currentposition += len;
            }

            return(BackValue);
        }