| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package storage |
| 6 |
| 7 import ( |
| 8 "github.com/luci/luci-go/common/errors" |
| 9 "github.com/luci/luci-go/logdog/api/logpb" |
| 10 "github.com/luci/luci-go/logdog/common/types" |
| 11 |
| 12 "github.com/golang/protobuf/proto" |
| 13 ) |
| 14 |
| 15 // Entry is a logpb.LogEntry wrapper that lazily evaluates / unmarshals the |
| 16 // underlying LogEntry data as needed. |
| 17 // |
| 18 // Entry is not goroutine-safe. |
| 19 type Entry struct { |
| 20 D []byte |
| 21 |
| 22 streamIndex types.MessageIndex |
| 23 le *logpb.LogEntry |
| 24 } |
| 25 |
| 26 // MakeEntry creates a new Entry. |
| 27 // |
| 28 // All Entry must be backed by data. The index, "idx", is optional. If <0, it |
| 29 // will be calculated by unmarshalling the data into a LogEntry and pulling the |
| 30 // value from there. |
| 31 func MakeEntry(d []byte, idx types.MessageIndex) *Entry { |
| 32 return &Entry{ |
| 33 D: d, |
| 34 streamIndex: idx, |
| 35 } |
| 36 } |
| 37 |
| 38 // GetStreamIndex returns the LogEntry's stream index. |
| 39 // |
| 40 // If this needs to be calculated by unmarshalling the LogEntry, this will be |
| 41 // done. If this fails, an error will be returned. |
| 42 // |
| 43 // If GetLogEntry has succeeded, subsequent GetStreamIndex calls will always |
| 44 // be fast and succeed. |
| 45 func (e *Entry) GetStreamIndex() (types.MessageIndex, error) { |
| 46 if e.streamIndex < 0 { |
| 47 le, err := e.GetLogEntry() |
| 48 if err != nil { |
| 49 return -1, err |
| 50 } |
| 51 |
| 52 e.streamIndex = types.MessageIndex(le.StreamIndex) |
| 53 } |
| 54 |
| 55 return e.streamIndex, nil |
| 56 } |
| 57 |
| 58 // GetLogEntry returns the unmarshalled LogEntry data. |
| 59 // |
| 60 // The first time this is called, the LogEntry will be unmarshalled from its |
| 61 // underlying data. |
| 62 func (e *Entry) GetLogEntry() (*logpb.LogEntry, error) { |
| 63 if e.le == nil { |
| 64 if e.D == nil { |
| 65 // This can happen with keys-only results. |
| 66 return nil, errors.New("no log entry data") |
| 67 } |
| 68 |
| 69 var le logpb.LogEntry |
| 70 if err := proto.Unmarshal(e.D, &le); err != nil { |
| 71 return nil, errors.Annotate(err).Reason("failed to unmar
shal").Err() |
| 72 } |
| 73 e.le = &le |
| 74 } |
| 75 |
| 76 return e.le, nil |
| 77 } |
| OLD | NEW |