This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
records.go
58 lines (54 loc) · 1.62 KB
/
records.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
package warc
// Records provides utility functions for slices of records.
//
// A WARC format file is the simple concatenation of one or more WARC records.
// The first record usually describes the records to follow. In general,
// record content is either the direct result of a retrieval attempt — web
// pages, inline images, URL redirection information, DNS hostname lookup
// results, standalone files, etc. — or is synthesized material (e.g.,
// metadata, transformed content) that provides additional information about
// archived content.
type Records []*Record
// FilterTypes return all record types that match a provide
// list of RecordTypes
func (rs Records) FilterTypes(types ...RecordType) Records {
res := Records{}
for _, rec := range rs {
for _, t := range types {
if rec.Type == t {
res = append(res, rec)
}
}
}
return res
}
// TargetURIRecord returns a record matching uri optionally filtered by
// a list of record types. There are a number of "gotchas" if multiple
// record types of the same url are in the list.
// TODO - eliminate "gotchas"
func (rs Records) TargetURIRecord(uri string, types ...RecordType) *Record {
for _, rec := range rs {
if rec.TargetURI() == uri {
if len(types) == 0 {
return rec
}
for _, t := range types {
if rec.Type == t {
return rec
}
}
}
}
return nil
}
// RemoveTargetURIRecords returns a Records slice with all records
// that refer to uri removed
func (rs Records) RemoveTargetURIRecords(uri string) (recs Records) {
recs = rs
for i, rec := range rs {
if rec.TargetURI() == uri {
recs = append(recs[:i], recs[i+1:]...)
}
}
return
}