-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfile_connection.go
executable file
·75 lines (66 loc) · 1.91 KB
/
file_connection.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
package dsc
import (
"errors"
"fmt"
"github.com/viant/toolbox"
"os"
)
type fileConnection struct {
*AbstractConnection
URL string
ext string
files map[string]*os.File
}
func (fc *fileConnection) Close() error {
for _, file := range fc.files {
file.Close()
}
return nil
}
func getFile(filename string, connection Connection) (*os.File, error) {
fileConn, ok := connection.(*fileConnection)
if !ok {
return nil, fmt.Errorf("invalid connection type")
}
var err error
if _, ok := fileConn.files[filename]; !ok {
if len(fileConn.files) == 0 {
fileConn.files = make(map[string]*os.File)
}
if !toolbox.FileExists(filename) {
fileConn.files[filename], err = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
} else {
fileConn.files[filename], err = os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644)
}
if err != nil {
return nil, err
}
}
return fileConn.files[filename], nil
}
func (fc *fileConnection) Unwrap(target interface{}) interface{} {
return errors.New("unsupported")
}
type fileConnectionProvider struct {
*AbstractConnectionProvider
}
func (cp *fileConnectionProvider) NewConnection() (Connection, error) {
config := cp.Config()
url := config.Get("url")
ext := config.Get("ext")
var fileConnection = &fileConnection{URL: url, ext: ext}
var connection = fileConnection
var super = NewAbstractConnection(config, cp.ConnectionProvider.ConnectionPool(), connection)
fileConnection.AbstractConnection = super
return connection, nil
}
func newFileConnectionProvider(config *Config) ConnectionProvider {
if config.MaxPoolSize == 0 {
config.MaxPoolSize = 1
}
fileConnectionProvider := &fileConnectionProvider{}
var connectionProvider ConnectionProvider = fileConnectionProvider
super := NewAbstractConnectionProvider(config, make(chan Connection, config.MaxPoolSize), connectionProvider)
fileConnectionProvider.AbstractConnectionProvider = super
return connectionProvider
}