// Конструктор public Mag_Store(Stream tab_stream, Stream index1, Stream index2) { tp_triple = new PTypeRecord( //new NamedType("id", new PType(PTypeEnumeration.integer)), // Возможно, это временное решение new NamedType("subj", new PType(PTypeEnumeration.integer)), new NamedType("pred", new PType(PTypeEnumeration.integer)), new NamedType("obj", tp_ov)); table = new TableView(tab_stream, tp_triple); IndexViewImmutable <spo> index_spo_i = new IndexViewImmutable <spo>(index1) { KeyProducer = ob => new spo(((object[])ob)[1]), Table = table, Scale = null }; index_spo = new IndexDynamic <spo, IndexViewImmutable <spo> >(true, index_spo_i); table.RegisterIndex(index_spo); IndexViewImmutable <ops> index_ops_i = new IndexViewImmutable <ops>(index2) { KeyProducer = ob => new ops(((object[])ob)[1]), Table = table, Scale = null }; index_ops = new IndexDynamic <ops, IndexViewImmutable <ops> >(true, index_ops_i); table.RegisterIndex(index_ops); }
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(); }
/// <summary> /// Вызывает GetAllByLevel, отсеивает удалённые строки где row[0] = false и возвращает содержимое row[1]. /// </summary> /// <typeparam name="Tkey"></typeparam> /// <typeparam name="IndexImmut"></typeparam> /// <param name="index"></param> /// <param name="levelFunc">Функция применяется к содержимому таблицы row[1] </param> /// <returns></returns> public static IEnumerable <object[]> GetAllUndeletedByLevel <Tkey, IndexImmut>( this IndexDynamic <Tkey, IndexImmut> index, Func <object[], int> levelFunc) where IndexImmut : IIndexImmutable <Tkey> => index.GetAllByLevel(ent => levelFunc(ent.Get().CastRow <object, object[]>().Item2)) .Select(ent => (object[])ent.Get()) .Where(row => !(bool)row[0]) .Select(row => (object[])row[1]);
private void config_Add() { ConfigItem first = txt_TextFirst.Value; ConfigItem last = txt_TextLast.Value; if (first == null || last == null || string.IsNullOrEmpty(first.Text) || string.IsNullOrEmpty(last.Text)) { MessageBox.Show("Please input text first and last config."); return; } IndexDynamic id = db.FindItemFirstByContainFieldValue(new CNSPLIT() { SITE = Domain, TEXT_FIRST = first.Text, TEXT_LAST = last.Text }, "SITE,TEXT_FIRST,TEXT_LAST"); if (id.Index == -1 || id.Item == null) { object o = new CNSPLIT() { SITE = Domain, TEXT_FIRST = first.Text, TEXT_LAST = last.Text, SKIP_LINE_BOTTOM = (byte)last.SkipLine, SKIP_LINE_TOP = (byte)first.SkipLine, }; object add = db.AddItem(o); if (add != null && add.GetType().Name != typeof(EditStatus).Name) { CNSPLIT val = add.Convert <CNSPLIT>(); list_TextFirst.Items.Insert(0, new ComboboxItem() { Value = val.ID, Text = first.SkipLine.ToString() + " | " + first.Text }); list_TextLast.Items.Insert(0, new ComboboxItem() { Value = val.ID, Text = last.SkipLine.ToString() + " | " + last.Text }); txt_TextFirst.Reset(); txt_TextLast.Reset(); } else { MessageBox.Show("Add object fail"); } } else { MessageBox.Show("Config exist"); } }
// Конструктор public Store(Stream tab_stream, Stream index1, Stream index2, Stream index3, INametable nametable) { this.nametable = nametable; this.table = new TableView( tab_stream, PolarExtension.GetPolarType <IRecord <int, int, IUnion <int, string> > >()); IndexViewImmutable <spo> index_spo_i = new IndexViewImmutable <spo>(index1) { KeyProducer = ob => new spo( ob.CastRow <object, object>().Item2 .CastRow <int, int, object[]>()), Table = this.table, Scale = null, }; this.index_spo = new IndexDynamic <spo, IndexViewImmutable <spo> >(true, index_spo_i); this.table.RegisterIndex(this.index_spo); IndexViewImmutable <ops> index_ops_i = new IndexViewImmutable <ops>(index2) { KeyProducer = ob => new ops( ob.CastRow <object, object>().Item2 .CastRow <int, int, object[]>()), Table = this.table, Scale = null, }; this.index_ops = new IndexDynamic <ops, IndexViewImmutable <ops> >(true, index_ops_i); this.table.RegisterIndex(this.index_ops); IndexViewImmutable <int> index_p_i = new IndexViewImmutable <int>(index3) { KeyProducer = ob => (int)ob.CastRow <object, object[]>().Item2[1], Table = this.table, Scale = null }; this.index_p = new IndexDynamic <int, IndexViewImmutable <int> >(true, index_p_i); this.table.RegisterIndex(this.index_p); if (this.table.Count() > 0) { this.table.BuildIndexes(); } }
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 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 FirstIntGraph(string path) { PType tp_tabelement = new PTypeRecord( new NamedType("subject", new PType(PTypeEnumeration.integer)), new NamedType("predicate", new PType(PTypeEnumeration.integer)), new NamedType("obj", ObjectVariantsPolarType.ObjectVariantPolarType)); Func <object, SPO_Troyka> spokeyproducer = v => { object[] va = (object[])((object[])v)[1]; return(new SPO_Troyka((int)va[0], (int)va[1], va[2].ToOVariant(ng.coding_table.GetStringByCode))); //.ToComparable() }; Func <object, PO_Pair> pokeyproducer = v => { object[] va = (object[])((object[])v)[1]; return(new PO_Pair((int)va[1], va[2].ToOVariant(ng.coding_table.GetStringByCode))); }; Func <object, PO_Pair> oskeyproducer = v => { object[] va = (object[])((object[])v)[1]; return(new PO_Pair((int)va[0], va[2].ToOVariant(ng.coding_table.GetStringByCode))); }; Func <object, SP_Pair> spkeyproducer = v => { object[] va = (object[])((object[])v)[1]; return(new SP_Pair((int)va[0], (int)va[1])); }; Func <object, int> skeyproducer = v => { object[] va = (object[])((object[])v)[1]; return((int)va[0]); }; Func <object, int> pkeyproducer = v => { object[] va = (object[])((object[])v)[1]; return((int)va[1]); }; Func <object, ObjectVariants> okeyproducer = v => { object[] va = (object[])((object[])v)[1]; return(va[2].ToOVariant(ng.coding_table.GetStringByCode)); }; // Опорная таблица table = new TableView(path + "stable", tp_tabelement); if (table.Count() == 0) { table.Fill(new object[0]); } ng = new NodeGeneratorInt(path + "coding", table.Count() == 0); // Индекс spo spo_ind = new IndexDynamic <SPO_Troyka, IndexHalfkeyImmutable <SPO_Troyka> >(false) { Table = table, IndexArray = new IndexHalfkeyImmutable <SPO_Troyka>(path + "spo_ind") { Table = table, KeyProducer = spokeyproducer, HalfProducer = sp => sp.GetHashCode() }, KeyProducer = spokeyproducer, }; po_ind = new IndexDynamic <PO_Pair, IndexHalfkeyImmutable <PO_Pair> >(false) { Table = table, IndexArray = new IndexHalfkeyImmutable <PO_Pair>(path + "po_ind") { Table = table, KeyProducer = pokeyproducer, HalfProducer = sp => sp.GetHashCode() }, KeyProducer = pokeyproducer }; so_ind = new IndexDynamic <PO_Pair, IndexHalfkeyImmutable <PO_Pair> >(false) { Table = table, IndexArray = new IndexHalfkeyImmutable <PO_Pair>(path + "so_ind") { Table = table, KeyProducer = oskeyproducer, HalfProducer = sp => sp.GetHashCode() }, KeyProducer = pokeyproducer }; sp_ind = new IndexDynamic <SP_Pair, IndexHalfkeyImmutable <SP_Pair> >(false) { Table = table, IndexArray = new IndexHalfkeyImmutable <SP_Pair>(path + "sp_ind") { Table = table, KeyProducer = spkeyproducer, HalfProducer = sp => sp.GetHashCode() }, KeyProducer = spkeyproducer }; s_ind = new IndexDynamic <int, IndexKeyImmutable <int> >(false) { Table = table, IndexArray = new IndexKeyImmutable <int>(path + "s_ind") { Table = table, KeyProducer = skeyproducer, }, KeyProducer = skeyproducer }; p_ind = new IndexDynamic <int, IndexKeyImmutable <int> >(false) { Table = table, IndexArray = new IndexKeyImmutable <int>(path + "p_ind") { Table = table, KeyProducer = pkeyproducer, }, KeyProducer = pkeyproducer }; o_ind = new IndexDynamic <ObjectVariants, IndexHalfkeyImmutable <ObjectVariants> >(false) { Table = table, IndexArray = new IndexHalfkeyImmutable <ObjectVariants>(path + "o_ind") { Table = table, KeyProducer = okeyproducer, HalfProducer = sp => sp.GetHashCode() }, KeyProducer = okeyproducer }; }
//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 static void Main4(string[] args) { Random rnd = new Random(); Console.WriteLine("Start Task04_Sequenses_Main3"); 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); IndexViewImmutable <string> name_index = new IndexViewImmutable <string>(path + "name_index.pac") { Table = tab_person, KeyProducer = v => (string)((object[])((object[])v)[1])[1] }; IndexDynamic <string, IndexViewImmutable <string> > index_person_name = new IndexDynamic <string, IndexViewImmutable <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 { tab_person.Warmup(); } // Проверим работу 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"); string search_string = "=66666"; var query = index_person_name.GetAllByLevel((PaEntry entry) => { var name = (string)((object[])((object[])entry.Get())[1])[1]; if (name.StartsWith(search_string)) { return(0); } return(name.CompareTo(search_string)); }); foreach (object v in query.Select(entry => ((object[])entry.Get())[1])) { Console.WriteLine(tp_person.Interpret(v)); } }
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"); }
//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); }