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 pathrid.go
161 lines (139 loc) · 4.18 KB
/
rid.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
package orient
// ref: com.orientechnologies.orient.core.id
import (
"fmt"
"gopkg.in/istreamdata/orientgo.v2/obinary/rw"
"io"
"strconv"
"strings"
)
type OIdentifiable interface {
GetIdentity() RID
GetRecord() interface{}
}
var (
_ OIdentifiable = RID{}
)
const (
ridPrefix = '#'
ridSeparator = ':'
clusterIdMax = 32767
clusterIdInvalid = -1
clusterPosInvalid = -1
ridSerializedSize = rw.SizeShort + rw.SizeLong
)
// RID encapsulates the two aspects of an OrientDB RecordID - ClusterID:ClusterPos.
// ORecordId in Java world.
type RID struct {
ClusterID int16
ClusterPos int64
}
// NewEmptyRID returns an RID with the default "invalid" settings.
// Invalid settings indicate that the Document has not yet been saved
// to the DB (which assigns it a valid RID) or it indicates that
// it is not a true Document with a Class (e.g., it is a result of a Property query)
func NewEmptyRID() RID {
return RID{ClusterID: clusterIdInvalid, ClusterPos: clusterPosInvalid}
}
func (rid RID) checkClusterLimits() {
if rid.ClusterID < -2 {
panic(fmt.Sprint("RecordId cannot support negative cluster id. You've used:", rid.ClusterID))
}
if rid.ClusterID > clusterIdMax {
panic(fmt.Sprint("RecordId cannot support cluster id major than 32767. You've used:", rid.ClusterID))
}
}
// NewRID creates a RID with given ClusterId and ClusterPos. It will check value for validity.
func NewRID(cid int16, pos int64) RID {
rid := RID{ClusterID: cid, ClusterPos: pos}
rid.checkClusterLimits()
return rid
}
// NewRIDInCluster creates an empty RID inside specified cluster
func NewRIDInCluster(cid int16) RID {
rid := RID{ClusterID: cid, ClusterPos: clusterPosInvalid}
rid.checkClusterLimits()
return rid
}
// GetIdentity implements OIdentifiable interface on RID
func (r RID) GetIdentity() RID {
return r
}
func (r RID) GetRecord() interface{} {
return nil
}
// String converts RID to #N:M string format
func (rid RID) String() string {
return fmt.Sprintf(
string(ridPrefix)+"%d"+string(ridSeparator)+"%d",
rid.ClusterID, rid.ClusterPos,
)
}
func (r RID) IsValid() bool {
return r.ClusterID != clusterIdInvalid
}
func (rid RID) IsPersistent() bool {
return rid.ClusterID > -1 && rid.ClusterPos > clusterPosInvalid
}
func (rid RID) IsNew() bool {
return rid.ClusterPos < 0
}
func (rid RID) IsTemporary() bool {
return rid.ClusterID != -1 && rid.ClusterPos < clusterPosInvalid
}
// Next is a shortcut for rid.NextRID().String()
func (rid RID) Next() string {
return rid.NextRID().String()
}
// NextRID returns next RID in current cluster
func (rid RID) NextRID() RID {
rid.checkClusterLimits()
rid.ClusterPos++ // uses local copy of rid
return rid
}
func (rid *RID) FromStream(r io.Reader) error {
buf := make([]byte, ridSerializedSize)
if err := rw.NewReader(r).ReadRawBytes(buf); err != nil {
return err
}
rid.ClusterID = int16(rw.Order.Uint16(buf))
rid.ClusterPos = int64(rw.Order.Uint64(buf[rw.SizeShort:]))
return nil
}
func (rid RID) ToStream(w io.Writer) error {
bw := rw.NewWriter(w)
bw.WriteShort(rid.ClusterID)
bw.WriteLong(rid.ClusterPos)
return bw.Err()
}
// ParseRID converts a string of form #N:M or N:M to a RID.
func ParseRID(s string) (RID, error) {
s = strings.TrimSpace(s)
if s == "" {
return NewEmptyRID(), nil
} else if !strings.Contains(s, string(ridSeparator)) {
return NewEmptyRID(), fmt.Errorf("Argument '%s' is not a RecordId in form of string. Format must be: <cluster-id>:<cluster-position>", s)
}
s = strings.TrimLeft(s, string(ridPrefix))
parts := strings.Split(s, string(ridSeparator))
if len(parts) != 2 {
return NewEmptyRID(), fmt.Errorf("Argument received '%s' is not a RecordId in form of string. Format must be: #<cluster-id>:<cluster-position>. Example: #3:12", s)
}
id, err := strconv.ParseInt(parts[0], 10, 16)
if err != nil {
return NewEmptyRID(), fmt.Errorf("Invalid RID string to ParseRID: %s", s)
}
pos, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return NewEmptyRID(), fmt.Errorf("Invalid RID string to ParseRID: %s", s)
}
return NewRID(int16(id), int64(pos)), nil
}
// MustParseRID is a version of ParseRID which panics on errors
func MustParseRID(s string) RID {
rid, err := ParseRID(s)
if err != nil {
panic(err)
}
return rid
}