Ejemplo n.º 1
0
        public DataContext(Frontend frontend, byte[] id, DatabasePath path)
            : base(frontend)
        {
            Log.WriteLine ("DataContext-{0}..ctor(frontend={1}, id={2}, path={3})", id.ToHexadecimal (), frontend.ToString (), id.ToHexadecimal (), path);
            this.id = id;
            this.path = path;
            KeyValueStorageConfiguration kvscPrecomputedQueries = new KeyValueStorageConfiguration ("PrecomputedQueries", "Sqlite");
            KeyValueStorageConfiguration kvscMeta = new KeyValueStorageConfiguration ("Meta", "Sqlite");
            KeyValueStorageConfiguration kvscTemporary = new KeyValueStorageConfiguration ("Temporary", "Sqlite");
            precomputedQueries = kvscPrecomputedQueries.OpenStorage<byte[]> (path);
            meta = kvscMeta.OpenStorage<byte[]> (path);
            temporary = kvscTemporary.OpenStorage<byte[]> (path);
            try {
                byte[] cid = meta.Get ("ID".SHA256 ());
                if (cid == null) {
                    meta.Put ("ID".SHA256 (), id);
                    meta.Put ("Solid".SHA256 (), new byte[] { 0 });
                } else {
                    if (ByteSequenceComparer.Shared.Compare (cid, id) != 0)
                        throw new InvalidDataException ("Wrong ID");//should i even do this?
                }
            } catch {
                meta.Put ("ID".SHA256 (), id);
                meta.Put ("Solid".SHA256 (), new byte[] { 0 });
            }
            foreach (var t in frontend.GetTables ())
                perTableRows.Add (t, new SortedDictionary<byte[], Row> (ByteSequenceComparer.Shared));
            foreach (var kvp in temporary) {
                byte[] objectID = kvp.Key;
                //BaseDataObject bdo = new BaseDataObject (this.Frontend, objectID);
                System.IO.MemoryStream ms = new MemoryStream (kvp.Value, 16, kvp.Value.Length - 16);
                byte[] GuidBytes = new byte[16];
                System.Buffer.BlockCopy (kvp.Value, 0, GuidBytes, 0, 16);
                BaseDataObjectVersion bdov = BaseDataObjectTypeIdAttribute.GetAttribFor (new Guid (GuidBytes)).Deserialize (this.Frontend, null, ms.ToArray ());
                var row = bdov as Row;
                if (!references.ContainsKey (bdov.ID)) {
                    if (row != null) {
                        perTableRows [row.Table].Add (bdov.ID, row);
                    }
                    objects.Add (bdov.ID, row);
                }
                foreach (var r in bdov.ReplacingIDs) {
                    if (!references.ContainsKey (r)) {
                        references.Add (r, new SortedSet<byte[]> (ByteSequenceComparer.Shared));
                    }
                    references [r].Add (bdov.ID);
                }

            }
            //this.frontendInstanceBase = (FrontendInstance)frontendInstanceBase;
        }