Skip to content

Instantly share code, notes, and snippets.

@acud
Created November 18, 2021 15:23
Show Gist options
  • Save acud/f4041c60cf4ae4408a973aff3506bc26 to your computer and use it in GitHub Desktop.
Save acud/f4041c60cf4ae4408a973aff3506bc26 to your computer and use it in GitHub Desktop.
// PinStore implements storage abstractions for user pinned content.
type PinningStore struct {
s Store // Store can also implement MetaStore. In boltdb this is possible through using alternate buckets
m MetaStore
}
type Store interface {
Get(swarm.Address) swarm.Chunk
Put(swarm.Chunk)
Iterate(IterFn) error
Delete(swarm.Address)
Count() int
}
type MetaStore interface {
Get(k []byte) []byte
Put(k []byte, v []byte)
Iterate(IterFn)
Delete(k []byte)
}
// PinStore implements an ubiquitous Get method that
// can blindly check all buckets for a chunk address.
// This allows the PinStore to act as an aggregate for
// chunk lookups.
func (p PinStore) Get(swarm.Address) swarm.Chunk {
buckets := p.m.Get(bucketsKey) //buckets store the pin bucket name for all pins
for _,b := range buckets{
if ch := u.s.Get(addr); ch != nil {
return ch
}
}
return nil
}
// NewContext returns a new pinning context.
func (p PinStore) NewContext() (GetterPutter,string) {
buckets := p.m.Get(bucketsKey) //buckets store the pin bucket name for all pins
bucket := p.newContext()
buckets = append(buckets, bucket))
return p.s.PrefixStore(bucket),bucket // boltdb simple store needs to support writes which are prefixed by a bucket name
}
// Context returns a GetterPutter that can write chunks
// into a bucket. It requires that pinId exists as a
// pinning context.
func (p PinStore) Context(pinId string) GetterPutter {
buckets := p.m.Get(bucketsKey) //buckets store the pin bucket name for all pins
return p.s.PrefixStore(buckets[pinId]) // boltdb simple store needs to support writes which are prefixed by a bucket name
}
func (p PinStore) Unpin(pinId string) {
buckets := p.m.Get(bucketsKey) //buckets store the pin bucket name for all pins
p.destroy(buckets[pinId])
}
func (p PinStore) destroy(key string) {
return p.s.DeleteBucket(key)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment