@@ -89,14 +89,26 @@ func GetAllWorkspaceIDs(ctx context.Context) ([]string, error) {
8989}
9090
9191type WorkspaceSnapshot struct {
92+ WorkspaceID string
9293 Path string
9394 Timestamp time.Time
9495 Partition int32
9596 NumPartitions int32
97+ Offset int64
9698}
9799
98100const WORKSPACE_SNAPSHOT_SELECT_QUERY = `
99- SELECT path, timestamp, partition, num_partitions FROM workspace_snapshot WHERE workspace_id = $1 ORDER BY timestamp DESC LIMIT 1
101+ SELECT
102+ workspace_id,
103+ path,
104+ timestamp,
105+ partition,
106+ num_partitions,
107+ "offset"
108+ FROM workspace_snapshot
109+ WHERE workspace_id = $1
110+ ORDER BY "offset" DESC, timestamp DESC
111+ LIMIT 1
100112`
101113
102114func GetWorkspaceSnapshot (ctx context.Context , workspaceID string ) (* WorkspaceSnapshot , error ) {
@@ -108,10 +120,12 @@ func GetWorkspaceSnapshot(ctx context.Context, workspaceID string) (*WorkspaceSn
108120
109121 workspaceSnapshot := & WorkspaceSnapshot {}
110122 err = db .QueryRow (ctx , WORKSPACE_SNAPSHOT_SELECT_QUERY , workspaceID ).Scan (
123+ & workspaceSnapshot .WorkspaceID ,
111124 & workspaceSnapshot .Path ,
112125 & workspaceSnapshot .Timestamp ,
113126 & workspaceSnapshot .Partition ,
114127 & workspaceSnapshot .NumPartitions ,
128+ & workspaceSnapshot .Offset ,
115129 )
116130 if err != nil {
117131 if err == pgx .ErrNoRows {
@@ -122,19 +136,62 @@ func GetWorkspaceSnapshot(ctx context.Context, workspaceID string) (*WorkspaceSn
122136 return workspaceSnapshot , nil
123137}
124138
139+ func GetLatestWorkspaceSnapshots (ctx context.Context , workspaceIDs []string ) (map [string ]* WorkspaceSnapshot , error ) {
140+ if len (workspaceIDs ) == 0 {
141+ return nil , nil
142+ }
143+
144+ db , err := GetDB (ctx )
145+ if err != nil {
146+ return nil , err
147+ }
148+ defer db .Release ()
149+
150+ const query = `
151+ SELECT DISTINCT ON (workspace_id) workspace_id, path, timestamp, partition, num_partitions, "offset"
152+ FROM workspace_snapshot
153+ WHERE workspace_id = ANY($1)
154+ ORDER BY workspace_id, "offset" DESC, timestamp DESC
155+ `
156+ rows , err := db .Query (ctx , query , workspaceIDs )
157+ if err != nil {
158+ return nil , err
159+ }
160+ defer rows .Close ()
161+
162+ var snapshots []* WorkspaceSnapshot
163+ for rows .Next () {
164+ var snapshot WorkspaceSnapshot
165+ err := rows .Scan (& snapshot .WorkspaceID , & snapshot .Path , & snapshot .Timestamp , & snapshot .Partition , & snapshot .NumPartitions , & snapshot .Offset )
166+ if err != nil {
167+ return nil , err
168+ }
169+ snapshots = append (snapshots , & snapshot )
170+ }
171+ if err := rows .Err (); err != nil {
172+ return nil , err
173+ }
174+
175+ snapshotMap := make (map [string ]* WorkspaceSnapshot )
176+ for _ , snapshot := range snapshots {
177+ snapshotMap [snapshot .WorkspaceID ] = snapshot
178+ }
179+ return snapshotMap , nil
180+ }
181+
125182const WORKSPACE_SNAPSHOT_INSERT_QUERY = `
126- INSERT INTO workspace_snapshot (workspace_id, path, timestamp, partition, num_partitions)
127- VALUES ($1, $2, $3, $4, $5)
183+ INSERT INTO workspace_snapshot (workspace_id, path, timestamp, partition, num_partitions, "offset" )
184+ VALUES ($1, $2, $3, $4, $5, $6 )
128185`
129186
130- func WriteWorkspaceSnapshot (ctx context.Context , workspaceID string , snapshot * WorkspaceSnapshot ) error {
187+ func WriteWorkspaceSnapshot (ctx context.Context , snapshot * WorkspaceSnapshot ) error {
131188 db , err := GetDB (ctx )
132189 if err != nil {
133190 return err
134191 }
135192 defer db .Release ()
136193
137- _ , err = db .Exec (ctx , WORKSPACE_SNAPSHOT_INSERT_QUERY , workspaceID , snapshot .Path , snapshot .Timestamp , snapshot .Partition , snapshot .NumPartitions )
194+ _ , err = db .Exec (ctx , WORKSPACE_SNAPSHOT_INSERT_QUERY , snapshot . WorkspaceID , snapshot .Path , snapshot .Timestamp , snapshot .Partition , snapshot .NumPartitions , snapshot . Offset )
138195 if err != nil {
139196 return err
140197 }
0 commit comments