Platform.Data.Doublets C++
Loading...
Searching...
No Matches
SplitMemoryLinksBase.h
Go to the documentation of this file.
2{
3 template <typename TSelf, typename TLinksOptions, typename TMemory, typename TInternalSourcesTreeMethods,
4 typename TInternalLinksSourcesLinkedListMethods, typename TInternalTargetsTreeMethods,
5 typename TExternalSourcesTreeMethods, typename TExternalTargetsTreeMethods,
6 typename TUnusedLinksListMethods, typename... TBase>
7 struct SplitMemoryLinksBase : public Interfaces::Polymorph<TSelf, TBase...>
8 {
9 public:
10 using LinksOptionsType = TLinksOptions;
11 using LinkAddressType = typename LinksOptionsType::LinkAddressType;
12 using LinkType = typename LinksOptionsType::LinkType;
13 using WriteHandlerType = typename LinksOptionsType::WriteHandlerType;
14 using ReadHandlerType = typename LinksOptionsType::ReadHandlerType;
15 static constexpr LinksConstants<LinkAddressType> Constants = LinksOptionsType::Constants;
16 static constexpr bool UseLinkedList = false;
17
18 public:
19 TMemory _dataMemory;
20 TMemory _indexMemory;
23 std::unique_ptr<TInternalSourcesTreeMethods> InternalSourcesTreeMethods;
24 std::unique_ptr<TInternalLinksSourcesLinkedListMethods> InternalSourcesListMethods;
25 std::unique_ptr<TInternalTargetsTreeMethods> InternalTargetsTreeMethods;
26 std::unique_ptr<TExternalSourcesTreeMethods> ExternalSourcesTreeMethods;
27 std::unique_ptr<TExternalTargetsTreeMethods> ExternalTargetsTreeMethods;
28 std::unique_ptr<TUnusedLinksListMethods> UnusedLinksListMethods;
29 std::byte* _header;
30 std::byte* _linksDataParts;
31 std::byte* _linksIndexParts;
32
33 public:
39
45
51
57
58 public:
60 {
61 return *reinterpret_cast<LinksHeader<LinkAddressType>*>(_header);
62 }
63
68
70 {
71 return this->GetHeaderReference().AllocatedLinks - this->GetHeaderReference().FreeLinks;
72 }
73
74 public:
75 static constexpr std::size_t LinkDataPartSizeInBytes = sizeof(RawLinkDataPart<LinkAddressType>);
76
77 static constexpr std::size_t LinkIndexPartSizeInBytes = sizeof(RawLinkIndexPart<LinkAddressType>);
78
79 static constexpr std::size_t LinkHeaderSizeInBytes = sizeof(LinksHeader<LinkAddressType>);
80
81 static constexpr std::size_t DefaultLinksSizeStep = 1 * 1024 * 1024;
82
83 SplitMemoryLinksBase(TMemory&& dataMemory, TMemory&& indexMemory)
84 : SplitMemoryLinksBase(std::move(dataMemory), std::move(indexMemory), DefaultLinksSizeStep)
85 {
86 }
87
88 SplitMemoryLinksBase(TMemory&& dataMemory, TMemory&& indexMemory, std::uint64_t memoryReservationStep)
89 : _dataMemory{std::move(dataMemory)}, _indexMemory{std::move(indexMemory)},
92 {
93
95 }
96
97 void Init(TMemory& dataMemory, TMemory& indexMemory)
98 {
99 if (indexMemory.ReservedCapacity() < LinkHeaderSizeInBytes)
100 {
101 indexMemory.ReservedCapacity(LinkHeaderSizeInBytes);
102 }
103 SetPointers(dataMemory, indexMemory);
104 auto allocatedLinks{this->GetHeaderReference().AllocatedLinks};
105 auto minimumDataReservedCapacity = (allocatedLinks + 1) * LinkDataPartSizeInBytes;
106 if (minimumDataReservedCapacity < dataMemory.UsedCapacity())
107 {
108 minimumDataReservedCapacity = dataMemory.UsedCapacity();
109 }
110 if (minimumDataReservedCapacity < _dataMemoryReservationStepInBytes)
111 {
112 minimumDataReservedCapacity = _dataMemoryReservationStepInBytes;
113 }
114 auto minimumIndexReservedCapacity = allocatedLinks * LinkIndexPartSizeInBytes + LinkHeaderSizeInBytes;
115 if (minimumIndexReservedCapacity < indexMemory.UsedCapacity())
116 {
117 minimumIndexReservedCapacity = indexMemory.UsedCapacity();
118 }
119 if (minimumIndexReservedCapacity < _indexMemoryReservationStepInBytes)
120 {
121 minimumIndexReservedCapacity = _indexMemoryReservationStepInBytes;
122 }
123 // Check for alignment
124 if (minimumDataReservedCapacity % _dataMemoryReservationStepInBytes > 0)
125 {
126 minimumDataReservedCapacity = ((minimumDataReservedCapacity / _dataMemoryReservationStepInBytes) *
129 }
130 if (minimumIndexReservedCapacity % _indexMemoryReservationStepInBytes > 0)
131 {
132 minimumIndexReservedCapacity = ((minimumIndexReservedCapacity / _indexMemoryReservationStepInBytes) *
135 }
136 if (dataMemory.ReservedCapacity() < minimumDataReservedCapacity)
137 {
138 dataMemory.ReservedCapacity(minimumDataReservedCapacity);
139 }
140 if (indexMemory.ReservedCapacity() < minimumIndexReservedCapacity)
141 {
142 indexMemory.ReservedCapacity(minimumIndexReservedCapacity);
143 }
144 SetPointers(dataMemory, indexMemory);
145 // Ensure correctness _memory.UsedCapacity over _header->AllocatedLinks
146 dataMemory.UsedCapacity((this->GetHeaderReference().AllocatedLinks * LinkDataPartSizeInBytes) +
147 LinkDataPartSizeInBytes); // First link is read only zero link.
148 indexMemory.UsedCapacity((this->GetHeaderReference().AllocatedLinks * LinkIndexPartSizeInBytes) +
150 // Ensure correctness _memory.ReservedLinks over _header->ReservedCapacity
151 this->GetHeaderReference().ReservedLinks =
152 std::min((dataMemory.ReservedCapacity() - LinkDataPartSizeInBytes) / LinkDataPartSizeInBytes,
153 (indexMemory.ReservedCapacity() - LinkHeaderSizeInBytes) / LinkIndexPartSizeInBytes);
154 }
155
156 LinkAddressType Count(const LinkType& restriction) const
157 {
158 // Query length selects the constrained parts: total, address, one value, or a pair.
159 // The total excludes reusable records recorded in the free-list header.
160 if (std::ranges::size(restriction) == 0)
161 {
162 return this->Total();
163 }
164 auto index = GetIndex(*this, restriction);
165 auto any = Constants.Any;
166 if (std::ranges::size(restriction) == 1)
167 {
168 if (index == any)
169 {
170 return this->Total();
171 }
172 return this->Exists(index) ? LinkAddressType{1} : LinkAddressType{0};
173 }
174 if (std::ranges::size(restriction) == 2)
175 {
176 // A value-only query can use either the internal or external usage indexes.
177 auto value = restriction[1];
178 if (index == any)
179 {
180 if (value == any)
181 {
182 return this->Total(); // Any - как отсутствие ограничения
183 }
184 auto externalReferencesRange = Constants.ExternalReferencesRange;
185 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(value))
186 {
187 return ExternalSourcesTreeMethods->CountUsages(value) +
188 ExternalTargetsTreeMethods->CountUsages(value);
189 }
190 else
191 {
192 if (UseLinkedList)
193 {
194 return InternalSourcesListMethods->CountUsages(value) +
195 InternalTargetsTreeMethods->CountUsages(value);
196 }
197 else
198 {
199 return InternalSourcesTreeMethods->CountUsages(value) +
200 InternalTargetsTreeMethods->CountUsages(value);
201 }
202 }
203 }
204 else
205 {
206 if (!this->Exists(index))
207 {
208 return LinkAddressType{0};
209 }
210 if (value == any)
211 {
212 return LinkAddressType{1};
213 }
214 auto& storedLinkValue = this->GetLinkDataPartReference(index);
215 if ((storedLinkValue.Source == value) || (storedLinkValue.Target == value))
216 {
217 return LinkAddressType{1};
218 }
219 return LinkAddressType{0};
220 }
221 }
222 if (std::ranges::size(restriction) == 3)
223 {
224 // Source and target wildcards determine which usage tree can answer the query.
225 // Concrete source/target values require an exact pair search.
226 auto externalReferencesRange = Constants.ExternalReferencesRange;
227 auto source = GetSource(*this, restriction);
228 auto target = GetTarget(*this, restriction);
229 if (index == any)
230 {
231 if ((source == any) && (target == any))
232 {
233 return this->Total();
234 }
235 else if (source == any)
236 {
237 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(target))
238 {
239 return ExternalTargetsTreeMethods->CountUsages(target);
240 }
241 else
242 {
243 return InternalTargetsTreeMethods->CountUsages(target);
244 }
245 }
246 else if (target == any)
247 {
248 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(source))
249 {
250 return ExternalSourcesTreeMethods->CountUsages(source);
251 }
252 else
253 {
254 if (UseLinkedList)
255 {
256 return InternalSourcesTreeMethods->CountUsages(source);
257 }
258 else
259 {
260 return InternalSourcesTreeMethods->CountUsages(source);
261 }
262 }
263 }
264 else // if(source != Any && target != Any)
265 {
266 // Эквивалент Exists(source, target) => Count(Any, source, target) > 0
267 LinkAddressType linkAddress;
268 if (Constants.IsExternalReferencesRangeEnabled)
269 {
270 if (externalReferencesRange.Contains(source) && externalReferencesRange.Contains(target))
271 {
272 linkAddress = ExternalSourcesTreeMethods->Search(source, target);
273 }
274 else if (externalReferencesRange.Contains(source))
275 {
276 linkAddress = InternalTargetsTreeMethods->Search(source, target);
277 }
278 else if (externalReferencesRange.Contains(target))
279 {
280 if (UseLinkedList)
281 {
282 linkAddress = ExternalSourcesTreeMethods->Search(source, target);
283 }
284 else
285 {
286 linkAddress = InternalSourcesTreeMethods->Search(source, target);
287 }
288 }
289 else
290 {
291 if (UseLinkedList || InternalSourcesTreeMethods->CountUsages(source) >
292 InternalTargetsTreeMethods->CountUsages(target))
293 {
294 linkAddress = InternalTargetsTreeMethods->Search(source, target);
295 }
296 else
297 {
298 linkAddress = InternalSourcesTreeMethods->Search(source, target);
299 }
300 }
301 }
302 else
303 {
304 if (UseLinkedList || InternalSourcesTreeMethods->CountUsages(source) >
305 InternalTargetsTreeMethods->CountUsages(target))
306 {
307 linkAddress = InternalTargetsTreeMethods->Search(source, target);
308 }
309 else
310 {
311 linkAddress = InternalSourcesTreeMethods->Search(source, target);
312 }
313 }
314 return (linkAddress == Constants.Null) ? LinkAddressType{0} : LinkAddressType{1};
315 }
316 }
317 else
318 {
319 if (!this->Exists(index))
320 {
321 return LinkAddressType{0};
322 }
323 if ((source == any) && (target == any))
324 {
325 return LinkAddressType{1};
326 }
327 auto& storedLinkValue = GetLinkDataPartReference(index);
328 // Three-part restrictions match each endpoint in its own position.
329 if ((source == any || storedLinkValue.Source == source) &&
330 (target == any || storedLinkValue.Target == target))
331 {
332 return LinkAddressType{1};
333 }
334 return LinkAddressType{0};
335 }
336 }
337 throw Platform::Exceptions::NotSupportedException();
338 }
339 //
340 void SetPointers(TMemory& dataMemory, TMemory& indexMemory)
341 {
342 _linksDataParts = static_cast<std::byte*>(dataMemory.Pointer());
343 _linksIndexParts = static_cast<std::byte*>(indexMemory.Pointer());
345 if (UseLinkedList)
346 {
348 std::make_unique<TInternalLinksSourcesLinkedListMethods>(_linksDataParts, _linksIndexParts);
349 }
350 else
351 {
353 std::make_unique<TInternalSourcesTreeMethods>(_linksDataParts, _linksIndexParts, _header);
354 }
356 std::make_unique<TInternalTargetsTreeMethods>(_linksDataParts, _linksIndexParts, _header);
358 std::make_unique<TExternalSourcesTreeMethods>(_linksDataParts, _linksIndexParts, _header);
360 std::make_unique<TExternalTargetsTreeMethods>(_linksDataParts, _linksIndexParts, _header);
361 UnusedLinksListMethods = std::make_unique<TUnusedLinksListMethods>(_linksDataParts, _header);
362 }
363
364 LinkAddressType Count(const LinkType& restriction)
365 {
366 using namespace Platform::Exceptions;
367 auto length = std::ranges::size(restriction);
368 // Если нет ограничений, тогда возвращаем общее число связей находящихся в хранилище.
369 if (length == 0)
370 {
371 return Total();
372 }
373 auto constants = Constants;
374 auto any = constants.Any;
375 auto index = GetIndex(*this, restriction);
376 if (length == 1)
377 {
378 if (index == any)
379 {
380 return Total();
381 }
382 return Exists(index) ? LinkAddressType{1} : LinkAddressType{0};
383 }
384 if (length == 2)
385 {
386 auto value = restriction[1];
387 if (index == any)
388 {
389 if (value == any)
390 {
391 return Total(); // Any - как отсутствие ограничения
392 }
393 LinkAddressType count{};
394 if (value == constants.Null)
395 {
396 Each(restriction, [&](const auto&) {
397 ++count;
398 return constants.Continue;
399 });
400 return count;
401 }
402 count = Count(LinkType{index, value, any});
403 // The target pass adds only records absent from the source pass.
404 Each(LinkType{index, any, value}, [&](const auto& link) {
405 if (link[constants.SourcePart] != value)
406 ++count;
407 return constants.Continue;
408 });
409 return count;
410 }
411 else
412 {
413 if (!Exists(index))
414 {
415 return LinkAddressType{0};
416 }
417 if (value == any)
418 {
419 return LinkAddressType{1};
420 }
421 auto& storedLinkValue = GetLinkDataPartReference(index);
422 if ((storedLinkValue.Source == value) || (storedLinkValue.Target == value))
423 {
424 return LinkAddressType{1};
425 }
426 return LinkAddressType{0};
427 }
428 }
429 if (length == 3)
430 {
431 auto externalReferencesRange = constants.ExternalReferencesRange;
432 auto source = GetSource(*this, restriction);
433 auto target = GetTarget(*this, restriction);
434 if (index == any)
435 {
436 if ((source == any) && (target == any))
437 {
438 return Total();
439 }
440 else if (source == any)
441 {
442 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(target))
443 {
444 return ExternalTargetsTreeMethods->CountUsages(target);
445 }
446 else
447 {
448 return InternalTargetsTreeMethods->CountUsages(target);
449 }
450 }
451 else if (target == any)
452 {
453 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(source))
454 {
455 return ExternalSourcesTreeMethods->CountUsages(source);
456 }
457 else
458 {
459 if (UseLinkedList)
460 {
461 return InternalSourcesListMethods->CountUsages(source);
462 }
463 else
464 {
465 return InternalSourcesTreeMethods->CountUsages(source);
466 }
467 }
468 }
469 else // if(source != Any && target != Any)
470 {
471 // Эквивалент Exists(source, target) => Count(Any, source, target) > 0
472 LinkAddressType linkAddress;
473 if (Constants.IsExternalReferencesRangeEnabled)
474 {
475 if (externalReferencesRange.Contains(source) && externalReferencesRange.Contains(target))
476 {
477 linkAddress = ExternalSourcesTreeMethods->Search(source, target);
478 }
479 else if (externalReferencesRange.Contains(source))
480 {
481 linkAddress = InternalTargetsTreeMethods->Search(source, target);
482 }
483 else if (externalReferencesRange.Contains(target))
484 {
485 if (UseLinkedList)
486 {
487 linkAddress = ExternalSourcesTreeMethods->Search(source, target);
488 }
489 else
490 {
491 linkAddress = InternalSourcesTreeMethods->Search(source, target);
492 }
493 }
494 else
495 {
496 if (UseLinkedList || (InternalSourcesTreeMethods->CountUsages(source) >
497 InternalTargetsTreeMethods->CountUsages(target)))
498 {
499 linkAddress = InternalTargetsTreeMethods->Search(source, target);
500 }
501 else
502 {
503 linkAddress = InternalSourcesTreeMethods->Search(source, target);
504 }
505 }
506 }
507 else
508 {
509 if (UseLinkedList || (InternalSourcesTreeMethods->CountUsages(source) >
510 InternalTargetsTreeMethods->CountUsages(target)))
511 {
512 linkAddress = InternalTargetsTreeMethods->Search(source, target);
513 }
514 else
515 {
516 linkAddress = InternalSourcesTreeMethods->Search(source, target);
517 }
518 }
519 return (linkAddress == constants.Null) ? LinkAddressType{0} : LinkAddressType{1};
520 }
521 }
522 else
523 {
524 if (!Exists(index))
525 {
526 return LinkAddressType{0};
527 }
528 if ((source == any) && (target == any))
529 {
530 return LinkAddressType{1};
531 }
532 auto& storedLinkValue = GetLinkDataPartReference(index);
533 // Three-part restrictions match each endpoint in its own position.
534 if ((source == any || storedLinkValue.Source == source) &&
535 (target == any || storedLinkValue.Target == target))
536 {
537 return LinkAddressType{1};
538 }
539 return LinkAddressType{0};
540 }
541 }
542 throw Platform::Exceptions::NotSupportedException();
543 }
544
545 LinkAddressType Each(const LinkType& restriction, const ReadHandlerType& handler) const
546 {
547 // Enumerate only live records and propagate the callback's request to stop.
548 // Query shape determines whether to scan addresses or traverse a usage index.
549 using namespace Platform::Exceptions;
550 auto constants = Constants;
551 auto $break = constants.Break;
552 auto length = std::ranges::size(restriction);
553 if (length == 0)
554 {
555 // An unrestricted scan skips the zero record and entries on the free list.
556 for (auto linkAddress = LinkAddressType{1}; (linkAddress <= this->GetHeaderReference().AllocatedLinks);
557 ++linkAddress)
558 {
559 if (Exists(linkAddress) && (handler(GetLinkStruct(linkAddress)) == $break))
560 {
561 return $break;
562 }
563 }
564 return constants.Continue;
565 }
566 auto $continue = constants.Continue;
567 auto any = constants.Any;
568 auto index = GetIndex(*this, restriction);
569 if (length == 1)
570 {
571 if (index == any)
572 {
573 return Each(LinkType{}, handler);
574 }
575 if (!Exists(index))
576 {
577 return $continue;
578 }
579 return handler(GetLinkStruct(index));
580 }
581 if (length == 2)
582 {
583 // The value may be internal or external, which selects the matching index family.
584 auto value = restriction[1];
585 if (index == any)
586 {
587 if (value == any)
588 {
589 return Each(LinkType{}, handler);
590 }
591 if (value == constants.Null)
592 {
593 // Null endpoints are absent from the usage indexes.
594 return Each(LinkType{}, [&](const auto& link) {
595 return link[constants.SourcePart] == value || link[constants.TargetPart] == value
596 ? handler(link)
597 : $continue;
598 });
599 }
600 if (Each(LinkType{index, value, any}, handler) == $break)
601 {
602 return $break;
603 }
604 return Each(LinkType{index, any, value}, [&](const auto& link) {
605 return link[constants.SourcePart] == value ? $continue : handler(link);
606 });
607 }
608 else
609 {
610 if (!Exists(index))
611 {
612 return $continue;
613 }
614 if (value == any)
615 {
616 return handler(GetLinkStruct(index));
617 }
618 auto& storedLinkValue = GetLinkDataPartReference(index);
619 if ((storedLinkValue.Source == value) || (storedLinkValue.Target == value))
620 {
621 return handler(GetLinkStruct(index));
622 }
623 return $continue;
624 }
625 }
626 if (length == 3)
627 {
628 // Wildcards choose source/target traversal; concrete values use the pair lookup.
629 // A specified address is checked directly instead of enumerating unrelated links.
630 auto externalReferencesRange = constants.ExternalReferencesRange;
631 auto source = GetSource(*this, restriction);
632 auto target = GetTarget(*this, restriction);
633 if (index == any)
634 {
635 if ((source == any) && (target == any))
636 {
637 return Each(LinkType{}, handler);
638 }
639 else if (source == any)
640 {
641 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(target))
642 {
643 return ExternalTargetsTreeMethods->EachUsage(target, handler);
644 }
645 else
646 {
647 return InternalTargetsTreeMethods->EachUsage(target, handler);
648 }
649 }
650 else if (target == any)
651 {
652 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(source))
653 {
654 return ExternalSourcesTreeMethods->EachUsage(source, handler);
655 }
656 else
657 {
658 if (UseLinkedList)
659 {
660 return InternalSourcesListMethods->EachUsage(source, handler);
661 }
662 else
663 {
664 return InternalSourcesTreeMethods->EachUsage(source, handler);
665 }
666 }
667 }
668 else // if(source != Any && target != Any)
669 {
670 LinkAddressType linkAddress;
671 if (Constants.IsExternalReferencesRangeEnabled)
672 {
673 if (externalReferencesRange.Contains(source) && externalReferencesRange.Contains(target))
674 {
675 linkAddress = ExternalSourcesTreeMethods->Search(source, target);
676 }
677 else if (externalReferencesRange.Contains(source))
678 {
679 linkAddress = InternalTargetsTreeMethods->Search(source, target);
680 }
681 else if (externalReferencesRange.Contains(target))
682 {
683 if (UseLinkedList)
684 {
685 linkAddress = ExternalSourcesTreeMethods->Search(source, target);
686 }
687 else
688 {
689 linkAddress = InternalSourcesTreeMethods->Search(source, target);
690 }
691 }
692 else
693 {
694 if (UseLinkedList || (InternalSourcesTreeMethods->CountUsages(source) ==
695 InternalTargetsTreeMethods->CountUsages(target)))
696 {
697 linkAddress = InternalTargetsTreeMethods->Search(source, target);
698 }
699 else
700 {
701 linkAddress = InternalSourcesTreeMethods->Search(source, target);
702 }
703 }
704 }
705 else
706 {
707 if (UseLinkedList || (InternalSourcesTreeMethods->CountUsages(source) >
708 InternalTargetsTreeMethods->CountUsages(target)))
709 {
710 linkAddress = InternalTargetsTreeMethods->Search(source, target);
711 }
712 else
713 {
714 linkAddress = InternalSourcesTreeMethods->Search(source, target);
715 }
716 }
717 return (linkAddress == constants.Null) ? $continue : handler(GetLinkStruct(linkAddress));
718 }
719 }
720 else
721 {
722 if (!Exists(index))
723 {
724 return $continue;
725 }
726 if ((source == any) && (target == any))
727 {
728 return handler(GetLinkStruct(index));
729 }
730 auto& storedLinkValue = GetLinkDataPartReference(index);
731 // Three-part restrictions match each endpoint in its own position.
732 if ((source == any || storedLinkValue.Source == source) &&
733 (target == any || storedLinkValue.Target == target))
734 {
735 return handler(GetLinkStruct(index));
736 }
737 return $continue;
738 }
739 }
740 throw Platform::Exceptions::NotSupportedException();
741 }
742
743 LinkAddressType Update(const LinkType& restriction, const LinkType& substitution,
744 const WriteHandlerType& handler)
745 {
746 auto constants = Constants;
747 auto $null = constants.Null;
748 auto externalReferencesRange = constants.ExternalReferencesRange;
749 auto linkIndex = GetIndex(*this, restriction);
750 auto before = this->GetLinkStruct(linkIndex);
751 auto& link = this->GetLinkDataPartReference(linkIndex);
752 auto source = link.Source;
753 auto target = link.Target;
754 auto& rootAsSource = this->GetHeaderReference().RootAsSource;
755 auto& rootAsTarget = this->GetHeaderReference().RootAsTarget;
756 // Будет корректно работать только в том случае, если пространство выделенной связи предварительно заполнено
757 // нулями
758 if (source != $null)
759 {
760 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(source))
761 {
762 ExternalSourcesTreeMethods->Detach(&rootAsSource, linkIndex);
763 }
764 else
765 {
766 if (UseLinkedList)
767 {
768 InternalSourcesListMethods->Detach(source, linkIndex);
769 }
770 else
771 {
772 InternalSourcesTreeMethods->Detach(&(GetLinkIndexPartReference(source).RootAsSource),
773 linkIndex);
774 }
775 }
776 }
777 if (target != $null)
778 {
779 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(target))
780 {
781 ExternalTargetsTreeMethods->Detach(&rootAsTarget, linkIndex);
782 }
783 else
784 {
785 InternalTargetsTreeMethods->Detach(&(GetLinkIndexPartReference(target).RootAsTarget), linkIndex);
786 }
787 }
788 source = link.Source = GetSource(*this, substitution);
789 target = link.Target = GetTarget(*this, substitution);
790 if (source != $null)
791 {
792 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(source))
793 {
794 ExternalSourcesTreeMethods->Attach(&rootAsSource, linkIndex);
795 }
796 else
797 {
798 if (UseLinkedList)
799 {
800 InternalSourcesListMethods->AttachAsLast(source, linkIndex);
801 }
802 else
803 {
804 InternalSourcesTreeMethods->Attach(&(GetLinkIndexPartReference(source).RootAsSource),
805 linkIndex);
806 }
807 }
808 }
809 if (target != $null)
810 {
811 if (Constants.IsExternalReferencesRangeEnabled && externalReferencesRange.Contains(target))
812 {
813 ExternalTargetsTreeMethods->Attach(&rootAsTarget, linkIndex);
814 }
815 else
816 {
817 InternalTargetsTreeMethods->Attach(&(GetLinkIndexPartReference(target).RootAsTarget), linkIndex);
818 }
819 }
820 return handler ? handler(before, LinkType{linkIndex, source, target}) : Constants.Continue;
821 }
822
824 {
825 using namespace Platform::Exceptions;
826 auto freeLink = this->GetHeaderReference().FirstFreeLink;
827 if (freeLink != Constants.Null)
828 {
829 UnusedLinksListMethods->Detach(freeLink);
830 }
831 else
832 {
833 auto maximumPossibleInnerReference = Constants.InternalReferencesRange.Maximum;
834 if (this->GetHeaderReference().AllocatedLinks >= maximumPossibleInnerReference)
835 {
836 throw Platform::Exceptions::LinksLimitReachedException();
837 }
838 if (this->GetHeaderReference().AllocatedLinks >= this->GetHeaderReference().ReservedLinks)
839 {
840 _dataMemory.ReservedCapacity(_dataMemory.ReservedCapacity() + _dataMemoryReservationStepInBytes);
841 _indexMemory.ReservedCapacity(_indexMemory.ReservedCapacity() + _indexMemoryReservationStepInBytes);
843 this->GetHeaderReference().ReservedLinks =
844 std::min((_dataMemory.ReservedCapacity() - LinkDataPartSizeInBytes) / LinkDataPartSizeInBytes,
846 }
847 freeLink = ++this->GetHeaderReference().AllocatedLinks;
848 _dataMemory.UsedCapacity(_dataMemory.UsedCapacity() + LinkDataPartSizeInBytes);
849 _indexMemory.UsedCapacity(_indexMemory.UsedCapacity() + LinkIndexPartSizeInBytes);
850 }
851 return handler ? handler(LinkType{}, GetLinkStruct(freeLink)) : Constants.Continue;
852 }
853
854 LinkAddressType Delete(const LinkType& restriction, const WriteHandlerType& handler)
855 {
856 auto linkAddress = GetIndex(*this, restriction);
857 auto before = GetLinkStruct(linkAddress);
858 if (linkAddress < this->GetHeaderReference().AllocatedLinks)
859 {
860 UnusedLinksListMethods->AttachAsFirst(linkAddress);
861 return handler ? handler(before, LinkType{}) : Constants.Continue;
862 }
863 else if (linkAddress == this->GetHeaderReference().AllocatedLinks)
864 {
865 --this->GetHeaderReference().AllocatedLinks;
866 _dataMemory.UsedCapacity(_dataMemory.UsedCapacity() - LinkDataPartSizeInBytes);
867 _indexMemory.UsedCapacity(_indexMemory.UsedCapacity() - LinkIndexPartSizeInBytes);
868 // Убираем все связи, находящиеся в списке свободных в конце файла, до тех пор, пока не дойдём до первой
869 // существующей связи Позволяет оптимизировать количество выделенных связей (AllocatedLinks)
870 while ((this->GetHeaderReference().AllocatedLinks > LinkAddressType{0}) &&
871 this->IsUnusedLink(this->GetHeaderReference().AllocatedLinks))
872 {
873 UnusedLinksListMethods->Detach(this->GetHeaderReference().AllocatedLinks);
874 --this->GetHeaderReference().AllocatedLinks;
875 _dataMemory.UsedCapacity(_dataMemory.UsedCapacity() - LinkDataPartSizeInBytes);
876 _indexMemory.UsedCapacity(_indexMemory.UsedCapacity() - LinkIndexPartSizeInBytes);
877 }
878 return handler ? handler(before, LinkType{}) : Constants.Continue;
879 }
880 return Constants.Continue;
881 }
882
883 public:
884 bool IsUnusedLink(LinkAddressType linkIndex) const
885 {
886 if (GetHeaderReference().FirstFreeLink != linkIndex) // May be this check is not needed
887 {
888 // TODO: Reduce access to memory in different location (should be enough to use just linkIndexPart)
889 const auto& linkDataPart = this->GetLinkDataPartReference(linkIndex);
890 const auto& linkIndexPart = this->GetLinkIndexPartReference(linkIndex);
891 return (linkIndexPart.SizeAsTarget == LinkAddressType{0}) &&
892 (linkDataPart.Source != LinkAddressType{0});
893 }
894 else
895 {
896 return true;
897 }
898 }
899
900 bool Exists(LinkAddressType linkAddress) const
901 {
902 return (linkAddress >= Constants.InternalReferencesRange.Minimum) &&
903 (linkAddress <= this->GetHeaderReference().AllocatedLinks) && !IsUnusedLink(linkAddress);
904 }
905
906 public:
908 {
909 const auto& link = this->GetLinkDataPartReference(linkIndex);
910 return LinkType{linkIndex, link.Source, link.Target};
911 }
912 };
913} // namespace Platform::Data::Doublets::Memory::Split::Generic
Definition ExternalLinksRecursionlessSizeBalancedTreeMethodsBase.h:2
Definition RawLinkDataPart.h:38
LinkAddressType Create(const LinkType &, const WriteHandlerType &handler)
Definition SplitMemoryLinksBase.h:823
LinkAddressType Count(const LinkType &restriction)
Definition SplitMemoryLinksBase.h:364
LinkAddressType Total() const
Definition SplitMemoryLinksBase.h:69
const LinksHeader< LinkAddressType > & GetHeaderReference() const
Definition SplitMemoryLinksBase.h:59
RawLinkDataPart< LinkAddressType > & GetLinkDataPartReference(LinkAddressType linkIndex)
Definition SplitMemoryLinksBase.h:40
const RawLinkDataPart< LinkAddressType > & GetLinkDataPartReference(LinkAddressType linkIndex) const
Definition SplitMemoryLinksBase.h:34
const RawLinkIndexPart< LinkAddressType > & GetLinkIndexPartReference(LinkAddressType linkIndex) const
Definition SplitMemoryLinksBase.h:46
LinkAddressType Count(const LinkType &restriction) const
Definition SplitMemoryLinksBase.h:156
LinksHeader< LinkAddressType > & GetHeaderReference()
Definition SplitMemoryLinksBase.h:64
LinkAddressType Update(const LinkType &restriction, const LinkType &substitution, const WriteHandlerType &handler)
Definition SplitMemoryLinksBase.h:743
LinkAddressType Delete(const LinkType &restriction, const WriteHandlerType &handler)
Definition SplitMemoryLinksBase.h:854
SplitMemoryLinksBase(TMemory &&dataMemory, TMemory &&indexMemory, std::uint64_t memoryReservationStep)
Definition SplitMemoryLinksBase.h:88
SplitMemoryLinksBase(TMemory &&dataMemory, TMemory &&indexMemory)
Definition SplitMemoryLinksBase.h:83
RawLinkIndexPart< LinkAddressType > & GetLinkIndexPartReference(LinkAddressType linkIndex)
Definition SplitMemoryLinksBase.h:52