232 lines
6.3 KiB
Go
232 lines
6.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func (s *Store) ListChildPaths(
|
|
parent string,
|
|
) ([]string, error) {
|
|
domainUUID, err := s.defaultDomainUUID()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.ListChildPathsInDomain(domainUUID, parent)
|
|
}
|
|
|
|
func (s *Store) ListChildPathsInDomain(domainUUID, parent string) ([]string, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), cityReadTimeout)
|
|
defer cancel()
|
|
parent = strings.TrimRight(normalizeBrowserPath(parent), "/")
|
|
generation := int64(0)
|
|
if s.sqlBackend == storePostgreSQL {
|
|
var err error
|
|
generation, err = s.NavigationGeneration(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
cacheKey := domainUUID + "\x00" + parent
|
|
s.pathCacheMu.Lock()
|
|
if s.childCache == nil || s.childCacheGeneration != generation {
|
|
s.childCache = map[string][]string{}
|
|
s.childCacheGeneration = generation
|
|
}
|
|
cached, ok := s.childCache[cacheKey]
|
|
epoch := s.pathCacheEpoch
|
|
s.pathCacheMu.Unlock()
|
|
if ok {
|
|
return append([]string(nil), cached...), nil
|
|
}
|
|
prefix := parent + "/"
|
|
pos := s.sqlStringPosition("rest", "/")
|
|
rows, err := s.DB.QueryContext(ctx, `WITH relative AS (
|
|
SELECT substr(path,length(`+s.sqlParam(1)+`)+1) AS rest FROM paths
|
|
WHERE domain_uuid=`+s.sqlParam(2)+` AND path>=`+s.sqlParam(3)+` AND path<`+s.sqlParam(4)+`
|
|
) SELECT DISTINCT CASE WHEN `+pos+`=0 THEN rest ELSE substr(rest,1,`+pos+`-1) END AS name
|
|
FROM relative WHERE rest!='' ORDER BY name LIMIT `+s.sqlParam(5), prefix, domainUUID, prefix, parent+"0", MaxNavigationEntries+1)
|
|
if err != nil {
|
|
return nil, s.sqlError("child paths", err)
|
|
}
|
|
defer rows.Close()
|
|
var out []string
|
|
for rows.Next() {
|
|
var name string
|
|
if err := rows.Scan(&name); err != nil {
|
|
return nil, s.sqlError("child paths", err)
|
|
}
|
|
out = append(out, prefix+name)
|
|
if len(out) > MaxNavigationEntries {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, s.sqlError("child paths", err)
|
|
}
|
|
s.pathCacheMu.Lock()
|
|
if s.childCacheGeneration == generation && s.pathCacheEpoch == epoch {
|
|
s.childCache[cacheKey] = append([]string(nil), out...)
|
|
}
|
|
s.pathCacheMu.Unlock()
|
|
return out, nil
|
|
}
|
|
|
|
func (s *Store) InvalidatePathCaches() {
|
|
s.pathCacheMu.Lock()
|
|
s.pathCacheEpoch++
|
|
s.childCache = map[string][]string{}
|
|
s.pathCacheMu.Unlock()
|
|
}
|
|
|
|
// ListChildEntriesPageInDomain uses the cached direct-child index first, then
|
|
// fetches metadata only for the requested page. Virtual folders therefore do
|
|
// not require a Revision scan, and very large directories remain bounded.
|
|
func (s *Store) ListChildEntriesPageInDomain(
|
|
domainUUID, parent string,
|
|
offset, limit int,
|
|
) ([]BrowserEntry, int, error) {
|
|
children, err := s.ListChildPathsInDomain(domainUUID, parent)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
total := len(children)
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
if limit <= 0 || limit > 400 {
|
|
limit = 120
|
|
}
|
|
if offset >= total {
|
|
return []BrowserEntry{}, total, nil
|
|
}
|
|
end := offset + limit
|
|
if end > total {
|
|
end = total
|
|
}
|
|
|
|
page := children[offset:end]
|
|
entries, _, err := s.ListSelectedChildEntriesInDomain(domainUUID, parent, page)
|
|
return entries, total, err
|
|
}
|
|
|
|
// The caller supplies a permission-filtered page, so hidden entries do not
|
|
// consume pagination slots and private virtual-parent metadata is never read.
|
|
func (s *Store) ListSelectedChildEntriesInDomain(domainUUID, parent string, page []string) ([]BrowserEntry, int, error) {
|
|
total := len(page)
|
|
if total == 0 {
|
|
return []BrowserEntry{}, 0, nil
|
|
}
|
|
if total > 400 {
|
|
return nil, 0, ErrNavigationBudget
|
|
}
|
|
var values strings.Builder
|
|
args := make([]any, 0, len(page)*2+1)
|
|
for index, child := range page {
|
|
if index > 0 {
|
|
values.WriteByte(',')
|
|
}
|
|
values.WriteString("(CAST(" + s.sqlParam(len(args)+1) + " AS TEXT),CAST(" + s.sqlParam(len(args)+2) + " AS INTEGER))")
|
|
args = append(args, child, index)
|
|
}
|
|
args = append(args, domainUUID)
|
|
tie := "r.rowid"
|
|
if s.sqlBackend == storePostgreSQL {
|
|
tie = "r.uuid"
|
|
}
|
|
query := fmt.Sprintf(`
|
|
WITH requested(path, ord) AS (VALUES %s),
|
|
resolved AS (
|
|
SELECT
|
|
requested.path,
|
|
requested.ord,
|
|
COALESCE(paths.object_uuid, '') AS object_uuid
|
|
FROM requested
|
|
LEFT JOIN paths
|
|
ON paths.domain_uuid = %s
|
|
AND paths.path = requested.path
|
|
),
|
|
latest AS (
|
|
SELECT
|
|
r.object_uuid,
|
|
r.uuid AS revision_uuid,
|
|
COALESCE(r.document_hash, '') AS document_hash,
|
|
COALESCE(r.created_at, 0) AS updated_at,
|
|
COALESCE(NULLIF(r.content_type, ''), 'application/octet-stream') AS content_type,
|
|
COALESCE(r.filename, '') AS filename,
|
|
COALESCE(substr(CAST(r.body AS TEXT), 1, 65536), '') AS source_prefix,
|
|
ROW_NUMBER() OVER (
|
|
PARTITION BY r.object_uuid
|
|
ORDER BY COALESCE(r.created_at_ms, r.created_at * 1000) DESC, %s DESC
|
|
) AS rn
|
|
FROM revisions r
|
|
JOIN resolved
|
|
ON resolved.object_uuid = r.object_uuid
|
|
)
|
|
SELECT
|
|
resolved.path,
|
|
resolved.object_uuid,
|
|
COALESCE(latest.revision_uuid, ''),
|
|
COALESCE(latest.document_hash, ''),
|
|
COALESCE(latest.updated_at, 0),
|
|
COALESCE(latest.content_type, ''),
|
|
COALESCE(latest.filename, ''),
|
|
COALESCE(latest.source_prefix, '')
|
|
FROM resolved
|
|
LEFT JOIN latest
|
|
ON latest.object_uuid = resolved.object_uuid
|
|
AND latest.rn = 1
|
|
ORDER BY resolved.ord
|
|
`, values.String(), s.sqlParam(len(args)), tie)
|
|
rows, err := s.DB.Query(query, args...)
|
|
if err != nil {
|
|
return nil, total, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
entries := make([]BrowserEntry, 0, len(page))
|
|
for rows.Next() {
|
|
var entry BrowserEntry
|
|
var documentHash string
|
|
var sourcePrefix string
|
|
if err := rows.Scan(
|
|
&entry.Path,
|
|
&entry.ObjectUUID,
|
|
&entry.RevisionUUID,
|
|
&documentHash,
|
|
&entry.UpdatedAt,
|
|
&entry.ContentType,
|
|
&entry.Filename,
|
|
&sourcePrefix,
|
|
); err != nil {
|
|
return nil, total, err
|
|
}
|
|
entry.Name = strings.TrimPrefix(
|
|
strings.TrimPrefix(entry.Path, strings.TrimRight(parent, "/")),
|
|
"/",
|
|
)
|
|
entry.IsDir = entry.ObjectUUID == ""
|
|
entry.DocumentHash = documentHash
|
|
if !entry.IsDir {
|
|
entry.Title = firstHTMLHeading(sourcePrefix)
|
|
}
|
|
if documentHash != "" && s.DocumentRoot != "" {
|
|
if size, err := s.DocumentSize(documentHash); err == nil {
|
|
entry.Size = int(size)
|
|
}
|
|
}
|
|
entries = append(entries, entry)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, total, err
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, total, err
|
|
}
|
|
if err := s.fillNativeBrowserTitles(entries); err != nil {
|
|
return nil, total, err
|
|
}
|
|
return entries, total, nil
|
|
}
|