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 TripleStoreInt32_(Func <Stream> 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))); 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 IndexKey32CompImm(stream_gen, table, ob => (int)((object[])ob)[0], null); string selected_chars = "!\"#$%&\'()*+,-./0123456789:;<=>?@abcdefghjklmnopqrstuwxyz{|}~абвгдежзийклмнопрстуфхцчшщъыьэюяё"; Func <object, int> halfKeyFun = ob => { object[] tri = (object[])ob; object[] pair = (object[])tri[2]; int tg = (int)pair[0]; if (tg == 1) // iri { return(1 - (int)pair[1]); } else if (tg == 2) // str { string s = (string)pair[1]; int len = s.Length; var chs = s.ToCharArray() .Concat(Enumerable.Repeat(' ', len < 4 ? 4 - len : 0)) .Take(4) .Select(ch => { int ind = selected_chars.IndexOf(ch); if (ind == -1) { ind = 0; // неизвестный символ помечается как '!' } return(ind); }).ToArray(); return((chs[0] << 24) | (chs[1] << 16) | (chs[2] << 8) | chs[3]); } throw new Exception("Err: 292333"); }; Test_keyfun = halfKeyFun; Comparer <object> comp = Comparer <object> .Create(new Comparison <object>((object a, object b) => { object[] aa = (object[])((object[])a)[2]; object[] bb = (object[])((object[])b)[2]; int a1 = (int)aa[0]; int b1 = (int)bb[0]; int cmp = a1.CompareTo(b1); if (cmp != 0) { return(cmp); } if (a1 == 1) { return(((int)aa[1]).CompareTo(((int)bb[1]))); } return(((string)aa[1]).CompareTo(((string)bb[1]))); })); o_index = new IndexKey32CompImm(stream_gen, table, halfKeyFun, comp); }
public static void Main15() { string path = "../../../"; Random rnd = new Random(); int cnt = 0; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); Console.WriteLine("Start Main15"); Func <Stream> streamGen = () => new FileStream(path + "Databases/g" + (cnt++) + ".bin", FileMode.OpenOrCreate, FileAccess.ReadWrite); PType tp_person = new PTypeRecord( new NamedType("id", new PType(PTypeEnumeration.integer)), new NamedType("name", new PType(PTypeEnumeration.sstring)), new NamedType("years", new PType(PTypeEnumeration.real))); Comparer <object> comp_string = 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)); })); UniversalSequenceBase table = new UniversalSequenceBase(tp_person, streamGen()); IndexKey32CompImm id_index = new IndexKey32CompImm(streamGen, table, ob => (int)((object[])ob)[0], null); //IndexKey32CompImm namehash_index = new IndexKey32CompImm(streamGen, table, // ob => Hashfunctions.HashRot13((string)((object[])ob)[1]), null); //IndexKey32CompImm namehash_index_full = new IndexKey32CompImm(streamGen, table, // ob => Hashfunctions.HashRot13((string)((object[])ob)[1]), comp_string); IndexViewImmutable nameview_index = new IndexViewImmutable(streamGen, table, comp_string, path + "Databases/", 50_000_000); int nelements = 1_000_000; Console.WriteLine($" nelements={nelements}"); // Загрузка bool toload = true; if (toload) { sw.Restart(); var dataflow = Enumerable.Range(0, nelements) .Select(nom => new object[] { nelements - nom - 1, "" + (nelements - nom - 1), rnd.NextDouble() * 100D }); table.Clear(); foreach (object[] element in dataflow) { table.AppendElement(element); } table.Flush(); id_index.Build(); //namehash_index.Build(); //namehash_index_full.Build(); nameview_index.Build(); sw.Stop(); Console.WriteLine($"Indexes ok. duration={sw.ElapsedMilliseconds}"); } else { sw.Restart(); table.Refresh(); id_index.Refresh(); //namehash_index.Refresh(); //namehash_index_full.Refresh(); nameview_index.Refresh(); sw.Stop(); Console.WriteLine($"refresh {nelements} ok. duration={sw.ElapsedMilliseconds}"); } int id = nelements * 2 / 3; int nprobe = 1000; int total = 0; var query1 = id_index.GetAllBySample(new object[] { id, null, -1.0D }); foreach (var obj in query1) { Console.WriteLine(tp_person.Interpret(obj)); } sw.Restart(); for (int i = 0; i < nprobe; i++) { id = rnd.Next(nelements); total += id_index.GetAllBySample(new object[] { id, null, -1.0D }).Count(); } sw.Stop(); Console.WriteLine($"GetById {nprobe} probes. duration={sw.ElapsedMilliseconds}"); // Работаем с именами string name = "" + (nelements * 2 / 3); //var query2 = namehash_index.GetAllBySample(new object[] { -1, name, -1.0D }); //foreach (var obj in query2) Console.WriteLine(tp_person.Interpret(obj)); //total = 0; //sw.Restart(); //for (int i = 0; i < nprobe; i++) //{ // name = "" + rnd.Next(nelements); // total += namehash_index.GetAllBySample(new object[] { -1, name, -1.0D }).Count(); //} //sw.Stop(); //Console.WriteLine($"GetByNameHash {nprobe} probes. duration={sw.ElapsedMilliseconds} total={total}"); // Работаем с именами и компаратором //name = "" + (nelements / 3); //var query3 = namehash_index_full.GetAllBySample(new object[] { -1, name, -1.0D }); //foreach (var obj in query3) Console.WriteLine(tp_person.Interpret(obj)); //total = 0; //sw.Restart(); //for (int i = 0; i < nprobe; i++) //{ // name = "" + rnd.Next(nelements); // total += namehash_index_full.GetAllBySample(new object[] { -1, name, -1.0D }).Count(); //} //sw.Stop(); //Console.WriteLine($"GetByNameHash Full {nprobe} probes. duration={sw.ElapsedMilliseconds} total={total}"); // Работаем с view индексом name = "" + (nelements * 2 / 3); var query4 = nameview_index.SearchAll(new object[] { -1, name, -1.0D }); foreach (var obj in query4) { Console.WriteLine(tp_person.Interpret(obj)); } nprobe = 1000; total = 0; sw.Restart(); for (int i = 0; i < nprobe; i++) { name = "" + rnd.Next(nelements); total += nameview_index.SearchAll(new object[] { -1, name, -1.0D }).Count(); } sw.Stop(); Console.WriteLine($"IndexView search for {nprobe} probes. duration={sw.ElapsedMilliseconds} total={total}"); }