aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/attributevector.h
blob: 3fbcc9f7ca3d61700a4c306e029ef40a5b6dc98e (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "address_space_usage.h"
#include "changevector.h"
#include "readable_attribute_vector.h"
#include <vespa/fastlib/text/normwordfolder.h>
#include <vespa/searchcommon/attribute/config.h>
#include <vespa/searchcommon/attribute/i_search_context.h>
#include <vespa/searchcommon/attribute/iattributevector.h>
#include <vespa/searchcommon/attribute/search_context_params.h>
#include <vespa/searchcommon/attribute/status.h>
#include <vespa/searchcommon/common/range.h>
#include <vespa/searchcommon/common/undefinedvalues.h>
#include <vespa/searchlib/common/address_space.h>
#include <vespa/searchlib/common/i_compactable_lid_space.h>
#include <vespa/searchlib/common/identifiable.h>
#include <vespa/searchlib/common/rcuvector.h>
#include <vespa/searchlib/queryeval/searchiterator.h>
#include <vespa/vespalib/objects/identifiable.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/fastos/time.h>
#include <cmath>
#include <mutex>
#include <shared_mutex>

class Fast_BufferedFile;
class FastOS_FileInterface;

namespace document {
    class ArithmeticValueUpdate;
    class MapValueUpdate;
    class FieldValue;
}

namespace vespalib {
    class GenericHeader;
}

namespace search {

    template <typename T> class ComponentGuard;
    class AttributeReadGuard;
    class AttributeWriteGuard;
    class AttributeSaver;
    class EnumStoreBase;
    class IAttributeSaveTarget;
    class IDocumentWeightAttribute;
    class QueryTermSimple;
    class QueryTermBase;

    namespace fef {
        class TermFieldMatchData;
    }

    namespace attribute {
        class AttributeHeader;
        class IPostingListSearchContext;
        class IPostingListAttributeBase;
        class Interlock;
        class InterlockGuard;
        class MultiValueMappingBase;
    }

    namespace fileutil {
        class LoadedBuffer;
    }
}

namespace search {


using search::attribute::WeightedType;
using search::attribute::Status;
using document::ArithmeticValueUpdate;
using document::MapValueUpdate;
using document::FieldValue;

template <typename T>
class UnWeightedType
{
public:
    UnWeightedType() : _value(T()) { }
    UnWeightedType(T v) : _value(v) { }
    const T & getValue() const { return _value; }
    void setValue(const T & v) { _value = v; }
    int32_t getWeight()  const { return 1; }
    void setWeight(int32_t w)  { (void) w; }

    bool operator==(const UnWeightedType<T> & rhs) const {
        return _value == rhs._value;
    }

private:
    T       _value;
};

template <typename T>
vespalib::asciistream & operator << (vespalib::asciistream & os, const UnWeightedType<T> & v);

class IExtendAttribute
{
public:
    virtual bool add(int64_t, int32_t = 1) { return false; }
    virtual bool add(double, int32_t = 1) { return false; }
    virtual bool add(const char *, int32_t = 1) { return false; }
    
    virtual ~IExtendAttribute() {}
};

class AttributeVector : public vespalib::Identifiable,
                        public attribute::IAttributeVector,
                        public common::ICompactableLidSpace,
                        public attribute::ReadableAttributeVector
{
protected:
    using Config = search::attribute::Config;
    using CollectionType = search::attribute::CollectionType;
    using BasicType = search::attribute::BasicType;
    using QueryTermSimpleUP = std::unique_ptr<QueryTermSimple>;
    using QueryPacketT = vespalib::stringref;
    using LoadedBufferUP = std::unique_ptr<fileutil::LoadedBuffer>;
public:
    typedef std::shared_ptr<AttributeVector> SP;
    class BaseName : public vespalib::string
    {
    public:
        typedef vespalib::string string;
        BaseName(const vespalib::stringref &s)
            : string(s),
              _name(createAttributeName(s))
        { }
        BaseName & operator = (const vespalib::stringref & s) {
            BaseName n(s);
            std::swap(*this, n);
            return *this;
        }

        BaseName(const vespalib::stringref &base,
                 const vespalib::stringref &snap,
                 const vespalib::stringref &name);
        ~BaseName();

        string getIndexName() const;
        string getSnapshotName() const;
        const string & getAttributeName() const { return _name; }
        string getDirName() const;
    private:
        static string createAttributeName(const vespalib::stringref & s);
        string _name;
    };

    using GenerationHandler = vespalib::GenerationHandler;
    using GenerationHolder = vespalib::GenerationHolder;
    typedef GenerationHandler::generation_t generation_t;

    virtual ~AttributeVector();
protected:
    /**
     * Will update statistics by calling onUpdateStat if necessary.
     */
    void updateStat(bool forceUpdate);

    void
    updateStatistics(uint64_t numValues,
                     uint64_t numUniqueValue,
                     uint64_t allocated,
                     uint64_t used,
                     uint64_t dead,
                     uint64_t onHold);

    void performCompactionWarning();

    void getByType(DocId doc, const char *&v) const {
        char tmp[1024]; v = getString(doc, tmp, sizeof(tmp));
    }

    void getByType(DocId doc, vespalib::string &v) const {
        char tmp[1024]; v = getString(doc, tmp, sizeof(tmp));
    }

    void getByType(DocId doc, largeint_t & v) const {
        v = getInt(doc);
    }

    void getByType(DocId doc, double &v) const {
        v = getFloat(doc);
    }

    uint32_t getByType(DocId doc, const char **v, uint32_t sz) const {
        return get(doc, v, sz);
    }

    uint32_t getByType(DocId doc, vespalib::string *v, uint32_t sz) const {
        return get(doc, v, sz);
    }

    uint32_t getByType(DocId doc, largeint_t * v, uint32_t sz) const {
        return get(doc, v, sz);
    }

    uint32_t getByType(DocId doc, double *v, uint32_t sz) const {
        return get(doc, v, sz);
    }


    AttributeVector(const vespalib::stringref &baseFileName, const Config & c);

    void checkSetMaxValueCount(int index) {
        _highestValueCount = std::max(index, _highestValueCount);
    }

    void setEnumMax(uint32_t e)          { _enumMax = e; setEnum(); }
    void setEnum(bool hasEnum_=true)     { _hasEnum = hasEnum_; }
    void setSortedEnum(bool sorted=true) { _hasSortedEnum = sorted; }
    void setNumDocs(uint32_t n)          { _status.setNumDocs(n); }
    void incNumDocs()                    { _status.incNumDocs(); }

    LoadedBufferUP loadDAT();
    LoadedBufferUP loadIDX();
    LoadedBufferUP loadWeight();
    LoadedBufferUP loadUDAT();

    class ValueModifier
    {
    public:
        ValueModifier(AttributeVector &attr);
        ValueModifier(const ValueModifier &rhs);
        ~ValueModifier();
    private:
        AttributeVector * stealAttr() const {
            AttributeVector * ret(_attr);
            _attr = NULL;
            return ret;
        }

        mutable AttributeVector * _attr;
    };

    class EnumModifier
    {
        std::unique_lock<std::shared_timed_mutex> _enumLock;
    public:
        EnumModifier(std::shared_timed_mutex &lock,
                     attribute::InterlockGuard &interlockGuard)
            : _enumLock(lock)
        {
            (void) interlockGuard;
        }
        EnumModifier(EnumModifier &&rhs)
            : _enumLock(std::move(rhs._enumLock))
        { }
        EnumModifier &operator=(EnumModifier &&rhs)
        {
            _enumLock = std::move(rhs._enumLock);
            return *this;
        }
        virtual ~EnumModifier() { }
    };

    EnumModifier getEnumModifier();
    ValueModifier getValueModifier() { return ValueModifier(*this); }

    void updateUncommittedDocIdLimit(DocId doc) {
        if (_uncommittedDocIdLimit <= doc)  {
            _uncommittedDocIdLimit = doc + 1;
        }
    }

    void updateCommittedDocIdLimit() {
        if (_uncommittedDocIdLimit != 0) {
            if (_uncommittedDocIdLimit > _committedDocIdLimit) {
                std::atomic_thread_fence(std::memory_order_release);
                _committedDocIdLimit = _uncommittedDocIdLimit;
            }
            _uncommittedDocIdLimit = 0;
        }
    }
    
public:
    std::unique_ptr<FastOS_FileInterface> openDAT();
    std::unique_ptr<FastOS_FileInterface> openIDX();
    std::unique_ptr<FastOS_FileInterface> openWeight();
    std::unique_ptr<FastOS_FileInterface> openUDAT();
    void incGeneration();
    void removeAllOldGenerations();

    generation_t getFirstUsedGeneration() const {
        return _genHandler.getFirstUsedGeneration();
    }

    generation_t getCurrentGeneration() const {
        return _genHandler.getCurrentGeneration();
    }

    /**
     * Used for unit testing. Must not be called from the thread owning the enum guard(s).
     */
    bool hasActiveEnumGuards();

    virtual IExtendAttribute * getExtendInterface();

protected:
    /**
     * Called when a new document has been added, but only for
     * multivalue, enumerated, and string attributes.
     * Can be overridden by subclasses that need to resize structures as a result of this.
     * Should return true if underlying structures were resized.
     **/
    virtual bool onAddDoc(DocId) { return false; }

    /**
     * Returns the number of readers holding a generation guard.
     * Should be called by the writer thread.
     */
    uint32_t getGenerationRefCount(generation_t gen) const {
        return _genHandler.getGenerationRefCount(gen);
    }

    const GenerationHandler & getGenerationHandler() const {
        return _genHandler;
    }

    GenerationHandler & getGenerationHandler() {
        return _genHandler;
    }

    GenerationHolder & getGenerationHolder() {
        return _genHolder;
    }

    template<typename T>
    bool clearDoc(ChangeVectorT< ChangeTemplate<T> > &changes, DocId doc);

    template<typename T>
    bool update(ChangeVectorT< ChangeTemplate<T> > &changes, DocId doc, const T & v) __attribute__((noinline));

    template<typename T>
    bool append(ChangeVectorT< ChangeTemplate<T> > &changes, DocId doc, const T &v, int32_t w, bool doCount = true) __attribute__((noinline));
    template<typename T, typename Accessor>
    bool append(ChangeVectorT< ChangeTemplate<T> > &changes, DocId doc, Accessor & ac) __attribute__((noinline));

    template<typename T>
    bool remove(ChangeVectorT< ChangeTemplate<T> > & changes, DocId doc, const T &v, int32_t w);

    template<typename T>
    bool adjustWeight(ChangeVectorT< ChangeTemplate<T> > &changes, DocId doc, const T &v, const ArithmeticValueUpdate &wd);

    template <typename T>
    static int32_t
    applyWeightChange(int32_t weight, const ChangeTemplate<T> &weightChange) {
        if (weightChange._type == ChangeBase::INCREASEWEIGHT) {
            return weight + weightChange._weight;
        } else if (weightChange._type == ChangeBase::MULWEIGHT) {
            return weight * weightChange._weight;
        } else if (weightChange._type == ChangeBase::DIVWEIGHT) {
            return weight / weightChange._weight;
        }
        return weight;
    }

    template<typename T>
    bool applyArithmetic(ChangeVectorT< ChangeTemplate<T> > &changes, DocId doc, const T &v, const ArithmeticValueUpdate & arithm);

    static double round(double v, double & r) { return r = v; }
    static largeint_t round(double v, largeint_t &r) { return r = static_cast<largeint_t>(::floor(v+0.5)); }

    template <typename BaseType, typename ChangeData>
    static BaseType
    applyArithmetic(const BaseType &value,
                    const ChangeTemplate<ChangeData> & arithmetic)
    {
        typedef typename ChangeData::DataType LargeType;
        if (attribute::isUndefined(value)) {
            return value;
        } else if (arithmetic._type == ChangeBase::ADD) {
            return value + static_cast<LargeType>(arithmetic._arithOperand);
        } else if (arithmetic._type == ChangeBase::SUB) {
            return value - static_cast<LargeType>(arithmetic._arithOperand);
        } else if (arithmetic._type == ChangeBase::MUL) {
            LargeType r;
            return round((static_cast<double>(value) *
                          arithmetic._arithOperand), r);
        } else if (arithmetic._type == ChangeBase::DIV) {
            LargeType r;
            return round(static_cast<double>(value) /
                         arithmetic._arithOperand, r);
        }
        return value;
    }

    virtual AddressSpace getEnumStoreAddressSpaceUsage() const;
    virtual AddressSpace getMultiValueAddressSpaceUsage() const;

public:
    DECLARE_IDENTIFIABLE_ABSTRACT(AttributeVector);
    bool isLoaded() const { return _loaded; }

    /** Return the fixed length of the attribute. If 0 then you must inquire each document. */
    virtual size_t getFixedWidth() const override;
    const Config &getConfig() const { return _config; }
    BasicType getInternalBasicType() const { return _config.basicType(); }
    CollectionType getInternalCollectionType() const { return _config.collectionType(); }
    const BaseName & getBaseFileName() const { return _baseFileName; }
    void setBaseFileName(const vespalib::stringref & name) { _baseFileName = name; }

    // Implements IAttributeVector
    virtual const vespalib::string & getName() const override;

    bool hasArrayType() const { return _config.collectionType().isArray(); }
    bool hasEnum() const override;
    bool hasSortedEnum() const { return _hasSortedEnum; }
    virtual bool hasEnum2Value() const;
    uint32_t getMaxValueCount() const override;
    uint32_t getEnumMax() const { return _enumMax; }

    // Implements IAttributeVector
    uint32_t getNumDocs() const override;
    uint32_t & getCommittedDocIdLimitRef() { return _committedDocIdLimit; }
    void setCommittedDocIdLimit(uint32_t committedDocIdLimit) {
        _committedDocIdLimit = committedDocIdLimit;
    }

    const Status & getStatus() const { return _status; }
    Status & getStatus() { return _status; }

    AddressSpaceUsage getAddressSpaceUsage() const;

    // Implements IAttributeVector
    virtual BasicType::Type getBasicType() const override;
    virtual CollectionType::Type getCollectionType() const override;
    virtual bool getIsFilter() const override;
    virtual bool getIsFastSearch() const override;
    virtual uint32_t getCommittedDocIdLimit() const override;
    virtual bool isImported() const override;

    /**
     * Updates the base file name of this attribute vector and saves
     * it to file(s)
     */
    bool saveAs(const vespalib::stringref &baseFileName);

    /**
     * Updates the base file name of this attribute vector and saves
     * it using the given saveTarget
     */
    bool saveAs(const vespalib::stringref &baseFileName, IAttributeSaveTarget &saveTarget);

    /** Saves this attribute vector to file(s) **/
    bool save();

    /** Saves this attribute vector using the given saveTarget **/
    bool save(IAttributeSaveTarget & saveTarget);

    attribute::AttributeHeader createAttributeHeader() const;

    /** Returns whether this attribute has load data files on disk **/
    bool hasLoadData() const;

    bool isEnumeratedSaveFormat() const;
    bool load();
    void commit(bool forceStatUpdate = false);
    void commit(uint64_t firstSyncToken, uint64_t lastSyncToken);
    void setCreateSerialNum(uint64_t createSerialNum);
    uint64_t getCreateSerialNum() const;
    virtual uint32_t getVersion() const;

////// Interface to access single documents.
    /**
     * Interface to access the individual elements both for update and
     * retrival are type specific.  They are accessed by their proper
     * type.
     */

    virtual uint32_t clearDoc(DocId doc) = 0;
    virtual largeint_t getDefaultValue() const = 0;
    virtual void getEnumValue(const EnumHandle *v, uint32_t *e, uint32_t sz) const = 0;

    uint32_t getEnumValue(EnumHandle eh) const {
        uint32_t e(0);
        getEnumValue(&eh, &e, 1);
        return e;
    }

    // Implements IAttributeVector
    virtual uint32_t get(DocId doc, EnumHandle *v, uint32_t sz) const override = 0;
    virtual uint32_t get(DocId doc, const char **v, uint32_t sz) const override = 0;
    virtual uint32_t get(DocId doc, largeint_t *v, uint32_t sz) const override = 0;
    virtual uint32_t get(DocId doc, double *v, uint32_t sz) const override = 0;

    virtual uint32_t get(DocId doc, vespalib::string *v, uint32_t sz) const = 0;


    // Implements IAttributeVector
    virtual uint32_t get(DocId doc, WeightedEnum *v, uint32_t sz) const override = 0;
    virtual uint32_t get(DocId doc, WeightedString *v, uint32_t sz) const override = 0;
    virtual uint32_t get(DocId doc, WeightedConstChar *v, uint32_t sz) const override = 0;
    virtual uint32_t get(DocId doc, WeightedInt *v, uint32_t sz) const override = 0;
    virtual uint32_t get(DocId doc, WeightedFloat *v, uint32_t sz) const override = 0;

    virtual int32_t getWeight(DocId doc, uint32_t idx) const;

    // Implements IAttributeVector
    bool findEnum(const char *value, EnumHandle &e) const override;
    const char * getStringFromEnum(EnumHandle e) const override;

///// Modify API
    virtual void onCommit() = 0;
    virtual bool addDoc(DocId &doc) = 0;
    virtual bool addDocs(DocId & startDoc, DocId & lastDoc, uint32_t numDocs);
    virtual bool addDocs(uint32_t numDocs);
    bool apply(DocId doc, const MapValueUpdate &map);

////// Search API

    // type-safe down-cast to attribute supporting direct document weight iterators
    const IDocumentWeightAttribute *asDocumentWeightAttribute() const override;

    const tensor::ITensorAttribute *asTensorAttribute() const override;

    /**
       - Search for equality
       - Range search
    */

    class SearchContext : public attribute::ISearchContext
    {
        template <class SC> friend class AttributeIteratorT;
        template <class SC> friend class FilterAttributeIteratorT;
        template <class PL> friend class AttributePostingListIteratorT;
        template <class PL> friend class FilterAttributePostingListIteratorT;
    protected:
        using QueryTermSimpleUP = std::unique_ptr<QueryTermSimple>;
    public:
        SearchContext(const SearchContext &) = delete;
        SearchContext & operator = (const SearchContext &) = delete;

        typedef std::unique_ptr<SearchContext> UP;
        ~SearchContext();

        // Implements attribute::ISearchContext
        unsigned int approximateHits() const override;
        queryeval::SearchIterator::UP createIterator(fef::TermFieldMatchData *matchData, bool strict) override;
        void fetchPostings(bool strict, const BitVector * filter) override;
        bool valid() const override { return false; }
        Int64Range getAsIntegerTerm() const override { return Int64Range(); }
        const QueryTermBase &queryTerm() const override {
            return *static_cast<const QueryTermBase *>(NULL);
        }
        const vespalib::string &attributeName() const override {
            return _attr.getName();
        }

        const AttributeVector & attribute() const { return _attr; }

    protected:
        SearchContext(const AttributeVector &attr);
        const AttributeVector & _attr;

        attribute::IPostingListSearchContext *_plsc;

        /**
         * Creates an attribute search iterator associated with this
         * search context. Postings lists are not used.
         **/
        virtual queryeval::SearchIterator::UP createFilterIterator(fef::TermFieldMatchData *matchData, bool strict);

        bool getIsFilter() const { return _attr.getConfig().getIsFilter(); }
    };

    SearchContext::UP getSearch(QueryPacketT searchSpec, const attribute::SearchContextParams &params) const;
     attribute::ISearchContext::UP createSearchContext(QueryTermSimpleUP term,
                                                       const attribute::SearchContextParams &params) const override;
    virtual SearchContext::UP getSearch(QueryTermSimpleUP term, const attribute::SearchContextParams &params) const = 0;
    virtual const EnumStoreBase *getEnumStoreBase() const;
    virtual const attribute::MultiValueMappingBase *getMultiValueBase() const;
private:
    /**
     * This is called before adding docs will commence.
     * The attributes can then ensure space is available to avoid reallocation as it grows.
     * @param docIdLimit
     */
    virtual void onAddDocs(DocId docIdLimit) = 0;
    void divideByZeroWarning();
    virtual bool applyWeight(DocId doc, const FieldValue &fv, const ArithmeticValueUpdate &wAdjust);
    virtual void onSave(IAttributeSaveTarget & saveTarget);
    virtual bool onLoad();
    std::unique_ptr<FastOS_FileInterface> openFile(const char *suffix);
    LoadedBufferUP loadFile(const char *suffix);


    BaseName               _baseFileName;
    Config                 _config;
    std::shared_ptr<attribute::Interlock> _interlock;
    mutable std::shared_timed_mutex _enumLock;
    GenerationHandler      _genHandler;
    GenerationHolder       _genHolder;
    Status                 _status;
    int                    _highestValueCount;
    uint32_t               _enumMax;
    uint32_t               _committedDocIdLimit; // docid limit for search
    uint32_t               _uncommittedDocIdLimit; // based on queued changes
    uint64_t               _createSerialNum;
    uint64_t               _compactLidSpaceGeneration; 
    bool                   _hasEnum;
    bool                   _hasSortedEnum;
    bool                   _loaded;
    bool                   _enableEnumeratedSave;
    fastos::TimeStamp      _nextStatUpdateTime;

////// Locking strategy interface. only available from the Guards.
    /**
     * Used to guard that a value you reference will always reference
     * a value. It might not be the same value, but at least it will
     * be a value for that document.  The guarantee holds as long as
     * the guard is alive.
    */
    GenerationHandler::Guard takeGenerationGuard() { return _genHandler.takeGuard(); }

    /// Clean up [0, firstUsed>
    virtual void removeOldGenerations(generation_t firstUsed);
    virtual void onGenerationChange(generation_t generation);
    virtual void onUpdateStat() = 0;
    /**
     * Used to regulate access to critical resources. Apply the
     * reader/writer guards.
     */
    std::shared_timed_mutex & getEnumLock() { return _enumLock; }

    friend class ComponentGuard<AttributeVector>;
    friend class AttributeValueGuard;
    friend class AttributeTest;
    friend class AttributeManagerTest;
public:
    bool headerTypeOK(const vespalib::GenericHeader &header) const;
    bool hasMultiValue() const override;
    bool hasWeightedSetType() const override;
    /**
     * Should be called by the writer thread.
     */
    void updateFirstUsedGeneration() {
        _genHandler.updateFirstUsedGeneration();
    }

    /**
     * Returns true if we might still have readers.  False positives
     * are possible if writer hasn't updated first used generation
     * after last reader left.
     */
    bool hasReaders() const { return _genHandler.hasReaders(); }

    /**
     * Add reserved initial document with docId 0 and undefined value.
     */
    void addReservedDoc();
    void enableEnumeratedSave(bool enable = true);
    bool getEnumeratedSave() const { return _hasEnum && _enableEnumeratedSave; }

    virtual attribute::IPostingListAttributeBase * getIPostingListAttributeBase();
    virtual const attribute::IPostingListAttributeBase * getIPostingListAttributeBase() const;
    bool hasPostings();
    virtual uint64_t getUniqueValueCount() const;
    virtual uint64_t getTotalValueCount() const;
    void compactLidSpace(uint32_t wantedLidLimit) override;
    virtual void clearDocs(DocId lidLow, DocId lidLimit);
    bool wantShrinkLidSpace() const { return _committedDocIdLimit < getNumDocs(); }
    bool canShrinkLidSpace() const override;
    void shrinkLidSpace() override;
    virtual void onShrinkLidSpace();
    size_t getEstimatedShrinkLidSpaceGain() const override;

    std::unique_ptr<attribute::AttributeReadGuard> makeReadGuard(bool stableEnumGuard) const override;

    void setInterlock(const std::shared_ptr<attribute::Interlock> &interlock);

    const std::shared_ptr<attribute::Interlock> &getInterlock() const {
        return _interlock;
    }

    std::unique_ptr<AttributeSaver> initSave();

    virtual std::unique_ptr<AttributeSaver> onInitSave();
    virtual uint64_t getEstimatedSaveByteSize() const;

    static bool isEnumerated(const vespalib::GenericHeader &header);

    virtual MemoryUsage getChangeVectorMemoryUsage() const;
};

}