Example #1
0
        void LoadUserStorageBase(byte[] userID, byte[] ur)
        {
            Log.WriteLine ("Database.LoadUserStorageBase()");

            {
                byte[] repoInfo = userStorage.GetRawRepositoryInfo (ur);
                Log.WriteLine ("Raw Repository info:{0}", repoInfo.ToHexadecimal ());
                System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer (typeof(ChunkRepositoryConfiguration));
                ChunkRepositoryConfiguration cri = (ChunkRepositoryConfiguration)xmls.Deserialize (new MemoryStream (repoInfo, false));
                Log.WriteLine ("Repository info id:{0}", cri.ID);
                DatabasePath dbp = GetRepositoryPath (cri.ID);
                if (dataStorage.ContainsKey (ur)) {
                    Log.WriteLine ("repo {0} is already loaded. loading encrypted parts with new keys.", ur);
                    foreach (var sk in userStorage.GetUserStorage (userID).SymmetricKeys) {
                        if (!dataStorage [ur].LencryptedData.ContainsKey (sk.Key)) {
                            Log.WriteLine ("loading key {0}", sk.Key.ToHexadecimal ());
                            KeyValueStorageConfiguration ESC = new KeyValueStorageConfiguration ();
                            ESC.Type = cri.Data.Type;
                            ESC.Path = sk.Key.ToHexadecimal ();
                            dataStorage [ur].LencryptedData.Add (sk.Key, new AESEncryptingKeyValueStorage (ESC.OpenStorage<byte[]> (dbp.CreatePath ("Encrypted")), sk.Value));
                        }
                    }
                } else {
                    SortedDictionary<byte[], KeyValueStorage<byte[]>> EncryptedData = new SortedDictionary<byte[], KeyValueStorage<byte[]>> ();
                    foreach (var sk in userStorage.GetUserStorage (userID).SymmetricKeys) {
                        KeyValueStorageConfiguration ESC = new KeyValueStorageConfiguration ();
                        ESC.Type = cri.Data.Type;
                        ESC.Path = sk.Key.ToHexadecimal ();
                        EncryptedData.Add (sk.Key, new AESEncryptingKeyValueStorage (ESC.OpenStorage<byte[]> (dbp.CreatePath ("Encrypted")), sk.Value));
                    }
                    ChunkRepository cr = new ChunkRepository (cri.ID, cri.Data.OpenStorage<byte[]> (dbp), cri.TopLevels.OpenStorage<byte[][]> (dbp), cri.Dependencies.OpenStorage<byte[][]> (dbp), cri.Meta.OpenStorage<byte[]> (dbp), cri.MetaTopLevels.OpenStorage<byte[][]> (dbp), cri.MetaDependencies.OpenStorage<byte[][]> (dbp), cri.Signatures.OpenStorage<byte[][]> (dbp), cri.ChunkSymmetricKeys.OpenStorage<byte[][]> (dbp), cri.Index.OpenStorage<byte[]> (dbp), cri.InternalMeta.OpenStorage<byte[]> (dbp), EncryptedData);
                    dataStorage.Add (cr.ID, cr);
                    ChunkRepositoryAdded (this, new ChunkRepositoryAddedEventArgs (cr));
                }
                //encryptedStorageManager.Add (ur, new EncryptedStorageManager (cr, userStorage));
            }
            {
                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 ());
                    throw new NotImplementedException ();
                    byte[] repoInfo = userStorage.GetRawRepositoryInfo (ur);
                    SortedDictionary<byte[], KeyValueStorage<byte[]>> EncryptedData = new SortedDictionary<byte[], KeyValueStorage<byte[]>> ();
                    System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer (typeof(ChunkRepositoryConfiguration));
                    ChunkRepositoryConfiguration cri = (ChunkRepositoryConfiguration)xmls.Deserialize (new MemoryStream (repoInfo, false));
                    DatabasePath dbp = GetRepositoryPath (cri.ID);
                    KeyValueStorageConfiguration ESC = new KeyValueStorageConfiguration ();
                    ESC.Type = cri.Data.Type;
                    ESC.Path = cus.ToHexadecimal ();
                    byte[] ak = null;
                    EncryptedData.Add (cus, new AESEncryptingKeyValueStorage (ESC.OpenStorage<byte[]> (dbp.CreatePath ("Encrypted")), ak));
                    ChunkRepository cr = new ChunkRepository (cri.ID, cri.Data.OpenStorage<byte[]> (dbp), cri.TopLevels.OpenStorage<byte[][]> (dbp), cri.Dependencies.OpenStorage<byte[][]> (dbp), cri.Meta.OpenStorage<byte[]> (dbp), cri.MetaTopLevels.OpenStorage<byte[][]> (dbp), cri.MetaDependencies.OpenStorage<byte[][]> (dbp), cri.Signatures.OpenStorage<byte[][]> (dbp), cri.ChunkSymmetricKeys.OpenStorage<byte[][]> (dbp), cri.Index.OpenStorage<byte[]> (dbp), cri.InternalMeta.OpenStorage<byte[]> (dbp), EncryptedData);
                    dataStorage.Add (cr.ID, cr);
                    ChunkRepositoryAdded (this, new ChunkRepositoryAddedEventArgs (cr));
                }
            }
        }
Example #2
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;
        }