/
objects.go
84 lines (63 loc) · 1.27 KB
/
objects.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
package loge
import (
"reflect"
)
const (
UNLOCKED = 0
LOCKED = 1
)
type LogeObject struct {
DB *LogeDB
Type *LogeType
Key LogeKey
Current *LogeObjectVersion
RefCount uint32
LinkName string
Lock SpinLock
Loaded bool
}
type LogeObjectVersion struct {
LogeObj *LogeObject
Version int
Object interface{}
Dirty bool
}
func InitializeObject(db *LogeDB, t *LogeType, key LogeKey) *LogeObject {
return &LogeObject{
DB: db,
Type: t,
Key: key,
Current: nil,
RefCount: 0,
Loaded: false,
}
}
func (obj *LogeObject) NewVersion() *LogeObjectVersion {
var current = obj.Current
var newObj = obj.Type.Copy(current.Object)
return &LogeObjectVersion{
LogeObj: obj,
Version: current.Version + 1,
Object: newObj,
Dirty: true,
}
}
func (obj *LogeObject) Applicable(version *LogeObjectVersion) bool {
return version.Version == obj.Current.Version + 1
}
func (obj *LogeObject) ApplyVersion(version *LogeObjectVersion, batch LogeWriteBatch) {
obj.Current = version
if obj.LinkName == "" {
batch.Store(obj)
} else {
batch.StoreLinks(obj)
}
version.Dirty = false
if obj.LinkName != "" {
version.Object.(*LinkSet).Freeze()
}
}
func (version *LogeObjectVersion) HasValue() bool {
var value = reflect.ValueOf(version.Object)
return !value.IsNil()
}