-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexternal_store.go
66 lines (55 loc) · 1.34 KB
/
external_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
package imapsql
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
)
type ExtStoreObj interface {
Sync() error
io.Reader
io.Writer
io.Closer
}
type ExternalError struct {
// true if error was caused by an attempt to access non-existent key.
NonExistent bool
Key string
Err error
}
// Unwrap implements Unwrap() for Go 1.13 'errors'.
func (err ExternalError) Unwrap() error {
return err.Err
}
// Cause implements Cause() for pkg/errors.
func (err ExternalError) Cause() error {
return err.Err
}
func (err ExternalError) Error() string {
if err.NonExistent {
return fmt.Sprintf("external: non-existent key %s", err.Key)
}
return fmt.Sprintf("external: %v", err.Err)
}
/*
ExternalStore is an interface used by go-imap-sql to store message bodies
outside of main database.
*/
type ExternalStore interface {
Create(key string, objectSize int64) (ExtStoreObj, error)
// Open returns the ExtStoreObj that reads the message body specified by
// passed key.
//
// If no such message exists - ExternalError with NonExistent = true is
// returned.
Open(key string) (ExtStoreObj, error)
// Delete removes a set of keys from store. Non-existent keys are ignored.
Delete(keys []string) error
}
func randomKey() (string, error) {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}