示例#1
0
func (tv *tableValue) get(context *transactionContext, key interface{}, destination *serverVariable) {
	context.logger.Debug("Executing 'get' on table %s with key %s, prefix %s", tv.table, key, tv.prefix)

	strKey := fmt.Sprint(key)

	// if no prefix, we resolve token
	if len(tv.prefix) == 0 {
		token := nrv.HashToken(strKey)
		if context.token != nil && *context.token != token {
			context.setError("Token conflict: %s!=%s", token, *context.token)
			return
		}
		context.token = &token
	}

	row := &rowValue{
		table:   tv,
		key:     strKey,
		context: context,
	}

	// make sure we get data from db
	row.getRow()

	destination.value = row
}
示例#2
0
func (tv *tableValue) set(context *transactionContext, key interface{}, value serverValue) {
	context.logger.Debug("Executing 'set' on table %s with key %s, prefix %s", tv.table, key, tv.prefix)

	strKey := fmt.Sprint(key)

	// if no prefix, we resolve token
	if len(tv.prefix) == 0 {
		token := nrv.HashToken(strKey)
		if context.token != nil && *context.token != token {
			context.setError("Token conflict: %s!=%s", token, *context.token)
			return
		}
		context.token = &token
	}

	if mapVal, isMap := value.(*mapValue); isMap {
		if !context.dry {
			mapVal.remove("_timestamp")
			mapVal.remove("_key1")
			mapVal.remove("_key2")
			mapVal.remove("_key3")
			mapVal.remove("_key4")

			bytes, err := mapVal.toTransactionValue().Marshall()
			if err != nil {
				context.setError("Couldn't marshall value: %s", err)
				return
			}

			l := len(tv.prefix) + 1
			keys := make([]string, l)
			for i := 0; i < l-1; i++ {
				keys[i] = tv.prefix[i]
			}
			keys[l-1] = fmt.Sprintf("%s", key)

			err = context.storageTrx.Set(tv.table, keys, bytes)
			if err != nil {
				context.setError("Couldn't set value into table: %s", err)
				return
			}
		}

	} else {
		context.setError("Can only store a map into table")
		return
	}
}