Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(657)

Unified Diff: logdog/common/storage/entry.go

Issue 2435883002: LogDog: Fix archival Get/Tail implementations. (Closed)
Patch Set: LogDog: Fix archival Get/Tail implementations. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: logdog/common/storage/entry.go
diff --git a/logdog/common/storage/entry.go b/logdog/common/storage/entry.go
new file mode 100644
index 0000000000000000000000000000000000000000..2ff78ff2c7f31681a36c05d25984000681c373b0
--- /dev/null
+++ b/logdog/common/storage/entry.go
@@ -0,0 +1,77 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package storage
+
+import (
+ "github.com/luci/luci-go/common/errors"
+ "github.com/luci/luci-go/logdog/api/logpb"
+ "github.com/luci/luci-go/logdog/common/types"
+
+ "github.com/golang/protobuf/proto"
+)
+
+// Entry is a logpb.LogEntry wrapper that lazily evaluates / unmarshals the
+// underlying LogEntry data as needed.
+//
+// Entry is not goroutine-safe.
+type Entry struct {
+ D []byte
+
+ streamIndex types.MessageIndex
+ le *logpb.LogEntry
+}
+
+// MakeEntry creates a new Entry.
+//
+// All Entry must be backed by data. The index, "idx", is optional. If <0, it
+// will be calculated by unmarshalling the data into a LogEntry and pulling the
+// value from there.
+func MakeEntry(d []byte, idx types.MessageIndex) *Entry {
+ return &Entry{
+ D: d,
+ streamIndex: idx,
+ }
+}
+
+// GetStreamIndex returns the LogEntry's stream index.
+//
+// If this needs to be calculated by unmarshalling the LogEntry, this will be
+// done. If this fails, an error will be returned.
+//
+// If GetLogEntry has succeeded, subsequent GetStreamIndex calls will always
+// be fast and succeed.
+func (e *Entry) GetStreamIndex() (types.MessageIndex, error) {
+ if e.streamIndex < 0 {
+ le, err := e.GetLogEntry()
+ if err != nil {
+ return -1, err
+ }
+
+ e.streamIndex = types.MessageIndex(le.StreamIndex)
+ }
+
+ return e.streamIndex, nil
+}
+
+// GetLogEntry returns the unmarshalled LogEntry data.
+//
+// The first time this is called, the LogEntry will be unmarshalled from its
+// underlying data.
+func (e *Entry) GetLogEntry() (*logpb.LogEntry, error) {
+ if e.le == nil {
+ if e.D == nil {
+ // This can happen with keys-only results.
+ return nil, errors.New("no log entry data")
+ }
+
+ var le logpb.LogEntry
+ if err := proto.Unmarshal(e.D, &le); err != nil {
+ return nil, errors.Annotate(err).Reason("failed to unmarshal").Err()
+ }
+ e.le = &le
+ }
+
+ return e.le, nil
+}

Powered by Google App Engine
This is Rietveld 408576698