Ejemplo n.º 1
0
        /// <summary>
        /// Create/Get Data Store for IPersistent typed Key and "Simple typed" Value.
        /// NOTE: Simple type means one of the integer, numeric(decimals,...), char data types, byte array
        /// or a string
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="createIfNotExist"></param>
        /// <param name="container"></param>
        /// <param name="comparer"></param>
        /// <param name="name"></param>
        /// <param name="isDataInKeySegment"> </param>
        /// <param name="mruManaged"> </param>
        /// <returns></returns>
        public ISortedDictionary <TKey, TValue> GetPersistentKey <TKey, TValue>(object container, string name,
                                                                                System.Collections.Generic.IComparer <TKey> comparer = null,
                                                                                bool createIfNotExist   = true,
                                                                                bool isDataInKeySegment = true,
                                                                                bool mruManaged         = true)
            where TKey : IPersistent, new()
        {
            if (!CollectionOnDisk.IsSimpleType(typeof(TValue)))
            {
                throw new ArgumentException(string.Format("Type of TValue ({0}) isn't an SOP simple type.",
                                                          typeof(TValue)));
            }

            BTreeAlgorithm.CurrentOnValueUnpack =
                PersistentTypeKeySimpleValue <TKey, TValue> .Collection_OnKeyUnpack;

            var r2 = CreateDictionary <PersistentTypeKeySimpleValue <TKey, TValue>, TKey, TValue>(createIfNotExist,
                                                                                                  container, name,
                                                                                                  containerDod =>
            {
                var
                r =
                    new PersistentTypeKeySimpleValue
                    <TKey,
                     TValue
                    >(
                        container,
                        comparer,
                        name,
                        DataStoreType
                        .
                        SopOndisk,
                        null,
                        isDataInKeySegment);
                containerDod.
                SetCurrentValueInMemoryData
                    (r);
                return(r);
            }, mruManaged);

            return(r2);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Pack serializes this node to the Stream.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="writer"></param>
        public void Pack(IInternalPersistent parent, System.IO.BinaryWriter writer)
        {
            //** Pack the Node data
            writer.Write(Count);
            writer.Write(HintSizeOnDisk);
            writer.Write(ParentAddress);
            #region a.) Write children node addresses...
            short ii = 0;
            if (ChildrenAddresses != null)
            {
                for (; ii <= Count; ii++)
                {
                    writer.Write(ChildrenAddresses[ii]);
                }
            }
            #endregion
            #region b.) Write fillers for future children...
            for (; ii <= Slots.Length; ii++)
            {
                writer.Write(-1L);
            }
            #endregion

            bool?  isKeySimpleType = null;
            object simpleKey       = null;
            var    parentTree      = (BTreeAlgorithm)parent;
            byte[] keyOfEmptySlots = null;

            int keySizeOnDisk = parentTree.HintKeySizeOnDisk;
            if (keySizeOnDisk > 0)
            {
                keyOfEmptySlots = new byte[keySizeOnDisk];
            }
            //** Pack each Item's Key & Value if in Key region...
            for (short i = 0; i < Slots.Length; i++)
            {
                if (isKeySimpleType == null)
                {
                    isKeySimpleType = Slots[i] != null &&
                                      (CollectionOnDisk.IsSimpleType(Slots[i].Key) || Slots[i].Key is string);
                    if (isKeySimpleType.Value)
                    {
                        simpleKey = Slots[i].Key;
                    }
                }
                if (i < Count)
                {
                    // Pack Node item key and/or value (if value is in key segment)
                    PackSlotItem(parentTree, writer, Slots[i], ref keySizeOnDisk, isKeySimpleType);
                    if (keyOfEmptySlots == null || keySizeOnDisk > keyOfEmptySlots.Length)
                    {
                        parentTree.HintKeySizeOnDisk = keySizeOnDisk;
                        keyOfEmptySlots = new byte[keySizeOnDisk];
                    }
                }
                else
                {
                    // Pack fillers so a Node can be kept in a contiguous space on disk... (fragmentation is costly)
                    PackSlotFillers(parentTree, writer, simpleKey, ref keySizeOnDisk, ref keyOfEmptySlots);
                }
            }
        }