-
Notifications
You must be signed in to change notification settings - Fork 3
/
persistence.go
167 lines (138 loc) · 3.86 KB
/
persistence.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
package graph
import (
"os"
"unsafe"
mmap "github.com/edsrzf/mmap-go"
)
// raw defines a struct for binary SimpleGraphs format.
type raw struct {
file *os.File
data mmap.MMap
FIndicesLen uint64
FIndPtrLen uint64
BIndicesLen uint64
BIndPtrLen uint64
FIndPtr []uint64
FIndices []uint32
BIndPtr []uint64
BIndices []uint32
}
func (raw *raw) close() error {
var err1, err2 error
if raw.data != nil {
err1 = raw.data.Unmap()
}
if raw.file != nil {
err2 = raw.file.Close()
}
if err1 != nil {
return err1
}
return err2
}
func loadRaw(filename string) (*raw, error) {
var err error
raw := &raw{}
raw.file, err = os.OpenFile(filename, os.O_RDONLY, 0644)
if err != nil {
raw.close()
return nil, err
}
raw.data, err = mmap.Map(raw.file, mmap.RDONLY, 0)
if err != nil {
raw.close()
return nil, err
}
x := 0
copy((*[8]byte)(unsafe.Pointer(&raw.FIndPtrLen))[:], raw.data[x:x+8])
x += 8
copy((*[8]byte)(unsafe.Pointer(&raw.FIndicesLen))[:], raw.data[x:x+8])
x += 8
copy((*[8]byte)(unsafe.Pointer(&raw.BIndPtrLen))[:], raw.data[x:x+8])
x += 8
copy((*[8]byte)(unsafe.Pointer(&raw.BIndicesLen))[:], raw.data[x:x+8])
x += 8
raw.FIndPtr = ((*[1 << 40]uint64)(unsafe.Pointer(&raw.data[x])))[0:int(raw.FIndPtrLen)]
x += 8 * int(raw.FIndPtrLen)
raw.FIndices = ((*[1 << 40]uint32)(unsafe.Pointer(&raw.data[x])))[0:int(raw.FIndicesLen)]
x += 4 * int(raw.FIndicesLen)
raw.BIndPtr = ((*[1 << 40]uint64)(unsafe.Pointer(&raw.data[x])))[0:int(raw.BIndPtrLen)]
x += 8 * int(raw.BIndPtrLen)
raw.BIndices = ((*[1 << 40]uint32)(unsafe.Pointer(&raw.data[x])))[0:int(raw.BIndicesLen)]
return raw, nil
}
// Save saves a SimpleGraph to a file in raw (binary) format.
func (g SimpleGraph) Save(filename string) error {
FIndPtr := g.FMat().IndPtr
FIndices := g.FMat().Indices
BIndPtr := g.BMat().IndPtr
BIndices := g.BMat().Indices
output, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer output.Close()
FIndPtrLen := int64(len(FIndPtr))
FIndicesLen := int64(len(FIndices))
BIndPtrLen := int64(len(BIndPtr))
BIndicesLen := int64(len(BIndices))
FIndPtrBytes := 8 * len(FIndPtr)
FIndicesBytes := 4 * len(FIndices)
BIndPtrBytes := 8 * len(BIndPtr)
BIndicesBytes := 4 * len(BIndices)
err = output.Truncate(int64(8 + 8 + 8 + 8 + FIndPtrBytes + FIndicesBytes + BIndPtrBytes + BIndicesBytes))
if err != nil {
return err
}
data, err := mmap.Map(output, mmap.RDWR, 0)
if err != nil {
return err
}
defer data.Unmap()
x := 0
copy(data[x:x+8], ((*[8]byte)(unsafe.Pointer(&FIndPtrLen))[:]))
x += 8
copy(data[x:x+8], ((*[8]byte)(unsafe.Pointer(&FIndicesLen))[:]))
x += 8
copy(data[x:x+8], ((*[8]byte)(unsafe.Pointer(&BIndPtrLen))[:]))
x += 8
copy(data[x:x+8], ((*[8]byte)(unsafe.Pointer(&BIndicesLen))[:]))
x += 8
if len(FIndPtr) > 0 {
copy(data[x:x+FIndPtrBytes],
((*[1 << 40]byte)(unsafe.Pointer(&FIndPtr[0]))[:FIndPtrBytes]))
x += FIndPtrBytes
}
if len(FIndices) > 0 {
copy(data[x:x+FIndicesBytes],
((*[1 << 40]byte)(unsafe.Pointer(&FIndices[0]))[:FIndicesBytes]))
x += FIndicesBytes
}
if len(FIndPtr) > 0 {
copy(data[x:x+BIndPtrBytes],
((*[1 << 40]byte)(unsafe.Pointer(&BIndPtr[0]))[:BIndPtrBytes]))
x += BIndPtrBytes
}
if len(BIndices) > 0 {
copy(data[x:x+BIndicesBytes],
((*[1 << 40]byte)(unsafe.Pointer(&BIndices[0]))[:BIndicesBytes]))
x += BIndicesBytes
}
return nil
}
// Load loads a raw (binary) SimpleGraph file and returns a SimpleGraph.
func Load(fn string) (SimpleGraph, error) {
raw, err := loadRaw(fn)
if err != nil {
return SimpleGraph{}, err
}
find := make([]uint32, raw.FIndicesLen)
findptr := make([]uint64, raw.FIndPtrLen)
bind := make([]uint32, raw.BIndicesLen)
bindptr := make([]uint64, raw.BIndPtrLen)
copy(find, raw.FIndices)
copy(findptr, raw.FIndPtr)
copy(bind, raw.BIndices)
copy(bindptr, raw.BIndPtr)
return FromRaw(findptr, find, bindptr, bind)
}