-
Notifications
You must be signed in to change notification settings - Fork 352
/
model.go
192 lines (161 loc) · 4.07 KB
/
model.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package catalog
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
"github.com/treeverse/lakefs/pkg/block"
)
const (
DefaultContentType = "application/octet-stream"
)
type Metadata map[string]string
type CommitVersion int
type CommitGeneration int64
type Repository struct {
Name string
StorageNamespace string
DefaultBranch string
CreationDate time.Time
ReadOnly bool
}
type DBEntry struct {
CommonLevel bool
Path string
PhysicalAddress string
CreationDate time.Time
Size int64
Checksum string
Metadata Metadata
Expired bool
AddressType AddressType
ContentType string
}
type CommitLog struct {
Reference string
Committer string
Message string
CreationDate time.Time
Metadata Metadata
MetaRangeID string
Parents []string
Generation CommitGeneration
Version CommitVersion
}
type Branch struct {
Name string
Reference string
}
type Tag struct {
ID string
CommitID string
}
// AddressType is the type of an entry address
type AddressType int32
const (
// Deprecated: indicates that the address might be relative or full.
// Used only for backward compatibility and should not be used for creating entries.
AddressTypeByPrefixDeprecated AddressType = 0
// AddressTypeRelative indicates that the address is relative to the storage namespace.
// For example: "foo/bar"
AddressTypeRelative AddressType = 1
// AddressTypeFull indicates that the address is the full address of the object in the object store.
// For example: "s3://bucket/foo/bar"
AddressTypeFull AddressType = 2
)
//nolint:staticcheck
func (at AddressType) ToIdentifierType() block.IdentifierType {
switch at {
case AddressTypeByPrefixDeprecated:
return block.IdentifierTypeUnknownDeprecated
case AddressTypeRelative:
return block.IdentifierTypeRelative
case AddressTypeFull:
return block.IdentifierTypeFull
default:
panic(fmt.Sprintf("unknown address type: %d", at))
}
}
func (j Metadata) Value() (driver.Value, error) {
if j == nil {
return json.Marshal(struct{}{})
}
return json.Marshal(j)
}
func (j *Metadata) Scan(src interface{}) error {
if src == nil {
return nil
}
data, ok := src.([]byte)
if !ok {
return ErrInvalidMetadataSrcFormat
}
return json.Unmarshal(data, j)
}
func ContentTypeOrDefault(ct string) string {
if ct == "" {
return DefaultContentType
}
return ct
}
// DBEntryBuilder DBEntry builder
type DBEntryBuilder struct {
dbEntry DBEntry
}
func NewDBEntryBuilder() *DBEntryBuilder {
return &DBEntryBuilder{}
}
func (b *DBEntryBuilder) CommonLevel(commonLevel bool) *DBEntryBuilder {
b.dbEntry.CommonLevel = commonLevel
return b
}
func (b *DBEntryBuilder) Path(path string) *DBEntryBuilder {
b.dbEntry.Path = path
return b
}
func (b *DBEntryBuilder) RelativeAddress(relative bool) *DBEntryBuilder {
if relative {
b.dbEntry.AddressType = AddressTypeRelative
} else {
b.dbEntry.AddressType = AddressTypeFull
}
return b
}
func (b *DBEntryBuilder) PhysicalAddress(physicalAddress string) *DBEntryBuilder {
b.dbEntry.PhysicalAddress = physicalAddress
return b
}
func (b *DBEntryBuilder) CreationDate(creationDate time.Time) *DBEntryBuilder {
b.dbEntry.CreationDate = creationDate
return b
}
func (b *DBEntryBuilder) Size(size int64) *DBEntryBuilder {
b.dbEntry.Size = size
return b
}
func (b *DBEntryBuilder) Checksum(checksum string) *DBEntryBuilder {
b.dbEntry.Checksum = checksum
return b
}
func (b *DBEntryBuilder) Metadata(metadata Metadata) *DBEntryBuilder {
b.dbEntry.Metadata = metadata
return b
}
func (b *DBEntryBuilder) Expired(expired bool) *DBEntryBuilder {
b.dbEntry.Expired = expired
return b
}
func (b *DBEntryBuilder) AddressType(addressType AddressType) *DBEntryBuilder {
b.dbEntry.AddressType = addressType
return b
}
func (b *DBEntryBuilder) ContentType(contentType string) *DBEntryBuilder {
b.dbEntry.ContentType = contentType
return b
}
func (b *DBEntryBuilder) Build() DBEntry {
if !b.dbEntry.CommonLevel && b.dbEntry.ContentType == "" {
b.dbEntry.ContentType = DefaultContentType
}
return b.dbEntry
}