-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
handler.go
77 lines (65 loc) · 2.03 KB
/
handler.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
// +build !oss
/*
* Copyright 2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Dgraph Community License (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* https://github.com/dgraph-io/dgraph/blob/master/licenses/DCL.txt
*/
package backup
import (
"fmt"
"io"
"net/url"
"path"
"github.com/dgraph-io/dgraph/x"
)
const backupFmt = "r%d-g%d.backup"
// handler interface is implemented by URI scheme handlers.
type handler interface {
// Handlers know how to Write and Close their location.
io.WriteCloser
// Create prepares the location for write operations.
Create(*url.URL, *Request) error
// Load will scan location URI for backup files, then load them with loadFunc.
Load(*url.URL, uint64, loadFn) error
}
// getHandler returns a handler for the URI scheme.
// Returns new handler on success, nil otherwise.
func getHandler(scheme string) handler {
switch scheme {
case "file", "":
return &fileHandler{}
case "s3":
return &s3Handler{}
}
return nil
}
// getInfo scans the file name and returns its the read timestamp and group ID.
// If the file is not scannable, returns an error.
// Returns read timestamp and group ID, or an error otherwise.
func getInfo(file string) (uint64, int, error) {
var readTs uint64
var groupId int
_, err := fmt.Sscanf(path.Base(file), backupFmt, &readTs, &groupId)
return readTs, groupId, err
}
// loadFn is a function that will receive the current file being read.
// A reader and the backup readTs are passed as arguments.
type loadFn func(io.Reader, int) error
// Load will scan location l for backup files, then load them sequentially through reader.
// If since is non-zero, handlers might filter files based on readTs.
// Returns nil on success, error otherwise.
func Load(l string, since uint64, fn loadFn) error {
uri, err := url.Parse(l)
if err != nil {
return err
}
h := getHandler(uri.Scheme)
if h == nil {
return x.Errorf("Unsupported URI: %v", uri)
}
return h.Load(uri, since, fn)
}