public Nametable32(Func <Stream> stream_gen) { PType tp_elem = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), new NamedType("str", new PType(PTypeEnumeration.sstring))); cod_str = new UniversalSequenceBase(tp_elem, stream_gen()); offsets = new UniversalSequenceBase(new PType(PTypeEnumeration.longinteger), stream_gen()); index_str = new IndexKey32CompImmutable(stream_gen, cod_str, ob => true, ob => Hashfunctions.HashRot13((string)((object[])ob)[1]), null); dyna_index = new Dictionary <string, int>(); }
public Nametable32(Func <Stream> stream_gen) { PType tp_elem = new PTypeRecord( new NamedType("code", new PType(PTypeEnumeration.integer)), new NamedType("str", new PType(PTypeEnumeration.sstring))); table = new UniversalSequenceBase(tp_elem, stream_gen()); str_offsets = new UniversalSequence <long>(new PType(PTypeEnumeration.longinteger), stream_gen()); Comparer <object> comp_str = Comparer <object> .Create(new Comparison <object>((object a, object b) => { var aa = (string)((object[])a)[1]; var bb = (string)((object[])b)[1]; return(aa.CompareTo(bb)); })); name_index = new IndexKey32CompImm(stream_gen, table, ob => Hashfunctions.HashRot13((string)((object[])ob)[1]), comp_str); dyna_index = new Dictionary <string, int>(); }
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); }