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 {
// 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 check/use other url fields
ctx := context.Background() // XXX ok?
storLink, err := Dial(ctx, "tcp", u.Host)
if err != nil {
return nil, err
......
......@@ -210,7 +210,7 @@ func storageMain(argv []string) {
}
// 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 {
log.Fatal(err)
}
......
......@@ -19,13 +19,14 @@ package zodb
// logic to open storages by URL
import (
"context"
"fmt"
"net/url"
"strings"
)
// 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
var storageRegistry = map[string]StorageOpener{}
......@@ -46,7 +47,7 @@ func RegisterStorage(scheme string, opener StorageOpener) {
// Storage authors should register their storages with RegisterStorage
//
// TODO readonly
func OpenStorageURL(storageURL string) (IStorage, error) {
func OpenStorageURL(ctx context.Context, storageURL string) (IStorage, error) {
// no scheme -> file://
if !strings.Contains(storageURL, "://") {
storageURL = "file://" + storageURL
......@@ -62,5 +63,5 @@ func OpenStorageURL(storageURL string) (IStorage, error) {
return nil, fmt.Errorf("zodb: URL scheme \"%s://\" not supported", u.Scheme)
}
return opener(u)
return opener(ctx, u)
}
......@@ -22,6 +22,7 @@
package fs1
import (
"context"
"encoding/binary"
"fmt"
"io"
......@@ -663,7 +664,7 @@ func (dh *DataHeader) LoadData(r io.ReaderAt /* *os.File */, buf *[]byte) error
}
// Open opens FileStorage XXX text
func Open(path string) (*FileStorage, error) {
func Open(ctx context.Context, path string) (*FileStorage, error) {
fs := &FileStorage{}
f, err := os.Open(path) // XXX opens in O_RDONLY
......@@ -682,7 +683,7 @@ func Open(path string) (*FileStorage, error) {
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
topPos, index, err := LoadIndexFile(path + ".index")
if err != nil {
......
......@@ -18,6 +18,7 @@
package fs1
import (
"context"
"fmt"
"io"
"reflect"
......@@ -85,7 +86,7 @@ func checkLoad(t *testing.T, fs *FileStorage, xid zodb.Xid, expect oidLoadedOk)
}
func xfsopen(t testing.TB, path string) *FileStorage {
fs, err := Open(path)
fs, err := Open(context.Background(), path)
if err != nil {
t.Fatal(err)
}
......
......@@ -19,16 +19,17 @@ package fs1
// open URL support
import (
"context"
"net/url"
"../../../zodb"
)
// 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
// 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() {
......
......@@ -19,6 +19,7 @@ package zodbtools
// Catobj - dump content of a database object
import (
"context"
"flag"
"fmt"
"io"
......@@ -116,7 +117,7 @@ func catobjMain(argv []string) {
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 {
log.Fatal(err)
}
......
......@@ -37,6 +37,7 @@ txn ...
package zodbtools
import (
"context"
"crypto/sha1"
"flag"
"fmt"
......@@ -245,7 +246,7 @@ func dumpMain(argv []string) {
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 {
log.Fatal(err)
}
......
......@@ -21,6 +21,7 @@ package zodbtools
import (
"bytes"
"context"
"io/ioutil"
"regexp"
"testing"
......@@ -72,7 +73,7 @@ func loadZdumpPy(t *testing.T, path string) string {
func withTestdata1Fs(t testing.TB, f func(fs *fs1.FileStorage)) {
// 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 {
t.Fatal(err)
}
......
......@@ -20,6 +20,7 @@
package zodbtools
import (
"context"
"flag"
"fmt"
"io"
......@@ -113,7 +114,7 @@ func infoMain(argv []string) {
}
storUrl := argv[0]
stor, err := zodb.OpenStorageURL(storUrl) // TODO read-only
stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil {
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