Exemple #1
0
// Given a request, follow the segments through sitetree to find the page that is being requested. Doesn't
// understand actions, so just finds the page. Returns ID of SiteTree_Live record or 0 if it can't find a
// matching page.
// @todo Understand BaseController actions, or break on the furthest it gets up the tree
// @todo cache site tree
func findPageToRender(r *http.Request) (int, error) {
	siteCache := getSiteCache()
	if siteCache != nil {
		id, found := siteCache.findPageToRender(r)
		if found {
			// fmt.Printf("page cache hit %d\n", id)
			return id, nil
		}
	}

	s := strings.Trim(r.URL.Path, "/")
	path := strings.Split(s, "/")

	if len(path) == 0 || path[0] == "" {
		// find a home page ID
		r, e := orm.Query("select \"ID\" from \"SiteTree_Live\" where \"URLSegment\"='home' and \"ParentID\"=0")
		defer r.Close()

		if e != nil {
			return 0, e
		}
		if !r.Next() {
			return 0, nil
		}

		var ID int
		e = r.Scan(&ID)

		return ID, e
	}

	currParentID := 0
	for _, p := range path {
		r, e := orm.Query("select \"ID\",\"ParentID\" from \"SiteTree_Live\" where \"URLSegment\"='" + p + "' and \"ParentID\"=" + strconv.Itoa(currParentID))
		defer r.Close()

		if e != nil {
			return 0, e
		}
		if !r.Next() {
			return 0, nil
		}

		var ID, ParentID int
		e = r.Scan(&ID, &ParentID)
		currParentID = ID
	}

	// if we get to the end, we've found a matching ID in SiteTree_Live
	return currParentID, nil
}
Exemple #2
0
// primeSiteCache is responsible for re-computing the data structures in the cache. It does this
// by requerying the database, rebuilding the structure, and finally replacing the data structures
// atomically. In this way, a request being processed will either get the old version or the new
// version, but whichever version it's using won't be replaced mid-request.
func primeSiteCache() (*SiteCache, error) {
	r, e := orm.Query(`select "ID","ClassName","ParentID","Title","MenuTitle","URLSegment" from "SiteTree_Live"`)
	defer r.Close()

	if e != nil {
		fmt.Printf("ERROR EXECUTING SQL: %s\n", e)
		return nil, e
	}

	newCache := newSiteCache()

	for r.Next() {
		e := newCache.ReadRow(r)
		if e != nil {
			return nil, e
		}
	}

	newCache.derivePaths()

	fmt.Printf("primeSiteCache: %s\n", newCache)

	return newCache, nil
}