當前位置: 首頁>>代碼示例>>Golang>>正文


Golang db.Entity類代碼示例

本文整理匯總了Golang中github.com/contiv/volplugin/db.Entity的典型用法代碼示例。如果您正苦於以下問題:Golang Entity類的具體用法?Golang Entity怎麽用?Golang Entity使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Entity類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: setKey

func setKey(value db.Entity) {
	path, err := value.Path()
	if err != nil {
		panic(err)
	}

	content, err := jsonio.Write(value)
	if err != nil {
		panic(err)
	}

	switch Driver {
	case "etcd":
		client, err := getEtcdClient()
		if err != nil {
			panic(err)
		}

		if _, err := client.Set(context.Background(), fmt.Sprintf("/volplugin/%v", path), string(content), nil); err != nil {
			panic(err)
		}
	case "consul":
		client, err := getConsulClient()
		if err != nil {
			panic(err)
		}

		if _, err := client.KV().Put(&api.KVPair{Key: fmt.Sprintf("volplugin/%v", path), Value: content}, nil); err != nil {
			panic(err)
		}
	}
}
開發者ID:unclejack,項目名稱:volplugin,代碼行數:32,代碼來源:init_test.go

示例2: WatchStop

// WatchStop stops a watch.
func (c *Client) WatchStop(obj db.Entity) error {
	path, err := obj.Path()
	if err != nil {
		return err
	}

	return helpers.WatchStop(c, path, c.watchers, &c.watcherMutex)
}
開發者ID:unclejack,項目名稱:volplugin,代碼行數:9,代碼來源:client.go

示例3: WatchStop

// WatchStop stops a watch for a given object.
func (c *Client) WatchStop(obj db.Entity) error {
	path, err := obj.Path()
	if err != nil {
		return err
	}

	return c.watchStopPath(path)
}
開發者ID:contiv,項目名稱:volplugin,代碼行數:9,代碼來源:client.go

示例4: ListPrefix

// ListPrefix is used to list a subtree of an entity, such as listing volume by policy.
func (c *Client) ListPrefix(prefix string, obj db.Entity) ([]db.Entity, error) {
	resp, err := c.client.Get(context.Background(), c.qualified(path.Join(obj.Prefix(), prefix)), &client.GetOptions{Recursive: true})
	if err != nil {
		return nil, err
	}

	return c.traverse(resp.Node, obj), nil
}
開發者ID:unclejack,項目名稱:volplugin,代碼行數:9,代碼來源:client.go

示例5: Watch

// Watch watches a given object for changes.
func (c *Client) Watch(obj db.Entity) (chan db.Entity, chan error) {
	path, err := obj.Path()
	if err != nil {
		errChan := make(chan error, 1)
		errChan <- err
		return make(chan db.Entity), errChan
	}

	return c.watchPath(obj, path, false)
}
開發者ID:contiv,項目名稱:volplugin,代碼行數:11,代碼來源:client.go

示例6: Watch

// Watch watches a given object for changes.
func (c *Client) Watch(obj db.Entity) (chan db.Entity, chan error) {
	path, err := obj.Path()
	if err != nil {
		errChan := make(chan error, 1)
		errChan <- err
		return nil, errChan
	}

	return helpers.WrapWatch(c, obj, path, false, c.watchers, &c.watcherMutex, c.startWatch)
}
開發者ID:unclejack,項目名稱:volplugin,代碼行數:11,代碼來源:client.go

示例7: setKey

func setKey(value db.Entity) {
	client, err := getEtcdClient()
	if err != nil {
		panic(err)
	}

	path, err := value.Path()
	if err != nil {
		panic(err)
	}

	content, err := jsonio.Write(value)
	if err != nil {
		panic(err)
	}

	if _, err := client.Set(context.Background(), fmt.Sprintf("/volplugin/%v", path), string(content), nil); err != nil {
		panic(err)
	}
}
開發者ID:contiv,項目名稱:volplugin,代碼行數:20,代碼來源:entities_test.go

示例8: traverse

// traverse walks the keyspace and converts anything that looks like an entity
// into an entity and returns it as part of the array.
//
// traverse will log & skip errors to ensure bad data will not break this routine.
func (c *Client) traverse(node *client.Node, obj db.Entity) []db.Entity {
	entities := []db.Entity{}
	if node.Dir {
		for _, inner := range node.Nodes {
			entities = append(entities, c.traverse(inner, obj)...)
		}
	} else {
		copy := obj.Copy()

		doAppend := true

		if err := jsonio.Read(copy, []byte(node.Value)); err != nil {
			// This is kept this way so a buggy policy won't break listing all of them
			logrus.Errorf("Received error retrieving value at path %q during list: %v", node.Key, err)
			doAppend = false
		}

		if err := copy.SetKey(c.trimPath(node.Key)); err != nil {
			logrus.Error(err)
			doAppend = false
		}

		// same here. fire hooks to retrieve the full entity. only log but don't append on error.
		if copy.Hooks().PostGet != nil {
			if err := copy.Hooks().PostGet(c, copy); err != nil {
				logrus.Errorf("Error received trying to run fetch hooks during %q list: %v", node.Key, err)
				doAppend = false
			}
		}

		if doAppend {
			entities = append(entities, copy)
		}
	}

	return entities
}
開發者ID:contiv,項目名稱:volplugin,代碼行數:41,代碼來源:client.go

示例9: Delete

// Delete removes the object from the store.
func (c *Client) Delete(obj db.Entity) error {
	if obj.Hooks().PreDelete != nil {
		if err := obj.Hooks().PreDelete(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	path, err := obj.Path()
	if err != nil {
		return err
	}

	if _, err := c.client.Delete(context.Background(), c.qualified(path), nil); err != nil {
		return errors.EtcdToErrored(err)
	}

	if obj.Hooks().PostDelete != nil {
		if err := obj.Hooks().PostDelete(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	return nil
}
開發者ID:contiv,項目名稱:volplugin,代碼行數:25,代碼來源:client.go

示例10: WatchPrefixStop

// WatchPrefixStop stops a watch.
func (c *Client) WatchPrefixStop(obj db.Entity) error {
	return helpers.WatchStop(c, obj.Prefix(), c.watchers, &c.watcherMutex)
}
開發者ID:unclejack,項目名稱:volplugin,代碼行數:4,代碼來源:client.go

示例11: WatchPrefix

// WatchPrefix watches all items under the given entity's prefix
func (c *Client) WatchPrefix(obj db.Entity) (chan db.Entity, chan error) {
	return helpers.WrapWatch(c, obj, obj.Prefix(), true, c.watchers, &c.watcherMutex, c.startWatch)
}
開發者ID:unclejack,項目名稱:volplugin,代碼行數:4,代碼來源:client.go

示例12: WatchPrefix

// WatchPrefix watches all items under the given entity's prefix
func (c *Client) WatchPrefix(obj db.Entity) (chan db.Entity, chan error) {
	return c.watchPath(obj, obj.Prefix(), true)
}
開發者ID:contiv,項目名稱:volplugin,代碼行數:4,代碼來源:client.go

示例13: WatchPrefixStop

// WatchPrefixStop stops
func (c *Client) WatchPrefixStop(obj db.Entity) error {
	return c.watchStopPath(obj.Prefix())
}
開發者ID:contiv,項目名稱:volplugin,代碼行數:4,代碼來源:client.go

示例14: Get

// Get retrieves the item from etcd's key/value store and then populates obj with its data.
func (c *Client) Get(obj db.Entity) error {
	if obj.Hooks().PreGet != nil {
		if err := obj.Hooks().PreGet(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	path, err := obj.Path()
	if err != nil {
		return err
	}

	resp, err := c.client.Get(context.Background(), c.qualified(path), nil)
	if err != nil {
		return errors.EtcdToErrored(err)
	}

	if err := jsonio.Read(obj, []byte(resp.Node.Value)); err != nil {
		return err
	}

	if err := obj.SetKey(c.trimPath(resp.Node.Key)); err != nil {
		return err
	}

	if obj.Hooks().PostGet != nil {
		if err := obj.Hooks().PostGet(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	return obj.Validate()
}
開發者ID:contiv,項目名稱:volplugin,代碼行數:34,代碼來源:client.go

示例15: Set

// Set takes the object and commits it to the database.
func (c *Client) Set(obj db.Entity) error {
	if err := obj.Validate(); err != nil {
		return err
	}

	if obj.Hooks().PreSet != nil {
		if err := obj.Hooks().PreSet(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	content, err := jsonio.Write(obj)
	if err != nil {
		return err
	}

	path, err := obj.Path()
	if err != nil {
		return err
	}

	if _, err := c.client.Set(context.Background(), c.qualified(path), string(content), nil); err != nil {
		return errors.EtcdToErrored(err)
	}

	if obj.Hooks().PostSet != nil {
		if err := obj.Hooks().PostSet(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	return nil
}
開發者ID:contiv,項目名稱:volplugin,代碼行數:34,代碼來源:client.go


注:本文中的github.com/contiv/volplugin/db.Entity類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。