aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/update/fieldpathupdate.h
blob: cbe2e2f5ec21c2d3b93e8cf65ca091c95bd5d4bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "updatevisitor.h"
#include <vespa/document/base/fieldpath.h>
#include <vespa/document/util/identifiableid.h>

namespace document {

class DocumentTypeRepo;
class Field;
class FieldValue;
class BucketIdFactory;
class Document;
class DataType;

namespace select { class Node; }
namespace fieldvalue { class IteratorHandler; }

class FieldPathUpdate
{
public:
    using nbostream = vespalib::nbostream;
    using stringref = vespalib::stringref;

    enum FieldPathUpdateType {
        Add    = IDENTIFIABLE_CLASSID(AddFieldPathUpdate),
        Assign = IDENTIFIABLE_CLASSID(AssignFieldPathUpdate),
        Remove = IDENTIFIABLE_CLASSID(RemoveFieldPathUpdate)
    };

    virtual ~FieldPathUpdate();

    FieldPathUpdateType type() const noexcept { return _type; }
    void applyTo(Document& doc) const;

    virtual bool operator==(const FieldPathUpdate& other) const;
    bool operator!=(const FieldPathUpdate& other) const {
        return ! (*this == other);
    }

    const vespalib::string& getOriginalFieldPath() const { return _originalFieldPath; }
    const vespalib::string& getOriginalWhereClause() const { return _originalWhereClause; }

    /**
     * Check that a given field value is of the type inferred by
     * the field path.
     * @throws IllegalArgumentException upon datatype mismatch.
     */
    void checkCompatibility(const FieldValue& fv, const DataType & type) const;

    virtual void print(std::ostream& out, bool verbose, const std::string& indent) const = 0;

    virtual void accept(UpdateVisitor &visitor) const = 0;
    virtual uint8_t getSerializedType() const = 0;

    /** Deserializes and creates a new FieldPathUpdate instance.
     * Requires type id to be not yet consumed.
     */
    static std::unique_ptr<FieldPathUpdate> createInstance(const DocumentTypeRepo& repo, const DataType &type, nbostream & stream);

protected:
    /** To be used for deserialization */
    FieldPathUpdate(FieldPathUpdateType type) noexcept;
    FieldPathUpdate(const FieldPathUpdate &);
    FieldPathUpdate & operator =(const FieldPathUpdate &);

    static stringref getString(nbostream & stream);
    FieldPathUpdate(FieldPathUpdateType type, stringref fieldPath, stringref whereClause = stringref());

    virtual void deserialize(const DocumentTypeRepo& repo, const DataType& type, nbostream & stream);

    /** @return the datatype of the last path element in the field path */
    const DataType& getResultingDataType(const FieldPath & path) const;
    enum SerializedMagic {AssignMagic=0, RemoveMagic=1, AddMagic=2};
private:
    // TODO: rename to createIteratorHandler?
    virtual std::unique_ptr<fieldvalue::IteratorHandler> getIteratorHandler(Document& doc, const DocumentTypeRepo & repo) const = 0;

    FieldPathUpdateType _type;
    vespalib::string    _originalFieldPath;
    vespalib::string    _originalWhereClause;
};

}