private async Task HandleMessage(UpdateIndexDto dto) { using (var db = new ApplicationDbContext()) { Farm farm; switch (dto.UpdateMethod) { case EUpdateMethod.Create: farm = await db.Farms.Include(f => f.Products).Where(f => f.FarmId == dto.FarmId).FirstAsync(); //good idea but different time zone or system time on different servers do not allow to use a timestamp to filter out old messages if (farm != null && farm.UpdateDateTime > farm.IndexDateTime) // && farm.UpdateDateTime == dto.UpdateTime) { AddFarmToIndex(farm, _indexWriter, db); await db.SaveChangesAsync(); _indexWriter.Commit(); } break; case EUpdateMethod.Update: farm = await db.Farms.Include(f => f.Products).Where(f => f.FarmId == dto.FarmId).FirstAsync(); if (farm != null && farm.UpdateDateTime > farm.IndexDateTime) // && farm.UpdateDateTime == dto.UpdateTime) { UpdateFarmInIndex(farm, _indexWriter, db); await db.SaveChangesAsync(); _indexWriter.Commit(); } break; case EUpdateMethod.Delete: farm = await db.Farms.FindAsync(dto.FarmId); if (farm != null) { DeleteFarmFromIndex(farm, _indexWriter, db); await db.SaveChangesAsync(); _indexWriter.Commit(); } break; } } }
private static void DeleteFarmFromIndex(Farm farm, IndexWriter indexWriter, ApplicationDbContext db) { indexWriter.DeleteDocuments(new Term("id", farm.FarmId.ToString())); db.Farms.Remove(farm); }
private async Task CreateIndex(ApplicationDbContext db) { var farms = await db.Farms.Include(f => f.Products).ToListAsync(); Parallel.ForEach(farms, farm => //foreach (var farm in farms) { try { if (!farm.DeleteWhenRemovedFromIndex) { AddFarmToIndex(farm, _indexWriter, db); } else { //since it is not yet in the index we can simply remove it from the db db.Farms.Remove(farm); } } catch (Exception) { //TODO error handling. Should certainly not abort the adddition of documents } }); await db.SaveChangesAsync(); _indexWriter.Optimize(); _indexWriter.Commit(); }
private static void UpdateFarmInIndex(Farm farm, IndexWriter indexWriter, ApplicationDbContext db) { var doc = CreateDocumentFromFarm(farm); indexWriter.UpdateDocument(new Term("id", farm.FarmId.ToString()), doc); db.Entry(farm).State = EntityState.Modified; farm.IndexDateTime = DateTime.Now; }
private void AddFarmToIndex(Farm farm, IndexWriter indexWriter, ApplicationDbContext db) { var doc = CreateDocumentFromFarm(farm); indexWriter.AddDocument(doc); db.Entry(farm).State = EntityState.Modified; farm.IndexDateTime = DateTime.Now; }
private async Task UpdateIndex(ApplicationDbContext db) { var farms = await GetNewAddedQueryable(db).ToListAsync(); Parallel.ForEach(farms, farm => { try { AddFarmToIndex(farm, _indexWriter, db); } catch (Exception) { //TODO error handling. Should certainly not abort the adddition of documents } }); farms = await db.Farms.Include(f => f.Products).Where(f => f.UpdateDateTime > f.IndexDateTime && f.IndexDateTime != DateTime.MinValue).ToListAsync(); Parallel.ForEach(farms, farm => { try { UpdateFarmInIndex(farm, _indexWriter, db); } catch (Exception) { //TODO error handling. Should certainly not abort the adddition of documents } }); farms = await db.Farms.Where(f => f.DeleteWhenRemovedFromIndex).ToListAsync(); Parallel.ForEach(farms, farm => { try { DeleteFarmFromIndex(farm, _indexWriter, db); } catch (Exception) { //TODO error handling. Should certainly not abort the adddition of documents } }); await db.SaveChangesAsync(); _indexWriter.Optimize(); _indexWriter.Commit(); }
private static IQueryable<Farm> GetNewAddedQueryable(ApplicationDbContext db) { return db.Farms.Where(f => f.UpdateDateTime > f.IndexDateTime && f.IndexDateTime == DateTime.MinValue && f.DeleteWhenRemovedFromIndex == false); }
private async Task InitialiseIndex() { int totalHits = 0; try { var searcher = new IndexSearcher(_azureDirectory); var hits = searcher.Search( new QueryParser(Version.LUCENE_30, "name", new StandardAnalyzer(Version.LUCENE_30)).Parse("*:*"), 1); totalHits = hits.TotalHits; } catch (FileNotFoundException) { //that's ok, means no index was created yet } using (var db = new ApplicationDbContext()) { if (totalHits > 0) { var farmTotal = await db.Farms.CountAsync(); var totalAdded = await GetNewAddedQueryable(db).CountAsync(); if (farmTotal == totalHits + totalAdded) { await UpdateIndex(db); } else { //Unfortunately not consistent index _indexWriter.DeleteAll(); await CreateIndex(db); } } else { await CreateIndex(db); } } _throttlingCall.LastTimeExecuteWasCalled = DateTime.Now; await InformWebRolesAboutNewIndex(); }