// Конструктор 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 TripleStore32(Func <Stream> stream_gen, string tmp_dir) { // Тип Object Variants PType tp_ov = new PTypeUnion( new NamedType("dummy", new PType(PTypeEnumeration.none)), new NamedType("iri", new PType(PTypeEnumeration.integer)), new NamedType("str", new PType(PTypeEnumeration.sstring))); tp_triple = new PTypeRecord( new NamedType("subj", new PType(PTypeEnumeration.integer)), new NamedType("pred", new PType(PTypeEnumeration.integer)), new NamedType("obj", tp_ov)); table = new UniversalSequenceBase(tp_triple, stream_gen()); var spo_comparer = Comparer <object> .Create(new Comparison <object>((object a, object b) => { object[] aa = (object[])a; object[] bb = (object[])b; int cmp = ((int)aa[0]).CompareTo((int)bb[0]); return(cmp); })); keyFunc = tri => (int)((object[])tri)[0]; index_spo = new UniversalSequenceCompKey32(stream_gen(), keyFunc, spo_comparer, table); indexTest = new IndexViewImmutable(stream_gen, table, Comparer <object> .Create(new Comparison <object>((object a, object b) => { object[] aa = (object[])a; object[] bb = (object[])b; int cmp = ((int)aa[0]).CompareTo((int)bb[0]); return(cmp); })), tmp_dir, 20_000_000 ); }
// Конструктор 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 static void Main1() { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); string path = "../../../Databases/"; int NumberOfRecords = 1000000; Random rnd = new Random(); Console.WriteLine("Start Universal Index"); PType tp_table_element = new PTypeRecord( new NamedType("name", new PType(PTypeEnumeration.sstring)), new NamedType("age", new PType(PTypeEnumeration.integer))); IBearingTableImmutable table = new TableViewImmutable(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); } IIndexImmutable<string> s_index = new IndexViewImmutable<string>(path + "s_index") { Table = (IBearingTable) table, KeyProducer = va => (string)((object[])va)[0] }; if (tobuild) { sw.Restart(); s_index.Build(); sw.Stop(); Console.WriteLine("s_index Build ok. Duration {0}", sw.ElapsedMilliseconds); } sw.Restart(); int cnt = 0; for (int i = 0; i < 1000; i++) { int c = s_index.GetAllByKey(rnd.Next(NumberOfRecords - 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); }
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 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 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 Main4() { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); string path = "../../../Databases/"; int NumberOfRecords = 1000000; Random rnd = new Random(); Console.WriteLine("Start Universal Index. Main4()"); 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 aa = new IndexViewImmutable<int>(path + "dyna_index_int") { Table = table, KeyProducer = va => (int)((object[])(((object[])va)[1]))[1] }; bool tobuild_s_index = false; if (tobuild_s_index) aa.Build(); else aa.Warmup(); IndexDynamic<int, IndexViewImmutable<int>> a_index = new IndexDynamic<int, IndexViewImmutable<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); }