func storiesByKeys(g *cayley.Handle, keys []string) []*Story { stories := make([]*Story, 0, len(keys)) it, _ := cayley.StartPath(g, keys...).Save(StoryID, StoryID).Save(StoryTitle, StoryTitle).Save(StoryDescription, StoryDescription).Save(SiteID, SiteID).BuildIterator().Optimize() defer it.Close() for cayley.RawNext(it) { s := Story{} results := map[string]graph.Value{} it.TagResults(results) s.Id = atoi(g.NameOf(results[StoryID])) s.Title = g.NameOf(results[StoryTitle]) s.Desc = g.NameOf(results[StoryDescription]) s.Site = Site(Site_value[g.NameOf(results[SiteID])]) s.annotate() stories = append(stories, &s) } return stories }
// viewIDs retrieves the item IDs associated with the view. func viewIDs(v *view.View, path *path.Path, key string, graphDB *cayley.Handle) ([]string, embeddedRels, error) { // Build the Cayley iterator. it := path.BuildIterator() it, _ = it.Optimize() defer it.Close() // tagOrder will allow us to look up the ordering of a tag or // the tag corresponding to an order on demand. tagOrder := make(map[string]string) // Extract any tags and the ordering in the View value. var viewTags []string for idx, pth := range v.Paths { alias := strconv.Itoa(idx+1) + "_" for _, segment := range pth.Segments { if segment.Tag != "" { viewTags = append(viewTags, alias+segment.Tag) tagOrder[alias+segment.Tag] = alias + strconv.Itoa(segment.Level) tagOrder[alias+strconv.Itoa(segment.Level)] = alias + segment.Tag } } } // Retrieve the end path and tagged item IDs. var ids []string var embeds embeddedRels for it.Next() { // Tag the results. resultTags := make(map[string]graph.Value) it.TagResults(resultTags) // Extract the tagged item IDs. taggedIDs := make(map[string]relList) for _, tag := range viewTags { if t, ok := resultTags[tag]; ok { // Append the view item ID. ids = append(ids, quad.NativeOf(graphDB.NameOf(t)).(string)) // Add the tagged ID to the tagged map for embedded // relationship extraction. current, ok := taggedIDs[tag] if !ok { taggedIDs[tag] = []string{quad.NativeOf(graphDB.NameOf(t)).(string)} continue } updated := append(current, quad.NativeOf(graphDB.NameOf(t)).(string)) taggedIDs[tag] = updated } } // Extract any IDs that need to be embedded in view items. embed, err := extractEmbeddedRels(v, taggedIDs, tagOrder, key) if err != nil { return ids, embeds, err } embeds = append(embeds, embed...) } if it.Err() != nil { return ids, embeds, it.Err() } // Remove duplicates. found := make(map[string]bool) j := 0 for i, x := range ids { if !found[x] { found[x] = true ids[j] = ids[i] j++ } } ids = ids[:j] // Add root item. if v.ReturnRoot == true { ids = append(ids, key) } return ids, embeds, nil }