-
Notifications
You must be signed in to change notification settings - Fork 8
/
store.go
73 lines (61 loc) · 1.63 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package oaipmh
import (
"context"
"time"
)
type Store interface {
ListSets(ctx context.Context, q *RequestConfig) (Resumable, error)
ListIdentifiers(ctx context.Context, q *RequestConfig) (Resumable, error)
ListRecords(ctx context.Context, q *RequestConfig) (Resumable, error)
GetRecord(ctx context.Context, q *RequestConfig) (Record, []Error, error)
ListMetadataFormats(ctx context.Context, q *RequestConfig) ([]MetadataFormat, error)
}
type RequestConfig struct {
ID string // ID of the harvest step parsed from resumption token
FirstRequest *Request
CurrentRequest RawToken
StoreCursor string
OrgID string
DatasetID string
TotalSize int
Finished bool
}
func (q *RequestConfig) IsResumedRequest() bool {
return q.CurrentRequest.HarvestID != ""
}
func (q *RequestConfig) NextResumptionToken(res *Resumable) *ResumptionToken {
cursor := q.CurrentRequest.Cursor
token := RawToken{
HarvestID: q.ID,
Cursor: cursor + res.Len(),
StorePayload: res.StorePayload,
}
rt := &ResumptionToken{
CompleteListSize: q.TotalSize,
Cursor: cursor,
}
if rt.CompleteListSize > token.Cursor {
rt.Token = token.String()
rt.ExperationDate = time.Now().Add(1 * time.Minute).Format(TimeFormat)
}
return rt
}
type Resumable struct {
Sets []Set
Headers []Header
Records []Record
Errors []Error
StorePayload string
Total int // Total only needs to be returned the first time
}
func (res *Resumable) Len() int {
size := len(res.Records)
if size > 0 {
return size
}
size = len(res.Headers)
if size > 0 {
return size
}
return len(res.Sets)
}