Skip to content

Commit

Permalink
feat: Log: NewFrom functions
Browse files Browse the repository at this point in the history
  • Loading branch information
glouvigny authored and aeddi committed Jun 12, 2019
1 parent ec5eb13 commit abeb867
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 13 deletions.
93 changes: 82 additions & 11 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,21 +412,92 @@ func (l *Log) ToBuffer() ([]byte, error) {
return json.Marshal(l.ToJSON())
}

// TODO:
//func (l *Log) ToMultihash() {
// return LogIO.toMultihash(this._storage, this,
// {
// format
// })
//}
func (l *Log) ToMultihash() (cid.Cid, error) {
return ToMultihash(l.Storage, l)
}

func NewFromMultihash(services *io.IpfsServices, identity *identityprovider.Identity, hash cid.Cid, logOptions *NewLogOptions, fetchOptions *FetchOptions) (*Log, error) {
data, err := FromMultihash(services, hash, &FetchOptions{
Length: fetchOptions.Length,
Exclude: fetchOptions.Exclude,
ProgressChan: fetchOptions.ProgressChan,
})

if err != nil {
return nil, err
}

// TODO: fromMultihash
heads := []*entry.Entry{}
for _, e := range data.Values {
for _, h := range data.Heads {
if e.Hash.String() == h.String() {
heads = append(heads, e)
break
}
}
}

// TODO: fromEntryHash
return NewLog(services, identity, &NewLogOptions{
ID: data.ID,
AccessController: logOptions.AccessController,
Entries: data.Values,
Heads: heads,
Clock: lamportclock.New(data.Clock.ID, data.Clock.Time),
SortFn: logOptions.SortFn,
}), nil
}


func NewFromEntryHash (services *io.IpfsServices, identity *identityprovider.Identity, hash cid.Cid, logOptions *NewLogOptions, fetchOptions *FetchOptions) *Log {
// TODO: need to verify the entries with 'key'
entries := FromEntryHash(services, []cid.Cid{hash}, &FetchOptions{
Length: fetchOptions.Length,
Exclude: fetchOptions.Exclude,
ProgressChan: fetchOptions.ProgressChan,
})

// TODO: fromJSON
return NewLog(services, identity, &NewLogOptions{
ID: logOptions.ID,
AccessController: logOptions.AccessController,
Entries: entries,
SortFn: logOptions.SortFn,
})
}

func NewFromJSON (services *io.IpfsServices, identity *identityprovider.Identity, jsonData []byte, logOptions *NewLogOptions, fetchOptions *entry.FetchOptions) *Log {
// TODO: need to verify the entries with 'key'
jsonLog := JSONLog{}

snapshot := FromJSON(services, jsonLog, &entry.FetchOptions{
Length: fetchOptions.Length,
Timeout: fetchOptions.Timeout,
ProgressChan: fetchOptions.ProgressChan,
})

return NewLog(services, identity, &NewLogOptions{
ID: logOptions.ID,
AccessController: logOptions.AccessController,
Entries: snapshot.Values,
SortFn: logOptions.SortFn,
})
}

func NewFromEntry (services *io.IpfsServices, identity *identityprovider.Identity, sourceEntries []*entry.Entry, logOptions *NewLogOptions, fetchOptions *entry.FetchOptions) *Log {
// TODO: need to verify the entries with 'key'
snapshot := FromEntry(services, sourceEntries, &entry.FetchOptions{
Length: fetchOptions.Length,
Exclude: fetchOptions.Exclude,
ProgressChan: fetchOptions.ProgressChan,
})

return NewLog(services, identity, &NewLogOptions{
ID: logOptions.ID,
AccessController: logOptions.AccessController,
Entries: snapshot.Values,
SortFn: logOptions.SortFn,
})
}

// TODO: fromEntry

// TODO: findTails

Expand Down
4 changes: 2 additions & 2 deletions log/log_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ type FetchOptions struct {
}


func ToMultihash (services *io.IpfsServices, log *Log) (cid.Cid, error) {
func ToMultihash(services *io.IpfsServices, log *Log) (cid.Cid, error) {
if len(log.Values()) < 1 {
return cid.Cid{}, errors.New(`Can't serialize an empty log`)
}

return io.WriteCBOR(services, log.ToJSON())
}

func FromMultihash (services *io.IpfsServices, hash cid.Cid, options *FetchOptions) (*Snapshot, error) {
func FromMultihash(services *io.IpfsServices, hash cid.Cid, options *FetchOptions) (*Snapshot, error) {
result, err := io.ReadCBOR(services, hash)
if err != nil {
return nil, err
Expand Down

0 comments on commit abeb867

Please sign in to comment.