summaryrefslogtreecommitdiffstats
path: root/vespalib/src
diff options
context:
space:
mode:
authorGeir Storli <geirstorli@yahoo.no>2016-09-02 14:49:40 +0200
committerGitHub <noreply@github.com>2016-09-02 14:49:40 +0200
commitde4551ec3f374e60566c8263898c69c940c721fa (patch)
treedea86a2ea6af933c1a017a6ba51f648b5e84894c /vespalib/src
parent18bfb62169690d3af7b23c218d64fce8b1a7a8c2 (diff)
parent6988803754f63ffac4c8175ce5dd3d8f725a1a6b (diff)
Merge pull request #531 from yahoo/havardpe/verify-rank-setup-with-rank-constants
Havardpe/verify rank setup with rank constants
Diffstat (limited to 'vespalib/src')
-rw-r--r--vespalib/src/vespa/vespalib/eval/value_cache/constant_tensor_loader.cpp29
-rw-r--r--vespalib/src/vespa/vespalib/eval/value_cache/constant_value.h15
2 files changed, 23 insertions, 21 deletions
diff --git a/vespalib/src/vespa/vespalib/eval/value_cache/constant_tensor_loader.cpp b/vespalib/src/vespa/vespalib/eval/value_cache/constant_tensor_loader.cpp
index a44a1e0ea53..501171083b4 100644
--- a/vespalib/src/vespa/vespalib/eval/value_cache/constant_tensor_loader.cpp
+++ b/vespalib/src/vespa/vespalib/eval/value_cache/constant_tensor_loader.cpp
@@ -66,42 +66,28 @@ struct AddressExtractor : ObjectTraverser {
}
};
-struct LoadError : ConstantValue {
- ValueType my_type;
- ErrorValue my_value;
- LoadError() : my_type(ValueType::error_type()), my_value() {}
- const ValueType &type() const override { return my_type; }
- const Value &value() const override { return my_value; }
-};
-
-struct TensorConstant : ConstantValue {
- ValueType my_type;
- TensorValue my_value;
- TensorConstant(std::unique_ptr<Tensor> tensor)
- : my_type(tensor->engine().type_of(*tensor)), my_value(std::move(tensor)) {}
- const ValueType &type() const override { return my_type; }
- const Value &value() const override { return my_value; }
-};
-
} // namespace vespalib::eval::<unnamed>
+using ErrorConstant = SimpleConstantValue<ErrorValue>;
+using TensorConstant = SimpleConstantValue<TensorValue>;
+
ConstantValue::UP
ConstantTensorLoader::create(const vespalib::string &path, const vespalib::string &type) const
{
ValueType value_type = ValueType::from_spec(type);
if (value_type.is_error()) {
LOG(warning, "invalid type specification: %s", type.c_str());
- return std::make_unique<LoadError>();
+ return std::make_unique<ErrorConstant>(ValueType::error_type());
}
File file(path);
if (!file.valid()) {
LOG(warning, "could not read file: %s", path.c_str());
- return std::make_unique<LoadError>();
+ return std::make_unique<ErrorConstant>(ValueType::error_type());
}
Slime slime;
if (slime::JsonFormat::decode(Memory(file.data, file.size), slime) == 0) {
LOG(warning, "file contains invalid json: %s", path.c_str());
- return std::make_unique<LoadError>();
+ return std::make_unique<ErrorConstant>(ValueType::error_type());
}
std::set<vespalib::string> indexed;
for (const auto &dimension: value_type.dimensions()) {
@@ -117,7 +103,8 @@ ConstantTensorLoader::create(const vespalib::string &path, const vespalib::strin
cells[i]["address"].traverse(extractor);
spec.add(address, cells[i]["value"].asDouble());
}
- return std::make_unique<TensorConstant>(_engine.create(spec));
+ auto tensor = _engine.create(spec);
+ return std::make_unique<TensorConstant>(_engine.type_of(*tensor), std::move(tensor));
}
} // namespace vespalib::eval
diff --git a/vespalib/src/vespa/vespalib/eval/value_cache/constant_value.h b/vespalib/src/vespa/vespalib/eval/value_cache/constant_value.h
index fbdb32ede88..570276a50ab 100644
--- a/vespalib/src/vespa/vespalib/eval/value_cache/constant_value.h
+++ b/vespalib/src/vespa/vespalib/eval/value_cache/constant_value.h
@@ -22,6 +22,21 @@ struct ConstantValue {
};
/**
+ * A simple implementation of a constant value that bundles together a
+ * ValueType instance with a specific Value subclass instance.
+ **/
+template <typename VALUE>
+struct SimpleConstantValue : ConstantValue {
+ ValueType my_type;
+ VALUE my_value;
+ template <typename... Args>
+ SimpleConstantValue(const ValueType &type_in, Args &&...args)
+ : my_type(type_in), my_value(std::forward<Args>(args)...) {}
+ const ValueType &type() const override { return my_type; }
+ const Value &value() const override { return my_value; }
+};
+
+/**
* An abstract factory of constant values. The typical use-case for
* this will be to load constant values from file with a cache on top
* to share constants among users.