-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
72 lines (60 loc) · 2.57 KB
/
io.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
// Copyright (C) 2022 CYBERCRYPT
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Package io contains the definition of the IO Provider, as well as various implementations of
// the concept.
package io
import (
"context"
"encoding/binary"
"errors"
"fmt"
)
// Error returned if data is not found during a "Get" or "Update" call.
var ErrNotFound = errors.New("not found")
// Error returned if data is found during a "Put" call.
var ErrAlreadyExists = errors.New("already exists")
// Types of data supported by an IO Provider.
type DataType uint16
const (
DataTypeSealedObject DataType = iota
DataTypeSealedAccess
DataTypeSealedNode
DataTypeEnd
)
// Bytes returns a byte representation of a DataType.
func (d DataType) Bytes() []byte {
b := make([]byte, binary.MaxVarintLen16)
binary.LittleEndian.PutUint16(b, uint16(d))
return b
}
// String returns a string representation of a DataType.
func (d DataType) String() string {
return fmt.Sprintf("%d", d)
}
// Provider is the interface an IO Provider must implement to handle data from D1.
type Provider interface {
// Put sends bytes to the IO Provider. The data is identified by an ID and a data type.
// Should return ErrAlreadyExists if the data already exists in the IO Provider.
Put(ctx context.Context, id []byte, dataType DataType, data []byte) error
// Get fetches data from the IO Provider. The data is identified by an ID and a data type.
// Should return ErrNotFound if the data does not exist in the IO Provider.
Get(ctx context.Context, id []byte, dataType DataType) ([]byte, error)
// Update is similar to Put but updates data previously sent to the IO Provider.
// Should return ErrNotFound if the data does not exist in the IO Provider.
Update(ctx context.Context, id []byte, dataType DataType, data []byte) error
// Delete removes data previously sent to the IO Provider.
// Should not error if the data does not exist in the IO Provider.
Delete(ctx context.Context, id []byte, dataType DataType) error
}