public static DummyRecord Create( string own_dc_hash, DataBase own_db ) { var rec = new DummyRecord(); rec.AssignOwnerDC(DummyContainer.Create( own_db )); return rec; }
public AccessFile( DataBase _db ) { if ( _db != null ) { db = _db; filepath = db.DbPath+"/_db_"+db.Name+".access"; } else throw new AccessException( " Database is not defined! " ); }
public DataStorage(DataBase _db) { Modified = true; if (_db != null) db = _db; parallel_opts.MaxDegreeOfParallelism = Environment.ProcessorCount * 4; }
/// <summary> /// Adding a new item to a chunk /// </summary> /// <param name="filepath">Path to a chunk file</param> /// <param name="db">Database</param> public static void AddItem( string filepath, DataBase db ) { if ( db != null ) { var elem= new InnerChunkElement(); var sw = new StreamWriter( File.Open( filepath, FileMode.Append ) ); using (var json_writer = new JsonTextWriter(sw)) { elem.ElementType = InnerChunkElement.ElemType.DB; elem.ElemDB = null; elem.ElemParentName = null; elem.ElemParentHash = null; elem.ElementName = (db).Name; elem.Contents = JsonConvert.SerializeObject(db, Formatting.Indented); sw.Write(JsonConvert.SerializeObject(elem, Formatting.Indented)); } } }
/// <summary> /// Collects indexes into temporary arrays /// </summary> /// <param name="db"></param> private void CollectIndexes( DataBase db ) { tmp_hashes_copy.Clear(); tmp_indexes_copy.Clear(); db.FillChunkManagerDCDict(); foreach ( var elem in inner_dc_dict ) { var dc_index_obj = elem.Value.GetIndex(); tmp_hashes_copy.Add( dc_index_obj.DwarfHashCode ); tmp_indexes_copy[dc_index_obj] = new KeyValuePair<IStructure, String>( elem.Value, null ); var tmp_records = elem.Value.GetRecordsInternal(); foreach( var rec in tmp_records ) { var rec_index_obj = rec.GetIndex(); tmp_hashes_copy.Add( rec_index_obj.DwarfHashCode ); tmp_indexes_copy[rec_index_obj] = new KeyValuePair<IStructure, String>( rec, dc_index_obj.DwarfHashCode ); } } }
/// <summary> /// Saves all indexes in format Name:Type:IndexHash /// </summary> public void RebuildIndexes( DataBase db ) { // collecting indexes CollectIndexes( db ); // cleaning indexes and hashes all_indexes.Clear(); all_hashes.Clear(); all_hashes = tmp_hashes_copy; all_indexes = tmp_indexes_copy; ClearIndexesDw(); var filepath = CurrentDbPath+@"/indexes.dw"; string contents = ReadIndexesDw(); var fs = File.Open( filepath, FileMode.Append ); // let's add new records to index.dw using ( var sw = new StreamWriter( fs ) ) { foreach ( var idx in AllIndexes ) { if ( idx.Value.Key is Record ) { var rec = idx.Value.Key as Record; var owner_dc = rec.OwnerDC; string hash_code = idx.Key.DwarfHashCode; sw.WriteLine( "Record:Record:"+hash_code+":"+owner_dc.GetIndex().DwarfHashCode); } else if ( idx.Value.Key is DataContainer ) { var dc = (DataContainer)idx.Value.Key; sw.WriteLine( dc.Name+ ":DataContainer:"+ dc.GetOwnerDB().Name+":"+ idx.Key.DwarfHashCode ); } } } }
public void LoadDCIndexes( DataBase db ) { var filepath = CurrentDbPath+@"/indexes.dw"; var rgx = new Regex(@"(.*):DataContainer:(.*):(.*)"); if ( File.Exists(filepath) ) { using ( var fs = File.OpenRead( filepath )) { var sr = new StreamReader( fs ); string line = sr.ReadLine(); while ( line != null ) { if ( line.IndexOf(":DataContainer:", 0, StringComparison.CurrentCulture) > 0 ) { if ( rgx.IsMatch( line ) ) { var mtc = rgx.Matches( line ); if ( mtc[0].Groups.Count > 0 ) { var dc_name = mtc[0].Groups[1].Value; var dc_hash = mtc[0].Groups[3].Value; var idx = Index.CreateFromDwarfHashCode( dc_hash ); var new_dc = GetDataContainer(dc_name); new_dc.AssignOwnerDB(db); all_indexes.TryAdd( idx, new KeyValuePair<IStructure, string>(new_dc, dc_name)); } } } line = sr.ReadLine(); } } } else throw new ChunkException( "Indexes.dw is absent! " ); }
/// <summary> /// Creates a new chunk for databases /// </summary> /// <param name="db">Database</param> public void CreateChunk( DataBase db ) { try { var db_filename = CurrentDbPath+ @"\db_"+ db.Name + ".dwarf"; Directory.CreateDirectory(CurrentDbPath); if ( !File.Exists(db_filename) ) { var filepath = CurrentDbPath + @"/db_"+ db.Name + ".dwarf"; ChunkFormat.AddItem( filepath, db); chunks_lst[ new IndexPair() { hash_min = null, hash_max = null } ] = db.Name; } else { Errors.Messages.DisplayError("Database \""+db.Name+"\" already exists!", "creating DB", "Choose another name", DateTime.Now); return; } } catch ( IOException ex ) { throw new ChunkException( "Error writing a new chunk!", ex ); } }
/// <summary> /// Cloning DC from another DB /// </summary> /// <param name="from">DB-transmitter</param> /// <param name="dc_name">DC name to clone</param> /// <returns></returns> public bool CloneDataContainer(DataBase from, String dc_name) { // TODO: cloning operation with calling an Transmit() function on the other DB if (from != null) { if (inner_dc_dict.ContainsKey(dc_name)) return false; var cloned_dc = new DataContainer(this, this.Name); if (from.Transmit(ref cloned_dc, dc_name)) { cloned_dc.AssignOwnerDB(this); // assigning new owner DB return true; } } return false; }
/// <summary> /// Loads database from a filesystem /// </summary> /// <param name="db_name">DB name</param> /// <param name="_cm">Chunk manager</param> /// <returns></returns> public static DataBase LoadFrom(string db_name, ChunkManager.ChunkManager _cm) { var cpath = Config.Config.Instance.DataDirectory + db_name; if (Directory.Exists(cpath)) { var db = new DataBase(db_name, _cm, false); db.DbPath = cpath; db.UpdateDataContainers(); return db; } else { Errors.Messages.DisplayError("Can't find a directory: " + cpath, "DB loading", "Check DB name or path existance", DateTime.Now); return null; } }
/// <summary> /// Creates a new DB /// </summary> /// <param name="db_name"></param> /// <param name="_cm"></param> /// <returns></returns> public static DataBase Create(string db_name, ChunkManager.ChunkManager _cm = null) { var cpath = Config.Config.Instance.DataDirectory + db_name; if (!Directory.Exists(cpath)) Directory.CreateDirectory(cpath); var new_db = new DataBase(db_name, _cm, true); CreateIndexesDw(db_name); new_db.chunk_manager.CreateChunk(new_db); new_db.DbPath = Config.Config.Instance.DataDirectory + db_name; return new_db; }
private void Load() { DwarfDB.User.User user = DwarfDB.User.User.New( "root", "12345678" );; cm = new DwarfDB.ChunkManager.ChunkManager(); db = DataBase.LoadFrom("employees", cm); dc_employee_load = db.GetDataContainer("employee", user); GridLoad(); }
public static DummyContainer Create( DataBase _owner_db ) { return new DummyContainer( _owner_db ); }
public DummyContainer( DataBase _owner_db ) : base(_owner_db, "dummy") { }