(ctx context.Context, fromMeta, toMeta *backend.BlockMeta, from backend.Reader, to backend.Writer)
| 12 | ) |
| 13 | |
| 14 | func CopyBlock(ctx context.Context, fromMeta, toMeta *backend.BlockMeta, from backend.Reader, to backend.Writer) error { |
| 15 | // Copy streams, efficient but can't cache. |
| 16 | copyStream := func(name string) error { |
| 17 | reader, size, err := from.StreamReader(ctx, name, (uuid.UUID)(fromMeta.BlockID), fromMeta.TenantID) |
| 18 | if err != nil { |
| 19 | return fmt.Errorf("error reading %s: %w", name, err) |
| 20 | } |
| 21 | defer reader.Close() |
| 22 | |
| 23 | return to.StreamWriter(ctx, name, (uuid.UUID)(toMeta.BlockID), toMeta.TenantID, reader, size) |
| 24 | } |
| 25 | |
| 26 | // Read entire object and attempt to cache |
| 27 | cpy := func(name string, cacheInfo *backend.CacheInfo) error { |
| 28 | cacheInfo.Meta = fromMeta |
| 29 | b, err := from.Read(ctx, name, (uuid.UUID)(fromMeta.BlockID), fromMeta.TenantID, cacheInfo) |
| 30 | if err != nil { |
| 31 | return fmt.Errorf("error reading %s: %w", name, err) |
| 32 | } |
| 33 | |
| 34 | cacheInfo.Meta = toMeta |
| 35 | return to.Write(ctx, name, (uuid.UUID)(toMeta.BlockID), toMeta.TenantID, b, cacheInfo) |
| 36 | } |
| 37 | |
| 38 | // Data |
| 39 | err := copyStream(DataFileName) |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | |
| 44 | // Bloom |
| 45 | cacheInfo := &backend.CacheInfo{Role: cache.RoleBloom} |
| 46 | for i := 0; i < common.ValidateShardCount(int(fromMeta.BloomShardCount)); i++ { |
| 47 | err = cpy(common.BloomName(i), cacheInfo) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Index (may not exist) |
| 54 | err = cpy(common.NameIndex, &backend.CacheInfo{Role: cache.RoleTraceIDIdx}) |
| 55 | if err != nil && !errors.Is(err, backend.ErrDoesNotExist) { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | // no-compact flag |
| 60 | if hasNoCompactFlag, err := from.HasNoCompactFlag(ctx, (uuid.UUID)(fromMeta.BlockID), fromMeta.TenantID); err != nil { |
| 61 | return err |
| 62 | } else if hasNoCompactFlag { |
| 63 | err = to.WriteNoCompactFlag(ctx, (uuid.UUID)(toMeta.BlockID), toMeta.TenantID) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Meta |
| 70 | err = to.WriteBlockMeta(ctx, toMeta) |
| 71 | return err |
no test coverage detected