Пример #1
0
        ChunkHeaderv1 LoadChunk(byte[] chunkID, byte[] chunkData)
        {
            ChunkHeaderv1 ch = null;

            Console.WriteLine("Pulled a chunk with a size of {0}", chunkData.Length);
            System.IO.MemoryStream MS = new System.IO.MemoryStream(chunkData);
            System.IO.BinaryReader BR = new System.IO.BinaryReader(MS);
            int chunkVersion          = BR.ReadInt32();

            if (chunkVersion == 1)
            {
                ch = ChunkHeaderv1.Deserialzie(BR.ReadBytes(BR.ReadInt32()));

                //chunkHeaders.Add (chunkID, ch);
            }
            else
            {
                throw new Exception(string.Format("Chunk meta data version is 0x{0:x} which is not supported by this version of BD2.", chunkVersion));
            }
            int sectionCount = BR.ReadInt32();

            for (int sectionID = 0; sectionID != sectionCount; sectionID++)
            {
                switch (chunkVersion)
                {
                case 1:
                    int SectionVersion = BR.ReadInt32();
                    switch (SectionVersion)
                    {
                    case 1:
                        int    payloadLength = BR.ReadInt32();
                        byte[] payload       = BR.ReadBytes(payloadLength);
                        Console.WriteLine("Trying to deserialize in {0} frontends.", frontendInstances.Count);
                        foreach (FrontendInstanceBase fib in frontendInstances)
                        {
                            fib.CreateObjects(chunkID, payload);
                        }
                        break;

                    default:
                        throw new Exception("This version of BD2 does not support the version of data provided.");
                    }
                    break;

                default:
                    throw new Exception("This version of BD2 does not support the version of data provided.");
                }
            }
            return(ch);
        }
Пример #2
0
        public byte[] CommitTransaction(SortedSet<BaseDataObjectVersion> objects)
        {
            Log.WriteLine ("Database.CommitTransaction()");
            int oc = objects.Count;
            objects.RemoveWhere (obj => obj.ChunkID != null);
            Log.WriteLine ("Ommitting {0} objects in commit", oc - objects.Count);
            System.IO.MemoryStream MS = new System.IO.MemoryStream ();
            System.IO.MemoryStream MSRP = new System.IO.MemoryStream ();
            System.IO.BinaryWriter MSBW = new System.IO.BinaryWriter (MS);
            SortedSet<byte[]> dependencies = new SortedSet<byte[]> (BD2.Core.ByteSequenceComparer.Shared);
            ChunkHeaderv1 ch = new ChunkHeaderv1 (DateTime.UtcNow, "");
            MSBW.Write (ch.Version);
            byte[] chbytes = ch.Serialize ();
            MSBW.Write (chbytes.Length);
            MSBW.Write (chbytes);
            MSBW.Write (1);//section count
            Log.WriteLine ("{0} sections", 1);
            int n = 0;
            //foreach (var tup in data) {
            MSBW.Write (1);
            Log.WriteLine ("Section version is {0}", 1);
            System.Collections.Generic.SortedDictionary <BaseDataObjectVersion, LinkedListNode<BaseDataObjectVersion>> ss = new  System.Collections.Generic.SortedDictionary <BaseDataObjectVersion, LinkedListNode<BaseDataObjectVersion>> ();
            System.Collections.Generic.LinkedList <BaseDataObjectVersion> ll = new LinkedList<BaseDataObjectVersion> ();
            foreach (BaseDataObjectVersion bdo in objects) {
                if (!ss.ContainsKey (bdo))
                    ss.Add (bdo, ll.AddLast (bdo));

                foreach (BaseDataObjectVersion dependency in bdo.GetDependenies ()) {
                    byte[] dep = dependency.ChunkID;
                    if (dep == null) {
                        if (ss.ContainsKey (bdo)) {

                        } else {

                            ss.Add (dependency, ll.AddBefore (ss [bdo], dependency));

                        }
                    } else {
                        if (!dependencies.Contains (dependency.ChunkID))
                            dependencies.Add (dependency.ChunkID);
                    }
                }
            }
            foreach (BaseDataObjectVersion bdo in ll) {
                n++;
                System.IO.MemoryStream MST = new System.IO.MemoryStream ();
                MST.Write (bdo.ObjectType.ToByteArray (), 0, 16);
                bdo.Serialize (MST, encryptedStorageManager);
                byte[] bytes = MST.ToArray ();
                Log.WriteLine ("object type+bytes: {0}", bytes.ToHexadecimal ());
                //Log.WriteLine ("Object of type {0} serialized to {1} bytes.", bdo.GetType ().FullName, bytes.Length);
                {
                    System.IO.MemoryStream MSC = new System.IO.MemoryStream ();
                    System.IO.BinaryWriter BWC = new System.IO.BinaryWriter (MSC);
                    BWC.Write (bytes.Length);
                    MSRP.Write (MSC.ToArray (), 0, 4);
                }

                MSRP.Write (bytes, 0, bytes.Length);
            }
            byte[] encoded = MSRP.ToArray ();
            Log.WriteLine ("{0} objects encoded in {1} bytes", objects.Count, encoded.Length);
            MSBW.Write (n);
            MSBW.Write (encoded.Length);
            MSBW.Write (encoded);
            Log.WriteLine ("encoded:{0}", encoded.ToHexadecimal ());
            //}
            if (n == 0) {
                Log.WriteLine ("No objects to save, nothing to do");
                return null;
            }
            System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create ();
            Log.WriteLine ("{0} dependencies", dependencies.Count);
            byte[][] deps = new byte [dependencies.Count][];
            int depid = 0;
            foreach (byte[] dep in dependencies)
                deps [depid++] = dep;
            Log.WriteLine ("Writing {0} bytes representing {1} objects to backend", MS.Length, n);
            byte[] buf = MS.ToArray ();
            byte[] chunkID = sha.ComputeHash (buf);
            foreach (var ds in dataStorage)
                Log.WriteLine ("We have: {0}", ds.Key.ToHexadecimal ());
            byte[] cus = userStorage.GetCommonStorage ();
            Log.WriteLine ("Common storage id is {0}", cus.ToHexadecimal ());
            if (!dataStorage.ContainsKey (cus)) {
                Log.WriteLine ("and we don't have it.");
                Log.WriteLine ("we only have:");
                foreach (var t in dataStorage)
                    Log.WriteLine ("    ID: {0}", t.Key.ToHexadecimal ());
            }
            dataStorage [cus].Push (chunkID, buf, deps, userStorage.Sign (chunkID));
            foreach (var bdo in objects) {
                bdo.SetChunkID (chunkID);
            }
            Log.WriteLine ("Chunk saved with ID:{0}", chunkID.ToHexadecimal ());
            return chunkID;
        }
Пример #3
0
        public byte[] CommitTransaction(SortedSet <BaseDataObjectVersion> objects)
        {
            System.IO.MemoryStream MS           = new System.IO.MemoryStream();
            System.IO.MemoryStream MSRP         = new System.IO.MemoryStream();
            System.IO.BinaryWriter MSBW         = new System.IO.BinaryWriter(MS);
            SortedSet <byte[]>     dependencies = new SortedSet <byte[]> (BD2.Core.ByteSequenceComparer.Shared);
            ChunkHeaderv1          ch           = new ChunkHeaderv1(DateTime.UtcNow, "");

            MSBW.Write(ch.Version);
            byte[] chbytes = ch.Serialize();
            MSBW.Write(chbytes.Length);
            MSBW.Write(chbytes);
            MSBW.Write(1);
            Console.WriteLine("{0} sections", 1);
            int n = 0;

            //foreach (var tup in data) {
            MSBW.Write(1);
            Console.WriteLine("Section version is {0}", 1);
            System.Collections.Generic.SortedDictionary <BaseDataObjectVersion, LinkedListNode <BaseDataObjectVersion> > ss = new  System.Collections.Generic.SortedDictionary <BaseDataObjectVersion, LinkedListNode <BaseDataObjectVersion> > ();
            System.Collections.Generic.LinkedList <BaseDataObjectVersion> ll = new LinkedList <BaseDataObjectVersion> ();
            foreach (BaseDataObjectVersion bdo in objects)
            {
                if (!ss.ContainsKey(bdo))
                {
                    ss.Add(bdo, ll.AddLast(bdo));
                }

                foreach (BaseDataObjectVersion dependency in bdo.GetDependenies())
                {
                    byte[] dep = dependency.ChunkID;
                    if (dep == null)
                    {
                        if (ss.ContainsKey(bdo))
                        {
                        }
                        else
                        {
                            ss.Add(dependency, ll.AddBefore(ss [bdo], dependency));
                        }
                    }
                    else
                    {
                        if (!dependencies.Contains(dependency.ChunkID))
                        {
                            dependencies.Add(dependency.ChunkID);
                        }
                    }
                }
            }
            foreach (BaseDataObjectVersion bdo in ll)
            {
                n++;
                System.IO.MemoryStream MST = new System.IO.MemoryStream();
                MST.Write(bdo.ObjectType.ToByteArray(), 0, 16);
                bdo.Serialize(MST, encryptedStorageManager);
                byte[] bytes = MST.ToArray();
                //Console.WriteLine ("Object of type {0} serialized to {1} bytes.", bdo.GetType ().FullName, bytes.Length);
                {
                    System.IO.MemoryStream MSC = new System.IO.MemoryStream();
                    System.IO.BinaryWriter BWC = new System.IO.BinaryWriter(MSC);
                    BWC.Write(bytes.Length);
                    MSRP.Write(MSC.ToArray(), 0, 4);
                }

                MSRP.Write(bytes, 0, bytes.Length);
            }
            byte[] encoded = MSRP.ToArray();
            Console.WriteLine("{0} objects encoded in {1} bytes", objects.Count, encoded.Length);
            MSBW.Write(encoded.Length);
            MSBW.Write(encoded);
            //}
            if (n == 0)
            {
                Console.WriteLine("No objects to save, nothing to do");
                return(null);
            }
            System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create();
            Console.WriteLine("{0} dependencies", dependencies.Count);
            byte[][] deps  = new byte [dependencies.Count][];
            int      depid = 0;

            foreach (byte[] dep in dependencies)
            {
                deps [depid++] = dep;
            }
            Console.WriteLine("Writing {0} bytes representing {1} objects to backend", MS.Length, n);
            byte[] buf     = MS.ToArray();
            byte[] chunkID = sha.ComputeHash(buf);
            dataStorage [DefaultStorage].Push(chunkID, buf, deps, userStorage.Sign(chunkID));
            foreach (var bdo in objects)
            {
                bdo.SetChunkID(chunkID);
            }
            return(chunkID);
        }