Added avatars to make it a bit more friendly
This commit is contained in:
@@ -1,62 +1,48 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Entry is the minimal metadata we expose to clients.
|
||||
// Entry is the index record returned to clients.
|
||||
// Keep metadata minimal to protect users.
|
||||
type Entry struct {
|
||||
Hash string `json:"hash"`
|
||||
Bytes int64 `json:"bytes"`
|
||||
StoredAt string `json:"stored_at"` // RFC3339Nano
|
||||
Private bool `json:"private"` // true if client marked encrypted
|
||||
CreatorTZ string `json:"creator_tz,omitempty"` // optional IANA TZ from client
|
||||
StoredAt string `json:"stored_at"` // RFC3339Nano string
|
||||
Private bool `json:"private"`
|
||||
CreatorTZ string `json:"creator_tz,omitempty"`
|
||||
Author string `json:"author,omitempty"` // pseudonymous (thumbprint), optional
|
||||
}
|
||||
|
||||
// Index is an in-memory map from hash -> Entry, safe for concurrent use.
|
||||
type Index struct {
|
||||
mu sync.RWMutex
|
||||
m map[string]Entry
|
||||
mu sync.RWMutex
|
||||
data map[string]Entry
|
||||
}
|
||||
|
||||
func New() *Index {
|
||||
return &Index{m: make(map[string]Entry)}
|
||||
return &Index{data: make(map[string]Entry)}
|
||||
}
|
||||
|
||||
func (ix *Index) Put(e Entry) error {
|
||||
if e.Hash == "" {
|
||||
return errors.New("empty hash")
|
||||
}
|
||||
ix.mu.Lock()
|
||||
ix.m[e.Hash] = e
|
||||
ix.data[e.Hash] = e
|
||||
ix.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ix *Index) Delete(hash string) error {
|
||||
if hash == "" {
|
||||
return errors.New("empty hash")
|
||||
}
|
||||
ix.mu.Lock()
|
||||
delete(ix.m, hash)
|
||||
delete(ix.data, hash)
|
||||
ix.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ix *Index) Get(hash string) (Entry, bool) {
|
||||
ix.mu.RLock()
|
||||
e, ok := ix.m[hash]
|
||||
ix.mu.RUnlock()
|
||||
return e, ok
|
||||
}
|
||||
|
||||
// All returns an unsorted copy of all entries.
|
||||
func (ix *Index) All() []Entry {
|
||||
ix.mu.RLock()
|
||||
out := make([]Entry, 0, len(ix.m))
|
||||
for _, v := range ix.m {
|
||||
out = append(out, v)
|
||||
out := make([]Entry, 0, len(ix.data))
|
||||
for _, e := range ix.data {
|
||||
out = append(out, e)
|
||||
}
|
||||
ix.mu.RUnlock()
|
||||
return out
|
||||
|
Reference in New Issue
Block a user