// Views returns the slice within the viewlists
778834
func (vm *ViewLists) Views() ViewList {
	vm.ro.RLock()
	defer vm.ro.RUnlock()
	return vm.lists
}
1204440
func (r *View) Index(needle []byte) int64 {
	if r.Remaining() == 0 {
		return -1
	}
	if len(needle) == 0 {
		return 0
	}

	rc := r.Clone()
	if !rc.indexDestructive(needle) {
		return -1
	}
	return rc.consumed - r.consumed
}
889431
func (v *View) ViewBufferLines() []string {
	lines := make([]string, len(v.viewLines))
	for i, l := range v.viewLines {
		str := lineType(l.line).String()
		str = strings.Replace(str, "\x00", " ", -1)
		lines[i] = str
	}
	return lines
}
1087908
func (vs Values) Strings() []string {
	ss := make([]string, len(vs))
	for i, v := range vs {
		ss[i] = v.String()
	}
	return ss
}
1054221
func (d *database) View(ctx context.Context, name string) (View, error) {
	escapedName := pathEscape(name)
	req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/view", escapedName))
	if err != nil {
		return nil, WithStack(err)
	}
	applyContextSettings(ctx, req)
	resp, err := d.conn.Do(ctx, req)
	if err != nil {
		return nil, WithStack(err)
	}
	if err := resp.CheckStatus(200); err != nil {
		return nil, WithStack(err)
	}
	var data viewInfo
	if err := resp.ParseBody("", &data); err != nil {
		return nil, WithStack(err)
	}
	view, err := newView(name, data.Type, d)
	if err != nil {
		return nil, WithStack(err)
	}
	return view, nil
}
908190
func (m *MediaTypeDefinition) IterateViews(it ViewIterator) error {
	o := m.Views
	// gather names and sort them
	names := make([]string, len(o))
	i := 0
	for n := range o {
		names[i] = n
		i++
	}
	sort.Strings(names)
	// iterate
	for _, n := range names {
		if err := it(o[n]); err != nil {
			return err
		}
	}
	return nil
}
476691
func (v *view) row(rowID uint64) *Row {
	row := NewRow()
	for _, frag := range v.allFragments() {
		fr := frag.row(rowID)
		if fr == nil {
			continue
		}
		row.Merge(fr)
	}
	return row

}
472736
func (b *Bucket) ViewURL(ddoc, name string,
	params map[string]interface{}) (string, error) {
	u, err := b.randomBaseURL()
	if err != nil {
		return "", err
	}

	values := url.Values{}
	for k, v := range params {
		switch t := v.(type) {
		case DocID:
			values[k] = []string{string(t)}
		case string:
			values[k] = []string{qParam(k, t)}
		case int:
			values[k] = []string{fmt.Sprintf(`%d`, t)}
		case bool:
			values[k] = []string{fmt.Sprintf(`%v`, t)}
		default:
			b, err := json.Marshal(v)
			if err != nil {
				return "", fmt.Errorf("unsupported value-type %T in Query, "+
					"json encoder said %v", t, err)
			}
			values[k] = []string{fmt.Sprintf(`%v`, string(b))}
		}
	}

	if ddoc == "" && name == "_all_docs" {
		u.Path = fmt.Sprintf("/%s/_all_docs", b.GetName())
	} else {
		u.Path = fmt.Sprintf("/%s/_design/%s/_view/%s", b.GetName(), ddoc, name)
	}
	u.RawQuery = values.Encode()

	return u.String(), nil
}
448672
func (f *Field) openViews() error {
	file, err := os.Open(filepath.Join(f.path, "views"))
	if os.IsNotExist(err) {
		return nil
	} else if err != nil {
		return errors.Wrap(err, "opening view directory")
	}
	defer file.Close()

	fis, err := file.Readdir(0)
	if err != nil {
		return errors.Wrap(err, "reading directory")
	}

	for _, fi := range fis {
		if !fi.IsDir() {
			continue
		}

		name := filepath.Base(fi.Name())
		f.logger.Debugf("open index/field/view: %s/%s/%s", f.index, f.name, fi.Name())
		view := f.newView(f.viewPath(name), name)
		if err := view.open(); err != nil {
			return fmt.Errorf("opening view: view=%s, err=%s", view.name, err)
		}
		view.rowAttrStore = f.rowAttrStore
		f.logger.Debugf("add index/field/view to field.viewMap: %s/%s/%s", f.index, f.name, view.name)
		f.viewMap[view.name] = view
	}

	return nil
}
11724
func (v *view) open() error {

	// Never keep a cache for field views.
	if strings.HasPrefix(v.name, viewBSIGroupPrefix) {
		v.cacheType = CacheTypeNone
	}

	if err := func() error {
		// Ensure the view's path exists.
		v.logger.Debugf("ensure view path exists: %s", v.path)
		if err := os.MkdirAll(v.path, 0777); err != nil {
			return errors.Wrap(err, "creating view directory")
		} else if err := os.MkdirAll(filepath.Join(v.path, "fragments"), 0777); err != nil {
			return errors.Wrap(err, "creating fragments directory")
		}

		v.logger.Debugf("open fragments for index/field/view: %s/%s/%s", v.index, v.field, v.name)
		if err := v.openFragments(); err != nil {
			return errors.Wrap(err, "opening fragments")
		}

		return nil
	}(); err != nil {
		v.close()
		return err
	}

	v.logger.Debugf("successfully opened index/field/view: %s/%s/%s", v.index, v.field, v.name)
	return nil
}
1145924
func (c *Client) FindViews(ctx context.Context, filter platform.ViewFilter) ([]*platform.View, int, error) {
	ds := []*platform.View{}
	op := getOp(platform.OpFindViews)
	if filter.ID != nil {
		d, err := c.FindViewByID(ctx, *filter.ID)
		if err != nil && platform.ErrorCode(err) != platform.ENotFound {
			return nil, 0, &platform.Error{
				Err: err,
				Op:  op,
			}
		}
		if d != nil {
			ds = append(ds, d)
		}

		return ds, 1, nil
	}

	err := c.db.View(func(tx *bolt.Tx) error {
		dashs, err := c.findViews(ctx, tx, filter)
		if err != nil {
			return &platform.Error{
				Err: err,
				Op:  op,
			}
		}
		ds = dashs
		return nil
	})

	return ds, len(ds), err
}
283413
func NewView() *View {
	view := new(View)
	view.templateLoader = make(map[string]*TemplateManager)
	view.FuncMap = make(template.FuncMap)
	view.FuncMap["Html"] = func(s string) template.HTML { return template.HTML(s) }
	return view
}
203463
func LoadViews(p string) *template.Template {

	var viewpath string
	if filepath.IsAbs(p) {
		viewpath = p
	} else {
		_, f, _, _ := runtime.Caller(1)
		viewpath = path.Join(path.Dir(f), p) + "/"
	}

	exp := regexp.MustCompile("[^/]+\\.html$")
	pool := template.New("")

	filepath.Walk(viewpath, func(fullpath string, info os.FileInfo, err error) error {
		if exp.MatchString(fullpath) {
			name := strings.Replace(strings.Replace(fullpath, viewpath, "", -1), filepath.Ext(fullpath), "", -1)
			name = strings.Trim(name, "/")
			tmp, err := template.ParseFiles(fullpath)
			if err != nil {
				panic(err)
			}
			if _, err = pool.AddParseTree(name, tmp.Tree); err != nil {
				panic(err)
			}
		}
		return nil
	})

	templates = pool

	return templates
}
1122678
func (h *ViewHandler) handleGetViews(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	req := decodeGetViewsRequest(ctx, r)

	views, _, err := h.ViewService.FindViews(ctx, req.filter)
	if err != nil {
		EncodeError(ctx, err, w)
		return
	}

	if err := encodeResponse(ctx, w, http.StatusOK, newGetViewsResponse(views)); err != nil {
		logEncodingError(h.Logger, r, err)
		return
	}
}
488747
func (c *Cellar) View(fn func(*Tx) error) error {
	t, err := c.Begin(false)
	if err != nil {
		return err
	}

	// Make sure the transaction rolls back in the event of a panic.
	defer func() {
		if t.cellar != nil {
			_ = t.rollback()
		}
	}()

	// Mark as a managed tx so that the inner function cannot manually rollback.
	t.managed = true

	// If an error is returned from the function then pass it through.
	err = fn(t)
	t.managed = false
	if err != nil {
		_ = t.Rollback()
		return err
	}

	if err := t.Rollback(); err != nil {
		return err
	}

	return nil
}
382903
func viewsByTime(name string, t time.Time, q TimeQuantum) []string { // nolint: unparam
	a := make([]string, 0, len(q))
	for _, unit := range q {
		view := viewByTimeUnit(name, t, unit)
		if view == "" {
			continue
		}
		a = append(a, view)
	}
	return a
}
1270306
func ReadTemplates(app *App) (*template.Template, error) {
	rv, err := lookForTemplates(app.Config.TemplatesDir)
	if err != nil {
		return nil, err
	}

	if len(rv) == 0 {
		return nil, fmt.Errorf("no templates available in %s", app.Config.TemplatesDir)
	}

	// assign custom functions and parse templates.
	t, err := template.New("").Funcs(TemplateHelpers()).ParseFiles(rv...)
	if err != nil {
		return nil, err
	}

	// attach method helpers
	return t, nil
}