-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathchunk.py
More file actions
73 lines (57 loc) · 2.02 KB
/
chunk.py
File metadata and controls
73 lines (57 loc) · 2.02 KB
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
from typing import (
TYPE_CHECKING,
Iterable,
)
from docarray.array.memory import DocumentArrayInMemory
if TYPE_CHECKING: # pragma: no cover
from docarray.document import Document
class ChunkArray(DocumentArrayInMemory):
"""
:class:`ChunkArray` inherits from :class:`DocumentArray`.
It's a subset of Documents.
:param docs: Set of sub-documents (i.e chunks) of `reference_doc`
:param reference_doc: Reference :class:`Document` for the sub-documents
"""
def __init__(self, docs, reference_doc: 'Document'):
"""
Set constructor method.
:param doc_views: protobuf representation of the chunks
:param reference_doc: parent document
"""
self._ref_doc = reference_doc
super().__init__(docs)
if isinstance(docs, Iterable) and self._ref_doc is not None:
for d in docs:
d.parent_id = self._ref_doc.id
d.granularity = self._ref_doc.granularity + 1
def append(self, document: 'Document'):
"""Add a sub-document (i.e chunk) to the current Document.
:param document: Sub-document to be appended
.. note::
Comparing to :attr:`DocumentArray.append()`, this method adds more safeguard to
make sure the added chunk is legit.
"""
document.parent_id = self._ref_doc.id
document.granularity = self._ref_doc.granularity + 1
super().append(document)
@property
def reference_doc(self) -> 'Document':
"""
Get the document that :class:`ChunkArray` belongs to.
:return: reference doc
"""
return self._ref_doc
@property
def granularity(self) -> int:
"""
Get granularity of all document in this array.
:return: granularity
"""
return self._ref_doc.granularity + 1
@property
def adjacency(self) -> int:
"""
Get adjacency of all document in this array.
:return: adjacency
"""
return self._ref_doc.adjacency