Read 实现 BackendProtocol.Read
(ctx context.Context, path string, offset, limit int)
| 112 | |
| 113 | // Read 实现 BackendProtocol.Read |
| 114 | func (b *StateBackend) Read(ctx context.Context, path string, offset, limit int) (string, error) { |
| 115 | b.mu.RLock() |
| 116 | defer b.mu.RUnlock() |
| 117 | |
| 118 | data, exists := b.files[path] |
| 119 | if !exists { |
| 120 | return "", fmt.Errorf("file not found: %s", path) |
| 121 | } |
| 122 | |
| 123 | totalLines := len(data.Lines) |
| 124 | |
| 125 | // 处理 offset |
| 126 | if offset < 0 { |
| 127 | offset = 0 |
| 128 | } |
| 129 | if offset >= totalLines { |
| 130 | return "", nil |
| 131 | } |
| 132 | |
| 133 | // 处理 limit |
| 134 | endLine := totalLines |
| 135 | if limit > 0 { |
| 136 | endLine = min(offset+limit, totalLines) |
| 137 | } |
| 138 | |
| 139 | selectedLines := data.Lines[offset:endLine] |
| 140 | return strings.Join(selectedLines, "\n"), nil |
| 141 | } |
| 142 | |
| 143 | // Write 实现 BackendProtocol.Write |
| 144 | func (b *StateBackend) Write(ctx context.Context, path, content string) (*WriteResult, error) { |