MCPcopy Create free account
hub / github.com/astercloud/aster / Prune

Method Prune

pkg/memory/logic/store_postgres.go:426–478  ·  view source on GitHub ↗

Prune 清理低价值 Memory

(ctx context.Context, criteria PruneCriteria)

Source from the content-addressed store, hash-verified

424
425// Prune 清理低价值 Memory
426func (s *PostgreSQLStore) Prune(ctx context.Context, criteria PruneCriteria) (int, error) {
427 if s.closed {
428 return 0, ErrStoreClosed
429 }
430
431 args := []any{}
432 argIndex := 1
433
434 // 构建 OR 条件
435 conditions := []string{}
436
437 if criteria.MinConfidence > 0 {
438 conditions = append(conditions, fmt.Sprintf("confidence < $%d", argIndex))
439 args = append(args, criteria.MinConfidence)
440 argIndex++
441 }
442
443 if criteria.SinceLastAccess > 0 {
444 conditions = append(conditions, fmt.Sprintf("last_accessed < $%d", argIndex))
445 args = append(args, time.Now().Add(-criteria.SinceLastAccess))
446 argIndex++
447 }
448
449 if criteria.MinAccessCount > 0 && criteria.MaxAge > 0 {
450 conditions = append(conditions, fmt.Sprintf("(access_count < $%d AND created_at < $%d)", argIndex, argIndex+1))
451 args = append(args, criteria.MinAccessCount, time.Now().Add(-criteria.MaxAge))
452 // argIndex += 2 不需要,后续没有使用
453 }
454
455 if len(conditions) == 0 {
456 return 0, nil
457 }
458
459 // 构建查询
460 query := fmt.Sprintf(`
461 DELETE FROM %s
462 WHERE %s
463 `, s.tableName, conditions[0])
464
465 var querySb463 strings.Builder
466 for i := 1; i < len(conditions); i++ {
467 querySb463.WriteString(" OR " + conditions[i])
468 }
469 query += querySb463.String()
470
471 result, err := s.db.ExecContext(ctx, query, args...)
472 if err != nil {
473 return 0, NewStoreError("DELETE_ERROR", "failed to prune memories", err)
474 }
475
476 rowsAffected, _ := result.RowsAffected()
477 return int(rowsAffected), nil
478}
479
480// Close 关闭存储
481func (s *PostgreSQLStore) Close() error {

Callers

nothing calls this directly

Calls 3

NewStoreErrorFunction · 0.85
AddMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected