This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changes.go
99 lines (89 loc) · 1.95 KB
/
changes.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Changes feed support
//
// At present, this driver provides only rudamentary Changes feed support. It
// supports only one-off changes feeds (no continuous support), and this is
// implemented by scanning the database directory, and returning each document
// and its most recent revision only.
package fs
import (
"context"
"io"
"os"
"strings"
"github.com/go-kivik/fsdb/cdb/decode"
"github.com/go-kivik/kivik/driver"
)
type changes struct {
db *db
ctx context.Context
infos []os.FileInfo
}
var _ driver.Changes = &changes{}
func (c *changes) ETag() string { return "" }
func (c *changes) LastSeq() string { return "" }
func (c *changes) Pending() int64 { return 0 }
func ignoreDocID(name string) bool {
if name[0] != '_' {
return false
}
if strings.HasPrefix(name, "_design/") {
return false
}
if strings.HasPrefix(name, "_local/") {
return false
}
return true
}
func (c *changes) Next(ch *driver.Change) error {
for {
if len(c.infos) == 0 {
return io.EOF
}
candidate := c.infos[len(c.infos)-1]
c.infos = c.infos[:len(c.infos)-1]
if candidate.IsDir() {
continue
}
for _, ext := range decode.Extensions() {
if strings.HasSuffix(candidate.Name(), "."+ext) {
base := strings.TrimSuffix(candidate.Name(), "."+ext)
docid, err := filename2id(base)
if err != nil {
// ignore unrecognized files
continue
}
if ignoreDocID(docid) {
continue
}
rev, err := c.db.currentRev(candidate.Name(), ext)
if err != nil {
return err
}
if rev == "" {
rev = "1-"
}
ch.ID = docid
ch.Changes = []string{rev}
return nil
}
}
}
}
func (c *changes) Close() error {
return nil
}
func (d *db) Changes(ctx context.Context, _ map[string]interface{}) (driver.Changes, error) {
f, err := os.Open(d.path())
if err != nil {
return nil, err
}
dir, err := f.Readdir(-1)
if err != nil {
return nil, err
}
return &changes{
db: d,
ctx: ctx,
infos: dir,
}, nil
}