public void Dispose() { var timer = new Stopwatch(); timer.Start(); this.ratingGraph = null; // Free up memory/increase Index accessing speed by converting List<> objects to arrays foreach (var recipe in this.snapshot.recipeMap.Values) { recipe.Ingredients = recipe.Ingredients.ToArray(); } foreach (var ingredientNode in this.snapshot.ingredientMap.Values) { var temp = new List<RecipeNode[]>(); int usedTags = 0; for (int count = 0; count < RecipeTag.NumberOfTags; count++) { RecipeNode[] nodes = null; if (ingredientNode.RecipesByTag[count] != null) { nodes = ingredientNode.RecipesByTag[count].ToArray(); usedTags += 1 << count; } temp.Add(nodes); } ingredientNode.RecipesByTag = temp.ToArray(); ingredientNode.AvailableTags = usedTags; } // Force garbage collection now, since there might be several hundred megs of unreachable allocations GC.Collect(); timer.Stop(); ModelingSession.Log.InfoFormat("Cleaning up Indexer took {0}ms.", timer.ElapsedMilliseconds); }
public void Dispose() { var timer = new Stopwatch(); timer.Start(); ratingGraph = null; //Free up memory/increase index accessing speed by converting List<> objects to arrays foreach (var r in snapshot.recipeMap.Values) { r.Ingredients = r.Ingredients.ToArray(); } foreach (var i in snapshot.ingredientMap.Values) { var temp = new List <RecipeNode[]>(); var usedTags = 0; for (var c = 0; c < RecipeTag.NUM_TAGS; c++) { RecipeNode[] nodes = null; if (i.RecipesByTag[c] != null) { nodes = i.RecipesByTag[c].ToArray(); usedTags += (1 << c); } temp.Add(nodes); } i.RecipesByTag = temp.ToArray(); i.AvailableTags = usedTags; } GC.Collect(); //Force garbage collection now, since there might be several hundred megs of unreachable allocations timer.Stop(); ModelingSession.Log.InfoFormat("Cleaning up Indexer took {0}ms.", timer.ElapsedMilliseconds); }
public void Dispose() { var timer = new Stopwatch(); timer.Start(); ratingGraph = null; //Free up memory/increase index accessing speed by converting List<> objects to arrays foreach (var r in snapshot.recipeMap.Values) { r.Ingredients = r.Ingredients.ToArray(); } foreach (var i in snapshot.ingredientMap.Values) { var temp = new List<RecipeNode[]>(); var usedTags = 0; for (var c = 0; c < RecipeTag.NUM_TAGS; c++) { RecipeNode[] nodes = null; if (i.RecipesByTag[c] != null) { nodes = i.RecipesByTag[c].ToArray(); usedTags += (1 << c); } temp.Add(nodes); } i.RecipesByTag = temp.ToArray(); i.AvailableTags = usedTags; } GC.Collect(); //Force garbage collection now, since there might be several hundred megs of unreachable allocations timer.Stop(); ModelingSession.Log.InfoFormat("Cleaning up Indexer took {0}ms.", timer.ElapsedMilliseconds); }
public void Index(IKPCContext context) { var timer = new Stopwatch(); timer.Start(); ratingGraph = new RatingGraph(); var loader = context.ModelerLoader; foreach (var dataItem in loader.LoadRatingGraph()) { var r = dataItem.Rating; var uid = dataItem.UserId; var rid = dataItem.RecipeId; if (r < 4) //Rating too low to worry about { continue; //TODO: Might not be needed, DB should only query for 4 or 5 star ratings } ratingGraph.AddRating(r, uid, rid); } ModelingSession.Log.InfoFormat("Building Rating Graph took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); //Create empty recipe nodes without links snapshot.recipeMap = (from o in loader.LoadRecipeGraph() select new RecipeNode() { RecipeId = o.Id, Rating = o.Rating, Tags = o.Tags, Hidden = o.Hidden, Ingredients = new List<IngredientUsage>() }).ToDictionary(k => k.RecipeId); ModelingSession.Log.InfoFormat("Building empty RecipeNodes took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); //Build tag index foreach (var r in snapshot.recipeMap.Values) { if (r.Hidden) continue; //_recipeList does not include Hidden recipes so they don't get picked at random foreach (var tag in r.Tags) { var nodes = snapshot.recipeList[tag.Value] as List<RecipeNode>; if (nodes == null) snapshot.recipeList[tag.Value] = nodes = new List<RecipeNode>(); nodes.Add(r); } } for (var i = 0; i < snapshot.recipeList.Length; i++) { var list = snapshot.recipeList[i] as List<RecipeNode>; if (list != null) { snapshot.recipeList[i] = list.ToArray(); } else //No recipes in DB use this tag { snapshot.recipeList[i] = new RecipeNode[0]; } } ModelingSession.Log.InfoFormat("Indexing recipes by tag took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); //Loop through ingredient usages and fill in vertices on graph //For each item: Create IngredientUsage and add to recipe, create IngredientNode (if necessary) and add recipe to IngredientNode foreach (var o in loader.LoadIngredientGraph()) { var rid = o.RecipeId; var ingid = o.IngredientId; var qty = o.Qty; var unit = o.Unit; var convType = Unit.GetConvType(unit); List<RecipeNode>[] nodes; IngredientNode ingNode; var node = snapshot.recipeMap[rid]; if (!snapshot.ingredientMap.TryGetValue(ingid, out ingNode)) //New ingredient, create node for it { nodes = new List<RecipeNode>[RecipeTag.NUM_TAGS]; snapshot.ingredientMap.Add(ingid, ingNode = new IngredientNode() { IngredientId = ingid, RecipesByTag = nodes, ConvType = convType }); } else { nodes = ingNode.RecipesByTag as List<RecipeNode>[]; } //For each tag the recipe has, we need to create a link through ingNode.RecipesByTag to the recipe if (!node.Hidden) //Don't index Hidden recipes { foreach (var tag in node.Tags) { if (nodes[tag.Value] == null) { nodes[tag.Value] = new List<RecipeNode>(); } nodes[tag.Value].Add(node); //Add ingredient link to RecipeNode } } var usages = node.Ingredients as List<IngredientUsage>; //Add ingredient usage to recipe usages.Add(new IngredientUsage() { Amt = qty, Ingredient = ingNode, Unit = unit }); } ModelingSession.Log.InfoFormat("Creating IngredientUsage vertices took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); //Create suggestion links for each recipe foreach (var r in snapshot.recipeMap.Values) { r.Suggestions = (from s in ratingGraph.GetSimilarRecipes(r.RecipeId) select snapshot.recipeMap[s]).ToArray(); } ModelingSession.Log.InfoFormat("Building suggestions for each recipe took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); }
public void Index(IKPCContext context) { var timer = new Stopwatch(); timer.Start(); this.ratingGraph = new RatingGraph(); var loader = context.ModelerLoader; this.CreateRatingGraph(loader); ModelingSession.Log.InfoFormat("Building Rating Graph took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); this.CreateEmptyRecipeNodes(loader); ModelingSession.Log.InfoFormat("Building empty RecipeNodes took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); this.IndexRecipes(); ModelingSession.Log.InfoFormat("Indexing recipes by tag took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); this.CreatIngredientUsageVertices(loader); ModelingSession.Log.InfoFormat("Creating IngredientUsage vertices took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); this.CreateSuggestionLinks(); ModelingSession.Log.InfoFormat("Building suggestions for each recipe took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); }
public void Index(IKPCContext context) { var timer = new Stopwatch(); timer.Start(); ratingGraph = new RatingGraph(); var loader = context.ModelerLoader; foreach (var dataItem in loader.LoadRatingGraph()) { var r = dataItem.Rating; var uid = dataItem.UserId; var rid = dataItem.RecipeId; if (r < 4) //Rating too low to worry about { continue; //TODO: Might not be needed, DB should only query for 4 or 5 star ratings } ratingGraph.AddRating(r, uid, rid); } ModelingSession.Log.InfoFormat("Building Rating Graph took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); //Create empty recipe nodes without links snapshot.recipeMap = (from o in loader.LoadRecipeGraph() select new RecipeNode() { RecipeId = o.Id, Rating = o.Rating, Tags = o.Tags, Hidden = o.Hidden, Ingredients = new List <IngredientUsage>() }).ToDictionary(k => k.RecipeId); ModelingSession.Log.InfoFormat("Building empty RecipeNodes took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); //Build tag index foreach (var r in snapshot.recipeMap.Values) { if (r.Hidden) { continue; //_recipeList does not include Hidden recipes so they don't get picked at random } foreach (var tag in r.Tags) { var nodes = snapshot.recipeList[tag.Value] as List <RecipeNode>; if (nodes == null) { snapshot.recipeList[tag.Value] = nodes = new List <RecipeNode>(); } nodes.Add(r); } } for (var i = 0; i < snapshot.recipeList.Length; i++) { var list = snapshot.recipeList[i] as List <RecipeNode>; if (list != null) { snapshot.recipeList[i] = list.ToArray(); } else //No recipes in DB use this tag { snapshot.recipeList[i] = new RecipeNode[0]; } } ModelingSession.Log.InfoFormat("Indexing recipes by tag took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); //Loop through ingredient usages and fill in vertices on graph //For each item: Create IngredientUsage and add to recipe, create IngredientNode (if necessary) and add recipe to IngredientNode foreach (var o in loader.LoadIngredientGraph()) { var rid = o.RecipeId; var ingid = o.IngredientId; var qty = o.Qty; var unit = o.Unit; var convType = Unit.GetConvType(unit); List <RecipeNode>[] nodes; IngredientNode ingNode; var node = snapshot.recipeMap[rid]; if (!snapshot.ingredientMap.TryGetValue(ingid, out ingNode)) //New ingredient, create node for it { nodes = new List <RecipeNode> [RecipeTag.NUM_TAGS]; snapshot.ingredientMap.Add(ingid, ingNode = new IngredientNode() { IngredientId = ingid, RecipesByTag = nodes, ConvType = convType }); } else { nodes = ingNode.RecipesByTag as List <RecipeNode>[]; } //For each tag the recipe has, we need to create a link through ingNode.RecipesByTag to the recipe if (!node.Hidden) //Don't index Hidden recipes { foreach (var tag in node.Tags) { if (nodes[tag.Value] == null) { nodes[tag.Value] = new List <RecipeNode>(); } nodes[tag.Value].Add(node); //Add ingredient link to RecipeNode } } var usages = node.Ingredients as List <IngredientUsage>; //Add ingredient usage to recipe usages.Add(new IngredientUsage() { Amt = qty, Ingredient = ingNode, Unit = unit }); } ModelingSession.Log.InfoFormat("Creating IngredientUsage vertices took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); //Create suggestion links for each recipe foreach (var r in snapshot.recipeMap.Values) { r.Suggestions = (from s in ratingGraph.GetSimilarRecipes(r.RecipeId) select snapshot.recipeMap[s]).ToArray(); } ModelingSession.Log.InfoFormat("Building suggestions for each recipe took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); }
public void Index(IKpcContext context) { var timer = new Stopwatch(); timer.Start(); this.ratingGraph = new RatingGraph(); var loader = context.ModelerLoader; foreach (var dataItem in loader.LoadRatingGraph()) { var rating = dataItem.Rating; var userId = dataItem.UserId; var recipeId = dataItem.RecipeId; if (rating < 4) { continue; // TODO: Might not be needed, DB should only query for 4 or 5 star ratings } this.ratingGraph.AddRating(rating, userId, recipeId); } ModelingSession.Log.InfoFormat("Building Rating Graph took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); // Create empty recipe nodes without links this.snapshot.recipeMap = (from recipeGraph in loader.LoadRecipeGraph() select new RecipeNode { RecipeId = recipeGraph.Id, Rating = recipeGraph.Rating, Tags = recipeGraph.Tags, Hidden = recipeGraph.Hidden, Ingredients = new List<IngredientUsage>() }).ToDictionary(k => k.RecipeId); ModelingSession.Log.InfoFormat("Building empty RecipeNodes took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); // Build tag index foreach (var recipe in this.snapshot.recipeMap.Values) { if (recipe.Hidden) { continue; // RecipeList does not include Hidden recipes so they don't get picked at random } foreach (var tag in recipe.Tags) { var nodes = this.snapshot.recipeList[tag.Value] as List<RecipeNode>; if (nodes == null) { this.snapshot.recipeList[tag.Value] = nodes = new List<RecipeNode>(); } nodes.Add(recipe); } } for (var i = 0; i < this.snapshot.recipeList.Length; i++) { var list = this.snapshot.recipeList[i] as List<RecipeNode>; if (list != null) { this.snapshot.recipeList[i] = list.ToArray(); } else { // No recipes in DB use this tag this.snapshot.recipeList[i] = new RecipeNode[0]; } } ModelingSession.Log.InfoFormat("Indexing recipes by tag took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); // Loop through ingredient usages and fill in vertices on graph // For each item: Create IngredientUsage and add to recipe, create IngredientNode (if necessary) and add recipe to IngredientNode foreach (var ingredientGraph in loader.LoadIngredientGraph()) { var recipeId = ingredientGraph.RecipeId; var ingredientId = ingredientGraph.IngredientId; var quantity = ingredientGraph.Quantity; var unit = ingredientGraph.Unit; var convertedType = Unit.GetConvertedType(unit); List<RecipeNode>[] nodes; IngredientNode ingredientNode; var node = this.snapshot.recipeMap[recipeId]; if (!this.snapshot.ingredientMap.TryGetValue(ingredientId, out ingredientNode)) { // New ingredient, create node for it nodes = new List<RecipeNode>[RecipeTag.NumberOfTags]; this.snapshot.ingredientMap.Add( ingredientId, ingredientNode = new IngredientNode { IngredientId = ingredientId, RecipesByTag = nodes, ConvType = convertedType }); } else { nodes = ingredientNode.RecipesByTag as List<RecipeNode>[]; } // For each tag the recipe has, we need to create a link through ingNode.RecipesByTag to the recipe if (!node.Hidden) { // Don't index Hidden recipes foreach (var tag in node.Tags) { if (nodes[tag.Value] == null) { nodes[tag.Value] = new List<RecipeNode>(); } nodes[tag.Value].Add(node); // Add ingredient link to RecipeNode } } var usages = node.Ingredients as List<IngredientUsage>; // Add ingredient usage to recipe usages.Add(new IngredientUsage { Amount = quantity, Ingredient = ingredientNode, Unit = unit }); } ModelingSession.Log.InfoFormat("Creating IngredientUsage vertices took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); timer.Start(); // Create suggestion links for each recipe foreach (var recipe in this.snapshot.recipeMap.Values) { recipe.Suggestions = (from s in this.ratingGraph.GetSimilarRecipes(recipe.RecipeId) select this.snapshot.recipeMap[s]).ToArray(); } ModelingSession.Log.InfoFormat("Building suggestions for each recipe took {0}ms.", timer.ElapsedMilliseconds); timer.Reset(); }