| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package memory | 5 package memory |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "sync" | 9 "sync" |
| 10 "time" | 10 "time" |
| 11 | 11 |
| 12 "github.com/luci/luci-go/common/config" | |
| 13 "github.com/luci/luci-go/logdog/common/storage" | 12 "github.com/luci/luci-go/logdog/common/storage" |
| 14 "github.com/luci/luci-go/logdog/common/types" | 13 "github.com/luci/luci-go/logdog/common/types" |
| 14 "github.com/luci/luci-go/luci_config/common/cfgtypes" |
| 15 ) | 15 ) |
| 16 | 16 |
| 17 type logStream struct { | 17 type logStream struct { |
| 18 logs map[types.MessageIndex][]byte | 18 logs map[types.MessageIndex][]byte |
| 19 latestIndex types.MessageIndex | 19 latestIndex types.MessageIndex |
| 20 } | 20 } |
| 21 | 21 |
| 22 type rec struct { | 22 type rec struct { |
| 23 index types.MessageIndex | 23 index types.MessageIndex |
| 24 data []byte | 24 data []byte |
| 25 } | 25 } |
| 26 | 26 |
| 27 type streamKey struct { | 27 type streamKey struct { |
| 28 » project config.ProjectName | 28 » project cfgtypes.ProjectName |
| 29 path types.StreamPath | 29 path types.StreamPath |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Storage is an implementation of the storage.Storage interface that stores | 32 // Storage is an implementation of the storage.Storage interface that stores |
| 33 // data in memory. | 33 // data in memory. |
| 34 // | 34 // |
| 35 // This is intended for testing, and not intended to be performant. | 35 // This is intended for testing, and not intended to be performant. |
| 36 type Storage struct { | 36 type Storage struct { |
| 37 // MaxGetCount, if not zero, is the maximum number of records to retriev
e from | 37 // MaxGetCount, if not zero, is the maximum number of records to retriev
e from |
| 38 // a single Get request. | 38 // a single Get request. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 } | 143 } |
| 144 if !cb(storage.MakeEntry(dataCopy, r.index)) { | 144 if !cb(storage.MakeEntry(dataCopy, r.index)) { |
| 145 break | 145 break |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 | 148 |
| 149 return nil | 149 return nil |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Tail implements storage.Storage. | 152 // Tail implements storage.Storage. |
| 153 func (s *Storage) Tail(project config.ProjectName, path types.StreamPath) (*stor
age.Entry, error) { | 153 func (s *Storage) Tail(project cfgtypes.ProjectName, path types.StreamPath) (*st
orage.Entry, error) { |
| 154 var r *rec | 154 var r *rec |
| 155 | 155 |
| 156 // Find the latest log, then return it. | 156 // Find the latest log, then return it. |
| 157 err := s.run(func() error { | 157 err := s.run(func() error { |
| 158 ls := s.getLogStreamLocked(project, path, false) | 158 ls := s.getLogStreamLocked(project, path, false) |
| 159 if ls == nil { | 159 if ls == nil { |
| 160 return storage.ErrDoesNotExist | 160 return storage.ErrDoesNotExist |
| 161 } | 161 } |
| 162 | 162 |
| 163 r = &rec{ | 163 r = &rec{ |
| 164 index: ls.latestIndex, | 164 index: ls.latestIndex, |
| 165 data: ls.logs[ls.latestIndex], | 165 data: ls.logs[ls.latestIndex], |
| 166 } | 166 } |
| 167 return nil | 167 return nil |
| 168 }) | 168 }) |
| 169 if err != nil { | 169 if err != nil { |
| 170 return nil, err | 170 return nil, err |
| 171 } | 171 } |
| 172 return storage.MakeEntry(r.data, r.index), nil | 172 return storage.MakeEntry(r.data, r.index), nil |
| 173 } | 173 } |
| 174 | 174 |
| 175 // Count returns the number of log records for the given stream. | 175 // Count returns the number of log records for the given stream. |
| 176 func (s *Storage) Count(project config.ProjectName, path types.StreamPath) (c in
t) { | 176 func (s *Storage) Count(project cfgtypes.ProjectName, path types.StreamPath) (c
int) { |
| 177 s.run(func() error { | 177 s.run(func() error { |
| 178 if st := s.getLogStreamLocked(project, path, false); st != nil { | 178 if st := s.getLogStreamLocked(project, path, false); st != nil { |
| 179 c = len(st.logs) | 179 c = len(st.logs) |
| 180 } | 180 } |
| 181 return nil | 181 return nil |
| 182 }) | 182 }) |
| 183 return | 183 return |
| 184 } | 184 } |
| 185 | 185 |
| 186 // SetErr sets the storage's error value. If not nil, all operations will fail | 186 // SetErr sets the storage's error value. If not nil, all operations will fail |
| (...skipping 10 matching lines...) Expand all Loading... |
| 197 | 197 |
| 198 if s.err != nil { | 198 if s.err != nil { |
| 199 return s.err | 199 return s.err |
| 200 } | 200 } |
| 201 if s.closed { | 201 if s.closed { |
| 202 return errors.New("storage is closed") | 202 return errors.New("storage is closed") |
| 203 } | 203 } |
| 204 return f() | 204 return f() |
| 205 } | 205 } |
| 206 | 206 |
| 207 func (s *Storage) getLogStreamLocked(project config.ProjectName, path types.Stre
amPath, create bool) *logStream { | 207 func (s *Storage) getLogStreamLocked(project cfgtypes.ProjectName, path types.St
reamPath, create bool) *logStream { |
| 208 key := streamKey{ | 208 key := streamKey{ |
| 209 project: project, | 209 project: project, |
| 210 path: path, | 210 path: path, |
| 211 } | 211 } |
| 212 | 212 |
| 213 ls := s.streams[key] | 213 ls := s.streams[key] |
| 214 if ls == nil && create { | 214 if ls == nil && create { |
| 215 ls = &logStream{ | 215 ls = &logStream{ |
| 216 logs: map[types.MessageIndex][]byte{}, | 216 logs: map[types.MessageIndex][]byte{}, |
| 217 latestIndex: -1, | 217 latestIndex: -1, |
| 218 } | 218 } |
| 219 | 219 |
| 220 if s.streams == nil { | 220 if s.streams == nil { |
| 221 s.streams = map[streamKey]*logStream{} | 221 s.streams = map[streamKey]*logStream{} |
| 222 } | 222 } |
| 223 s.streams[key] = ls | 223 s.streams[key] = ls |
| 224 } | 224 } |
| 225 | 225 |
| 226 return ls | 226 return ls |
| 227 } | 227 } |
| OLD | NEW |