-
Notifications
You must be signed in to change notification settings - Fork 12
/
file.go
156 lines (132 loc) · 3.28 KB
/
file.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
package codes
import (
"io"
"runtime"
"github.com/amsokol/go-errors"
"github.com/amsokol/go-eccodes/debug"
cio "github.com/amsokol/go-eccodes/io"
"github.com/amsokol/go-eccodes/native"
)
type Reader interface {
Next() (Message, error)
}
type Writer interface {
}
type File interface {
Reader
Writer
Close()
}
type file struct {
file cio.File
}
type fileIndexed struct {
index native.Ccodes_index
}
var emptyFilter = map[string]interface{}{}
func OpenFile(f cio.File) (File, error) {
return &file{file: f}, nil
}
func OpenFileByPathWithFilter(path string, filter map[string]interface{}) (File, error) {
if filter == nil {
filter = emptyFilter
}
var k string
for key, value := range filter {
if len(k) > 0 {
k += ","
}
k += key
if value != nil {
switch value.(type) {
case int64, int:
k += ":l"
case float64, float32:
k += ":d"
case string:
k += ":s"
}
}
}
i, err := native.Ccodes_index_new_from_file(native.DefaultContext, path, k)
if err != nil {
return nil, errors.Wrap(err, "failed to create filtered index")
}
for key, value := range filter {
if value != nil {
err = nil
switch value.(type) {
case int64:
err = native.Ccodes_index_select_long(i, key, value.(int64))
if err != nil {
err = errors.Wrapf(err, "failed to set filter condition '%s'=%d", key, value.(int64))
}
case int:
err = native.Ccodes_index_select_long(i, key, int64(value.(int)))
if err != nil {
err = errors.Wrapf(err, "failed to set filter condition '%s'=%d", key, value.(int64))
}
case float64:
err = native.Ccodes_index_select_double(i, key, value.(float64))
if err != nil {
err = errors.Wrapf(err, "failed to set filter condition '%s'=%f", key, value.(float64))
}
case float32:
err = native.Ccodes_index_select_double(i, key, float64(value.(float32)))
if err != nil {
err = errors.Wrapf(err, "failed to set filter condition '%s'=%f", key, value.(float64))
}
case string:
err = native.Ccodes_index_select_string(i, key, value.(string))
if err != nil {
err = errors.Wrapf(err, "failed to set filter condition '%s'='%s'", key, value.(string))
}
}
if err != nil {
native.Ccodes_index_delete(i)
return nil, err
}
}
}
file := &fileIndexed{index: i}
runtime.SetFinalizer(file, fileIndexedFinalizer)
return file, nil
}
func (f *file) Next() (Message, error) {
handle, err := native.Ccodes_handle_new_from_file(native.DefaultContext, f.file.Native(), native.ProductAny)
if err != nil {
if err == io.EOF {
return nil, err
}
return nil, errors.Wrap(err, "failed create new handle from file")
}
return newMessage(handle), nil
}
func (f *file) Close() {
f.file = nil
}
func (f *fileIndexed) isOpen() bool {
return f.index != nil
}
func (f *fileIndexed) Next() (Message, error) {
handle, err := native.Ccodes_handle_new_from_index(f.index)
if err != nil {
if err == io.EOF {
return nil, err
}
return nil, errors.Wrap(err, "failed to create handle from index")
}
return newMessage(handle), nil
}
func (f *fileIndexed) Close() {
if f.isOpen() {
defer func() { f.index = nil }()
native.Ccodes_index_delete(f.index)
}
}
func fileIndexedFinalizer(f *fileIndexed) {
if f.isOpen() {
debug.MemoryLeakLogger.Print("file is not closed")
f.Close()
}
}