/* --- KeyValue functions --- */
// Create a new key/value
970049
func NewKeyValue(host *Host, name string) (*KeyValue, error) {
	kv := &KeyValue{host, name}
	query := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s %s, %s %s)", name, keyCol, defaultStringType, valCol, defaultStringType)
	if _, err := kv.host.db.Exec(query); err != nil {
		return nil, err
	}
	if Verbose {
		log.Println("Created table " + name + " in database " + host.dbname)
	}
	return kv, nil

}
661247
func (r *KeyResource) Create(key, title string) (*Key, error) {

	in := Key{ Title: title, Key: key }
	out := Key{ }
	const path = "/user/keys"
	if err := r.client.do("POST", path, &in, &out); err != nil {
		return nil, err
	}

	return &out, nil
}
1300168
func (u *Unit) NewValue(value int64, explicitSign Sign) (*Value, error) {
	if (value < 0 && explicitSign == Positive) || (value > 0 && explicitSign == Negative) {
		return nil, errors.New("Invalid value/explicitSign combination")
	}

	return &Value{
		unit:         u,
		Value:        value,
		ExplicitSign: explicitSign,
	}, nil
}
132006
func (e *RegisteredEntity) KeyFieldValues(entity DomainObject) map[string]FieldValue {
	v := reflect.ValueOf(entity).Elem()
	fieldValues := make(map[string]FieldValue)

	// populate partition key values
	for _, pk := range e.table.Key.PartitionKeys {
		fieldName := e.table.ColToField[pk]
		value := v.FieldByName(fieldName)
		fieldValues[pk] = value.Interface()
	}

	// populate clustering key values
	for _, ck := range e.table.Key.ClusteringKeys {
		fieldName := e.table.ColToField[ck.Name]
		value := v.FieldByName(fieldName)
		if !value.IsValid() {
			// this should never happen
			panic("Field " + fieldName + " is not a valid field for " + e.table.StructName)
		}
		fieldValues[ck.Name] = value.Interface()
	}

	return fieldValues
}
802542
func encodeKeyValue(k, v string) (string, string) {
	k = strings.ToLower(k)
	if strings.HasSuffix(k, binHdrSuffix) {
		val := base64.StdEncoding.EncodeToString([]byte(v))
		v = string(val)
	}
	return k, v
}
1033595
func (k *KeyValueStringSlice) Put(key, value string) {
	for i, storedKey := range k.Keys {
		if storedKey == key {
			k.Values[i] = value
			return
		}
	}
	k.Keys = append(k.Keys, key)
	k.Values = append(k.Values, value)
}
990305
func (w *newValueWriter) ComputedKeysMap() map[string]bool {
	w.once.Do(w.init)
	return w.computedKeys
}
10079
func NewMemoryKeyValue() KeyValue {
	db := memdb.New(comparer.DefaultComparer, 128)
	return &memKeys{db: db}
}
587660
func (enc *Encoder) EncodeKeyval(key, value interface{}) error {
	enc.scratch.Reset()
	if enc.needSep {
		if _, err := enc.scratch.Write(space); err != nil {
			return err
		}
	}
	if err := writeKey(&enc.scratch, key); err != nil {
		return err
	}
	if _, err := enc.scratch.Write(equals); err != nil {
		return err
	}
	if err := writeValue(&enc.scratch, value); err != nil {
		return err
	}
	_, err := enc.w.Write(enc.scratch.Bytes())
	enc.needSep = true
	return err
}
618964
func NewArgs(initialArgs ...KeyValuePair) Args {
	args := Args{fields: map[string]map[string]bool{}}
	for _, arg := range initialArgs {
		args.Add(arg.Key, arg.Value)
	}
	return args
}
400619
func encodeKeyValue(w io.Writer, p map[string]string) error {
	for k, v := range p {
		if err := writeKeyValuePair(w, k, v); err != nil {
			return err
		}
	}
	return nil
}
367969
func NewKeyValuesStrings(key string, values ...string) KeyValues {
	iv := make([]interface{}, len(values))
	for i := 0; i < len(values); i++ {
		iv[i] = values[i]
	}
	return KeyValues{Key: key, Values: iv}
}
1328438
func (c *Client) KeyCreate(publicKey string) (*Key, error) {
	params := struct {
		PublicKey string `json:"public_key"`
	}{
		PublicKey: publicKey,
	}
	var keyRes Key
	return &keyRes, c.Post(&keyRes, "/account/keys", params)
}
777731
func newKeyValueFromJSONConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) {
	file := cfg.RequiredString("file")
	if err := cfg.Validate(); err != nil {
		return nil, err
	}
	strictness := opt.DefaultStrict
	if env.IsDev() {
		// Be more strict in dev mode.
		strictness = opt.StrictAll
	}
	opts := &opt.Options{
		// The default is 10,
		// 8 means 2.126% or 1/47th disk check rate,
		// 10 means 0.812% error rate (1/2^(bits/1.44)) or 1/123th disk check rate,
		// 12 means 0.31% or 1/322th disk check rate.
		// TODO(tgulacsi): decide which number is the best here. Till that go with the default.
		Filter: filter.NewBloomFilter(10),
		Strict: strictness,
	}
	db, err := leveldb.OpenFile(file, opts)
	if err != nil {
		return nil, err
	}
	is := &kvis{
		db:       db,
		path:     file,
		opts:     opts,
		readOpts: &opt.ReadOptions{Strict: strictness},
		// On machine crash we want to reindex anyway, and
		// fsyncs may impose great performance penalty.
		writeOpts: &opt.WriteOptions{Sync: false},
	}
	return is, nil
}
1150695
func (w *newValueWriter) init() {
	if w.computedKeys == nil {
		w.computedKeys = make(map[string]bool)
	}
}
1155725
func (w *KeyedValue) init() {
	w.values = make(map[string]interface{})
	w.waiters = make(map[string]*Value)
}
753936
func (w *KeyedValue) Init(k string, v interface{}) {
	w.lock.Lock()
	defer w.lock.Unlock()

	// If we have a waiter, set the value.
	_, val := w.valueWaiter(k)
	if val != nil {
		w.setValue(k, v)
	}
}