public void Test_Indexed_Persons_Sorted_Paging_By_Index() { const int ITEMS_COUNT = 100; const string INDEX_NAME_NAME = "name"; // definizione indice su entità Person var persons = new Index <Person>( new IndexMaker <Person, string>(INDEX_NAME_NAME, p => p.FirstName) ); // Generiamo ITEMS_COUNT entità casuali var fakerRandomizer = new Bogus.Randomizer(); var fakerNameDataset = new Bogus.DataSets.Name("en"); for (var i = 0; i < ITEMS_COUNT; i++) { persons.Add(new Person( fakerNameDataset.FirstName(), fakerNameDataset.LastName(), fakerRandomizer.Int(1900, 2000) * 10000 + fakerRandomizer.Int(1, 12) * 100 + fakerRandomizer.Int(1, 31))); } // Enumeriamo gli ultimi 10 item ordinati desc per l'indice creato const int PAGE_SIZE = ITEMS_COUNT / 10; _output.WriteLine($"Ultimi {PAGE_SIZE} item in ordine inverso:\n"); var lastPageByName = persons[INDEX_NAME_NAME].AsEnumerable().Skip(ITEMS_COUNT - PAGE_SIZE).Take(PAGE_SIZE).Reverse(); foreach (var p in lastPageByName) { _output.WriteLine($"{p.FirstName}"); } }
public Employee() { var person = new Person(); UserName = person.UserName; FirstName = person.FirstName; LastName = person.LastName; Email = person.Email; Phone = person.Phone; DateOfBirth = person.DateOfBirth; var bogusRandom = new Bogus.Randomizer(); var bogusDate = new Bogus.DataSets.Date(); DateHired = bogusDate.Between(DateTime.Today.AddYears(-5), DateTime.Today); DateContractEnd = bogusDate.Between(DateTime.Today, DateTime.Today.AddYears(-5)); ChildrenCount = bogusRandom.Int(0, 6); ShoeSize = bogusRandom.Double(7, 12); ChangeInPocket = bogusRandom.Double(0, 5); CarValue = bogusRandom.Double(500, 40000); }
protected override void Seed(ApplicationDbContext context) { base.Seed(context); int movieCount = 20; int crewCount = 40; //weight actors 0.8 directors 0.2 int reviewCount = 20; int userCount = 30; #region UserRoles const string adminRoleName = "Admin"; const string userRoleName = "UserNormal"; var roleStore = new RoleStore <IdentityRole>(context); var roleManager = new RoleManager <IdentityRole>(roleStore); var adminRole = new IdentityRole { Name = adminRoleName }; var userRole = new IdentityRole { Name = userRoleName }; roleManager.Create(adminRole); roleManager.Create(userRole); #endregion #region Users var store = new UserStore <ApplicationUser>(context); var manager = new UserManager <ApplicationUser>(store); #region Admin user account var admin = new ApplicationUser() { Id = Guid.NewGuid().ToString(), UserName = "******", Email = "*****@*****.**", EmailConfirmed = true }; var checkAdminUser = manager.Create(admin, "pass123"); if (checkAdminUser.Succeeded) { manager.AddToRole(admin.Id, adminRoleName); } else { var errors = checkAdminUser.Errors; errors.ForEach(e => Debug.WriteLine(e)); } #endregion var users = new Faker <ApplicationUser>() .Rules((f, u) => { var userEmail = f.Internet.Email(); u.Id = Guid.NewGuid().ToString(); u.AvatarUri = f.Internet.Avatar(); u.DisplayName = f.Name.FullName(); u.UserName = userEmail; u.Email = userEmail; u.EmailConfirmed = true; }) .Generate(userCount); users.ForEach(u => { var checkUser = manager.Create(u, "pass123"); if (checkUser.Succeeded) { manager.AddToRole(u.Id, userRoleName); } else { checkUser.Errors.ForEach(e => Debug.WriteLine(e)); } }); #endregion #region Gernes string[] genreNames = { "Action", "Action", "Adventure", "Comedy", "Crime", "Drama", "Fantasy", "Horror", "Romance", "Science fiction", "Thriller" }; context.Genres.AddRange(genreNames.Select(genre => new Genre() { Title = genre })); #endregion #region Movies var random = new Bogus.Randomizer(); var reviewGenerator = new Faker <Review>() .Rules((f, r) => { r.AuthorId = f.PickRandom(users.Select(u => u.Id)); r.Title = f.Lorem.Sentence(); r.Body = f.Lorem.Sentences(); r.CreatedOn = f.Date.Past(); r.Rating = f.Random.Float(0, 10); }); var movies = new Faker <Movie>() .Rules((f, m) => { m.Title = f.Hacker.Adjective() + " " + f.Hacker.Noun(); m.Year = f.Random.Int(2000, 2018); m.Length = f.Random.Int(60, 180); m.Rating = f.Random.Float(0f, 10f); m.Image = f.Image.PicsumUrl(320, 320); m.Description = f.Lorem.Sentences(); m.Reviews = reviewGenerator.Generate(random.Int(3, 8)); m.Comments = Get20Comments(); m.Reviews.ForEach(r => r.Comments = Get20Comments()); }) .Generate(movieCount); //movies = Enumerable.Range(0, movieCount).Zip(movies, (i, m) => //{ // m.Reviews.ForEach(r => r.MovieId = i); // m.Id = i; // return m; //}).ToList(); #endregion #region FilmCrewMember var crew = new Faker <FilmCrewMember>() .Rules((f, c) => { c.Bio = f.Lorem.Sentences(); c.DoB = f.Date.Past(60, new DateTime(1990, 2, 15)); c.ImageUri = f.Internet.Avatar(); c.Name = f.Name.FullName(); c.Role = (MovieRole)f.Random.WeightedRandom(new int[] { 0, 1 }, new float[] { 0.8f, 0.2f }); c.Comments = Get20Comments(); }) .Generate(crewCount); var moviesArr = movies.ToList(); crew.ForEach(c => c.Movies = random.ListItems <Movie>(moviesArr, random.Number(3, 10))); context.FilmCrewMembers.AddRange(crew); #endregion List <Comment> Get20Comments() { var comments = new Faker <Comment>() .Rules((f, c) => { c.AuthorId = f.PickRandom(users.Select(u => u.Id)); c.CommentBody = f.Lorem.Sentences(); c.CreatedOn = f.Date.Past(); }) .Generate(20); var parentCommentsCount = random.Int(8, 13); var parentComments = comments.Take(parentCommentsCount).ToArray(); var childrenComments = comments.Skip(parentCommentsCount).ToArray(); childrenComments.ForEach(c => c.CommentParentId = random.Number(0, 2)); return(parentComments.Concat(childrenComments).ToList()); } context.Users.ForEach(u => u.EmailConfirmed = true); context.SaveChanges(); }
public void Test_Indexed_Persons_Find_By_Index() { const int ITEMS_COUNT = 1000000; const string INDEX_NAME_YEAR = "year"; const string INDEX_NAME_MONTH = "month"; const string INDEX_NAME_DAY = "day"; const string INDEX_NAME_NAME = "name"; // Lookup string[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; // definizione indici su entità Person var persons = new Index <Sample_Entities.Person>( new IndexMaker <Person, string>(INDEX_NAME_NAME, p => p.FirstName), new IndexMaker <Person, int>(INDEX_NAME_YEAR, p => p.Date / 10000), new IndexMaker <Person, int>(INDEX_NAME_DAY, p => p.Date % 100), new IndexMaker <Person, string>(INDEX_NAME_MONTH, p => months[p.Date / 100 % 100 - 1]) ); // Generiamo ITEMS_COUNT entità casuali var fakerRandomizer = new Bogus.Randomizer(); var fakerNameDataset = new Bogus.DataSets.Name("en"); for (var i = 0; i < ITEMS_COUNT; i++) { persons.Add(new Person( fakerNameDataset.FirstName(), fakerNameDataset.LastName(), fakerRandomizer.Int(1900, 2000) * 10000 + fakerRandomizer.Int(1, 12) * 100 + fakerRandomizer.Int(1, 31))); } var sw = new Stopwatch(); sw.Reset(); // Ricerca su indice year const int YEAR_TO_SEARCH_FOR = 1971; if (persons[INDEX_NAME_YEAR][YEAR_TO_SEARCH_FOR].Any()) { _output.WriteLine($"Indice '{INDEX_NAME_YEAR}', ricerca su valore {YEAR_TO_SEARCH_FOR}, trovati {persons[INDEX_NAME_YEAR][YEAR_TO_SEARCH_FOR].Count} item"); } else { _output.WriteLine("Nessuno."); } _output.WriteLine($"Tempo impiegato: {sw.ElapsedMilliseconds} msec\n"); sw.Restart(); // Ricerca su indice month const string MONTH_TO_SEARCH_FOR = "Nov"; if (persons[INDEX_NAME_MONTH][MONTH_TO_SEARCH_FOR].Any()) { _output.WriteLine($"Indice '{INDEX_NAME_MONTH}', ricerca su valore {MONTH_TO_SEARCH_FOR}, trovati {persons[INDEX_NAME_MONTH][MONTH_TO_SEARCH_FOR].Count} item"); } else { _output.WriteLine("Nessuno."); } _output.WriteLine($"Tempo impiegato: {sw.ElapsedMilliseconds} msec\n"); // Ricerca su indice day sw.Restart(); const int DAY_TO_SEARCH_FOR = 17; if (persons[INDEX_NAME_DAY][DAY_TO_SEARCH_FOR].Any()) { _output.WriteLine($"Indice '{INDEX_NAME_DAY}', ricerca su valore {DAY_TO_SEARCH_FOR}, trovati {persons[INDEX_NAME_DAY][DAY_TO_SEARCH_FOR].Count} item"); } else { _output.WriteLine("Nessuno."); } _output.WriteLine($"Tempo impiegato: {sw.ElapsedMilliseconds} msec\n"); // Ricerca su indice name sw.Restart(); const string NAME_TO_SEARCH_FOR = "Lawrence"; if (persons[INDEX_NAME_NAME][NAME_TO_SEARCH_FOR].Any()) { _output.WriteLine($"Indice '{INDEX_NAME_NAME}', ricerca su valore {NAME_TO_SEARCH_FOR}, trovati {persons[INDEX_NAME_NAME][NAME_TO_SEARCH_FOR].Count} item"); } else { _output.WriteLine("Nessuno."); } _output.WriteLine($"Tempo impiegato: {sw.ElapsedMilliseconds} msec\n"); }