1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5package db
6
7import "strings"
8
9const (
10 prefixMeta = "meta"
11 prefixDir = "dir"
12 prefixIdx = "idx"
13 prefixSession = "s"
14 suffixActive = "active"
15 suffixArchived = "archived"
16 suffixGoal = "goal"
17 suffixMeta = "meta"
18 suffixTask = "task"
19 suffixStatusIdx = "idx"
20 suffixStatus = "status"
21 suffixEvents = "evt"
22 suffixEvtSeq = "evt_seq"
23)
24
25// KeySchemaVersion returns the key for the schema version.
26func KeySchemaVersion() []byte {
27 return []byte(join(prefixMeta, "schema_version"))
28}
29
30// KeyDirActive resolves the key storing the active session ID for a directory.
31func KeyDirActive(dirHash string) []byte {
32 return []byte(join(prefixDir, dirHash, suffixActive))
33}
34
35// KeyDirArchived emits the per-directory archived index key.
36func KeyDirArchived(dirHash, tsHex, sid string) []byte {
37 return []byte(join(prefixDir, dirHash, suffixArchived, tsHex, sid))
38}
39
40// KeyIdxActive returns the index key for active sessions.
41func KeyIdxActive(sid string) []byte {
42 return []byte(join(prefixIdx, suffixActive, sid))
43}
44
45// KeyIdxArchived returns the global archive index key.
46func KeyIdxArchived(tsHex, sid string) []byte {
47 return []byte(join(prefixIdx, suffixArchived, tsHex, sid))
48}
49
50// KeySessionMeta returns the session metadata document key.
51func KeySessionMeta(sid string) []byte {
52 return []byte(join(prefixSession, sid, suffixMeta))
53}
54
55// KeySessionGoal returns the session goal document key.
56func KeySessionGoal(sid string) []byte {
57 return []byte(join(prefixSession, sid, suffixGoal))
58}
59
60// KeySessionTask returns the task document key.
61func KeySessionTask(sid, taskID string) []byte {
62 return []byte(join(prefixSession, sid, suffixTask, taskID))
63}
64
65// KeySessionTaskStatusIndex returns the membership key for the status index.
66func KeySessionTaskStatusIndex(sid, status, taskID string) []byte {
67 return []byte(join(prefixSession, sid, suffixStatusIdx, suffixStatus, status, taskID))
68}
69
70// KeySessionEventSeq returns the counter key for event sequences.
71func KeySessionEventSeq(sid string) []byte {
72 return []byte(join(prefixSession, sid, suffixMeta, suffixEvtSeq))
73}
74
75// KeySessionEvent returns the key for a specific event sequence.
76func KeySessionEvent(sid string, seq uint64) []byte {
77 return []byte(join(prefixSession, sid, suffixEvents, Uint64Hex(seq)))
78}
79
80// PrefixSessionTasks returns the key prefix covering all tasks for sid.
81func PrefixSessionTasks(sid string) []byte {
82 return []byte(join(prefixSession, sid, suffixTask))
83}
84
85// PrefixSessionStatusIndex returns the prefix for tasks with the given status.
86func PrefixSessionStatusIndex(sid, status string) []byte {
87 return []byte(join(prefixSession, sid, suffixStatusIdx, suffixStatus, status))
88}
89
90// PrefixSessionEvents returns the prefix for all events in a session.
91func PrefixSessionEvents(sid string) []byte {
92 return []byte(join(prefixSession, sid, suffixEvents))
93}
94
95// PrefixDirArchived returns the prefix for archived sessions belonging to dir.
96func PrefixDirArchived(dirHash string) []byte {
97 return []byte(join(prefixDir, dirHash, suffixArchived))
98}
99
100// PrefixIdxActive returns the prefix for scanning active sessions.
101func PrefixIdxActive() []byte {
102 return []byte(join(prefixIdx, suffixActive))
103}
104
105// PrefixIdxArchived returns the prefix for scanning the archive index.
106func PrefixIdxArchived() []byte {
107 return []byte(join(prefixIdx, suffixArchived))
108}
109
110func join(parts ...string) string {
111 var b strings.Builder
112 for _, part := range parts {
113 if part == "" {
114 continue
115 }
116 if b.Len() > 0 {
117 b.WriteByte('/')
118 }
119 b.WriteString(part)
120 }
121 return b.String()
122}