Conditions | 12 |
Total Lines | 129 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like postgres.*Watch.getChanges often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package postgres |
||
229 | func (w *Watch) getChanges(ctx context.Context, value types.XID8, tenantID string) (*base.DataChanges, error) { |
||
230 | // Initialize a new TupleChanges instance. |
||
231 | changes := &base.DataChanges{} |
||
232 | |||
233 | slog.DebugContext(ctx, "retrieving changes for transaction", slog.Any("id", value), slog.Any("tenant_id", tenantID)) |
||
234 | |||
235 | // Construct the SQL SELECT statement for retrieving the changes from the RelationTuplesTable. |
||
236 | tbuilder := w.database.Builder.Select("entity_type, entity_id, relation, subject_type, subject_id, subject_relation, expired_tx_id"). |
||
237 | From(RelationTuplesTable). |
||
238 | Where(squirrel.Eq{"tenant_id": tenantID}).Where(squirrel.Or{ |
||
239 | squirrel.Eq{"created_tx_id": value}, |
||
240 | squirrel.Eq{"expired_tx_id": value}, |
||
241 | }) |
||
242 | |||
243 | // Generate the SQL query and arguments. |
||
244 | tquery, targs, err := tbuilder.ToSql() |
||
245 | if err != nil { |
||
246 | slog.ErrorContext(ctx, "error while building sql query for relation tuples", slog.Any("error", err)) |
||
247 | return nil, err |
||
248 | } |
||
249 | |||
250 | slog.DebugContext(ctx, "executing sql query for relation tuples", slog.Any("query", tquery), slog.Any("arguments", targs)) |
||
251 | |||
252 | // Execute the SQL query and retrieve the result rows. |
||
253 | var trows pgx.Rows |
||
254 | trows, err = w.database.ReadPool.Query(ctx, tquery, targs...) |
||
255 | if err != nil { |
||
256 | slog.ErrorContext(ctx, "failed to execute sql query for relation tuples", slog.Any("error", err)) |
||
257 | return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
||
258 | } |
||
259 | // Ensure the rows are closed after processing. |
||
260 | defer trows.Close() |
||
261 | |||
262 | abuilder := w.database.Builder.Select("entity_type, entity_id, attribute, value, expired_tx_id"). |
||
263 | From(AttributesTable). |
||
264 | Where(squirrel.Eq{"tenant_id": tenantID}).Where(squirrel.Or{ |
||
265 | squirrel.Eq{"created_tx_id": value}, |
||
266 | squirrel.Eq{"expired_tx_id": value}, |
||
267 | }) |
||
268 | |||
269 | aquery, aargs, err := abuilder.ToSql() |
||
270 | if err != nil { |
||
271 | slog.ErrorContext(ctx, "error while building SQL query for attributes", slog.Any("error", err)) |
||
272 | return nil, err |
||
273 | } |
||
274 | |||
275 | slog.DebugContext(ctx, "executing sql query for attributes", slog.Any("query", aquery), slog.Any("arguments", aargs)) |
||
276 | |||
277 | var arows pgx.Rows |
||
278 | arows, err = w.database.ReadPool.Query(ctx, aquery, aargs...) |
||
279 | if err != nil { |
||
280 | slog.ErrorContext(ctx, "error while executing SQL query for attributes", slog.Any("error", err)) |
||
281 | return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
||
282 | } |
||
283 | // Ensure the rows are closed after processing. |
||
284 | defer arows.Close() |
||
285 | |||
286 | // Set the snapshot token for the changes. |
||
287 | changes.SnapToken = snapshot.Token{Value: value}.Encode().String() |
||
288 | |||
289 | // Iterate through the result rows. |
||
290 | for trows.Next() { |
||
291 | var expiredXID types.XID8 |
||
292 | |||
293 | rt := storage.RelationTuple{} |
||
294 | // Scan the result row into a RelationTuple instance. |
||
295 | err = trows.Scan(&rt.EntityType, &rt.EntityID, &rt.Relation, &rt.SubjectType, &rt.SubjectID, &rt.SubjectRelation, &expiredXID) |
||
296 | if err != nil { |
||
297 | slog.ErrorContext(ctx, "error while scanning row for relation tuples", slog.Any("error", err)) |
||
298 | return nil, err |
||
299 | } |
||
300 | |||
301 | // Determine the operation type based on the expired transaction ID. |
||
302 | op := base.DataChange_OPERATION_CREATE |
||
303 | if expiredXID.Uint == value.Uint { |
||
304 | op = base.DataChange_OPERATION_DELETE |
||
305 | } |
||
306 | |||
307 | // Append the change to the list of changes. |
||
308 | changes.DataChanges = append(changes.DataChanges, &base.DataChange{ |
||
309 | Operation: op, |
||
310 | Type: &base.DataChange_Tuple{ |
||
311 | Tuple: rt.ToTuple(), |
||
312 | }, |
||
313 | }) |
||
314 | } |
||
315 | |||
316 | // Iterate through the result rows. |
||
317 | for arows.Next() { |
||
318 | var expiredXID types.XID8 |
||
319 | |||
320 | rt := storage.Attribute{} |
||
321 | |||
322 | var valueStr string |
||
323 | |||
324 | // Scan the result row into a RelationTuple instance. |
||
325 | err = arows.Scan(&rt.EntityType, &rt.EntityID, &rt.Attribute, &valueStr, &expiredXID) |
||
326 | if err != nil { |
||
327 | slog.ErrorContext(ctx, "error while scanning row for attributes", slog.Any("error", err)) |
||
328 | return nil, err |
||
329 | } |
||
330 | |||
331 | // Unmarshal the JSON data from `valueStr` into `rt.Value`. |
||
332 | rt.Value = &anypb.Any{} |
||
333 | err = protojson.Unmarshal([]byte(valueStr), rt.Value) |
||
334 | if err != nil { |
||
335 | slog.ErrorContext(ctx, "failed to unmarshal attribute value", slog.Any("error", err)) |
||
336 | return nil, err |
||
337 | } |
||
338 | |||
339 | // Determine the operation type based on the expired transaction ID. |
||
340 | op := base.DataChange_OPERATION_CREATE |
||
341 | if expiredXID.Uint == value.Uint { |
||
342 | op = base.DataChange_OPERATION_DELETE |
||
343 | } |
||
344 | |||
345 | // Append the change to the list of changes. |
||
346 | changes.DataChanges = append(changes.DataChanges, &base.DataChange{ |
||
347 | Operation: op, |
||
348 | Type: &base.DataChange_Attribute{ |
||
349 | Attribute: rt.ToAttribute(), |
||
350 | }, |
||
351 | }) |
||
352 | } |
||
353 | |||
354 | slog.DebugContext(ctx, "successfully retrieved changes for transaction", slog.Any("id", value)) |
||
355 | |||
356 | // Return the changes and no error. |
||
357 | return changes, nil |
||
358 | } |
||
359 |