/
ngt.go
813 lines (725 loc) · 19.3 KB
/
ngt.go
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
//
// Copyright (C) 2019-2021 vdaas.org vald team <vald@vdaas.org>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Package service manages the main logic of server.
package service
import (
"context"
"encoding/gob"
"io/fs"
"math"
"os"
"path/filepath"
"reflect"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/vdaas/vald/internal/config"
core "github.com/vdaas/vald/internal/core/algorithm/ngt"
"github.com/vdaas/vald/internal/errgroup"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/file"
"github.com/vdaas/vald/internal/log"
"github.com/vdaas/vald/internal/observability/trace"
"github.com/vdaas/vald/internal/safety"
"github.com/vdaas/vald/pkg/agent/core/ngt/model"
"github.com/vdaas/vald/pkg/agent/core/ngt/service/kvs"
"github.com/vdaas/vald/pkg/agent/core/ngt/service/vqueue"
"github.com/vdaas/vald/pkg/agent/internal/metadata"
)
type NGT interface {
Start(ctx context.Context) <-chan error
Search(vec []float32, size uint32, epsilon, radius float32) ([]model.Distance, error)
SearchByID(uuid string, size uint32, epsilon, radius float32) ([]model.Distance, error)
Insert(uuid string, vec []float32) (err error)
InsertMultiple(vecs map[string][]float32) (err error)
Update(uuid string, vec []float32) (err error)
UpdateMultiple(vecs map[string][]float32) (err error)
Delete(uuid string) (err error)
DeleteMultiple(uuids ...string) (err error)
GetObject(uuid string) (vec []float32, err error)
CreateIndex(ctx context.Context, poolSize uint32) (err error)
SaveIndex(ctx context.Context) (err error)
Exists(string) (uint32, bool)
CreateAndSaveIndex(ctx context.Context, poolSize uint32) (err error)
IsIndexing() bool
IsSaving() bool
Len() uint64
NumberOfCreateIndexExecution() uint64
NumberOfProactiveGCExecution() uint64
UUIDs(context.Context) (uuids []string)
DeleteVQueueBufferLen() uint64
InsertVQueueBufferLen() uint64
DeleteVQueueChannelLen() uint64
InsertVQueueChannelLen() uint64
GetDimensionSize() int
Close(ctx context.Context) error
}
type ngt struct {
// instances
core core.NGT
eg errgroup.Group
kvs kvs.BidiMap
vq vqueue.Queue
// statuses
indexing atomic.Value
saving atomic.Value
lastNoice uint64 // last number of create index execution this value prevent unnecessary saveindex.
// counters
nocie uint64 // number of create index execution
nogce uint64 // number of proactive GC execution
// configurations
inMem bool // in-memory mode
dim int // dimension size
alen int // auto indexing length
lim time.Duration // auto indexing time limit
dur time.Duration // auto indexing check duration
sdur time.Duration // auto save index check duration
minLit time.Duration // minimum load index timeout
maxLit time.Duration // maximum load index timeout
litFactor time.Duration // load index timeout factor
enableProactiveGC bool // if this value is true, agent component will purge GC memory more proactive
path string // index path
poolSize uint32 // default pool size
radius float32 // default radius
epsilon float32 // default epsilon
idelay time.Duration // initial delay duration
dcd bool // disable commit daemon
}
const (
kvsFileName = "ngt-meta.kvsdb"
)
func New(cfg *config.NGT, opts ...Option) (nn NGT, err error) {
n := new(ngt)
for _, opt := range append(defaultOptions, opts...) {
if err := opt(n); err != nil {
return nil, errors.ErrOptionFailed(err, reflect.ValueOf(opt))
}
}
n.kvs = kvs.New()
n.dim = cfg.Dimension
err = n.initNGT(
core.WithInMemoryMode(n.inMem),
core.WithIndexPath(n.path),
core.WithDefaultPoolSize(n.poolSize),
core.WithDefaultRadius(n.radius),
core.WithDefaultEpsilon(n.epsilon),
core.WithDimension(cfg.Dimension),
core.WithDistanceTypeByString(cfg.DistanceType),
core.WithObjectTypeByString(cfg.ObjectType),
core.WithBulkInsertChunkSize(cfg.BulkInsertChunkSize),
core.WithCreationEdgeSize(cfg.CreationEdgeSize),
core.WithSearchEdgeSize(cfg.SearchEdgeSize),
)
if err != nil {
return nil, err
}
if n.dur == 0 || n.alen == 0 {
n.dcd = true
}
if n.vq == nil {
n.vq, err = vqueue.New(
vqueue.WithErrGroup(n.eg),
vqueue.WithInsertBufferSize(cfg.VQueue.InsertBufferSize),
vqueue.WithDeleteBufferSize(cfg.VQueue.DeleteBufferSize),
vqueue.WithInsertBufferPoolSize(cfg.VQueue.InsertBufferPoolSize),
vqueue.WithDeleteBufferPoolSize(cfg.VQueue.DeleteBufferPoolSize),
)
if err != nil {
return nil, err
}
}
n.indexing.Store(false)
n.saving.Store(false)
return n, nil
}
func (n *ngt) initNGT(opts ...core.Option) (err error) {
if n.inMem {
log.Debug("vald agent starts with in-memory mode")
n.core, err = core.New(opts...)
return err
}
_, err = os.Stat(n.path)
if os.IsNotExist(err) {
log.Debugf("index file not exists,\tpath: %s,\terr: %v", n.path, err)
n.core, err = core.New(opts...)
return err
}
if os.IsPermission(err) {
log.Errorf("no permission for index path,\tpath: %s,\terr: %v", n.path, err)
return err
}
log.Debugf("load index from %s", n.path)
agentMetadata, err := metadata.Load(filepath.Join(n.path, metadata.AgentMetadataFileName))
if err != nil {
log.Warnf("cannot read metadata from %s: %s", metadata.AgentMetadataFileName, err)
}
if os.IsNotExist(err) || agentMetadata == nil || agentMetadata.NGT == nil || agentMetadata.NGT.IndexCount == 0 {
log.Warnf("cannot read metadata from %s: %v", metadata.AgentMetadataFileName, err)
if fi, err := os.Stat(filepath.Join(n.path, kvsFileName)); os.IsNotExist(err) || fi.Size() == 0 {
log.Warn("kvsdb file is not exist")
n.core, err = core.New(opts...)
return err
}
if os.IsPermission(err) {
log.Errorf("no permission for kvsdb file,\tpath: %s,\terr: %v", filepath.Join(n.path, kvsFileName), err)
return err
}
}
var timeout time.Duration
if agentMetadata != nil && agentMetadata.NGT != nil {
log.Debugf("the backup index size is %d. starting to load...", agentMetadata.NGT.IndexCount)
timeout = time.Duration(
math.Min(
math.Max(
float64(agentMetadata.NGT.IndexCount)*float64(n.litFactor),
float64(n.minLit),
),
float64(n.maxLit),
),
)
} else {
log.Debugf("cannot inspect the backup index size. starting to load default value.")
timeout = time.Duration(math.Min(float64(n.minLit), float64(n.maxLit)))
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
eg, _ := errgroup.New(ctx)
eg.Go(safety.RecoverFunc(func() (err error) {
n.core, err = core.Load(opts...)
return err
}))
eg.Go(safety.RecoverFunc(n.loadKVS))
ech := make(chan error, 1)
// NOTE: when it exceeds the timeout while loading,
// it should exit this function and leave this goroutine running.
go func() {
defer close(ech)
err = safety.RecoverFunc(func() (err error) {
err = eg.Wait()
if err != nil {
return err
}
cancel()
return nil
})()
if err != nil {
ech <- err
}
}()
select {
case err := <-ech:
return err
case <-ctx.Done():
if ctx.Err() == context.DeadlineExceeded {
log.Errorf("cannot load index backup data within the timeout %s. the process is going to be killed.", timeout)
err := metadata.Store(
filepath.Join(n.path, metadata.AgentMetadataFileName),
&metadata.Metadata{
IsInvalid: true,
NGT: &metadata.NGT{
IndexCount: 0,
},
},
)
if err != nil {
return err
}
return errors.ErrIndexLoadTimeout
}
}
return nil
}
func (n *ngt) loadKVS() error {
gob.Register(map[string]uint32{})
f, err := file.Open(
filepath.Join(n.path, kvsFileName),
os.O_RDONLY|os.O_SYNC,
os.ModePerm,
)
if err != nil {
return err
}
defer func() {
if f != nil {
derr := f.Close()
if derr != nil {
err = errors.Wrap(err, derr.Error())
}
}
}()
m := make(map[string]uint32)
err = gob.NewDecoder(f).Decode(&m)
if err != nil {
log.Errorf("error decoding kvsdb file,\terr: %v", err)
return err
}
for k, id := range m {
n.kvs.Set(k, id)
}
return nil
}
func (n *ngt) Start(ctx context.Context) <-chan error {
if n.dcd {
return nil
}
ech := make(chan error, 2)
n.eg.Go(safety.RecoverFunc(func() (err error) {
vqech, err := n.vq.Start(ctx)
if err != nil {
return err
}
if n.sdur == 0 {
n.sdur = n.dur + time.Second
}
if n.lim == 0 {
n.lim = n.dur * 2
}
defer close(ech)
timer := time.NewTimer(n.idelay)
select {
case <-ctx.Done():
timer.Stop()
return ctx.Err()
case <-timer.C:
}
timer.Stop()
tick := time.NewTicker(n.dur)
sTick := time.NewTicker(n.sdur)
limit := time.NewTicker(n.lim)
defer tick.Stop()
defer sTick.Stop()
defer limit.Stop()
for {
err = nil
select {
case <-ctx.Done():
err = n.CreateIndex(ctx, n.poolSize)
if err != nil && !errors.Is(err, errors.ErrUncommittedIndexNotFound) {
ech <- err
return errors.Wrap(ctx.Err(), err.Error())
}
return ctx.Err()
case <-tick.C:
if n.vq.IVQLen() >= n.alen {
err = n.CreateIndex(ctx, n.poolSize)
}
case <-limit.C:
err = n.CreateAndSaveIndex(ctx, n.poolSize)
case <-sTick.C:
err = n.SaveIndex(ctx)
case err := <-vqech:
if err != nil {
ech <- err
err = nil
}
}
if err != nil && err != errors.ErrUncommittedIndexNotFound {
ech <- err
runtime.Gosched()
err = nil
}
}
}))
return ech
}
func (n *ngt) Search(vec []float32, size uint32, epsilon, radius float32) ([]model.Distance, error) {
if n.IsIndexing() {
return nil, errors.ErrCreateIndexingIsInProgress
}
sr, err := n.core.Search(vec, int(size), epsilon, radius)
if err != nil {
return nil, err
}
ds := make([]model.Distance, 0, len(sr))
for _, d := range sr {
if err = d.Error; d.ID == 0 && err != nil {
log.Warnf("an error occurred while searching: %s", err)
continue
}
key, ok := n.kvs.GetInverse(d.ID)
if ok {
ds = append(ds, model.Distance{
ID: key,
Distance: d.Distance,
})
} else {
log.Warn("not found", d.ID, d.Distance)
}
}
return ds, nil
}
func (n *ngt) SearchByID(uuid string, size uint32, epsilon, radius float32) (dst []model.Distance, err error) {
if n.IsIndexing() {
return nil, errors.ErrCreateIndexingIsInProgress
}
log.Debugf("SearchByID\tuuid: %s size: %d epsilon: %f radius: %f", uuid, size, epsilon, radius)
vec, err := n.GetObject(uuid)
if err != nil {
log.Debugf("SearchByID\tuuid: %s's vector not found", uuid)
return nil, err
}
return n.Search(vec, size, epsilon, radius)
}
func (n *ngt) Insert(uuid string, vec []float32) (err error) {
return n.insert(uuid, vec, time.Now().UnixNano(), true)
}
func (n *ngt) insert(uuid string, vec []float32, t int64, validation bool) (err error) {
if len(uuid) == 0 {
err = errors.ErrUUIDNotFound(0)
return err
}
if validation && !n.vq.DVExists(uuid) {
// if delete schedule exists we can insert new vector
_, ok := n.kvs.Get(uuid)
if ok || n.vq.IVExists(uuid) {
return errors.ErrUUIDAlreadyExists(uuid)
}
}
return n.vq.PushInsert(uuid, vec, t)
}
func (n *ngt) InsertMultiple(vecs map[string][]float32) (err error) {
t := time.Now().UnixNano()
for uuid, vec := range vecs {
ierr := n.insert(uuid, vec, t, true)
if ierr != nil {
if err != nil {
err = errors.Wrap(ierr, err.Error())
} else {
err = ierr
}
}
}
return err
}
func (n *ngt) Update(uuid string, vec []float32) (err error) {
now := time.Now().UnixNano()
if err = n.readyForUpdate(uuid, vec); err != nil {
return err
}
err = n.delete(uuid, now)
if err != nil {
return err
}
now++
return n.insert(uuid, vec, now, false)
}
func (n *ngt) UpdateMultiple(vecs map[string][]float32) (err error) {
uuids := make([]string, 0, len(vecs))
for uuid, vec := range vecs {
if err = n.readyForUpdate(uuid, vec); err != nil {
delete(vecs, uuid)
} else {
uuids = append(uuids, uuid)
}
}
err = n.DeleteMultiple(uuids...)
if err != nil {
return err
}
return n.InsertMultiple(vecs)
}
func (n *ngt) Delete(uuid string) (err error) {
return n.delete(uuid, time.Now().UnixNano())
}
func (n *ngt) delete(uuid string, t int64) (err error) {
if len(uuid) == 0 {
err = errors.ErrUUIDNotFound(0)
return err
}
_, ok := n.kvs.Get(uuid)
if !ok && !n.vq.IVExists(uuid) {
return errors.ErrObjectIDNotFound(uuid)
}
return n.vq.PushDelete(uuid, t)
}
func (n *ngt) DeleteMultiple(uuids ...string) (err error) {
t := time.Now().UnixNano()
for _, uuid := range uuids {
ierr := n.delete(uuid, t)
if ierr != nil {
if err != nil {
err = errors.Wrap(ierr, err.Error())
} else {
err = ierr
}
}
}
return err
}
func (n *ngt) GetObject(uuid string) (vec []float32, err error) {
oid, ok := n.kvs.Get(uuid)
if !ok {
log.Debugf("GetObject\tuuid: %s's kvs data not found, trying to read from vqueue", uuid)
vec, ok := n.vq.GetVector(uuid)
if !ok {
log.Debugf("GetObject\tuuid: %s's vqueue data not found", uuid)
return nil, errors.ErrObjectIDNotFound(uuid)
}
return vec, nil
}
log.Debugf("GetObject\tGetVector oid: %d", oid)
vec, err = n.core.GetVector(uint(oid))
if err != nil {
log.Debugf("GetObject\tuuid: %s oid: %d's vector not found", uuid, oid)
return nil, errors.ErrObjectNotFound(err, uuid)
}
return vec, nil
}
func (n *ngt) CreateIndex(ctx context.Context, poolSize uint32) (err error) {
ctx, span := trace.StartSpan(ctx, "vald/agent-ngt/service/NGT.CreateIndex")
defer func() {
if span != nil {
span.End()
}
}()
if n.IsIndexing() || n.IsSaving() {
return nil
}
ic := n.vq.IVQLen() + n.vq.DVQLen()
if ic == 0 {
return errors.ErrUncommittedIndexNotFound
}
n.indexing.Store(true)
defer n.indexing.Store(false)
defer n.gc()
log.Infof("create index operation started, uncommitted indexes = %d", ic)
log.Debug("create index delete phase started")
n.vq.RangePopDelete(ctx, func(uuid string) bool {
var ierr error
oid, ok := n.kvs.Delete(uuid)
if ok {
ierr = n.core.Remove(uint(oid))
} else {
ierr = errors.ErrObjectIDNotFound(uuid)
}
if ierr != nil {
log.Error(ierr)
err = errors.Wrap(err, ierr.Error())
}
return true
})
log.Debug("create index delete phase finished")
n.gc()
log.Debug("create index insert phase started")
n.vq.RangePopInsert(ctx, func(uuid string, vector []float32) bool {
oid, ierr := n.core.Insert(vector)
if ierr != nil {
log.Error(ierr)
err = errors.Wrap(err, ierr.Error())
} else {
n.kvs.Set(uuid, uint32(oid))
}
return true
})
log.Debug("create index insert phase finished")
log.Debug("create graph and tree phase started")
log.Debugf("pool size = %d", poolSize)
cierr := n.core.CreateIndex(poolSize)
if cierr != nil {
log.Error("an error occurred on creating graph and tree phase:", cierr)
err = errors.Wrap(err, cierr.Error())
}
log.Debug("create graph and tree phase finished")
log.Info("create index operation finished")
atomic.AddUint64(&n.nocie, 1)
return err
}
func (n *ngt) SaveIndex(ctx context.Context) (err error) {
ctx, span := trace.StartSpan(ctx, "vald/agent-ngt/service/NGT.SaveIndex")
defer func() {
if span != nil {
span.End()
}
}()
if len(n.path) != 0 && !n.inMem {
err = n.saveIndex(ctx)
}
return
}
func (n *ngt) saveIndex(ctx context.Context) (err error) {
noice := atomic.LoadUint64(&n.nocie)
if atomic.LoadUint64(&n.lastNoice) == noice {
return
}
atomic.SwapUint64(&n.lastNoice, noice)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
// wait for not indexing & not saving
for n.IsIndexing() || n.IsSaving() {
runtime.Gosched()
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
n.saving.Store(true)
defer n.gc()
defer n.saving.Store(false)
eg, ctx := errgroup.New(ctx)
eg.Go(safety.RecoverFunc(func() (err error) {
if n.path != "" {
m := make(map[string]uint32, n.kvs.Len())
var mu sync.Mutex
n.kvs.Range(ctx, func(key string, id uint32) bool {
mu.Lock()
m[key] = id
mu.Unlock()
return true
})
var f *os.File
f, err = file.Open(
filepath.Join(n.path, kvsFileName),
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
fs.ModePerm,
)
if err != nil {
return err
}
defer func() {
if f != nil {
derr := f.Close()
if derr != nil {
err = errors.Wrap(err, derr.Error())
}
}
}()
gob.Register(map[string]uint32{})
err = gob.NewEncoder(f).Encode(&m)
if err != nil {
return err
}
err = f.Sync()
if err != nil {
return err
}
}
return nil
}))
eg.Go(safety.RecoverFunc(func() error {
return n.core.SaveIndex()
}))
err = eg.Wait()
if err != nil {
return err
}
return metadata.Store(
filepath.Join(n.path, metadata.AgentMetadataFileName),
&metadata.Metadata{
IsInvalid: false,
NGT: &metadata.NGT{
IndexCount: n.Len(),
},
},
)
}
func (n *ngt) CreateAndSaveIndex(ctx context.Context, poolSize uint32) (err error) {
ctx, span := trace.StartSpan(ctx, "vald/agent-ngt/service/NGT.CreateAndSaveIndex")
defer func() {
if span != nil {
span.End()
}
}()
err = n.CreateIndex(ctx, poolSize)
if err != nil {
return err
}
return n.SaveIndex(ctx)
}
func (n *ngt) Exists(uuid string) (oid uint32, ok bool) {
oid, ok = n.kvs.Get(uuid)
if !ok {
ok = n.vq.IVExists(uuid)
} else {
ok = !n.vq.DVExists(uuid)
}
return oid, ok
}
func (n *ngt) readyForUpdate(uuid string, vec []float32) (err error) {
if len(uuid) == 0 {
return errors.ErrUUIDNotFound(0)
}
if len(vec) == 0 {
return errors.ErrInvalidDimensionSize(len(vec), 0)
}
ovec, err := n.GetObject(uuid)
if err != nil || len(vec) != len(ovec) {
// if error (GetObject cannot find vector) or vector length is not equal let's try update
return nil
}
for i, v := range vec {
if v != ovec[i] {
// if difference exists return nil for update
return nil
}
}
// if no difference exists (same vector already exists) return error for skip update
return errors.ErrUUIDAlreadyExists(uuid)
}
func (n *ngt) IsSaving() bool {
s, ok := n.saving.Load().(bool)
return s && ok
}
func (n *ngt) IsIndexing() bool {
i, ok := n.indexing.Load().(bool)
return i && ok
}
func (n *ngt) UUIDs(ctx context.Context) (uuids []string) {
uuids = make([]string, 0, n.kvs.Len())
n.kvs.Range(ctx, func(uuid string, oid uint32) bool {
uuids = append(uuids, uuid)
return true
})
return uuids
}
func (n *ngt) NumberOfCreateIndexExecution() uint64 {
return atomic.LoadUint64(&n.nocie)
}
func (n *ngt) NumberOfProactiveGCExecution() uint64 {
return atomic.LoadUint64(&n.nogce)
}
func (n *ngt) gc() {
if n.enableProactiveGC {
runtime.GC()
atomic.AddUint64(&n.nogce, 1)
}
}
func (n *ngt) Len() uint64 {
return n.kvs.Len()
}
func (n *ngt) InsertVQueueBufferLen() uint64 {
return uint64(n.vq.IVQLen())
}
func (n *ngt) DeleteVQueueBufferLen() uint64 {
return uint64(n.vq.DVQLen())
}
func (n *ngt) InsertVQueueChannelLen() uint64 {
return uint64(n.vq.IVCLen())
}
func (n *ngt) DeleteVQueueChannelLen() uint64 {
return uint64(n.vq.DVCLen())
}
func (n *ngt) GetDimensionSize() int {
return n.dim
}
func (n *ngt) Close(ctx context.Context) (err error) {
if len(n.path) != 0 {
err = n.CreateAndSaveIndex(ctx, n.poolSize)
}
n.core.Close()
return
}