本文整理匯總了Golang中camlistore/org/pkg/sorted.NewKeyValue函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewKeyValue函數的具體用法?Golang NewKeyValue怎麽用?Golang NewKeyValue使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewKeyValue函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: newIndex
// newIndex returns a new sorted.KeyValue, using either the given config, or the default.
func newIndex(root string, indexConf jsonconfig.Obj) (sorted.KeyValue, error) {
if len(indexConf) > 0 {
return sorted.NewKeyValue(indexConf)
}
return sorted.NewKeyValue(jsonconfig.Obj{
"type": defaultIndexType,
"file": filepath.Join(root, defaultIndexFile),
})
}
示例2: newFromConfig
func newFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (blobserver.Storage, error) {
var (
smallPrefix = conf.RequiredString("smallBlobs")
largePrefix = conf.RequiredString("largeBlobs")
metaConf = conf.RequiredObject("metaIndex")
)
if err := conf.Validate(); err != nil {
return nil, err
}
small, err := ld.GetStorage(smallPrefix)
if err != nil {
return nil, fmt.Errorf("failed to load smallBlobs at %s: %v", smallPrefix, err)
}
large, err := ld.GetStorage(largePrefix)
if err != nil {
return nil, fmt.Errorf("failed to load largeBlobs at %s: %v", largePrefix, err)
}
largeSubber, ok := large.(subFetcherStorage)
if !ok {
return nil, fmt.Errorf("largeBlobs at %q of type %T doesn't support fetching sub-ranges of blobs",
largePrefix, large)
}
meta, err := sorted.NewKeyValue(metaConf)
if err != nil {
return nil, fmt.Errorf("failed to setup blobpacked metaIndex: %v", err)
}
sto := &storage{
small: small,
large: largeSubber,
meta: meta,
}
sto.init()
recoveryMu.Lock()
defer recoveryMu.Unlock()
if recovery {
log.Print("Starting recovery of blobpacked index")
if err := meta.Close(); err != nil {
return nil, err
}
if err := sto.reindex(context.TODO(), func() (sorted.KeyValue, error) {
return sorted.NewKeyValue(metaConf)
}); err != nil {
return nil, err
}
return sto, nil
}
// Check for a weird state: zip files exist, but no metadata about them
// is recorded. This is probably a corrupt state, and the user likely
// wants to recover.
if !sto.anyMeta() && sto.anyZipPacks() {
log.Fatal("Error: blobpacked storage detects non-zero packed zips, but no metadata. Please re-start in recovery mode with -recovery.")
}
return sto, nil
}
示例3: newFromConfig
func newFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
blobPrefix := config.RequiredString("blobSource")
kvConfig := config.RequiredObject("storage")
if err := config.Validate(); err != nil {
return nil, err
}
kv, err := sorted.NewKeyValue(kvConfig)
if err != nil {
return nil, err
}
ix, err := New(kv)
if err != nil {
return nil, err
}
sto, err := ld.GetStorage(blobPrefix)
if err != nil {
ix.Close()
return nil, err
}
ix.BlobSource = sto
// Good enough, for now:
ix.KeyFetcher = ix.BlobSource
return ix, err
}
示例4: newFromConfig
func newFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
blobPrefix := config.RequiredString("blobSource")
kvConfig := config.RequiredObject("storage")
if err := config.Validate(); err != nil {
return nil, err
}
kv, err := sorted.NewKeyValue(kvConfig)
if err != nil {
return nil, err
}
sto, err := ld.GetStorage(blobPrefix)
if err != nil {
return nil, err
}
ix, err := New(kv)
// TODO(mpl): next time we need to do another fix, make a new error
// type that lets us apply the needed fix depending on its value or
// something. For now just one value/fix.
if err == errMissingWholeRef {
// TODO: maybe we don't want to do that automatically. Brad says
// we have to think about the case on GCE/CoreOS in particular.
if err := ix.fixMissingWholeRef(sto); err != nil {
ix.Close()
return nil, fmt.Errorf("could not fix missing wholeRef entries: %v", err)
}
ix, err = New(kv)
}
if err != nil {
return nil, err
}
ix.InitBlobSource(sto)
return ix, err
}
示例5: newFromConfig
func newFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (storage blobserver.Storage, err error) {
var (
origin = config.RequiredString("origin")
cache = config.RequiredString("cache")
kvConf = config.RequiredObject("meta")
maxCacheBytes = config.OptionalInt64("maxCacheBytes", 512<<20)
)
if err := config.Validate(); err != nil {
return nil, err
}
cacheSto, err := ld.GetStorage(cache)
if err != nil {
return nil, err
}
originSto, err := ld.GetStorage(origin)
if err != nil {
return nil, err
}
kv, err := sorted.NewKeyValue(kvConf)
if err != nil {
return nil, err
}
// TODO: enumerate through kv and calculate current size.
// Maybe also even enumerate through cache to see if they match.
// Or even: keep it only in memory and not in kv?
s := &sto{
origin: originSto,
cache: cacheSto,
maxCacheBytes: maxCacheBytes,
kv: kv,
}
return s, nil
}
示例6: newSorted
func newSorted(t *testing.T) (kv sorted.KeyValue, clean func()) {
f, err := ioutil.TempFile("", "sqlite-test")
if err != nil {
t.Fatal(err)
}
db, err := sql.Open("sqlite3", f.Name())
if err != nil {
t.Fatalf("opening test database: %v", err)
}
for _, tableSql := range sqlite.SQLCreateTables() {
do(db, tableSql)
}
do(db, fmt.Sprintf(`REPLACE INTO meta VALUES ('version', '%d')`, sqlite.SchemaVersion()))
kv, err = sorted.NewKeyValue(jsonconfig.Obj{
"type": "sqlite",
"file": f.Name(),
})
if err != nil {
t.Fatal(err)
}
return kv, func() {
kv.Close()
os.Remove(f.Name())
}
}
示例7: newFromConfig
func newFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (blobserver.Storage, error) {
var (
smallPrefix = conf.RequiredString("smallBlobs")
largePrefix = conf.RequiredString("largeBlobs")
metaConf = conf.RequiredObject("metaIndex")
)
if err := conf.Validate(); err != nil {
return nil, err
}
small, err := ld.GetStorage(smallPrefix)
if err != nil {
return nil, fmt.Errorf("failed to load smallBlobs at %s: %v", smallPrefix, err)
}
large, err := ld.GetStorage(largePrefix)
if err != nil {
return nil, fmt.Errorf("failed to load largeBlobs at %s: %v", largePrefix, err)
}
largeSubber, ok := large.(subFetcherStorage)
if !ok {
return nil, fmt.Errorf("largeBlobs at %q of type %T doesn't support fetching sub-ranges of blobs",
largePrefix, large)
}
meta, err := sorted.NewKeyValue(metaConf)
if err != nil {
return nil, fmt.Errorf("failed to setup blobpacked metaIndex: %v", err)
}
sto := &storage{
small: small,
large: largeSubber,
meta: meta,
}
sto.init()
// Check for a weird state: zip files exist, but no metadata about them
// is recorded. This is probably a corrupt state, and the user likely
// wants to recover.
if !sto.anyMeta() && sto.anyZipPacks() {
log.Printf("Warning: blobpacked storage detects non-zero packed zips, but no metadata. Please re-start in recovery mode.")
// TODO: add a recovery mode.
// Old TODO was:
// fail with a "known corrupt" message and refuse to
// start unless in recovery mode (perhaps a new environment
// var? or flag passed down?) using StreamBlobs starting at
// "l:". Could even do it automatically if total size is
// small or fast enough? But that's confusing if it only
// sometimes finishes recovery. We probably want various
// server start-up modes anyway: "check", "recover", "garbage
// collect", "readonly". So might as well introduce that
// concept now.
// TODO: test start-up recovery mode, once it works.
}
return sto, nil
}
示例8: newStorage
// newStorage returns a new storage in path root with the given maxFileSize,
// or defaultMaxFileSize (512MB) if <= 0
func newStorage(root string, maxFileSize int64, indexConf jsonconfig.Obj) (s *storage, err error) {
fi, err := os.Stat(root)
if os.IsNotExist(err) {
return nil, fmt.Errorf("storage root %q doesn't exist", root)
}
if err != nil {
return nil, fmt.Errorf("Failed to stat directory %q: %v", root, err)
}
if !fi.IsDir() {
return nil, fmt.Errorf("storage root %q exists but is not a directory.", root)
}
var index sorted.KeyValue
if len(indexConf) > 0 {
index, err = sorted.NewKeyValue(indexConf)
} else {
index, err = kvfile.NewStorage(filepath.Join(root, indexKV))
}
if err != nil {
return nil, err
}
defer func() {
if err != nil {
index.Close()
}
}()
if maxFileSize <= 0 {
maxFileSize = defaultMaxFileSize
}
// Be consistent with trailing slashes. Makes expvar stats for total
// reads/writes consistent across diskpacked targets, regardless of what
// people put in their low level config.
root = strings.TrimRight(root, `\/`)
s = &storage{
root: root,
index: index,
maxFileSize: maxFileSize,
Generationer: local.NewGenerationer(root),
}
s.mu.Lock()
defer s.mu.Unlock()
if err := s.openAllPacks(); err != nil {
return nil, err
}
if _, _, err := s.StorageGeneration(); err != nil {
return nil, fmt.Errorf("Error initialization generation for %q: %v", root, err)
}
return s, nil
}
示例9: TestMongoKV
// TestMongoKV tests against a real MongoDB instance, using a Docker container.
func TestMongoKV(t *testing.T) {
// SetupMongoContainer may skip or fatal the test if docker isn't found or something goes wrong when setting up the container.
// Thus, no error is returned
containerID, ip := dockertest.SetupMongoContainer(t)
defer containerID.KillRemove(t)
kv, err := sorted.NewKeyValue(jsonconfig.Obj{
"type": "mongo",
"host": ip,
"database": "camlitest",
})
if err != nil {
t.Fatalf("mongo.NewKeyValue = %v", err)
}
kvtest.TestSorted(t, kv)
}
示例10: TestKvfileKV
func TestKvfileKV(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "camlistore-kvfilekv_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
dbname := filepath.Join(tmpDir, "testdb.kvfile")
kv, err := sorted.NewKeyValue(jsonconfig.Obj{
"type": "kv",
"file": dbname,
})
if err != nil {
t.Fatalf("Could not create kvfile sorted kv at %v: %v", dbname, err)
}
kvtest.TestSorted(t, kv)
}
示例11: TestLeveldbKV
func TestLeveldbKV(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "camlistore-leveldbkv_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
dbname := filepath.Join(tmpDir, "testdb.leveldb")
t.Logf("Testing leveldb %q.", dbname)
kv, err := sorted.NewKeyValue(jsonconfig.Obj{
"type": "leveldb",
"file": dbname,
})
if err != nil {
t.Fatalf("Could not create leveldb sorted kv at %v: %v", dbname, err)
}
kvtest.TestSorted(t, kv)
}
示例12: newFromConfig
func newFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (storage blobserver.Storage, err error) {
sto := &nsto{}
invConf := config.RequiredObject("inventory")
masterName := config.RequiredString("storage")
if err := config.Validate(); err != nil {
return nil, err
}
sto.inventory, err = sorted.NewKeyValue(invConf)
if err != nil {
return nil, fmt.Errorf("Invalid 'inventory' configuration: %v", err)
}
sto.master, err = ld.GetStorage(masterName)
if err != nil {
return nil, fmt.Errorf("Invalid 'storage' configuration: %v", err)
}
return sto, nil
}
示例13: TestMySQLKV
// TestMySQLKV tests against a real MySQL instance, using a Docker container.
func TestMySQLKV(t *testing.T) {
dbname := "camlitest_" + osutil.Username()
containerID, ip := dockertest.SetupMySQLContainer(t, dbname)
defer containerID.KillRemove(t)
kv, err := sorted.NewKeyValue(jsonconfig.Obj{
"type": "mysql",
"host": ip + ":3306",
"database": dbname,
"user": dockertest.MySQLUsername,
"password": dockertest.MySQLPassword,
})
if err != nil {
t.Fatalf("mysql.NewKeyValue = %v", err)
}
kvtest.TestSorted(t, kv)
}
示例14: newMongoSorted
func newMongoSorted(t *testing.T) (kv sorted.KeyValue, cleanup func()) {
dbname := "camlitest_" + osutil.Username()
containerID, ip := dockertest.SetupMongoContainer(t)
kv, err := sorted.NewKeyValue(jsonconfig.Obj{
"type": "mongo",
"host": ip,
"database": dbname,
})
if err != nil {
containerID.KillRemove(t)
t.Fatal(err)
}
return kv, func() {
kv.Close()
containerID.KillRemove(t)
}
}
示例15: TestPostgreSQLKV
// TestPostgreSQLKV tests against a real PostgreSQL instance, using a Docker container.
func TestPostgreSQLKV(t *testing.T) {
dbname := "camlitest_" + osutil.Username()
containerID, ip := dockertest.SetupPostgreSQLContainer(t, dbname)
defer containerID.KillRemove(t)
kv, err := sorted.NewKeyValue(jsonconfig.Obj{
"type": "postgres",
"host": ip,
"database": dbname,
"user": dockertest.PostgresUsername,
"password": dockertest.PostgresPassword,
"sslmode": "disable",
})
if err != nil {
t.Fatalf("postgres.NewKeyValue = %v", err)
}
kvtest.TestSorted(t, kv)
}