-
Notifications
You must be signed in to change notification settings - Fork 0
/
vol_rw.go
88 lines (73 loc) · 1.96 KB
/
vol_rw.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
package bakemono
import "log"
const MaxKeyLength = 4096
func (v *Vol) Set(key, value []byte) (err error) {
//log.Printf("DEBUG: set key: %s, value_len: %d", key, len(value))
err = v.checkSetRequest(key, value)
if err != nil {
return err
}
// make data chunk
ck := &Chunk{}
err = ck.Set(key, value)
if err != nil {
return err
}
// process data write position
binLenOnDisk := ck.GetBinaryLength()
if v.WritePos+binLenOnDisk > v.Length {
log.Printf("data write overflowed, start from dataOffset. set: writePos: %d, dataOffset: %d, len(value): %d", v.WritePos, v.DataOffset, len(value))
v.WritePos = v.DataOffset
}
writeOffset := v.WritePos
v.WritePos += binLenOnDisk
// set dir
_, err = v.Dm.Set(key, writeOffset, int(binLenOnDisk))
// write to disk
err = ck.WriteAt(v.Fp, int64(writeOffset))
if err != nil {
return err
}
return nil
}
func (v *Vol) checkSetRequest(key, value []byte) (err error) {
if len(key) > MaxKeyLength {
return ErrChunkKeyTooLarge
}
//if Offset(len(value)) > 10 * v.ChunkAvgSize {
// return ErrChunkDataTooLarge
//}
return nil
}
func (v *Vol) Get(key []byte) (hit bool, value []byte, err error) {
//log.Printf("DEBUG: get key: %s", key)
err = v.checkGetRequest(key)
if err != nil {
return false, nil, err
}
hit, _, d := v.Dm.Get(key)
if !hit {
return false, nil, nil
}
// read data
readOffset := d.offset()
approxSize := d.approxSize()
ck := &Chunk{}
err = ck.ReadAt(v.Fp, int64(readOffset), int64(approxSize))
if err != nil {
log.Printf("warning: failed to read data chunk. key: %s, offset: %d, approxSize: %d, err: %s", key, readOffset, approxSize, err)
return false, nil, err
}
ckKey, ckData := ck.GetKeyData()
if string(ckKey) != string(key) {
//log.Printf("warning: key mismatch. key: %s, ckKey: %s", key, ckKey)
return false, nil, nil
}
return true, ckData, nil
}
func (v *Vol) checkGetRequest(key []byte) (err error) {
if len(key) > MaxKeyLength {
return ErrChunkKeyTooLarge
}
return nil
}