forked from araddon/qlbridge
/
mockcsv.go
183 lines (157 loc) · 4.57 KB
/
mockcsv.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
// Mockcsv implements an in-memory csv data source for testing usage
// implemented by wrapping the mem-b-tree.
package mockcsv
import (
"fmt"
"strings"
u "github.com/araddon/gou"
"github.com/araddon/qlbridge/datasource"
"github.com/araddon/qlbridge/datasource/membtree"
"github.com/araddon/qlbridge/schema"
)
const (
// SchemaName is "mockcsv"
SchemaName = "mockcsv"
)
var (
// Ensure this Csv Data Source implements expected interfaces
_ schema.Source = (*Source)(nil)
_ schema.Alter = (*Source)(nil)
_ schema.Conn = (*Table)(nil)
_ schema.ConnUpsert = (*Table)(nil)
_ schema.ConnDeletion = (*Table)(nil)
// CsvGlobal mock csv in mem store
CsvGlobal = New()
// Schema the mock schema
sch *schema.Schema
)
// Schema global accessor to the mockcsv schema
func Schema() *schema.Schema {
if sch != nil {
return sch
}
if err := schema.RegisterSourceAsSchema(SchemaName, CsvGlobal); err != nil {
panic(fmt.Sprintf("Could not read schema %v", err))
}
sch, _ = schema.DefaultRegistry().Schema(SchemaName)
return sch
}
// LoadTable MockCsv is used for mocking so has a global data source we can load data into
func LoadTable(schemaName, name, csvRaw string) {
CsvGlobal.CreateTable(name, csvRaw)
schema.DefaultRegistry().SchemaRefresh(SchemaName)
}
// Source DataSource for testing creates an in memory b-tree per "table".
// Is not thread safe.
type Source struct {
s *schema.Schema
tablenamelist []string
tables map[string]*membtree.StaticDataSource
raw map[string]string
}
// Table converts the static csv-source into a schema.Conn source
type Table struct {
*membtree.StaticDataSource
}
// New create csv mock source.
func New() *Source {
return &Source{
tablenamelist: make([]string, 0),
raw: make(map[string]string),
tables: make(map[string]*membtree.StaticDataSource),
}
}
// Init no-op meets interface
func (m *Source) Init() {}
// Setup accept schema
func (m *Source) Setup(s *schema.Schema) error {
m.s = s
return nil
}
// DropTable Drop table schema
func (m *Source) DropTable(t string) error {
delete(m.raw, t)
delete(m.tables, t)
names := make([]string, 0, len(m.tables))
for tableName, _ := range m.raw {
names = append(names, tableName)
}
m.tablenamelist = names
return nil
}
// Open open connection to given tablename.
func (m *Source) Open(tableName string) (schema.Conn, error) {
tableName = strings.ToLower(tableName)
if ds, ok := m.tables[tableName]; ok {
return &Table{StaticDataSource: ds}, nil
}
err := m.loadTable(tableName)
if err != nil {
u.Errorf("could not load table %q err=%v", tableName, err)
return nil, err
}
ds := m.tables[tableName]
return &Table{StaticDataSource: ds}, nil
}
// Table get table
func (m *Source) Table(tableName string) (*schema.Table, error) {
tableName = strings.ToLower(tableName)
if ds, ok := m.tables[tableName]; ok {
return ds.Table(tableName)
}
err := m.loadTable(tableName)
if err != nil {
u.Errorf("could not load table %q err=%v", tableName, err)
return nil, err
}
ds, ok := m.tables[tableName]
if !ok {
return nil, schema.ErrNotFound
}
return ds.Table(tableName)
}
func (m *Source) loadTable(tableName string) error {
csvRaw, ok := m.raw[tableName]
if !ok {
return schema.ErrNotFound
}
sr := strings.NewReader(csvRaw)
//u.Debugf("mockcsv:%p load mockcsv: %q data:%v", m, tableName, csvRaw)
csvSource, _ := datasource.NewCsvSource(tableName, 0, sr, make(<-chan bool, 1))
ds := membtree.NewStaticData(tableName)
u.Infof("loaded columns table=%q cols=%v", tableName, csvSource.Columns())
ds.SetColumns(csvSource.Columns())
m.tables[tableName] = ds
// Now we are going to page through the Csv rows and Put into
// Static Data Source, ie copy into memory btree structure
for {
msg := csvSource.Next()
if msg == nil {
break
}
dm, ok := msg.Body().(*datasource.SqlDriverMessageMap)
if !ok {
return fmt.Errorf("Expected *datasource.SqlDriverMessageMap but got %T", msg.Body())
}
// We don't know the Key
ds.Put(nil, nil, dm.Values())
}
iter := &Table{StaticDataSource: ds}
tbl, err := ds.Table(tableName)
if err != nil {
return err
}
return datasource.IntrospectTable(tbl, iter)
}
// Close csv source.
func (m *Source) Close() error { return nil }
// Tables list of tables.
func (m *Source) Tables() []string { return m.tablenamelist }
// CreateTable create a csv table in this source.
func (m *Source) CreateTable(tableName, csvRaw string) {
if _, exists := m.raw[tableName]; !exists {
m.tablenamelist = append(m.tablenamelist, tableName)
}
m.raw[tableName] = csvRaw
m.loadTable(tableName)
}