-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
47 lines (41 loc) · 880 Bytes
/
base.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
/*
* NeoDB
*
* Copyright 2018 The Radon Authors.
* Copyright 2021-2030 The NeoDB Authors.
* Code is licensed under the GPLv3.
*
*/
package xbase
import (
"io"
"os"
"github.com/pkg/errors"
)
// WriteFile used to write data to file.
func WriteFile(file string, data []byte) error {
flag := os.O_RDWR | os.O_TRUNC
if _, err := os.Stat(file); os.IsNotExist(err) {
flag |= os.O_CREATE
}
f, err := os.OpenFile(file, flag, 0644)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()
n, err := f.Write(data)
if err != nil {
return errors.WithStack(err)
}
if n != len(data) {
return errors.WithStack(io.ErrShortWrite)
}
return f.Sync()
}
// TruncateQuery used to truncate the query with max length.
func TruncateQuery(query string, max int) string {
if max == 0 || len(query) <= max {
return query
}
return query[:max] + " [TRUNCATED]"
}