Couchbase Lite C++
Couchbase Lite C++ API
Loading...
Searching...
No Matches
QueryIndex.hh
Go to the documentation of this file.
1//
2// QueryIndex.hh
3//
4// Copyright (c) 2024 Couchbase, Inc All rights reserved.
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17//
18
19
20#pragma once
21#include "cbl++/Base.hh"
22#include "cbl++/Collection.hh"
23#include "cbl/CBLQueryIndex.h"
24
26
27namespace cbl {
28#ifdef COUCHBASE_ENTERPRISE
29 class IndexUpdater;
30#endif
31
33 class QueryIndex : private RefCounted {
34 public:
35 // Accessors:
36
38 std::string name() const {return internal::asString(CBLQueryIndex_Name(ref()));}
39
42
43#ifdef COUCHBASE_ENTERPRISE
44 // Index Updater:
45
53 inline IndexUpdater beginUpdate(size_t limit);
54#endif
55
56 protected:
57 friend class Collection;
58
60 if (!i && error->code != 0)
61 internal::check(false, *error);
62 QueryIndex index;
63 index._ref = (CBLRefCounted*)i;
64 return index;
65 }
66
68 };
69
70#ifdef COUCHBASE_ENTERPRISE
71
76 class IndexUpdater : private RefCounted {
77 public:
79 size_t count() const {return CBLIndexUpdater_Count(ref());}
80
84 fleece::Value value(size_t index) const {
85 return CBLIndexUpdater_Value(ref(), index);
86 }
87
94 void setVector(unsigned index, const float* _cbl_nullable vector, size_t dimension) {
95 CBLError error;
96 internal::check(CBLIndexUpdater_SetVector(ref(), index, vector, dimension, &error), error);
97 }
98
102 void skipVector(size_t index) {
104 }
105
110 void finish() {
111 CBLError error;
112 internal::check(CBLIndexUpdater_Finish(ref(), &error), error);
113 }
114
115 protected:
117 internal::check(i != nullptr || error->code == 0, *error);
118 IndexUpdater updater;
119 updater._ref = (CBLRefCounted*)i;
120 return updater;
121 }
122
123 friend class QueryIndex;
125 };
126
128 CBLError error {};
129 auto updater = CBLQueryIndex_BeginUpdate(ref(), limit, &error);
130 return IndexUpdater::adopt(updater, &error);
131 }
132#endif
133
135 CBLError error {};
136 return QueryIndex::adopt(CBLCollection_GetIndex(ref(), slice(name), &error), &error);
137 }
138}
139
#define CBL_REFCOUNTED_BOILERPLATE(CLASS, SUPER, C_TYPE)
Definition Base.hh:160
#define _cbl_nullable
#define CBL_ASSUME_NONNULL_END
#define CBL_ASSUME_NONNULL_BEGIN
CBLCollection *_Nullable ref() const
Returns a pointer to the underlying C object (CBLCollection), or NULL if this is a null reference.
Definition Collection.hh:373
std::string name() const
The collection's name.
Definition Collection.hh:136
QueryIndex getIndex(std::string_view name)
Get an index by name.
Definition QueryIndex.hh:134
friend class QueryIndex
Definition Collection.hh:371
Updates a lazy index by setting the computed vectors for the documents returned from QueryIndex::begi...
Definition QueryIndex.hh:76
void setVector(unsigned index, const float *_Nullable vector, size_t dimension)
Sets the vector for the value corresponding to the given index.
Definition QueryIndex.hh:94
size_t count() const
The total number of vectors to compute and set for updating the index.
Definition QueryIndex.hh:79
IndexUpdater() noexcept
Constructs a null reference (one that points to no object).
Definition QueryIndex.hh:124
static IndexUpdater adopt(const CBLIndexUpdater *_Nullable i, CBLError *error)
Definition QueryIndex.hh:116
fleece::Value value(size_t index) const
Get the value at the given index for computing the vector.
Definition QueryIndex.hh:84
CBLIndexUpdater *_Nullable ref() const
Returns a pointer to the underlying C object (CBLIndexUpdater), or NULL if this is a null reference.
Definition QueryIndex.hh:124
void finish()
Updates the index with the computed vectors and removes any index rows for which null vector was give...
Definition QueryIndex.hh:110
friend class QueryIndex
Definition QueryIndex.hh:123
void skipVector(size_t index)
Skip setting the vector for the value corresponding to the index.
Definition QueryIndex.hh:102
IndexUpdater beginUpdate(size_t limit)
Finds new or updated documents for which vectors need to be (re)computed and returns an IndexUpdater ...
Definition QueryIndex.hh:127
Collection collection() const
A index's collection.
Definition QueryIndex.hh:41
static QueryIndex adopt(const CBLQueryIndex *_Nullable i, CBLError *error)
Definition QueryIndex.hh:59
friend class Collection
Definition QueryIndex.hh:57
CBLQueryIndex *_Nullable ref() const
Returns a pointer to the underlying C object (CBLQueryIndex), or NULL if this is a null reference.
Definition QueryIndex.hh:67
QueryIndex() noexcept
Constructs a null reference (one that points to no object).
Definition QueryIndex.hh:67
std::string name() const
The index's name.
Definition QueryIndex.hh:38
_cbl_warn_unused CBLQueryIndex *_cbl_nullable CBLCollection_GetIndex(CBLCollection *collection, FLString name, CBLError *_cbl_nullable outError)
FLValue CBLIndexUpdater_Value(CBLIndexUpdater *updater, size_t index)
FLString CBLQueryIndex_Name(const CBLQueryIndex *index)
struct CBLQueryIndex CBLQueryIndex
bool CBLIndexUpdater_Finish(CBLIndexUpdater *updater, CBLError *_cbl_nullable outError)
CBLCollection * CBLQueryIndex_Collection(const CBLQueryIndex *index)
struct CBLIndexUpdater CBLIndexUpdater
bool CBLIndexUpdater_SetVector(CBLIndexUpdater *updater, size_t index, const float vector[_cbl_nullable], size_t dimension, CBLError *_cbl_nullable outError)
size_t CBLIndexUpdater_Count(const CBLIndexUpdater *updater)
void CBLIndexUpdater_SkipVector(CBLIndexUpdater *updater, size_t index)
_cbl_warn_unused CBLIndexUpdater *_cbl_nullable CBLQueryIndex_BeginUpdate(CBLQueryIndex *index, size_t limit, CBLError *_cbl_nullable outError)
struct CBLRefCounted CBLRefCounted
Definition Base.hh:46
fleece::slice slice
Convenience alias for fleece::slice, a non-owning view of a byte range.
Definition Base.hh:49