This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrecords.go
116 lines (96 loc) · 2.77 KB
/
records.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
package orient
import (
"fmt"
)
type ORecord interface {
OIdentifiable
Fill(rid RID, version int, content []byte) error // TODO: put to separate interface?
Content() ([]byte, error)
Version() int
SetVersion(v int)
SetRID(rid RID)
RecordType() RecordType
}
var (
_ ORecord = (*BytesRecord)(nil)
_ ORecord = (*Document)(nil)
)
// List of standard record types
const (
RecordTypeDocument RecordType = 'd'
RecordTypeBytes RecordType = 'b'
RecordTypeFlat RecordType = 'f'
)
func init() {
declareRecordType(RecordTypeDocument, "document", func() ORecord { return NewEmptyDocument() })
//declareRecordType(RecordTypeFlat,"flat",func() orient.ORecord { panic("flat record type is not implemented") }) // TODO: implement
declareRecordType(RecordTypeBytes, "bytes", func() ORecord { return NewBytesRecord() })
}
// RecordType defines a registered record type
type RecordType byte
var recordFactories = make(map[RecordType]RecordFactory)
// RecordFactory is a function to create records of certain type
type RecordFactory func() ORecord
func declareRecordType(tp RecordType, name string, fnc RecordFactory) {
if _, ok := recordFactories[tp]; ok {
panic(fmt.Errorf("record type byte '%v' already in use", tp))
}
recordFactories[tp] = fnc
}
// GetRecordFactory returns RecordFactory for a given type
func GetRecordFactory(tp RecordType) RecordFactory {
return recordFactories[tp]
}
// NewRecordOfType creates a new record of specified type
func NewRecordOfType(tp RecordType) ORecord {
fnc := GetRecordFactory(tp)
if fnc == nil {
panic(fmt.Errorf("record type '%c' is not supported", tp))
}
return fnc()
}
func NewBytesRecord() *BytesRecord { return &BytesRecord{} }
// BytesRecord is a rawest representation of a record. It's schema less.
// Use this if you need to store []byte without matter about the content.
// Useful also to store multimedia contents and binary files.
type BytesRecord struct {
RID RID
Vers int
Data []byte
}
func (r BytesRecord) Content() ([]byte, error) {
return r.Data, nil
}
func (r BytesRecord) Version() int {
return r.Vers
}
func (r *BytesRecord) SetVersion(v int) {
r.Vers = v
}
func (r *BytesRecord) SetRID(rid RID) {
r.RID = rid
}
func (r BytesRecord) RecordType() RecordType {
return RecordTypeBytes
}
// GetIdentity returns a record RID
func (r BytesRecord) GetIdentity() RID {
return r.RID
}
// GetRecord returns a record data
func (r BytesRecord) GetRecord() interface{} {
if r.Data == nil {
return nil
}
return r.Data
}
// Fill sets identity, version and raw data of the record
func (r *BytesRecord) Fill(rid RID, version int, content []byte) error {
r.RID = rid
r.Vers = version
r.Data = content
return nil
}
func (r BytesRecord) String() string {
return fmt.Sprintf("Bytes{RID: %s, Vers: %d, Data: [%d]}", r.RID, r.Vers, len(r.Data))
}