public NametableTry(string path) { PType tp_person = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), new NamedType("name", new PType(PTypeEnumeration.sstring))); tab_person = new TableView(path + "nametable", tp_person); // Индексы: Персона Func <object, int> person_code_keyproducer = v => (int)((object[])((object[])v)[1])[0]; ind_arr_person = new IndexKeyImmutable <int>(path + "code_ind") { Table = tab_person, KeyProducer = person_code_keyproducer, Scale = null }; ind_arr_person.Scale = new ScaleCell(path + "code_ind") { IndexCell = ind_arr_person.IndexCell }; index_person = new IndexDynamic <int, IndexKeyImmutable <int> >(true) { Table = tab_person, IndexArray = ind_arr_person, KeyProducer = person_code_keyproducer }; Func <object, string> name_keyproducer = v => (string)((object[])((object[])v)[1])[1]; ind_arr_person_name = new IndexViewImmutable <string>(path + "name_ind") { Table = tab_person, KeyProducer = name_keyproducer, Scale = null }; index_person_name = new IndexDynamic <string, IndexViewImmutable <string> >(false) { Table = tab_person, IndexArray = ind_arr_person_name, KeyProducer = name_keyproducer }; tab_person.RegisterIndex(index_person); tab_person.RegisterIndex(index_person_name); }
public Stan3TabsInt(string path) { PType tp_person = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), new NamedType("name", new PType(PTypeEnumeration.sstring)), new NamedType("age", new PType(PTypeEnumeration.integer))); PType tp_photo_doc = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), new NamedType("name", new PType(PTypeEnumeration.sstring))); PType tp_reflection = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), // Может не быть new NamedType("reflected", new PType(PTypeEnumeration.integer)), // ссылки на коды new NamedType("in_doc", new PType(PTypeEnumeration.integer))); tab_person = new TableView(path + "person", tp_person); tab_photo_doc = new TableView(path + "photo_doc", tp_photo_doc); tab_reflection = new TableView(path + "reflection", tp_reflection); // Индексы: Персона Func <object, int> person_code_keyproducer = v => (int)((object[])((object[])v)[1])[0]; ind_arr_person = new IndexKeyImmutable <int>(path + "person_ind") { Table = tab_person, KeyProducer = person_code_keyproducer, Scale = null }; ind_arr_person.Scale = new ScaleCell(path + "person_ind") { IndexCell = ind_arr_person.IndexCell }; ind_arr_person.Scale.Build(); index_person = new IndexDynamic <int, IndexKeyImmutable <int> >(true) { Table = tab_person, IndexArray = ind_arr_person, KeyProducer = person_code_keyproducer }; // Индексы - документ Func <object, int> photo_doc_code_keyproducer = v => (int)((object[])((object[])v)[1])[0]; ind_arr_photo_doc = new IndexKeyImmutable <int>(path + "photo_doc_ind") { Table = tab_photo_doc, KeyProducer = photo_doc_code_keyproducer, Scale = null }; ind_arr_photo_doc.Scale = new ScaleCell(path + "photo_doc_ind") { IndexCell = ind_arr_photo_doc.IndexCell }; ind_arr_photo_doc.Scale.Build(); index_photo_doc = new IndexDynamic <int, IndexKeyImmutable <int> >(true) { Table = tab_photo_doc, IndexArray = ind_arr_photo_doc, KeyProducer = photo_doc_code_keyproducer }; // Индекс - reflection-reflected Func <object, int> reflected_keyproducer = v => (int)((object[])((object[])v)[1])[1]; ind_arr_reflected = new IndexKeyImmutable <int>(path + "reflected_ind") { Table = tab_reflection, KeyProducer = reflected_keyproducer, Scale = null }; ind_arr_reflected.Scale = new ScaleCell(path + "reflected_ind") { IndexCell = ind_arr_reflected.IndexCell }; ind_arr_reflected.Scale.Build(); index_reflected = new IndexDynamic <int, IndexKeyImmutable <int> >(false) { Table = tab_reflection, IndexArray = ind_arr_reflected, KeyProducer = reflected_keyproducer }; // Индекс - reflection-in_doc Func <object, int> in_doc_keyproducer = v => (int)((object[])((object[])v)[1])[2]; ind_arr_in_doc = new IndexKeyImmutable <int>(path + "in_doc_ind") { Table = tab_reflection, KeyProducer = in_doc_keyproducer, Scale = null }; ind_arr_in_doc.Scale = new ScaleCell(path + "in_doc_ind") { IndexCell = ind_arr_in_doc.IndexCell }; ind_arr_in_doc.Scale.Build(); index_in_doc = new IndexDynamic <int, IndexKeyImmutable <int> >(false) { Table = tab_reflection, IndexArray = ind_arr_in_doc, KeyProducer = in_doc_keyproducer }; Func <object, string> name_keyproducer = v => (string)((object[])((object[])v)[1])[1]; ind_arr_person_name = new IndexViewImmutable <string>(path + "personname_ind") { Table = tab_person, KeyProducer = name_keyproducer, Scale = null }; ind_arr_person_name.Scale = new ScaleCell(path + "personname_ind") { IndexCell = ind_arr_in_doc.IndexCell }; ind_arr_person_name.Scale.Build(); index_person_name = new IndexDynamic <string, IndexViewImmutable <string> >(false) { Table = tab_person, IndexArray = ind_arr_person_name, KeyProducer = name_keyproducer }; tab_person.RegisterIndex(index_person); tab_person.RegisterIndex(index_person_name); tab_photo_doc.RegisterIndex(index_photo_doc); tab_reflection.RegisterIndex(index_reflected); tab_reflection.RegisterIndex(index_in_doc); }
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(); }
//static string path = "Databases/"; public static void Main4() { Random rnd = new Random(); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); Console.WriteLine("Start demo of table and Universal Index with PagedStorage"); // Тип основной таблицы 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))); // ======================== Теперь нам понадобится страничное хранилище ========================= // файл - носитель хранилища string filepath = dbpath + "storage.bin"; bool fob_exists = File.Exists(filepath); // этот признак используется при разных процессах инициализации // Открываем иили создаем файл-носитель хранилища FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.ReadWrite); // Создаем собственно блочное (страничное) хранилище FileOfBlocks fob = new FileOfBlocks(fs); // Далее идет корявый способ создания трех потоков (Stream), нужных для базы данных Stream first_stream = fob.GetFirstAsStream(); if (!fob_exists) { PagedStream.InitPagedStreamHead(first_stream, 8L, 0, PagedStream.HEAD_SIZE); fob.Flush(); } PagedStream main_stream = new PagedStream(fob, fob.GetFirstAsStream(), 8L); long sz = PagedStream.HEAD_SIZE; // Если main_stream нулевой длины, надо инициировать конфигурацию стримов if (main_stream.Length == 0) { // инициируем 3 головы для потоков PagedStream.InitPagedStreamHead(main_stream, 0L, 0L, -1L); PagedStream.InitPagedStreamHead(main_stream, sz, 0L, -1L); PagedStream.InitPagedStreamHead(main_stream, 2 * sz, 0L, -1L); main_stream.Flush(); fob.Flush(); } // создадим 3 потока PagedStream stream_person = new PagedStream(fob, main_stream, 0L); PagedStream stream_index = new PagedStream(fob, main_stream, 1 * sz); PagedStream stream_scale = new PagedStream(fob, main_stream, 2 * sz); // =========================== OK ========================== // База данных такая же, как и в программе 3, только вместо файлов используются потоки TableView tab_person = new TableView(stream_person, tp_person); Func <object, int> person_code_keyproducer = v => (int)((object[])((object[])v)[1])[0]; IndexKeyImmutable <int> ind_arr_person = new IndexKeyImmutable <int>(stream_index) { Table = tab_person, KeyProducer = person_code_keyproducer, Scale = null }; ind_arr_person.Scale = new ScaleCell(stream_scale) { IndexCell = ind_arr_person.IndexCell }; IndexDynamic <int, IndexKeyImmutable <int> > index_person = new IndexDynamic <int, IndexKeyImmutable <int> >(true) { Table = tab_person, IndexArray = ind_arr_person, KeyProducer = person_code_keyproducer }; tab_person.RegisterIndex(index_person); fob.DeactivateCache(); int nelements = 1000000; 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); } sw.Restart(); // Загрузка кеша ускоряет обработку fob.LoadCache(); sw.Stop(); Console.WriteLine("Cache load duration={0}", sw.ElapsedMilliseconds); // Проверим работу int search_key = nelements * 2 / 3; var ob = index_person.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 nte = 10000; sw.Restart(); for (int i = 0; i < nte; i++) { search_key = rnd.Next(nelements) + 1; ob = index_person.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 {2} search in {0} elements: {1} ms", nelements, sw.ElapsedMilliseconds, nte); }
public Stan3TabsInt(string path) { PType tp_person = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), new NamedType("name", new PType(PTypeEnumeration.sstring)), new NamedType("age", new PType(PTypeEnumeration.integer))); PType tp_photo_doc = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), new NamedType("name", new PType(PTypeEnumeration.sstring))); PType tp_reflection = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), // Может не быть new NamedType("reflected", new PType(PTypeEnumeration.integer)), // ссылки на коды new NamedType("in_doc", new PType(PTypeEnumeration.integer))); tab_person = new TableView(path + "person", tp_person); tab_photo_doc = new TableView(path + "photo_doc", tp_photo_doc); tab_reflection = new TableView(path + "reflection", tp_reflection); // Индексы: Персона Func<object, int> person_code_keyproducer = v => (int)((object[])((object[])v)[1])[0]; ind_arr_person = new IndexKeyImmutable<int>(path + "person_ind") { Table = tab_person, KeyProducer = person_code_keyproducer, Scale = null }; //ind_arr_person.Scale = new ScaleCell(path + "person_ind") { IndexCell = ind_arr_person.IndexCell }; //ind_arr_person.Scale.Build(); index_person = new IndexDynamic<int, IndexKeyImmutable<int>>(true) { Table = tab_person, IndexArray = ind_arr_person, KeyProducer = person_code_keyproducer }; // Индексы - документ Func<object, int> photo_doc_code_keyproducer = v => (int)((object[])((object[])v)[1])[0]; ind_arr_photo_doc = new IndexKeyImmutable<int>(path + "photo_doc_ind") { Table = tab_photo_doc, KeyProducer = photo_doc_code_keyproducer, Scale = null }; //ind_arr_photo_doc.Scale = new ScaleCell(path + "photo_doc_ind") { IndexCell = ind_arr_photo_doc.IndexCell }; //ind_arr_photo_doc.Scale.Build(); index_photo_doc = new IndexDynamic<int, IndexKeyImmutable<int>>(true) { Table = tab_photo_doc, IndexArray = ind_arr_photo_doc, KeyProducer = photo_doc_code_keyproducer }; // Индекс - reflection-reflected Func<object, int> reflected_keyproducer = v => (int)((object[])((object[])v)[1])[1]; ind_arr_reflected = new IndexKeyImmutable<int>(path + "reflected_ind") { Table = tab_reflection, KeyProducer = reflected_keyproducer, Scale = null }; //ind_arr_reflected.Scale = new ScaleCell(path + "reflected_ind") { IndexCell = ind_arr_reflected.IndexCell }; //ind_arr_reflected.Scale.Build(); index_reflected = new IndexDynamic<int, IndexKeyImmutable<int>>(false) { Table = tab_reflection, IndexArray = ind_arr_reflected, KeyProducer = reflected_keyproducer }; // Индекс - reflection-in_doc Func<object, int> in_doc_keyproducer = v => (int)((object[])((object[])v)[1])[2]; ind_arr_in_doc = new IndexKeyImmutable<int>(path + "in_doc_ind") { Table = tab_reflection, KeyProducer = in_doc_keyproducer, Scale = null }; //ind_arr_in_doc.Scale = new ScaleCell(path + "in_doc_ind") { IndexCell = ind_arr_in_doc.IndexCell }; //ind_arr_in_doc.Scale.Build(); index_in_doc = new IndexDynamic<int, IndexKeyImmutable<int>>(false) { Table = tab_reflection, IndexArray = ind_arr_in_doc, KeyProducer = in_doc_keyproducer }; Func<object, string> name_keyproducer = v => (string)((object[])((object[])v)[1])[1]; ind_arr_person_name = new IndexViewImmutable<string>(path + "personname_ind") { Table = tab_person, KeyProducer = name_keyproducer, Scale = null }; //ind_arr_person_name.Scale = new ScaleCell(path + "personname_ind") {IndexCell = ind_arr_in_doc.IndexCell}; //ind_arr_person_name.Scale.Build(); index_person_name = new IndexDynamic<string, IndexViewImmutable<string>>(false) { Table = tab_person, IndexArray = ind_arr_person_name, KeyProducer = name_keyproducer }; tab_person.RegisterIndex(index_person); tab_person.RegisterIndex(index_person_name); tab_photo_doc.RegisterIndex(index_photo_doc); tab_reflection.RegisterIndex(index_reflected); tab_reflection.RegisterIndex(index_in_doc); }
public static void Main3(string[] args) { Random rnd = new Random(); Console.WriteLine("Start Task04_Sequenses_Main2"); 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("rage", new PType(PTypeEnumeration.real))); TableView tab_person = new TableView(path + "tab_person.pac", tp_person); Func <object, int> person_code_keyproducer = v => (int)((object[])((object[])v)[1])[0]; IndexKeyImmutable <int> ind_arr_person = new IndexKeyImmutable <int>(path + "person_ind") { Table = tab_person, KeyProducer = person_code_keyproducer, Scale = null }; //ind_arr_person.Scale = new ScaleCell(path + "person_ind") { IndexCell = ind_arr_person.IndexCell }; IndexDynamic <int, IndexKeyImmutable <int> > index_person = new IndexDynamic <int, IndexKeyImmutable <int> >(true, ind_arr_person); tab_person.RegisterIndex(index_person); 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 { tab_person.Warmup(); } // Проверим работу int search_key = nelements * 2 / 3; var ob = index_person.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.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"); }
public static void Main5() { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); string path = "../../../Databases/"; int NumberOfRecords = 1000000; Random rnd = new Random(); Console.WriteLine("Start Universal Index. Main5()"); 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 = true; 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 aa = new IndexKeyImmutable<int>(path + "dyna_index_int") { Table = table, KeyProducer = va => (int)((object[])(((object[])va)[1]))[1] }; bool tobuild_a_index = true; if (tobuild_a_index) aa.Build(); else aa.Warmup(); IndexDynamic<int, IndexKeyImmutable<int>> a_index = new IndexDynamic<int, IndexKeyImmutable<int>>(true) { Table = table, KeyProducer = va => (int)((object[])(((object[])va)[1]))[1], IndexArray = aa }; Console.WriteLine("a_index Build ok. Duration {0}", sw.ElapsedMilliseconds); sw.Restart(); int cnt = 0; for (int i = 0; i < 1000; i++) { int c = a_index.GetAllByKey(rnd.Next(NumberOfRecords * 3 / 2 - 1)).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); }