Files
pages/internal/store/list_children.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

259 lines
5.5 KiB
Go

package store
import (
"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 string,
parent string,
) ([]string, error) {
parent = strings.TrimRight(parent, "/")
if parent == "/" {
parent = ""
}
cacheKey := domainUUID + "\x00" + parent
s.pathCacheMu.RLock()
cached, ok := s.childCache[cacheKey]
s.pathCacheMu.RUnlock()
if ok {
return append([]string(nil), cached...), nil
}
rows, err := s.DB.Query(`
SELECT path
FROM paths
WHERE domain_uuid = ?
ORDER BY path ASC
`, domainUUID)
if err != nil {
return nil, err
}
defer rows.Close()
var out []string
seen := map[string]bool{}
for rows.Next() {
var path string
err := rows.Scan(
&path,
)
if err != nil {
return nil, err
}
if parent != "" {
if !strings.HasPrefix(
path,
parent+"/",
) {
continue
}
} else {
if !strings.HasPrefix(
path,
"/",
) {
continue
}
}
trimmed :=
strings.TrimPrefix(
path,
parent,
)
trimmed =
strings.TrimPrefix(
trimmed,
"/",
)
if trimmed == "" {
continue
}
if slash := strings.Index(trimmed, "/"); slash >= 0 {
trimmed = trimmed[:slash]
}
child := parent + "/" + trimmed
if !seen[child] {
seen[child] = true
out = append(out, child)
}
}
s.pathCacheMu.Lock()
s.childCache[cacheKey] = append([]string(nil), out...)
s.pathCacheMu.Unlock()
return out, nil
}
func (s *Store) InvalidatePathCaches() {
s.pathCacheMu.Lock()
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("(?,?)")
args = append(args, child, index)
}
args = append(args, domainUUID)
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 = ?
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, r.rowid 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())
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
}
return entries, total, nil
}