-
Notifications
You must be signed in to change notification settings - Fork 1
/
storagefile.go
50 lines (41 loc) · 901 Bytes
/
storagefile.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
package raptordb
import (
"os"
"bufio"
"bytes"
"sync"
)
type SyncedBuffer struct {
lock sync.Mutex
buffer bytes.Buffer
}
type StorageFile struct {
Filename string
MaxKeyLen int
file *os.File
writer *bufio.Writer
}
func NewStorageFile(name string, maxKeyLen int) (store *StorageFile){
store = &StorageFile{Filename: name, MaxKeyLen: maxKeyLen}
store.initFileWriter()
return;
}
func (s *StorageFile) initFileWriter() {
if file, err := os.OpenFile(s.Filename, os.O_CREATE, 0777); err == nil {
s.file = file
s.writer = bufio.NewWriter(file)
println("Success allocating a new Writer")
}
}
func (s *StorageFile) Close() {
if s.file != nil {
s.file.Close()
println("storagefile: Call Close()")
}
}
func (s *StorageFile) Flush() {
s.writer.Flush()
}
func (s *StorageFile) Write(b []byte) {
s.writer.Write(b)
}