public static void WritePersistentData(
     IInternalPersistent parent,
     object objectToSerialize,
     BinaryWriter writer)
 {
     WritePersistentData(parent, objectToSerialize, writer, Sop.Collections.BTree.ItemType.Default);
 }
Example #2
0
        /// <summary>
        /// Serialize
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="writer"></param>
        public override void Pack(IInternalPersistent parent, BinaryWriter writer)
        {
            System.IO.BinaryWriter binaryWriter = writer;
            binaryWriter.Write(DiskBuffer.DataAddress);
            binaryWriter.Write(Count);
            binaryWriter.Write(this.StartAllocatableAddress);
            binaryWriter.Write(this.EndAllocatableAddress);
            binaryWriter.Write(this.NextAllocatableAddress);
            long l = -1;

            if (this.OccupiedBlocksHead != null)
            {
                l = this.OccupiedBlocksHead.DataAddress;
            }
            binaryWriter.Write(l);
            l = -1;
            if (OccupiedBlocksTail != null)
            {
                l = OccupiedBlocksTail.DataAddress;
            }
            binaryWriter.Write(l);
            binaryWriter.Write(RecycledSegment != null);
            if (RecycledSegment != null)
            {
                RecycledSegment.Pack(parent, writer);
            }
        }
Example #3
0
 public void Recycle(IInternalPersistent recycledObject)
 {
     Locker.Invoke(() =>
     {
         realMruManager.Recycle(recycledObject);
     });
 }
Example #4
0
        internal static int SizeOfMetaData(IInternalPersistent parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }
            if (!(parent is CollectionOnDisk))
            {
                throw new ArgumentException("Parent is not CollectionOnDisk type.");
            }

            if (Encoding != ((CollectionOnDisk)parent).File.Server.Encoding)
            {
                Encoding = ((CollectionOnDisk)parent).File.Server.Encoding;
                _sizeOf  = 0;
            }
            if (_sizeOf == 0)
            {
                var o = new LinkedItemOnDisk
                {
                    DiskBuffer = ((CollectionOnDisk)parent).DataBlockDriver.
                                 CreateBlock(((CollectionOnDisk)parent).File.DataBlockSize),
                    Data = new byte[0]
                };
                var w = new OnDiskBinaryWriter(Encoding)
                {
                    DataBlock = o.DiskBuffer
                };
                WritePersistentData(parent, o, w);
                _sizeOf = (int)w.BaseStream.Position;
                w.Close();
            }
            return(_sizeOf);
        }
Example #5
0
        /// <summary>
        /// Serialize this Collection meta info
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="writer"></param>
        public virtual void Pack(IInternalPersistent parent, System.IO.BinaryWriter writer)
        {
            writer.Write(DataAddress);
            writer.Write((int)DataBlockSize);
            writer.Write(Name);
            writer.Write(HintSizeOnDisk);
            if (DataBlockDriver == null)
            {
                writer.Write(DiskBuffer.DataAddress);
            }
            else
            {
                writer.Write(DataBlockDriver.GetId(this.DiskBuffer));
            }
            bool hasHeader = HeaderData != null;

            writer.Write(hasHeader);
            if (hasHeader)
            {
                HeaderData.Pack(parent, writer);
            }

            bool hasDeletedBlocks = deletedBlocks != null;

            writer.Write(hasDeletedBlocks);
            if (hasDeletedBlocks)
            {
                writer.Write(deletedBlocks.DiskBuffer.DataAddress);
            }
        }
Example #6
0
 public override void Pack(IInternalPersistent parent, BinaryWriter writer)
 {
     writer.Write(this.NextItemAddress);
     writer.Write(this.PreviousItemAddress);
     writer.Write(this.DiskBuffer.DataAddress);
     base.Pack(parent, writer);
 }
Example #7
0
 /// <summary>
 /// Traverse the Parent hierarchy and look for a Parent of a given Type.
 /// Example, one can look for the "File" container of a Collection or a Parent
 /// Collection of a Collection and so on and so forth..
 /// </summary>
 /// <param name="parent"> </param>
 /// <param name="findParentOfType"> </param>
 /// <param name="throwIfNotFound"> </param>
 /// <returns></returns>
 public static IInternalPersistent GetParent(IInternalPersistent parent, Type findParentOfType, bool throwIfNotFound = false)
 {
     if (findParentOfType == null)
     {
         throw new ArgumentNullException("findParentOfType");
     }
     if (parent != null)
     {
         Type t = parent.GetType();
         if (t == findParentOfType || t.IsSubclassOf(findParentOfType))
         {
             return(parent);
         }
         if (findParentOfType == typeof(File))
         {
             if (parent is OnDisk.Algorithm.Collection.ICollectionOnDisk)
             {
                 return(((OnDisk.Algorithm.Collection.ICollectionOnDisk)parent).File);
             }
         }
         if (parent is OnDisk.Algorithm.Collection.ICollectionOnDisk)
         {
             return(((OnDisk.Algorithm.Collection.ICollectionOnDisk)parent).GetParent(findParentOfType));
         }
         if (parent is File)
         {
             return(((File)parent).GetParent(findParentOfType));
         }
     }
     if (throwIfNotFound)
     {
         throw new ArgumentException(string.Format("Parent of type '{0}' not found.", findParentOfType.ToString()));
     }
     return(null);
 }
Example #8
0
 /// <summary>
 /// Serialize
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="writer"></param>
 public void Pack(IInternalPersistent parent, System.IO.BinaryWriter writer)
 {
     if (IsDirty)
     {
         Flush();
     }
     Btree.Pack(parent, writer);
 }
Example #9
0
 /// <summary>
 /// DeSerialize
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="reader"></param>
 public void Unpack(IInternalPersistent parent, System.IO.BinaryReader reader)
 {
     if (reader == null)
     {
         throw new ArgumentNullException("reader");
     }
     StartBlockAddress = reader.ReadInt64();
     EndBlockAddress   = reader.ReadInt64();
     Count             = reader.ReadInt32();
 }
Example #10
0
 /// <summary>
 /// Attempts to actually recycle an Object from recycle bin(RecycledObjects collection)
 /// </summary>
 /// <returns></returns>
 public IInternalPersistent GetRecycledObject()
 {
     if (RecycleEnabled && RecycledObjects.Count > 0)
     {
         IInternalPersistent r = RecycledObjects[0];
         RecycledObjects.RemoveAt(0);
         return(r);
     }
     return(null);
 }
Example #11
0
 /// <summary>
 /// Add Object to the RecycledObjects collection
 /// </summary>
 /// <param name="Object"></param>
 public void Recycle(IInternalPersistent Object)
 {
     if (RecycleEnabled)
     {
         if (Object is Recycling.IRecyclable)
         {
             ((Recycling.IRecyclable)Object).Initialize();
         }
         RecycledObjects.Add(Object);
     }
 }
Example #12
0
        /// <summary>
        /// Pack this Item for serialization to disk
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="writer"></param>
        public virtual void Pack(IInternalPersistent parent, System.IO.BinaryWriter writer)
        {
            bool isReference = Value.DiskBuffer.DataAddress > -1;

            writer.Write(isReference);
            if (isReference)
            {
                writer.Write(Value.DiskBuffer.DataAddress);
            }
            else
            {
                CollectionOnDisk.WritePersistentData(parent, Value, writer);
            }
        }
Example #13
0
 /// <summary>
 /// Create a Lookup table
 /// </summary>
 /// <returns></returns>
 public Algorithm.SortedDictionary.ISortedDictionaryOnDisk CreateLookupTable(IInternalPersistent parent)
 {
     Algorithm.SortedDictionary.ISortedDictionaryOnDisk lookup = null;
     if (Transaction != null)
     {
         lookup = ((Transaction.TransactionBase)Transaction).CreateCollection(this);
     }
     lookup.Parent = parent;
     if (parent is Algorithm.Collection.ICollectionOnDisk && lookup.Transaction == null)
     {
         lookup.Transaction = ((Algorithm.Collection.ICollectionOnDisk)parent).Transaction;
     }
     return(lookup);
 }
Example #14
0
        /// <summary>
        /// Unpack or read the contents of this object from a Binary Reader.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="reader"></param>
        public override void Unpack(IInternalPersistent parent, System.IO.BinaryReader reader)
        {
            DataIsUserDefined = false;
            bool?r = CollectionOnDisk.ReadPersistentData(parent, reader, ref Data);

            if (r == null)
            {
                DataIsUserDefined = true;
            }
            else if (!r.Value)
            {
                Data = null;
            }
        }
Example #15
0
        public override void Unpack(IInternalPersistent parent, BinaryReader reader)
        {
            if (CurrentItem == null)
            {
                FirstItem = new LinkedItemOnDisk(File.DataBlockSize);
                LastItem  = new LinkedItemOnDisk(File.DataBlockSize);
            }
            CurrentItem = FirstItem;

            long firstItemDataAddress = reader.ReadInt64();
            long lastItemDataAddress  = reader.ReadInt64();

            FirstItem.DiskBuffer.DataAddress = firstItemDataAddress;
            LastItem.DiskBuffer.DataAddress  = lastItemDataAddress;
            base.Unpack(parent, reader);
        }
 internal static PersistenceType GetPersistenceType(
     IInternalPersistent parent,
     object objectToSerialize,
     ItemType itemType)
 {
     if (objectToSerialize == null)
     {
         return(PersistenceType.Null);
     }
     if (objectToSerialize is Sop.IProxyObject)
     {
         objectToSerialize = ((Sop.IProxyObject)objectToSerialize).RealObject;
     }
     if (objectToSerialize is IInternalPersistent)
     {
         return(PersistenceType.Custom);
     }
     if (IsSimpleType(objectToSerialize))
     {
         return(PersistenceType.SimpleType);
     }
     if (objectToSerialize is string)
     {
         return(PersistenceType.String);
     }
     if (objectToSerialize is byte[])
     {
         return(PersistenceType.ByteArray);
     }
     if (itemType == ItemType.Key &&
         parent is BTreeAlgorithm &&
         ((BTreeAlgorithm)parent).onKeyPack != null)
     {
         return(PersistenceType.Custom);
     }
     if (itemType == ItemType.Value &&
         parent is BTreeAlgorithm &&
         ((BTreeAlgorithm)parent).onValuePack != null)
     {
         return(PersistenceType.Custom);
     }
     if (objectToSerialize is IPersistent)
     {
         return(PersistenceType.Custom);
     }
     return(PersistenceType.BinarySerialized);
 }
Example #17
0
 /// <summary>
 /// DeSerialize
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="reader"></param>
 public void Unpack(IInternalPersistent parent, System.IO.BinaryReader reader)
 {
     if (Btree == null)
     {
         if (((CollectionOnDisk)parent).Transaction != null)
         {
             Btree = ((Transaction.TransactionBase)((CollectionOnDisk)parent).Transaction).CreateCollection
                     (
                 ((CollectionOnDisk)parent).File);
         }
         else
         {
             Btree = ObjectServer.CreateDictionaryOnDisk(((CollectionOnDisk)parent).File);
         }
     }
     Locker.Invoke(() => { Btree.Unpack(parent, reader); });
 }
Example #18
0
        /// <summary>
        /// DeSerialize this Collection's Meta Info
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="reader"></param>
        public virtual void Unpack(IInternalPersistent parent, System.IO.BinaryReader reader)
        {
            long da = reader.ReadInt64();

            if (da >= 0)
            {
                DataAddress = da;
            }
            DataBlockSize  = (DataBlockSize)reader.ReadInt32();
            Name           = reader.ReadString();
            HintSizeOnDisk = reader.ReadInt32();
            long l = reader.ReadInt64();

            if (l >= 0)
            {
                DataBlockDriver.SetId(this.DiskBuffer, l);
            }
            bool hasHeader = reader.ReadBoolean();

            if (hasHeader)
            {
                if (HeaderData == null)
                {
                    HeaderData = new HeaderData();
                }
                HeaderData.Unpack(parent, reader);
            }

            MruMinCapacity = ((CollectionOnDisk)parent).File.Profile.MruMinCapacity;
            MruMaxCapacity = ((CollectionOnDisk)parent).File.Profile.MruMaxCapacity;

            bool hasDeletedBlocks = reader.ReadBoolean();

            if (hasDeletedBlocks)
            {
                _deletedBlocksAddress = reader.ReadInt64();
            }
            else if (deletedBlocks != null)
            {
                deletedBlocks = null;
            }
        }
Example #19
0
        /// <summary>
        /// Pack this File object
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="writer"></param>
        public void Pack(IInternalPersistent parent, BinaryWriter writer)
        {
            writer.Write(Size);
            writer.Write(Name);
            writer.Write(Filename);

            //** save the profile info
            Profile.Pack(writer);

            bool hasDeletedBlocks = DeletedCollections != null || _deletedCollectionsAddress != -1;

            writer.Write(hasDeletedBlocks);
            if (hasDeletedBlocks)
            {
                if (DeletedCollections != null && DeletedCollections.DataAddress < 0)
                {
                    DeletedCollections.Flush();
                }
                if (_deletedCollectionsAddress == -1 && DeletedCollections != null)
                {
                    _deletedCollectionsAddress = DeletedCollections.DataAddress;
                }
                writer.Write(_deletedCollectionsAddress);
            }
            writer.Write(_store != null || _storeAddress != -1);
            if (_store != null)
            {
                if (_store.IsOpen && _store.IsDirty)
                {
                    _store.RegisterChange();
                    _store.Flush();
                }
                if (_storeAddress == -1)
                {
                    _storeAddress = _store.DataAddress;
                }
            }
            if (_storeAddress != -1)
            {
                writer.Write(_storeAddress);
            }
        }
Example #20
0
 /// <summary>
 /// Add Object to the RecycledObjects collection
 /// </summary>
 /// <param name="Object"></param>
 public void Recycle(IInternalPersistent Object)
 {
     if (RecycledObjects.Count >= RecycledObjects.Capacity)
     {
         return;
     }
     if (Object == null)
     {
         throw new ArgumentNullException("Object");
     }
     if (!RecycleEnabled)
     {
         return;
     }
     if (Object is Recycling.IRecyclable)
     {
         ((Recycling.IRecyclable)Object).Initialize();
     }
     RecycledObjects.Add(Object);
 }
Example #21
0
            public override void Unpack(IInternalPersistent parent,
                                        BinaryReader reader)
            {
                if (DiskBuffer == null)
                {
                    Sop.DataBlock db = ((OnDiskBinaryReader)reader).DataBlock;
                    DiskBuffer = db;
                }
                NextItemAddress     = reader.ReadInt64();
                PreviousItemAddress = reader.ReadInt64();
                long l = reader.ReadInt64();

                if (DiskBuffer == null || (l > -1 && DiskBuffer.DataAddress != l))
                {
                    DiskBuffer = ((CollectionOnDisk)parent).DataBlockDriver.
                                 CreateBlock(((CollectionOnDisk)parent).DataBlockSize);
                    this.DiskBuffer.DataAddress = l;
                }
                DiskBuffer.IsDirty = false;
                base.Unpack(parent, reader);
            }
Example #22
0
        /// <summary>
        /// Unpack this item for DeSerialization from Stream
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="reader"></param>
        public virtual void Unpack(IInternalPersistent parent, System.IO.BinaryReader reader)
        {
            bool?r = CollectionOnDisk.ReadPersistentData(parent, reader, ref Key);

            if (r == null)
            {
                if (((BTreeAlgorithm)parent).onKeyUnpack != null)
                {
                    Key = ((BTreeAlgorithm)parent).onKeyUnpack(reader);
                }
                else
                {
                    throw new SopException("Can't Deserialize Custom persisted 'Key'.");
                }
            }
            if (Value == null)
            {
                var f = (File.File)InternalPersistent.GetParent(parent, typeof(File.File));
                Value = new ItemOnDisk(f.DataBlockSize);
            }
            bool valueIsReference = reader.ReadBoolean();

            if (valueIsReference)
            {
                Value.DiskBuffer.DataAddress = reader.ReadInt64();
            }
            else
            {
                object o = Value;
                CollectionOnDisk.ReadPersistentData(parent, reader, ref o);
                Value = (ItemOnDisk)o;
                if (Value.DataIsUserDefined && ((BTreeAlgorithm)parent).onValueUnpack != null)
                {
                    Value.Data = ((BTreeAlgorithm)parent).onValueUnpack(reader);
                }
            }
            IsDirty = false;
        }
Example #23
0
        /// <summary>
        /// Pack this File object
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="writer"></param>
        public void Pack(IInternalPersistent parent, BinaryWriter writer)
        {
            writer.Write(Size);
            writer.Write(Name);
            writer.Write(Filename);

            //** save the profile info
            //Writer.Write((short)Profile.ProfileScheme);
            //Writer.Write((int)((ServerProfile)Profile).DataBlockSize);
            //Writer.Write(((ServerProfile)Profile).BTreeSlotLength);
            //Writer.Write(((ServerProfile)Profile).CollectionSegmentSize);
            //Writer.Write(((ServerProfile)Profile).HasTrashBin);

            //writer.Write(CollectionGrowthSizeInNob);
            //bool HasDeletedBlocks = DeletedCollections != null;
            bool hasDeletedBlocks = DeletedCollections != null;

            writer.Write(hasDeletedBlocks);
            if (hasDeletedBlocks)
            {
                if (DeletedCollections.DataAddress < 0)
                {
                    DeletedCollections.Flush();
                }
                writer.Write(DeletedCollections.DataAddress);
            }
            writer.Write(_store != null);
            if (_store != null)
            {
                if (_store.IsOpen && _store.IsDirty)
                {
                    _store.RegisterChange();
                    _store.Flush();
                }
                writer.Write(_store.DataAddress);
            }
        }
Example #24
0
 public override void Pack(IInternalPersistent parent, BinaryWriter writer)
 {
     writer.Write(FirstItem.DiskBuffer.DataAddress);
     writer.Write(LastItem.DiskBuffer.DataAddress);
     base.Pack(parent, writer);
 }
Example #25
0
        /// <summary>
        /// DeSerialize
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="reader"></param>
        public override void Unpack(IInternalPersistent parent,
                                    BinaryReader reader)
        {
            System.IO.BinaryReader binaryReader = reader;
            long l = binaryReader.ReadInt64();

            if (l >= 0)
            {
                DiskBuffer.DataAddress = l;
            }
            long cnt = binaryReader.ReadInt64();
            long saa = binaryReader.ReadInt64();

            if ((Count > 0 && cnt == 0) ||
                StartAllocatableAddress > 0 && saa == 0)
            {
                binaryReader.ReadInt64();
                binaryReader.ReadInt64();
                binaryReader.ReadInt64();
                binaryReader.ReadInt64();
                if (reader.ReadBoolean())
                {
                    var rs = new DeletedBlockInfo();
                    rs.Unpack(parent, reader);
                }
                return;
            }
            Count = cnt;                   //BinaryReader.ReadInt32();
            StartAllocatableAddress = saa; // BinaryReader.ReadInt64();
            EndAllocatableAddress   = binaryReader.ReadInt64();
            NextAllocatableAddress  = binaryReader.ReadInt64();
            long          obh = binaryReader.ReadInt64();
            long          obt = binaryReader.ReadInt64();
            DataBlockSize dataBlockSize;

            if (parent != null)
            {
                File.File f = (File.File)InternalPersistent.GetParent(parent, typeof(File.File), true);
                dataBlockSize = f.DataBlockSize;
            }
            else
            {
                dataBlockSize = (DataBlockSize)DiskBuffer.Length;
            }
            if (obh >= 0)
            {
                OccupiedBlocksHead             = new Sop.DataBlock(dataBlockSize);
                OccupiedBlocksHead.DataAddress = obh;
            }
            else if (OccupiedBlocksHead != null)
            {
                OccupiedBlocksHead = null;
            }
            if (obt >= 0)
            {
                OccupiedBlocksTail             = new Sop.DataBlock(dataBlockSize);
                OccupiedBlocksTail.DataAddress = obt;
            }
            else if (OccupiedBlocksTail != null)
            {
                OccupiedBlocksTail = null;
            }

            if (reader.ReadBoolean())
            {
                RecycledSegment = new DeletedBlockInfo();
                RecycledSegment.Unpack(parent, reader);
                RecycledSegmentBeforeTransaction = (DeletedBlockInfo)RecycledSegment.Clone();
            }
        }
Example #26
0
 public override void Unpack(IInternalPersistent parent, System.IO.BinaryReader reader)
 {
 }
Example #27
0
 public override void Pack(IInternalPersistent parent, System.IO.BinaryWriter writer)
 {
 }
        /// <summary>
        /// Serialize Object in preparation for saving to disk/virtual store
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="objectToSerialize"></param>
        /// <param name="writer"></param>
        /// <param name="itemType"> </param>
        public static void WritePersistentData(
            IInternalPersistent parent,
            object objectToSerialize,
            BinaryWriter writer,
            ItemType itemType
            )
        {
            if (objectToSerialize != null)
            {
                long proxyRealDataAddress = 0;
                #region process proxy object
                if (objectToSerialize is Sop.IProxyObject)
                {
                    if (((Sop.IProxyObject)objectToSerialize).RealObject != null)
                    {
                        long.TryParse(objectToSerialize.ToString(), out proxyRealDataAddress);
                        objectToSerialize = ((Sop.IProxyObject)objectToSerialize).RealObject;
                    }
                    else
                    {
                        string s = objectToSerialize.ToString();
                        long   dataAddress;
                        if (long.TryParse(s, out dataAddress))
                        {
                            // save object's value
                            //** save persistence method used so we can deserialize
                            writer.Write((byte)PersistenceType.Custom);

                            //** save Value type so we can deserialize to the same type.
                            const int objectTypeId = (int)BuiltinTypes.SortedDictionaryOnDisk;
                            writer.Write(objectTypeId);

                            //** reserve space for the data size!
                            writer.Write(40);
                            writer.Write(dataAddress);
                            // set to null to prevent code
                            objectToSerialize = null;
                        }
                        else
                        {
                            throw new InvalidOperationException(
                                      string.Format("Unknown 'objectToSerialize' ({0}) type.",
                                                    objectToSerialize.GetType().ToString()));
                        }
                    }
                }
                #endregion
                if (objectToSerialize is IInternalPersistent)
                {
                    // save object's value
                    //** save persistence method used so we can deserialize
                    writer.Write((byte)PersistenceType.Custom);

                    var file = (File.IFile)InternalPersistent.GetParent(parent, typeof(File.File));
                    //** save Value type so we can deserialize to the same type.
                    int objectTypeId = file.Server.TypeStore.RegisterType(objectToSerialize);
                    writer.Write(objectTypeId);

                    //** save data value size and data value
                    int           posn      = 0;
                    Sop.DataBlock posnBlock = null;
                    if (objectToSerialize is BTreeNodeOnDisk)
                    {
                        posn      = (int)writer.BaseStream.Position;
                        posnBlock = ((OnDiskBinaryWriter)writer).DataBlock;
                    }
                    //** reserve space for the data size!
                    writer.Write(40);

                    if (objectToSerialize is SortedDictionaryOnDisk)
                    {
                        var sdod = (SortedDictionaryOnDisk)objectToSerialize;
                        if (proxyRealDataAddress > 0)
                        {
                            writer.Write(proxyRealDataAddress);
                        }
                        else
                        {
                            if (sdod.DataAddress == -1)
                            {
                                sdod.Flush();
                            }
                            writer.Write(sdod.DataAddress);
                        }
                    }
                    else
                    {
                        ((IInternalPersistent)objectToSerialize).Pack(parent, writer);
                    }

                    //** save data value size and data value
                    var bTreeNodeOnDisk = objectToSerialize as BTreeNodeOnDisk;
                    if (bTreeNodeOnDisk != null)
                    {
                        int cm = (bTreeNodeOnDisk).DiskBuffer.CountMembers(true);
                        if (cm > byte.MaxValue)
                        {
                            cm = byte.MaxValue;
                        }
                        posnBlock.Data[posn] = (byte)cm;
                    }
                }
                else if (IsSimpleType(objectToSerialize))
                {
                    writer.Write((byte)PersistenceType.SimpleType);
                    WriteSimpleType(objectToSerialize, writer);
                }
                else if (objectToSerialize is string)
                {
                    writer.Write((byte)PersistenceType.String);
                    var s = (string)objectToSerialize;
                    writer.Write(s);
                }
                else if (objectToSerialize is byte[])
                {
                    writer.Write((byte)PersistenceType.ByteArray);
                    var rawBytes = (byte[])objectToSerialize;
                    writer.Write(rawBytes.Length);
                    writer.Write(rawBytes);
                }
                else if (itemType == ItemType.Key &&
                         parent is BTreeAlgorithm &&
                         ((BTreeAlgorithm)parent).onKeyPack != null)
                {
                    // save object's value
                    //** save persistence method used so we can deserialize
                    writer.Write((byte)PersistenceType.Custom);
                    //** save Value type so we can deserialize to the same type.
                    var file         = (File.IFile)InternalPersistent.GetParent(parent, typeof(File.File));
                    int objectTypeId = file.Server.TypeStore.RegisterType(objectToSerialize);
                    writer.Write(objectTypeId);
                    //** reserve space for the data size!
                    writer.Write(40);
                    //((Algorithm.BTreeAlgorithm)Parent).onKeyPack(Writer, ObjectToSerialize);

                    int sizeOccupied = ((OnDiskBinaryWriter)writer).DataBlock.SizeOccupied;
                    var db           = ((OnDiskBinaryWriter)writer).DataBlock;

                    int posn = (int)writer.BaseStream.Position;
                    ((BTreeAlgorithm)parent).onKeyPack(writer, objectToSerialize);

                    //** write to SOP disk buffer if data wasn't written to it yet...
                    if (db == ((OnDiskBinaryWriter)writer).DataBlock &&
                        sizeOccupied == ((OnDiskBinaryWriter)writer).DataBlock.SizeOccupied)
                    {
                        ((OnDiskBinaryWriter)writer).write(posn, (int)writer.BaseStream.Position);
                    }
                }
                else if (itemType == ItemType.Value &&
                         parent is BTreeAlgorithm &&
                         ((BTreeAlgorithm)parent).onValuePack != null)
                {
                    // save object's value
                    //** save persistence method used so we can deserialize
                    writer.Write((byte)PersistenceType.Custom);
                    //** save Value type so we can deserialize to the same type.
                    var file         = (File.IFile)InternalPersistent.GetParent(parent, typeof(File.File));
                    int objectTypeId = file.Server.TypeStore.RegisterType(objectToSerialize);
                    writer.Write(objectTypeId);
                    //** reserve space for the data size!
                    writer.Write(40);
                    //((Algorithm.BTreeAlgorithm)Parent).onValuePack(Writer, ObjectToSerialize);

                    int           sizeOccupied = ((OnDiskBinaryWriter)writer).DataBlock.SizeOccupied;
                    Sop.DataBlock db           = ((OnDiskBinaryWriter)writer).DataBlock;

                    int posn = (int)writer.BaseStream.Position;
                    ((BTreeAlgorithm)parent).onValuePack(writer, objectToSerialize);
                    //** write to SOP disk buffer if data wasn't written to it yet...
                    if (db == ((OnDiskBinaryWriter)writer).DataBlock &&
                        sizeOccupied == ((OnDiskBinaryWriter)writer).DataBlock.SizeOccupied)
                    {
                        ((OnDiskBinaryWriter)writer).write(posn, (int)writer.BaseStream.Position);
                    }
                }
                else if (objectToSerialize is IPersistent)
                {
                    // save object's value
                    //** save persistence method used so we can deserialize
                    writer.Write((byte)PersistenceType.Custom);
                    //** save Value type so we can deserialize to the same type.
                    var file         = (File.IFile)InternalPersistent.GetParent(parent, typeof(File.File));
                    int objectTypeId = file.Server.TypeStore.RegisterType(objectToSerialize);
                    writer.Write(objectTypeId);
                    //** reserve space for the data size!
                    writer.Write(40);

                    //** save data value size and data value
                    int           posn      = (int)writer.BaseStream.Position;
                    Sop.DataBlock posnBlock = ((OnDiskBinaryWriter)writer).DataBlock;
                    ((IPersistent)objectToSerialize).Pack(writer);
                    int hintSize = ((IPersistent)objectToSerialize).HintSizeOnDisk;
                    if (hintSize > 0)
                    {
                        int sizeWritten = posnBlock.GetSizeOccupied(posn);
                        if (hintSize > sizeWritten)
                        {
                            var b = new byte[hintSize - sizeWritten];
                            writer.Write(b);
                        }
                    }
                }
                else if (objectToSerialize != null)
                {
                    // expects Data to be Binary Serializable
                    writer.Write((byte)PersistenceType.BinarySerialized);
                    int si  = (int)writer.BaseStream.Position;
                    var ser = new BinaryFormatter();
                    ser.Serialize(writer.BaseStream, objectToSerialize);
                    ((OnDiskBinaryWriter)writer).write(si, (int)writer.BaseStream.Position);
                }
            }
            else
            {
                writer.Write((byte)PersistenceType.Null);
            }

            //** mark unused next block as unoccupied so it and its linked blocks can be recycled..
            var w = writer as OnDiskBinaryWriter;
            if (w == null)
            {
                return;
            }
            if (w.DataBlock.SizeAvailable > 0 && w.DataBlock.Next != null && w.DataBlock.Next.SizeOccupied > 0)
            {
                w.DataBlock.Next.SizeOccupied = 0;
            }
        }
Example #29
0
        /// <summary>
        /// Unpack this object
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="reader"></param>
        public void Unpack(IInternalPersistent parent, BinaryReader reader)
        {
            if (this.Server == null && parent is Algorithm.Collection.ICollectionOnDisk)
            {
                this.Server = ((Algorithm.Collection.ICollectionOnDisk)parent).File.Server;
            }

            Size          = reader.ReadInt64();
            _name         = reader.ReadString();
            this.Filename = reader.ReadString();
            if (Server != null && (Server.HomePath != Server.Path &&
                                   !System.IO.File.Exists(Filename) && Filename.StartsWith(Server.HomePath)))
            {
                //** c://SopBin/o.dta -> c://SopBin3/o.dta
                string nameOfFile  = Filename.Substring(Server.HomePath.Length);
                string newFilename = string.Format("{0}{1}", Server.Path, nameOfFile);
                if (System.IO.File.Exists(newFilename))
                {
                    Filename = newFilename;
                }
            }

            //** Read the profile info...
            //ProfileSchemeType ps = (ProfileSchemeType)Reader.ReadInt16();
            //DataBlockSize dbs = (DataBlockSize)Reader.ReadInt32();
            //Profile.ProfileScheme = ps;
            //Profile.DataBlockSize = dbs;
            Profile = new Profile(((Algorithm.Collection.ICollectionOnDisk)parent).File.Profile);

            //CollectionGrowthSizeInNob = reader.ReadInt32();

            bool hasDeletedBlocks   = reader.ReadBoolean();
            long collectionRecycler = -1;

            if (hasDeletedBlocks)
            {
                collectionRecycler = reader.ReadInt64();
            }
            bool deserializeObjectStore = reader.ReadBoolean();

            if (deserializeObjectStore)
            {
                _store = Transaction != null ? ((Transaction.TransactionBase)Transaction).CreateCollection(this) :
                         ObjectServer.CreateDictionaryOnDisk(this);
                _store.DataAddress = reader.ReadInt64();
                if (!_store.IsOpen)
                {
                    _store.Open();
                }
            }
            if (Server != null && Server.HasTrashBin)
            {
                if (collectionRecycler >= 0)
                {
                    if (DeletedCollections == null)
                    {
                        CreateDeletedCollections();
                    }
                    DeletedCollections.DataAddress = collectionRecycler;
                    DeletedCollections.Load();
                }
                else if (DeletedCollections == null)
                {
                    CreateDeletedCollections();
                }
            }
        }
Example #30
0
 /// <summary>
 /// Write the contents of this object to the stream bound to a Binary Writer.
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="writer"></param>
 public override void Pack(IInternalPersistent parent, System.IO.BinaryWriter writer)
 {
     CollectionOnDisk.WritePersistentData(parent, Data, writer, Collections.BTree.ItemType.Value);
 }