Commit 2ab737be authored by Kirill Smelkov's avatar Kirill Smelkov

X Add cancelling context to zodb.OpenStorageURL

parent 5bb19440
...@@ -79,10 +79,9 @@ func (c *Client) Iterate(tidMin, tidMax zodb.Tid) zodb.IStorageIterator { ...@@ -79,10 +79,9 @@ func (c *Client) Iterate(tidMin, tidMax zodb.Tid) zodb.IStorageIterator {
// TODO read-only support // TODO read-only support
func openClientByURL(u *url.URL) (zodb.IStorage, error) { func openClientByURL(ctx context.Context, u *url.URL) (zodb.IStorage, error) {
// XXX for now url is treated as storage node URL // XXX for now url is treated as storage node URL
// XXX check/use other url fields // XXX check/use other url fields
ctx := context.Background() // XXX ok?
storLink, err := Dial(ctx, "tcp", u.Host) storLink, err := Dial(ctx, "tcp", u.Host)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -210,7 +210,7 @@ func storageMain(argv []string) { ...@@ -210,7 +210,7 @@ func storageMain(argv []string) {
} }
// XXX hack to use existing zodb storage for data // XXX hack to use existing zodb storage for data
zstor, err := fs1.Open(argv[0]) zstor, err := fs1.Open(context.Background(), argv[0]) // XXX context.Background -> ?
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
...@@ -19,13 +19,14 @@ package zodb ...@@ -19,13 +19,14 @@ package zodb
// logic to open storages by URL // logic to open storages by URL
import ( import (
"context"
"fmt" "fmt"
"net/url" "net/url"
"strings" "strings"
) )
// StorageOpener is a function to open a storage // StorageOpener is a function to open a storage
type StorageOpener func (u *url.URL) (IStorage, error) type StorageOpener func (ctx context.Context, u *url.URL) (IStorage, error)
// {} scheme -> StorageOpener // {} scheme -> StorageOpener
var storageRegistry = map[string]StorageOpener{} var storageRegistry = map[string]StorageOpener{}
...@@ -46,7 +47,7 @@ func RegisterStorage(scheme string, opener StorageOpener) { ...@@ -46,7 +47,7 @@ func RegisterStorage(scheme string, opener StorageOpener) {
// Storage authors should register their storages with RegisterStorage // Storage authors should register their storages with RegisterStorage
// //
// TODO readonly // TODO readonly
func OpenStorageURL(storageURL string) (IStorage, error) { func OpenStorageURL(ctx context.Context, storageURL string) (IStorage, error) {
// no scheme -> file:// // no scheme -> file://
if !strings.Contains(storageURL, "://") { if !strings.Contains(storageURL, "://") {
storageURL = "file://" + storageURL storageURL = "file://" + storageURL
...@@ -62,5 +63,5 @@ func OpenStorageURL(storageURL string) (IStorage, error) { ...@@ -62,5 +63,5 @@ func OpenStorageURL(storageURL string) (IStorage, error) {
return nil, fmt.Errorf("zodb: URL scheme \"%s://\" not supported", u.Scheme) return nil, fmt.Errorf("zodb: URL scheme \"%s://\" not supported", u.Scheme)
} }
return opener(u) return opener(ctx, u)
} }
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
package fs1 package fs1
import ( import (
"context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
...@@ -663,7 +664,7 @@ func (dh *DataHeader) LoadData(r io.ReaderAt /* *os.File */, buf *[]byte) error ...@@ -663,7 +664,7 @@ func (dh *DataHeader) LoadData(r io.ReaderAt /* *os.File */, buf *[]byte) error
} }
// Open opens FileStorage XXX text // Open opens FileStorage XXX text
func Open(path string) (*FileStorage, error) { func Open(ctx context.Context, path string) (*FileStorage, error) {
fs := &FileStorage{} fs := &FileStorage{}
f, err := os.Open(path) // XXX opens in O_RDONLY f, err := os.Open(path) // XXX opens in O_RDONLY
...@@ -682,7 +683,7 @@ func Open(path string) (*FileStorage, error) { ...@@ -682,7 +683,7 @@ func Open(path string) (*FileStorage, error) {
return nil, fmt.Errorf("%s: invalid magic %q", path, xxx) // XXX err? return nil, fmt.Errorf("%s: invalid magic %q", path, xxx) // XXX err?
} }
// TODO recreate index if missing / not sane // TODO recreate index if missing / not sane (cancel this job on ctx.Done)
// TODO verify index sane / topPos matches // TODO verify index sane / topPos matches
topPos, index, err := LoadIndexFile(path + ".index") topPos, index, err := LoadIndexFile(path + ".index")
if err != nil { if err != nil {
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package fs1 package fs1
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"reflect" "reflect"
...@@ -85,7 +86,7 @@ func checkLoad(t *testing.T, fs *FileStorage, xid zodb.Xid, expect oidLoadedOk) ...@@ -85,7 +86,7 @@ func checkLoad(t *testing.T, fs *FileStorage, xid zodb.Xid, expect oidLoadedOk)
} }
func xfsopen(t testing.TB, path string) *FileStorage { func xfsopen(t testing.TB, path string) *FileStorage {
fs, err := Open(path) fs, err := Open(context.Background(), path)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
...@@ -19,16 +19,17 @@ package fs1 ...@@ -19,16 +19,17 @@ package fs1
// open URL support // open URL support
import ( import (
"context"
"net/url" "net/url"
"../../../zodb" "../../../zodb"
) )
// TODO read-only support // TODO read-only support
func openByURL(u *url.URL) (zodb.IStorage, error) { func openByURL(ctx context.Context, u *url.URL) (zodb.IStorage, error) {
// TODO handle query // TODO handle query
// XXX u.Path is not always raw path - recheck and fix // XXX u.Path is not always raw path - recheck and fix
return Open(u.Host + u.Path) return Open(ctx, u.Host + u.Path)
} }
func init() { func init() {
......
...@@ -19,6 +19,7 @@ package zodbtools ...@@ -19,6 +19,7 @@ package zodbtools
// Catobj - dump content of a database object // Catobj - dump content of a database object
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"io" "io"
...@@ -116,7 +117,7 @@ func catobjMain(argv []string) { ...@@ -116,7 +117,7 @@ func catobjMain(argv []string) {
log.Fatal("only 1 object allowed with -raw") log.Fatal("only 1 object allowed with -raw")
} }
stor, err := zodb.OpenStorageURL(storUrl) // TODO read-only stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
...@@ -37,6 +37,7 @@ txn ... ...@@ -37,6 +37,7 @@ txn ...
package zodbtools package zodbtools
import ( import (
"context"
"crypto/sha1" "crypto/sha1"
"flag" "flag"
"fmt" "fmt"
...@@ -245,7 +246,7 @@ func dumpMain(argv []string) { ...@@ -245,7 +246,7 @@ func dumpMain(argv []string) {
log.Fatal(err) // XXX recheck log.Fatal(err) // XXX recheck
} }
stor, err := zodb.OpenStorageURL(storUrl) // TODO read-only stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
...@@ -21,6 +21,7 @@ package zodbtools ...@@ -21,6 +21,7 @@ package zodbtools
import ( import (
"bytes" "bytes"
"context"
"io/ioutil" "io/ioutil"
"regexp" "regexp"
"testing" "testing"
...@@ -72,7 +73,7 @@ func loadZdumpPy(t *testing.T, path string) string { ...@@ -72,7 +73,7 @@ func loadZdumpPy(t *testing.T, path string) string {
func withTestdata1Fs(t testing.TB, f func(fs *fs1.FileStorage)) { func withTestdata1Fs(t testing.TB, f func(fs *fs1.FileStorage)) {
// XXX -> zodb.OpenURL // XXX -> zodb.OpenURL
fs, err := fs1.Open("../../zodb/storage/fs1/testdata/1.fs") // XXX read-only, path? fs, err := fs1.Open(context.Background(), "../../zodb/storage/fs1/testdata/1.fs") // XXX read-only, path?
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
package zodbtools package zodbtools
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"io" "io"
...@@ -113,7 +114,7 @@ func infoMain(argv []string) { ...@@ -113,7 +114,7 @@ func infoMain(argv []string) {
} }
storUrl := argv[0] storUrl := argv[0]
stor, err := zodb.OpenStorageURL(storUrl) // TODO read-only stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment