public IndexDynamicTest() { var uniqPath = Path+DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace(":"," ")+ DateTime.Now.Second.ToString()+ @"/"; Directory.CreateDirectory(uniqPath); tableView = new TableView(uniqPath+ "table", TpTableElement); tableView.Fill(Enumerable.Range(0, NumberOfRecords).Select(i => (object) (new object[] {i.ToString(), i }))); // Делаем индекс var ha = new IndexHalfkeyImmutable<string>(uniqPath + "dyna_index_str_half") { Table = tableView, KeyProducer = va => (string) ((object[]) (((object[]) va)[1]))[0], HalfProducer = k => k.GetHashModifiedBernstein() }; ha.Scale = new ScaleCell(uniqPath + "dyna_index_str_half") {IndexCell = ha.IndexCell}; ha.Build(); sIndex = new IndexDynamic<string, IndexHalfkeyImmutable<string>>(true) { Table = tableView, KeyProducer = va => (string) ((object[]) (((object[]) va)[1]))[0], IndexArray = ha }; iIndex = new IndexDynamic<int, IIndexImmutable<int>>(true, new IndexKeyImmutable<int>(uniqPath + "int", tableView, va => (int) ((object[]) (((object[]) va)[1]))[1], new ScaleCell(uniqPath + "iisndexScale"))); iIndex.Build(); }
public NameTableUniversal(string path) { this.path = path; PType tp_tabelement = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), new NamedType("str", new PType(PTypeEnumeration.sstring))); this.table = new TableView(path + "cstable", tp_tabelement); //next_code = (int)table.Count(); offset_array = new IndexViewImmutable<int>(path + "offsets") { Table = this.table, KeyProducer = pair => (int)((object[])(((object[])pair)[1]))[0], tosort = false }; offsets = new IndexDynamic<int, IndexViewImmutable<int>>(true) { Table = this.table, //KeyProducer = pair => (int)((object[])pair)[0], KeyProducer = pair => (int)((object[])(((object[])pair)[1]))[0], IndexArray = offset_array }; table.RegisterIndex(offsets); s_index_array_path = path + "s_index"; s_index_array = new IndexHalfkeyImmutable<string>(s_index_array_path) { Table = table, KeyProducer = pair => (string)((object[])(((object[])pair)[1]))[1], HalfProducer = key => key.GetHashModifiedBernstein() }; s_index_array.Scale = new ScaleCell(path + "dyna_index_str_half") { IndexCell = s_index_array.IndexCell }; //s_index_array.Scale = new ScaleMemory() { IndexCell = s_index_array.IndexCell }; s_index = new IndexDynamic<string, IndexHalfkeyImmutable<string>>(true) { Table = table, KeyProducer = pair => (string)((object[])(((object[])pair)[1]))[1], IndexArray = s_index_array }; table.RegisterIndex(s_index); }
public void Indexes(int[] cnoms) { if (tp_element.Vid != PTypeEnumeration.record) { throw new Exception("Err in TableReletional Indexes: Type of element is not record"); } allindexes = new IIndexCommon[cnoms.Length]; PTypeRecord tp_r = (PTypeRecord)tp_element; this.cnoms = cnoms; int i = 0; foreach (int nom in cnoms) { if (nom < 0 || nom > tp_r.Fields.Length) { throw new Exception("Err in TableRelational Indexes: element number is out of range"); } PType tp = tp_r.Fields[nom].Type; if (tp.Vid == PTypeEnumeration.integer) { Func <object, int> keyproducer = v => (int)((object[])((object[])v)[1])[nom]; IndexKeyImmutable <int> ind_arr = new IndexKeyImmutable <int>(getstream()) { Table = table, KeyProducer = keyproducer, Scale = null }; ind_arr.Scale = new ScaleCell(getstream()) { IndexCell = ind_arr.IndexCell }; IndexDynamic <int, IndexKeyImmutable <int> > index = new IndexDynamic <int, IndexKeyImmutable <int> >(true, ind_arr); allindexes[i] = index; table.RegisterIndex(index); } else if (tp.Vid == PTypeEnumeration.sstring) { IndexHalfkeyImmutable <string> index_arr = new IndexHalfkeyImmutable <string>(getstream()) { Table = table, KeyProducer = v => (string)((object[])((object[])v)[1])[nom], HalfProducer = v => Hashfunctions.HashRot13(v) }; index_arr.Scale = new ScaleCell(getstream()) { IndexCell = index_arr.IndexCell }; IndexDynamic <string, IndexHalfkeyImmutable <string> > index = new IndexDynamic <string, IndexHalfkeyImmutable <string> >(false, index_arr); allindexes[i] = index; table.RegisterIndex(index); } else { throw new Exception($"Err: vid {tp.Vid} is not implemented in TableRelational Indexes"); } i++; } //BuildIndexes(); }
public static void Main5(string[] args) { Random rnd = new Random(); Console.WriteLine("Start Task04_Sequenses_Main4"); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); PType tp_person = new PTypeRecord( new NamedType("id", new PType(PTypeEnumeration.integer)), new NamedType("name", new PType(PTypeEnumeration.sstring)), new NamedType("age", new PType(PTypeEnumeration.real))); TableView tab_person = new TableView(path + "tab_person.pac", tp_person); IndexHalfkeyImmutable <string> name_index = new IndexHalfkeyImmutable <string>(path + "name_hindex.pac") { Table = tab_person, KeyProducer = v => (string)((object[])((object[])v)[1])[1], HalfProducer = v => Hashfunctions.HashRot13(v) }; name_index.Scale = new ScaleCell(path + "person_ind") { IndexCell = name_index.IndexCell }; IndexDynamic <string, IndexHalfkeyImmutable <string> > index_person_name = new IndexDynamic <string, IndexHalfkeyImmutable <string> >(false, name_index); tab_person.RegisterIndex(index_person_name); int nelements = 1_000_000; bool toload = true; // Загружать или нет новую базу данных if (toload) { sw.Restart(); // Очистим ячейки последовательности и индекса tab_person.Clear(); IEnumerable <object> flow = Enumerable.Range(0, nelements) .Select(i => { int id = nelements - i; string name = "=" + id.ToString() + "="; double age = rnd.NextDouble() * 100.0; return(new object[] { id, name, age }); }); tab_person.Fill(flow); // Теперь надо отсортировать индексный массив по ключу tab_person.BuildIndexes(); sw.Stop(); Console.WriteLine("Load ok. duration for {0} elements: {1} ms", nelements, sw.ElapsedMilliseconds); } else { sw.Restart(); tab_person.Warmup(); sw.Stop(); Console.WriteLine("Warmup ok. duration for {0} elements: {1} ms", nelements, sw.ElapsedMilliseconds); } // Проверим работу int search_key = nelements * 2 / 3; var ob = index_person_name.GetAllByKey("=" + search_key + "=") .Select(ent => ((object[])ent.Get())[1]) .FirstOrDefault(); if (ob == null) { throw new Exception("Didn't find person " + search_key); } Console.WriteLine("Person {0} has name {1}", search_key, ((object[])ob)[1]); // Засечем скорость выборок int nprobe = 1000; sw.Restart(); for (int i = 0; i < nprobe; i++) { search_key = rnd.Next(nelements) + 1; ob = index_person_name.GetAllByKey("=" + search_key + "=") .Select(ent => ((object[])ent.Get())[1]) .FirstOrDefault(); if (ob == null) { throw new Exception("Didn't find person " + search_key); } string nam = (string)((object[])ob)[1]; } sw.Stop(); Console.WriteLine($"Duration for {nprobe} search in {nelements} elements: {sw.ElapsedMilliseconds} ms"); }
//static string path = "Databases/"; public static void Main6() { Random rnd = new Random(); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); Console.WriteLine("Start demo of table and Universal Index with PagedStorage and id as string"); // Тип основной таблицы PType tp_person = new PTypeRecord( new NamedType("id", new PType(PTypeEnumeration.sstring)), new NamedType("name", new PType(PTypeEnumeration.sstring)), new NamedType("age", new PType(PTypeEnumeration.real))); // ======================== Страничное хранилище возьмем из описания PagedStreamStore ========================= //PagedStreamStore ps_store = new PagedStreamStore(path + "storage.bin", 4); // заказали 4 стрима, конкретные будут: ps_store[i] StreamStorage ps_store = new StreamStorage(dbpath + "storage6uindex.bin", 4); // База данных такая же, как и в программе 4, только идентификатор определен как строка, придется воспользоваться полуключем TableView tab_person; IndexHalfkeyImmutable <string> index_id_arr; IndexDynamic <string, IndexHalfkeyImmutable <string> > index_id; // Подключение к таблице tab_person = new TableView(ps_store[0], tp_person); // Подключение к индексу по уникальному идентификатору (нулевое поле) Func <object, string> person_id_keyproducer = v => (string)((object[])((object[])v)[1])[0]; index_id_arr = new IndexHalfkeyImmutable <string>(ps_store[1]) { Table = tab_person, KeyProducer = person_id_keyproducer, HalfProducer = s => Hashfunctions.HashRot13(s), Scale = null }; index_id_arr.Scale = new ScaleCell(ps_store[2]) { IndexCell = index_id_arr.IndexCell }; index_id = new IndexDynamic <string, IndexHalfkeyImmutable <string> >(true) { Table = tab_person, IndexArray = index_id_arr, KeyProducer = person_id_keyproducer }; tab_person.RegisterIndex(index_id); int nelements = 1000000; bool toload = false; // Загружать или нет новую базу данных if (toload) { sw.Restart(); // Очистим ячейки последовательности и индекса tab_person.Clear(); IEnumerable <object> flow = Enumerable.Range(0, nelements) .Select(i => { int id = nelements - i; string name = "=" + id.ToString() + "="; double age = rnd.NextDouble() * 100.0; return(new object[] { id.ToString(), name, age }); }); tab_person.Fill(flow); // Теперь надо отсортировать индексный массив по ключу tab_person.BuildIndexes(); sw.Stop(); Console.WriteLine("Load ok. duration for {0} elements: {1} ms", nelements, sw.ElapsedMilliseconds); } else { // Загрузка кеша ускоряет обработку sw.Restart(); ps_store.LoadCache(); sw.Stop(); Console.WriteLine("Cache load duration={0}", sw.ElapsedMilliseconds); } // Проверим работу int search_key = nelements * 2 / 3; var ob = index_id.GetAllByKey(search_key.ToString()) .Select(ent => ((object[])ent.Get())[1]) .FirstOrDefault(); if (ob == null) { throw new Exception("Didn't find person " + search_key); } Console.WriteLine("Person {0} has name {1}", search_key, ((object[])ob)[1]); // Засечем скорость выборок int nprobes = 10000; sw.Restart(); for (int i = 0; i < nprobes; i++) { search_key = rnd.Next(nelements) + 1; ob = index_id.GetAllByKey(search_key.ToString()) .Select(ent => ((object[])ent.Get())[1]) .FirstOrDefault(); if (ob == null) { throw new Exception("Didn't find person " + search_key); } string nam = (string)((object[])ob)[1]; } sw.Stop(); Console.WriteLine("Duration for {0} search in {1} elements: {2} ms", nprobes, nelements, sw.ElapsedMilliseconds); }
public static void Main7() { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); string path = "../../../Databases/"; int NumberOfRecords = 100000000; Random rnd = new Random(); Console.WriteLine("Start Universal Index. Main7()"); PType tp_table_element = new PTypeRecord( new NamedType("name", new PType(PTypeEnumeration.sstring)), new NamedType("age", new PType(PTypeEnumeration.integer))); TableView table = new TableView(path + "table", tp_table_element); sw.Restart(); bool tobuild = false; if (tobuild) { table.Fill(Enumerable.Range(0, NumberOfRecords).Select(i => (object)(new object[] { i.ToString(), i == NumberOfRecords / 2 ? -1 : i }))); sw.Stop(); Console.WriteLine("Load Table of {0} elements ok. Duration {1}", NumberOfRecords, sw.ElapsedMilliseconds); } else table.Warmup(); sw.Restart(); // Делаем индекс var ha = new IndexHalfkeyImmutable<string>(path + "dyna_index_str_half") { Table = table, KeyProducer = va => (string)((object[])(((object[])va)[1]))[0], HalfProducer = k => k.GetHashCode() }; ha.Scale = new ScaleCell(path + "dyna_index_str_half") { IndexCell = ha.IndexCell }; bool tobuild_h_index = false; if (tobuild_h_index) { ha.Build(); } else { ha.Warmup(); //ha.BuildScale(); } IndexDynamic<string, IndexHalfkeyImmutable<string>> h_index = new IndexDynamic<string, IndexHalfkeyImmutable<string>>(true) { Table = table, KeyProducer = va => (string)((object[])(((object[])va)[1]))[0], IndexArray = ha }; Console.WriteLine("h_index Build ok. Duration {0}", sw.ElapsedMilliseconds); sw.Restart(); int cnt = 0; for (int i = 0; i < 1000; i++) { int c = h_index.GetAllByKey(rnd.Next(NumberOfRecords * 3 / 2 - 1).ToString()).Count(); if (c > 1) Console.WriteLine("Unexpected Error: {0}", c); cnt += c; } sw.Stop(); Console.WriteLine("1000 GetAllByKey ok. Duration={0} cnt={1}", sw.ElapsedMilliseconds, cnt); }