Conditions | 12 |
Total Lines | 130 |
Code Lines | 73 |
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 |
||
230 | func (w *Watch) getChanges(ctx context.Context, value types.XID8, tenantID string) (*base.DataChanges, error) { |
||
231 | // Initialize a new TupleChanges instance. |
||
232 | changes := &base.DataChanges{} |
||
233 | |||
234 | slog.DebugContext(ctx, "retrieving changes for transaction", slog.Any("id", value), slog.Any("tenant_id", tenantID)) |
||
235 | |||
236 | // Construct the SQL SELECT statement for retrieving the changes from the RelationTuplesTable. |
||
237 | tbuilder := w.database.Builder.Select("entity_type, entity_id, relation, subject_type, subject_id, subject_relation, expired_tx_id"). |
||
238 | From(RelationTuplesTable). |
||
239 | Where(squirrel.Eq{"tenant_id": tenantID}).Where(squirrel.Or{ |
||
240 | squirrel.Eq{"created_tx_id": value}, |
||
241 | squirrel.Eq{"expired_tx_id": value}, |
||
242 | }) |
||
243 | |||
244 | // Generate the SQL query and arguments. |
||
245 | tquery, targs, err := tbuilder.ToSql() |
||
246 | if err != nil { |
||
247 | slog.ErrorContext(ctx, "error while building sql query for relation tuples", slog.Any("error", err)) |
||
248 | return nil, err |
||
249 | } |
||
250 | |||
251 | slog.DebugContext(ctx, "executing sql query for relation tuples", slog.Any("query", tquery), slog.Any("arguments", targs)) |
||
252 | |||
253 | // Execute the SQL query and retrieve the result rows. |
||
254 | var trows pgx.Rows |
||
255 | trows, err = w.database.ReadPool.Query(ctx, tquery, targs...) |
||
256 | if err != nil { |
||
257 | slog.ErrorContext(ctx, "failed to execute sql query for relation tuples", slog.Any("error", err)) |
||
258 | return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
||
259 | } |
||
260 | // Ensure the rows are closed after processing. |
||
261 | defer trows.Close() |
||
262 | |||
263 | abuilder := w.database.Builder.Select("entity_type, entity_id, attribute, value, expired_tx_id"). |
||
264 | From(AttributesTable). |
||
265 | Where(squirrel.Eq{"tenant_id": tenantID}).Where(squirrel.Or{ |
||
266 | squirrel.Eq{"created_tx_id": value}, |
||
267 | squirrel.Eq{"expired_tx_id": value}, |
||
268 | }) |
||
269 | |||
270 | aquery, aargs, err := abuilder.ToSql() |
||
271 | if err != nil { |
||
272 | slog.ErrorContext(ctx, "error while building SQL query for attributes", slog.Any("error", err)) |
||
273 | return nil, err |
||
274 | } |
||
275 | |||
276 | slog.DebugContext(ctx, "executing sql query for attributes", slog.Any("query", aquery), slog.Any("arguments", aargs)) |
||
277 | |||
278 | var arows pgx.Rows |
||
279 | arows, err = w.database.ReadPool.Query(ctx, aquery, aargs...) |
||
280 | if err != nil { |
||
281 | slog.ErrorContext(ctx, "error while executing SQL query for attributes", slog.Any("error", err)) |
||
282 | return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
||
283 | } |
||
284 | // Ensure the rows are closed after processing. |
||
285 | defer arows.Close() |
||
286 | |||
287 | // Set the snapshot token for the changes. |
||
288 | changes.SnapToken = snapshot.Token{Value: value}.Encode().String() |
||
289 | |||
290 | // Iterate through the result rows. |
||
291 | for trows.Next() { |
||
292 | var expiredXID types.XID8 |
||
293 | |||
294 | rt := storage.RelationTuple{} |
||
295 | // Scan the result row into a RelationTuple instance. |
||
296 | err = trows.Scan(&rt.EntityType, &rt.EntityID, &rt.Relation, &rt.SubjectType, &rt.SubjectID, &rt.SubjectRelation, &expiredXID) |
||
297 | if err != nil { |
||
298 | slog.ErrorContext(ctx, "error while scanning row for relation tuples", slog.Any("error", err)) |
||
299 | return nil, err |
||
300 | } |
||
301 | |||
302 | // Determine the operation type based on the expired transaction ID. |
||
303 | op := base.DataChange_OPERATION_CREATE |
||
304 | if expiredXID.Uint == value.Uint { |
||
305 | op = base.DataChange_OPERATION_DELETE |
||
306 | } |
||
307 | |||
308 | // Append the change to the list of changes. |
||
309 | changes.DataChanges = append(changes.DataChanges, &base.DataChange{ |
||
310 | Operation: op, |
||
311 | Type: &base.DataChange_Tuple{ |
||
312 | Tuple: rt.ToTuple(), |
||
313 | }, |
||
314 | }) |
||
315 | } |
||
316 | |||
317 | // Iterate through the result rows. |
||
318 | for arows.Next() { |
||
319 | var expiredXID types.XID8 |
||
320 | |||
321 | rt := storage.Attribute{} |
||
322 | |||
323 | var valueStr string |
||
324 | |||
325 | // Scan the result row into a RelationTuple instance. |
||
326 | err = arows.Scan(&rt.EntityType, &rt.EntityID, &rt.Attribute, &valueStr, &expiredXID) |
||
327 | if err != nil { |
||
328 | slog.ErrorContext(ctx, "error while scanning row for attributes", slog.Any("error", err)) |
||
329 | return nil, err |
||
330 | } |
||
331 | |||
332 | // Unmarshal the JSON data from `valueStr` into `rt.Value`. |
||
333 | rt.Value = &anypb.Any{} |
||
334 | unmarshaler := &jsonpb.Unmarshaler{} |
||
335 | err = unmarshaler.Unmarshal(strings.NewReader(valueStr), rt.Value) |
||
336 | if err != nil { |
||
337 | slog.ErrorContext(ctx, "failed to unmarshal attribute value", slog.Any("error", err)) |
||
338 | return nil, err |
||
339 | } |
||
340 | |||
341 | // Determine the operation type based on the expired transaction ID. |
||
342 | op := base.DataChange_OPERATION_CREATE |
||
343 | if expiredXID.Uint == value.Uint { |
||
344 | op = base.DataChange_OPERATION_DELETE |
||
345 | } |
||
346 | |||
347 | // Append the change to the list of changes. |
||
348 | changes.DataChanges = append(changes.DataChanges, &base.DataChange{ |
||
349 | Operation: op, |
||
350 | Type: &base.DataChange_Attribute{ |
||
351 | Attribute: rt.ToAttribute(), |
||
352 | }, |
||
353 | }) |
||
354 | } |
||
355 | |||
356 | slog.DebugContext(ctx, "successfully retrieved changes for transaction", slog.Any("id", value)) |
||
357 | |||
358 | // Return the changes and no error. |
||
359 | return changes, nil |
||
360 | } |
||
361 |