forked from fuxiaohei/jx
/
table.go
192 lines (168 loc) · 3.65 KB
/
table.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 jx
import (
"errors"
"github.com/Unknwon/com"
"github.com/fuxiaohei/jx/col"
"os"
"path"
"reflect"
)
var (
Nil = errors.New("nil")
Conflict = errors.New("conflict")
Wrong = errors.New("wrong")
)
type Table struct {
directory string
Object *Object
Chunk *col.Chunk
Pk *col.PK
}
// insert value to table.
// save value to chunk and pk.
func (t *Table) Insert(v interface{}) (e error) {
// set pk value, auto-increment or unique.
var pk interface{}
pk, e = t.Pk.SetPk(v, t.Object.Pk)
if e != nil {
// use table error, not pk error
if e == col.PKConflict {
e = Conflict
}
if e == col.PkEmpty {
e = Wrong
}
return
}
// write to chunk
uid, cursor, e := t.Chunk.Write(v)
if e != nil {
return
}
// write to pk
e = t.Pk.Write(pk, cursor, uid, 0)
return
}
// delete value in table.
// delete pk and data in chunk together.
func (t *Table) Delete(v interface{}) (e error) {
// get pkValue for chunk deleting
pk := reflect.ValueOf(v).Elem().FieldByName(t.Object.Pk).Interface()
pkValue, e := t.Pk.Get(pk)
if e != nil || pkValue == nil {
return
}
// delete in pk first.
if e = t.Pk.Delete(pk); e != nil {
return
}
// get value, if not found, no need to delete.
value, e := t.Chunk.Get(pkValue)
if e != nil || value == nil {
return
}
e = t.Chunk.Delete(pkValue)
return
}
// update value in table.
// update data and pk together.
func (t *Table) Update(v interface{}) (e error) {
// get pkValue for chunk updating
pk := reflect.ValueOf(v).Elem().FieldByName(t.Object.Pk).Interface()
pkValue, e := t.Pk.Get(pk)
if e != nil || pkValue == nil {
return
}
// write to data chunk
e = t.Chunk.Update(v, pkValue)
if e != nil {
return
}
// write to pk
e = t.Pk.Update(pk, pkValue)
return
}
// get value by value pk field.
// it not found, return error Nil.
func (t *Table) Get(v interface{}) (e error) {
pk := reflect.ValueOf(v).Elem().FieldByName(t.Object.Pk).Interface()
pkValue, e := t.Pk.Get(pk)
if e != nil {
return
}
if pkValue == nil {
e = Nil
return
}
value, e := t.Chunk.Get(pkValue)
if e != nil {
return
}
if value == nil {
e = Nil
return
}
// assign to passed value
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(value).Elem())
return
}
// init table.
// if first run, create chunk and pk.
// otherwise, read chunk data and pk data.
func (t *Table) init() (e error) {
if !com.IsDir(t.directory) {
e = t.firstInit()
return
}
// read pk file
dir := path.Join(t.directory, "_pk")
if t.Pk, e = col.NewPk(dir, t.Object.PkAuto); e != nil {
return
}
// read chunk file
dir = path.Join(t.directory, "_data")
if t.Chunk, e = col.NewChunk(dir, "data", ".dat", 1000, t.Object.DataType); e != nil {
return
}
// read last chunk as default
e = t.Chunk.ReadCursorFile(t.Pk.GetLastCursor(), true)
return
}
// first init for table,
// create data and pk directories and default files.
func (t *Table) firstInit() (e error) {
// create directory
e = os.MkdirAll(t.directory, os.ModePerm)
if e != nil {
return
}
// init data chunk
dir := path.Join(t.directory, "_data")
if t.Chunk, e = col.NewChunk(dir, "data", ".dat", 1000, t.Object.DataType); e != nil {
return
}
// init pk
dir = path.Join(t.directory, "_pk")
if t.Pk, e = col.NewPk(dir, t.Object.PkAuto); e != nil {
return
}
return
}
// optimize table data.
// chunk and pk are all optimized.
func (t *Table) Optimize() (e error) {
if e = t.Pk.Optimize(); e != nil {
return
}
e = t.Chunk.Optimize()
return
}
// create new table in directory with object definition.
func NewTable(directory string, obj *Object) (t *Table, e error) {
t = &Table{
directory: directory,
Object: obj,
}
e = t.init()
return
}