summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/attributevector.cpp
blob: 4c3134d3235b490b6c1844f5bb8c5a0b379b7d12 (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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "address_space_components.h"
#include "attribute_read_guard.h"
#include "attributefilesavetarget.h"
#include "attributeiterators.hpp"
#include "attributesaver.h"
#include "attributevector.h"
#include "attributevector.hpp"
#include "floatbase.h"
#include "interlock.h"
#include "ipostinglistattributebase.h"
#include "ipostinglistsearchcontext.h"
#include "stringbase.h"
#include <vespa/document/update/assignvalueupdate.h>
#include <vespa/document/update/mapvalueupdate.h>
#include <vespa/fastlib/io/bufferedfile.h>
#include <vespa/searchcommon/attribute/attribute_utils.h>
#include <vespa/searchlib/common/tunefileinfo.h>
#include <vespa/searchlib/index/dummyfileheadercontext.h>
#include <vespa/searchlib/query/query_term_decoder.h>
#include <vespa/searchlib/queryeval/emptysearch.h>
#include <vespa/searchlib/util/file_settings.h>
#include <vespa/searchlib/util/logutil.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/mmap_file_allocator_factory.h>
#include <vespa/vespalib/util/size_literals.h>
#include <thread>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.attribute.attributevector");

using vespalib::getLastErrorString;

using document::ValueUpdate;
using document::AssignValueUpdate;
using vespalib::make_string;
using vespalib::Array;
using vespalib::IllegalStateException;
using search::attribute::SearchContextParams;
using search::common::FileHeaderContext;
using search::index::DummyFileHeaderContext;
using search::queryeval::SearchIterator;

namespace {

const vespalib::string enumeratedTag = "enumerated";
const vespalib::string dataTypeTag = "datatype";
const vespalib::string collectionTypeTag = "collectiontype";
const vespalib::string docIdLimitTag = "docIdLimit";

}

namespace search {

IMPLEMENT_IDENTIFIABLE_ABSTRACT(AttributeVector, vespalib::Identifiable);

AttributeVector::BaseName::BaseName(vespalib::stringref base, vespalib::stringref name)
    : string(base),
      _name(name)
{
    if (!empty()) {
        push_back('/');
    }
    append(name);
}

AttributeVector::BaseName::~BaseName() = default;


AttributeVector::BaseName::string
AttributeVector::BaseName::createAttributeName(vespalib::stringref s)
{
    size_t p(s.rfind('/'));
    if (p == string::npos) {
       return s;
    } else {
        return s.substr(p+1);
    }
}


AttributeVector::BaseName::string
AttributeVector::BaseName::getDirName() const
{
    size_t p = rfind('/');
    if (p == string::npos) {
       return "";
    } else {
        return substr(0, p);
    }
}


AttributeVector::ValueModifier::ValueModifier(AttributeVector &attr)
    : _attr(&attr)
{ }


AttributeVector::ValueModifier::ValueModifier(const ValueModifier &rhs)
    : _attr(rhs.stealAttr())
{ }


AttributeVector::ValueModifier::~ValueModifier() {
    if (_attr) {
        _attr->incGeneration();
    }
}

namespace {

bool
allow_paged(const search::attribute::Config& config)
{
    if (!config.paged()) {
        return false;
    }
    using Type = search::attribute::BasicType::Type;
    if (config.basicType() == Type::REFERENCE || config.basicType() == Type::PREDICATE) {
        return false;
    }
    if (config.basicType() == Type::TENSOR) {
        return (!config.tensorType().is_error() && config.tensorType().is_dense());
    }
    return true;
}

std::unique_ptr<vespalib::alloc::MemoryAllocator>
make_memory_allocator(const vespalib::string& name, const search::attribute::Config& config)
{
    if (allow_paged(config)) {
        return vespalib::alloc::MmapFileAllocatorFactory::instance().make_memory_allocator(name);
    }
    return {};
}

}

AttributeVector::AttributeVector(vespalib::stringref baseFileName, const Config &c)
    : _baseFileName(baseFileName),
      _config(c),
      _interlock(std::make_shared<attribute::Interlock>()),
      _enumLock(),
      _genHandler(),
      _genHolder(),
      _status(),
      _highestValueCount(1),
      _enumMax(0),
      _committedDocIdLimit(0u),
      _uncommittedDocIdLimit(0u),
      _createSerialNum(0u),
      _compactLidSpaceGeneration(0u),
      _hasEnum(false),
      _loaded(false),
      _isUpdateableInMemoryOnly(attribute::isUpdateableInMemoryOnly(getName(), getConfig())),
      _nextStatUpdateTime(),
      _memory_allocator(make_memory_allocator(_baseFileName.getAttributeName(), c))
{
}

AttributeVector::~AttributeVector() = default;

void
AttributeVector::updateStat(bool force) {
    if (force) {
        onUpdateStat();
    } else if (_nextStatUpdateTime < vespalib::steady_clock::now()) {
        onUpdateStat();
        _nextStatUpdateTime = vespalib::steady_clock::now() + 5s;
    }
}

bool AttributeVector::hasEnum() const { return _hasEnum; }
uint32_t AttributeVector::getMaxValueCount() const { return _highestValueCount; }

bool
AttributeVector::isEnumerated(const vespalib::GenericHeader &header)
{
    return header.hasTag(enumeratedTag) &&
           header.getTag(enumeratedTag).asInteger() != 0;
}

void
AttributeVector::commit(bool forceUpdateStats)
{
    onCommit();
    updateCommittedDocIdLimit();
    updateStat(forceUpdateStats);
    _loaded = true;
}

void
AttributeVector::commit(const CommitParam & param)
{
    if (param.firstSerialNum() < getStatus().getLastSyncToken()) {
        LOG(error, "Expected first token to be >= %" PRIu64 ", got %" PRIu64 ".",
            getStatus().getLastSyncToken(), param.firstSerialNum());
        LOG_ABORT("should not be reached");
    }
    commit(param.forceUpdateStats());
    _status.setLastSyncToken(param.lastSerialNum());
}

bool
AttributeVector::addDocs(DocId &startDoc, DocId &lastDoc, uint32_t numDocs)
{
    if (numDocs != 0) {
        onAddDocs(getNumDocs() + numDocs);
        if (!addDoc(startDoc)) {
            return false;
        }
        lastDoc = startDoc;
        for (uint32_t i = 1; i < numDocs; ++i) {
            if (!addDoc(lastDoc)) {
                return false;
            }
        }
    }
    return true;
}


bool
AttributeVector::addDocs(uint32_t numDocs)
{
    DocId doc;
    return addDocs(doc, doc, numDocs);
}


void
AttributeVector::incGeneration()
{
    // Freeze trees etc, to stop new readers from accessing currently held data
    onGenerationChange(_genHandler.getNextGeneration());
    _genHandler.incGeneration();
    // Remove old data on hold lists that can no longer be reached by readers
    removeAllOldGenerations();
}


void
AttributeVector::updateStatistics(uint64_t numValues, uint64_t numUniqueValue, uint64_t allocated,
                                  uint64_t used, uint64_t dead, uint64_t onHold)
{
    _status.updateStatistics(numValues, numUniqueValue, allocated, used, dead, onHold);
}

vespalib::MemoryUsage
AttributeVector::getEnumStoreValuesMemoryUsage() const
{
    return vespalib::MemoryUsage();
}

void
AttributeVector::populate_address_space_usage(AddressSpaceUsage& usage) const
{
    // TODO: Stop inserting defaults here when code using AddressSpaceUsage no longer require these two components.
    usage.set(AddressSpaceComponents::enum_store, AddressSpaceComponents::default_enum_store_usage());
    usage.set(AddressSpaceComponents::multi_value, AddressSpaceComponents::default_multi_value_usage());
}

AddressSpaceUsage
AttributeVector::getAddressSpaceUsage() const
{
    AddressSpaceUsage usage;
    populate_address_space_usage(usage);
    return usage;
}

bool
AttributeVector::isImported() const
{
    return false;
}

bool
AttributeVector::headerTypeOK(const vespalib::GenericHeader &header) const
{
    return header.hasTag(dataTypeTag) &&
        header.hasTag(collectionTypeTag) &&
        header.hasTag(docIdLimitTag) &&
        header.getTag(dataTypeTag).asString() == 
        getConfig().basicType().asString() &&
        header.getTag(collectionTypeTag).asString() == 
        getConfig().collectionType().asString();
}

void AttributeVector::removeOldGenerations(generation_t firstUsed) { (void) firstUsed; }
void AttributeVector::onGenerationChange(generation_t generation) { (void) generation; }
const IEnumStore* AttributeVector::getEnumStoreBase() const { return nullptr; }
IEnumStore* AttributeVector::getEnumStoreBase() { return nullptr; }
const attribute::MultiValueMappingBase * AttributeVector::getMultiValueBase() const { return nullptr; }

bool
AttributeVector::save(vespalib::stringref fileName)
{
    TuneFileAttributes tune;
    DummyFileHeaderContext fileHeaderContext;
    AttributeFileSaveTarget saveTarget(tune, fileHeaderContext);
    return save(saveTarget, fileName);
}

bool
AttributeVector::save()
{
    return save(getBaseFileName());
}


bool
AttributeVector::save(IAttributeSaveTarget &saveTarget, vespalib::stringref fileName)
{
    commit();
    // First check if new style save is available.
    std::unique_ptr<AttributeSaver> saver(onInitSave(fileName));
    if (saver) {
        // Normally, new style save happens in background, but here it
        // will occur in the foreground.
        return saver->save(saveTarget);
    }
    // New style save not available, use old style save
    saveTarget.setHeader(createAttributeHeader(fileName));
    if (!saveTarget.setup()) {
        return false;
    }
    onSave(saveTarget);
    saveTarget.close();
    return true;
}

attribute::AttributeHeader
AttributeVector::createAttributeHeader(vespalib::stringref fileName) const {
    return attribute::AttributeHeader(fileName,
                                      getConfig().basicType(),
                                      getConfig().collectionType(),
                                      getConfig().tensorType(),
                                      getEnumeratedSave(),
                                      getConfig().predicateParams(),
                                      getConfig().hnsw_index_params(),
                                      getCommittedDocIdLimit(),
                                      getUniqueValueCount(),
                                      getTotalValueCount(),
                                      getCreateSerialNum(),
                                      getVersion());
}

void AttributeVector::onSave(IAttributeSaveTarget &)
{
    LOG_ABORT("should not be reached");
}

bool
AttributeVector::hasLoadData() const {
    FastOS_StatInfo statInfo;
    if (!FastOS_File::Stat(make_string("%s.dat", getBaseFileName().c_str()).c_str(), &statInfo)) {
        return false;
    }
    if (hasMultiValue() &&
        !FastOS_File::Stat(make_string("%s.idx", getBaseFileName().c_str()).c_str(), &statInfo))
    {
        return false;
    }
    if (hasWeightedSetType() &&
        !FastOS_File::Stat(make_string("%s.weight", getBaseFileName().c_str()).c_str(), &statInfo))
    {
        return false;
    }
    if (isEnumeratedSaveFormat() &&
        !FastOS_File::Stat(make_string("%s.udat", getBaseFileName().c_str()).c_str(), &statInfo))
    {
        return false;
    }
    return true;
}


bool
AttributeVector::isEnumeratedSaveFormat() const
{
    vespalib::string datName(vespalib::make_string("%s.dat", getBaseFileName().c_str()));
    Fast_BufferedFile   datFile;
    vespalib::FileHeader datHeader(FileSettings::DIRECTIO_ALIGNMENT);
    if ( ! datFile.OpenReadOnly(datName.c_str()) ) {
        LOG(error, "could not open %s: %s", datFile.GetFileName(), getLastErrorString().c_str());
        throw IllegalStateException(make_string("Failed opening attribute data file '%s' for reading",
                                                datFile.GetFileName()));
    }
    datHeader.readFile(datFile);
    
    return isEnumerated(datHeader);
}

bool
AttributeVector::load() {
    return load(nullptr);
}

bool
AttributeVector::load(vespalib::Executor * executor) {
    assert(!_loaded);
    bool loaded = onLoad(executor);
    if (loaded) {
        commit();
    }
    _loaded = loaded;
    return _loaded;
}

bool AttributeVector::onLoad(vespalib::Executor *) { return false; }
int32_t AttributeVector::getWeight(DocId, uint32_t) const { return 1; }

bool AttributeVector::findEnum(const char *, EnumHandle &) const { return false; }

std::vector<search::attribute::IAttributeVector::EnumHandle>
AttributeVector::findFoldedEnums(const char *) const {
    std::vector<EnumHandle> empty;
    return empty;
}

const char * AttributeVector::getStringFromEnum(EnumHandle) const { return nullptr; }

AttributeVector::SearchContext::SearchContext(const AttributeVector &attr) :
    _attr(attr),
    _plsc(nullptr)
{ }

AttributeVector::SearchContext::UP
AttributeVector::getSearch(QueryPacketT searchSpec, const SearchContextParams &params) const
{
    return getSearch(QueryTermDecoder::decodeTerm(searchSpec), params);
}

attribute::ISearchContext::UP
AttributeVector::createSearchContext(QueryTermSimpleUP term, const attribute::SearchContextParams &params) const
{
    return getSearch(std::move(term), params);
}

AttributeVector::SearchContext::~SearchContext() = default;

unsigned int
AttributeVector::SearchContext::approximateHits() const
{
    if (_plsc != nullptr) {
        return _plsc->approximateHits();
    }
    return std::max(uint64_t(_attr.getNumDocs()),
                    _attr.getStatus().getNumValues());
}

SearchIterator::UP
AttributeVector::SearchContext::
createIterator(fef::TermFieldMatchData *matchData, bool strict)
{
    if (_plsc != nullptr) {
        SearchIterator::UP res = _plsc->createPostingIterator(matchData, strict);
        if (res) {
            return res;
        }
    }
    return createFilterIterator(matchData, strict);
}


SearchIterator::UP
AttributeVector::SearchContext::
createFilterIterator(fef::TermFieldMatchData *matchData, bool strict)
{
    if (!valid())
        return std::make_unique<queryeval::EmptySearch>();
    if (getIsFilter()) {
        return SearchIterator::UP(strict ?
            new FilterAttributeIteratorStrict<AttributeVector::SearchContext>(*this, matchData) :
            new FilterAttributeIteratorT<AttributeVector::SearchContext>(*this, matchData));
    }
    return SearchIterator::UP(strict ?
            new AttributeIteratorStrict<AttributeVector::SearchContext>(*this, matchData) :
            new AttributeIteratorT<AttributeVector::SearchContext>(*this, matchData));
}


void
AttributeVector::SearchContext::fetchPostings(const queryeval::ExecuteInfo &execInfo) {
    if (_plsc != nullptr)
        _plsc->fetchPostings(execInfo);
}


bool
AttributeVector::apply(DocId doc, const MapValueUpdate &map) {
    bool retval(doc < getNumDocs());
    if (retval) {
        const ValueUpdate & vu(map.getUpdate());
        if (vu.inherits(ArithmeticValueUpdate::classId)) {
            const ArithmeticValueUpdate &au(static_cast<const ArithmeticValueUpdate &>(vu));
            retval = applyWeight(doc, map.getKey(), au);
        } else if (vu.inherits(AssignValueUpdate::classId)) {
            const AssignValueUpdate &au(static_cast<const AssignValueUpdate &>(vu));
            retval = applyWeight(doc, map.getKey(), au);
        } else {
            retval = false;
        }
    }
    return retval;
}


bool AttributeVector::applyWeight(DocId, const FieldValue &, const ArithmeticValueUpdate &) { return false; }

bool AttributeVector::applyWeight(DocId, const FieldValue&, const AssignValueUpdate&) { return false; }

void
AttributeVector::removeAllOldGenerations() {
    _genHandler.updateFirstUsedGeneration();
    removeOldGenerations(_genHandler.getFirstUsedGeneration());
}


void
AttributeVector::divideByZeroWarning() {
    LOG(warning,
        "applyArithmetic(): "
        "Divide by zero is an illegal operation on integer attributes "
        "or weighted sets. Ignoring operation.");
}


void
AttributeVector::performCompactionWarning()
{
    LOG(warning,
        "Could not perform compaction on MultiValueMapping "
        "with current generation = %" PRIu64,
        _genHandler.getCurrentGeneration());
}


void
AttributeVector::addReservedDoc()
{
    uint32_t docId = 42;
    addDoc(docId);      // Reserved
    assert(docId == 0u);
    assert(docId < getNumDocs());
    clearDoc(docId);
    commit();
    const vespalib::Identifiable::RuntimeClass &info = getClass();
    if (info.inherits(search::FloatingPointAttribute::classId)) {
        FloatingPointAttribute &vec =
            static_cast<FloatingPointAttribute &>(*this);
        if (hasMultiValue()) {
            bool appendedUndefined = vec.append(0, attribute::getUndefined<double>(), 1);
            assert(appendedUndefined);
            (void) appendedUndefined;
        } else {
            bool updatedUndefined = vec.update(0, attribute::getUndefined<double>());
            assert(updatedUndefined);
            (void) updatedUndefined;
        }
        commit();
    }
}

attribute::IPostingListAttributeBase *AttributeVector::getIPostingListAttributeBase() { return nullptr; }
const attribute::IPostingListAttributeBase *AttributeVector::getIPostingListAttributeBase() const { return nullptr; }
const IDocumentWeightAttribute * AttributeVector::asDocumentWeightAttribute() const { return nullptr; }
const tensor::ITensorAttribute *AttributeVector::asTensorAttribute() const { return nullptr; }
bool AttributeVector::hasPostings() { return getIPostingListAttributeBase() != nullptr; }
uint64_t AttributeVector::getUniqueValueCount() const { return getTotalValueCount(); }
uint64_t AttributeVector::getTotalValueCount() const { return getNumDocs(); }
void AttributeVector::setCreateSerialNum(uint64_t createSerialNum) { _createSerialNum = createSerialNum; }
uint64_t AttributeVector::getCreateSerialNum() const { return _createSerialNum; }
uint32_t AttributeVector::getVersion() const { return 0; }

void
AttributeVector::compactLidSpace(uint32_t wantedLidLimit) {
    commit();
    assert(_committedDocIdLimit >= wantedLidLimit);
    if (wantedLidLimit < _committedDocIdLimit) {
        clearDocs(wantedLidLimit, _committedDocIdLimit);
    }
    commit();
    _committedDocIdLimit = wantedLidLimit;
    _compactLidSpaceGeneration = _genHandler.getCurrentGeneration();
    incGeneration();
}


bool
AttributeVector::canShrinkLidSpace() const {
    return wantShrinkLidSpace() &&
        _compactLidSpaceGeneration < getFirstUsedGeneration();
}


void
AttributeVector::shrinkLidSpace()
{
    commit();
    removeAllOldGenerations();
    if (!canShrinkLidSpace()) {
        return;
    }
    uint32_t committedDocIdLimit = _committedDocIdLimit;
    clearDocs(committedDocIdLimit, getNumDocs());
    commit();
    _committedDocIdLimit = committedDocIdLimit;
    onShrinkLidSpace();
    attribute::IPostingListAttributeBase *pab = getIPostingListAttributeBase();
    if (pab != NULL) {
        pab->forwardedShrinkLidSpace(_committedDocIdLimit);
    }
    incGeneration();
    updateStat(true);
}

void AttributeVector::onShrinkLidSpace() {}

void
AttributeVector::clearDocs(DocId lidLow, DocId lidLimit)
{
    assert(lidLow <= lidLimit);
    assert(lidLimit <= getNumDocs());
    uint32_t count = 0;
    constexpr uint32_t commit_interval = 1000;
    for (DocId lid = lidLow; lid < lidLimit; ++lid) {
        clearDoc(lid);
        if ((++count % commit_interval) == 0) {
            commit();
        }
    }
}

AttributeVector::EnumModifier
AttributeVector::getEnumModifier()
{
    attribute::InterlockGuard interlockGuard(*_interlock);
    return EnumModifier(_enumLock, interlockGuard);
}


void AttributeVector::setInterlock(const std::shared_ptr<attribute::Interlock> &interlock) {
    _interlock = interlock;
}


std::unique_ptr<AttributeSaver>
AttributeVector::initSave(vespalib::stringref fileName)
{
    commit();
    return onInitSave(fileName);
}

std::unique_ptr<AttributeSaver>
AttributeVector::onInitSave(vespalib::stringref)
{
    return std::unique_ptr<AttributeSaver>();
}

bool
AttributeVector::hasActiveEnumGuards()
{
    std::unique_lock<std::shared_mutex> lock(_enumLock, std::defer_lock);
    for (size_t i = 0; i < 1000; ++i) {
        // Note: Need to run this in loop as try_lock() is allowed to fail spuriously and return false
        // even if the mutex is not currently locked by any other thread.
        if (lock.try_lock()) {
            return false;
        }
    }
    return true;
}

IExtendAttribute *AttributeVector::getExtendInterface() { return nullptr; }

uint64_t
AttributeVector::getEstimatedSaveByteSize() const
{
    uint64_t headerSize = FileSettings::DIRECTIO_ALIGNMENT;
    uint64_t totalValueCount = _status.getNumValues();
    uint64_t uniqueValueCount = _status.getNumUniqueValues();
    uint64_t docIdLimit = getCommittedDocIdLimit();
    uint64_t datFileSize = 0;
    uint64_t weightFileSize = 0;
    uint64_t idxFileSize = 0;
    uint64_t udatFileSize = 0;
    size_t fixedWidth = getFixedWidth();
    vespalib::MemoryUsage values_mem_usage = getEnumStoreValuesMemoryUsage();

    if (hasMultiValue()) {
        idxFileSize = headerSize + sizeof(uint32_t) * (docIdLimit + 1);
    }
    if (hasWeightedSetType()) {
        weightFileSize = headerSize + sizeof(int32_t) * totalValueCount;
    }
    if (hasEnum()) {
        datFileSize =  headerSize + sizeof(uint32_t) * totalValueCount;
        if (fixedWidth != 0) {
            udatFileSize = headerSize + fixedWidth * uniqueValueCount;
        } else {
            size_t unique_values_bytes = values_mem_usage.usedBytes() -
                    (values_mem_usage.deadBytes() + values_mem_usage.allocatedBytesOnHold());
            size_t ref_count_mem_usage = sizeof(uint32_t) * uniqueValueCount;
            udatFileSize = headerSize + unique_values_bytes - ref_count_mem_usage;
        }
    } else {
        BasicType::Type basicType(getBasicType());
        const Status &status = getStatus();
        int64_t memorySize = status.getUsed() - status.getDead();
        if (memorySize < 0) {
            memorySize = 0;
        }
        switch (basicType) {
        case BasicType::Type::PREDICATE:
        case BasicType::Type::TENSOR:
            datFileSize = headerSize + memorySize;
            break;
        case BasicType::Type::STRING:
            abort();
            break;
        default:
            datFileSize = headerSize + fixedWidth * totalValueCount;
            break;
        }
    }
    return datFileSize + weightFileSize + idxFileSize + udatFileSize;
}

size_t
AttributeVector::getEstimatedShrinkLidSpaceGain() const
{
    size_t canFree = 0;
    if (canShrinkLidSpace()) {
        uint32_t committedDocIdLimit = getCommittedDocIdLimit();
        uint32_t numDocs = getNumDocs();
        const attribute::Config &cfg = getConfig();
        if (committedDocIdLimit < numDocs) {
            size_t elemSize = 4;
            if (!cfg.collectionType().isMultiValue() && !cfg.fastSearch()) {
                BasicType::Type basicType(getBasicType());
                switch (basicType) {
                case BasicType::Type::PREDICATE:
                case BasicType::Type::TENSOR:
                case BasicType::Type::REFERENCE:
                    break;
                default:
                    elemSize = cfg.basicType().fixedSize();
                }
            }
            canFree = elemSize * (numDocs - committedDocIdLimit);
        }
    }
    return canFree;
}


namespace {

class ReadGuard : public attribute::AttributeReadGuard
{
    using GenerationHandler = vespalib::GenerationHandler;
    GenerationHandler::Guard _generationGuard;
    using EnumGuard = std::shared_lock<std::shared_mutex>;
    EnumGuard _enumGuard;
public:
    ReadGuard(const attribute::IAttributeVector *attr, GenerationHandler::Guard &&generationGuard, std::shared_mutex *enumLock)
        : attribute::AttributeReadGuard(attr),
          _generationGuard(std::move(generationGuard)),
          _enumGuard(enumLock != nullptr ? EnumGuard(*enumLock) : EnumGuard())
    {
    }
};

}

std::unique_ptr<attribute::AttributeReadGuard>
AttributeVector::makeReadGuard(bool stableEnumGuard) const
{
    return std::make_unique<ReadGuard>(this, _genHandler.takeGuard(), stableEnumGuard ? &_enumLock : nullptr);
}

vespalib::MemoryUsage
AttributeVector::getChangeVectorMemoryUsage() const
{
    return vespalib::MemoryUsage(0, 0, 0, 0);
}

bool
AttributeVector::commitIfChangeVectorTooLarge() {
    bool needCommit = getChangeVectorMemoryUsage().usedBytes() > getConfig().getMaxUnCommittedMemory();
    if (needCommit) {
        commit(false);
    }
    return needCommit;
}

void
AttributeVector::logEnumStoreEvent(const char *reason, const char *stage)
{
    vespalib::JSONStringer jstr;
    jstr.beginObject();
    jstr.appendKey("path").appendString(getBaseFileName());
    jstr.endObject();
    vespalib::string eventName(make_string("%s.attribute.enumstore.%s", reason, stage));
    EV_STATE(eventName.c_str(), jstr.toString().data());
}

void
AttributeVector::drain_hold(uint64_t hold_limit)
{
    incGeneration();
    for (int retry = 0; retry < 40; ++retry) {
        removeAllOldGenerations();
        updateStat(true);
        if (_status.getOnHold() <= hold_limit) {
            return;
        }
        std::this_thread::sleep_for(retry < 20 ? 20ms : 100ms);
    }
}

void
AttributeVector::update_config(const Config& cfg)
{
    commit(true);
    _config.setGrowStrategy(cfg.getGrowStrategy());
    if (cfg.getCompactionStrategy() == _config.getCompactionStrategy()) {
        return;
    }
    drain_hold(1_Mi); // Wait until 1MiB or less on hold
    _config.setCompactionStrategy(cfg.getCompactionStrategy());
    updateStat(true);
    commit(); // might trigger compaction
    drain_hold(1_Mi); // Wait until 1MiB or less on hold
}

vespalib::alloc::Alloc
AttributeVector::get_initial_alloc()
{
    return (_memory_allocator ? vespalib::alloc::Alloc::alloc_with_allocator(_memory_allocator.get()) : vespalib::alloc::Alloc::alloc());
}

template bool AttributeVector::append<StringChangeData>(ChangeVectorT< ChangeTemplate<StringChangeData> > &changes, uint32_t , const StringChangeData &, int32_t, bool);
template bool AttributeVector::update<StringChangeData>(ChangeVectorT< ChangeTemplate<StringChangeData> > &changes, uint32_t , const StringChangeData &);
template bool AttributeVector::remove<StringChangeData>(ChangeVectorT< ChangeTemplate<StringChangeData> > &changes, uint32_t , const StringChangeData &, int32_t);

}