// Constructor. Takes an argument that defines which hashfunction to use, and the size of the keys. // Sets the propper variables accordingly. public Hashing(Hashfunctions hashFun, int l) { l_value = l; q_value = 89; p_value = BigInteger.Pow(2, q_value) - 1; switch (hashFun) { case Hashfunctions.MultiplyShift: a_value_64 = getRandomULong(); h = MultiplyShift; break; case Hashfunctions.Multiply_Mod_Prime: a_value = getRandomBigDigInt(); b_value = getRandomBigDigInt(); l_squared = (ulong)BigInteger.Pow(2, l); h = Multiply_mod_prime; break; case Hashfunctions.fourUniversal: a_0 = getRandomBigDigInt(); a_1 = getRandomBigDigInt(); a_2 = getRandomBigDigInt(); a_3 = getRandomBigDigInt(); h = fourUniversal; break; default: throw new Exception($"Function {hashFun} not yet impemented"); } }
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 IEnumerable <object> GetByObjName(string s) { return(name_index.GetAllByKey(Hashfunctions.First4charsRu(s))); }
public TripleStoreInt32(Func <Stream> stream_gen) { // сначала таблица имен nt = new Nametable32(stream_gen); // Тип 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)), new NamedType("int", new PType(PTypeEnumeration.sstring)), new NamedType("date", new PType(PTypeEnumeration.sstring)), new NamedType("langstr", new PTypeRecord( new NamedType("lang", new PType(PTypeEnumeration.sstring)), new NamedType("str", new PType(PTypeEnumeration.sstring))))); PType 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()); // прямой ссылочный индекс s_index = new IndexKey32CompImmutable(stream_gen, table, ob => Enumerable.Repeat <int>((int)((object[])ob)[0], 1), null); // Обратный ссылочный индекс i_index = new IndexKey32Imm(stream_gen, table, obj => { object[] pair = (object[])((object[])obj)[2]; int tg = (int)pair[0]; if (tg != 1) { return(Enumerable.Empty <int>()); } return(Enumerable.Repeat <int>((int)pair[1], 1)); }, null); // Индекс по тексту объектов триплетов с предикатом http://fogid.net/o/name Comparer <object> comp = Comparer <object> .Create(new Comparison <object>((object a, object b) => { return(string.Compare((string)((object[])((object[])a)[2])[1], (string)((object[])((object[])b)[2])[1])); })); int name_code = Int32.MinValue; // nt.GetSetStr("http://fogid.net/o/name"); // Такое предварительное вычисление не работает!!! name_index = new IndexKey32CompImmutable(stream_gen, table, obj => { if (name_code == Int32.MinValue) { name_code = nt.GetSetStr("http://fogid.net/o/name"); } object[] tri = (object[])obj; //TODO: кодов имени может быть много... if ((int)tri[1] != name_code) { return(Enumerable.Empty <int>()); } object[] pair = (object[])tri[2]; int tg = (int)pair[0]; string data = null; if (tg == 2) { data = (string)pair[1]; } //else if (tg == 5) data = (string)((object[])pair[1])[1]; // пока будем работать только с простыми строками if (data != null) { return(Enumerable.Repeat <int>(Hashfunctions.First4charsRu(data), 1)); } return(Enumerable.Empty <int>()); }, //new int[] { Hashfunctions.First4charsRu((string)((object[])obj)[1]) } comp); }
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); }