Skip to content

Commit 030d9da

Browse files
committed
[ARROW-7277][Document] Add discussion about vector lifecycle
Change-Id: Ic5c0ce928a54a835a2e5a211de71e9d04fac0115
1 parent dc90ca3 commit 030d9da

1 file changed

Lines changed: 125 additions & 37 deletions

File tree

docs/source/java/vector.rst

Lines changed: 125 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,87 @@ Table with non-intuitive names (BigInt = 64 bit integer, etc).
3131

3232
It is important that vector is allocated before attempting to read or write,
3333
:class:`ValueVector` "should" strive to guarantee this order of operation:
34-
allocate > mutate > set valuecount > access > clear (or allocate to start the process over)
34+
create > allocate > mutate > set value count > access > clear (or allocate to start the process over).
35+
We will go through a concrete example to demonstrate each operation in the next section.
36+
37+
Vector Life Cycle
38+
====================
39+
As discussed above, each vector goes through several steps in its life cycle,
40+
and each step is triggered by a vector operation. In particular, we have the following vector operations:
41+
42+
1. **Vector creation**: we create a new vector object by, for example, the vector constructor.
43+
The following code creates a new ``IntVector`` by the constructor:
44+
45+
.. code-block:: Java
46+
47+
RootAllocator allocator = new RootAllocator(Long.MAX_VALUE);
48+
...
49+
IntVector vector = new IntVector("int vector", allocator);
50+
51+
By now, a vector object is created. However, no underlying memory has been allocated, so we need the
52+
following step.
53+
54+
2. **Vector allocation**: in this step, we allocate memory for the vector. For most vectors, we
55+
have two options: 1) if we know the maximum vector capacity, we can specify it by calling the
56+
``allocateNew(int)`` method; 2) otherwise, we should call the ``allocateNew()`` method, and a default
57+
capacity will be allocated for it. For our running example, we assume that the vector capacity never
58+
exceeds 10:
59+
60+
.. code-block:: Java
61+
62+
vector.allocateNew(10);
63+
64+
3. **Vector mutation**: now we can populate the vector with values we desire. For all vectors, we can populate
65+
vector values through vector writers (An example will be given in the next section). For primitive types,
66+
we can also mutate the vector by the set methods. There are two classes of set methods: 1) if we can
67+
be sure the vector has enough capacity, we can call the ``set(index, value)`` method. 2) if we are not sure
68+
about the vector capacity, we should call the ``setSafe(index, value)`` method, which will automatically
69+
take care of vector reallocation, if the capacity is not sufficient. For our running example, we know the
70+
vector has enough capacity, so we can call
71+
72+
.. code-block:: Java
73+
74+
vector.set(/*index*/5, /*value*/25);
75+
76+
4. **Set value count**: for this step, we set the value count of the vector by calling the
77+
``setValueCount(int)`` method:
78+
79+
.. code-block:: Java
80+
81+
vector.setValueCount(10);
82+
83+
After this step, the vector enters an immutable state. In other words, we should no longer mutate it.
84+
85+
5. **Vector access**: it is time to access vector values. Similarly, we have two options to access values:
86+
1) get methods and 2) vector reader. Vector reader works for all types of vectors, while get methods are
87+
only available for primitive vectors. A concrete example for vector reader will be given in the next section.
88+
Below is an example of vector access by get method:
89+
90+
.. code-block:: Java
91+
92+
int value = vector.get(5); // value == 25
93+
94+
6. **Vector clear**: when we are done with the vector, we should clear it to release its memory. This is done by
95+
calling the ``close()`` method:
96+
97+
.. code-block:: Java
98+
99+
vector.close();
100+
101+
Some points to note about the steps above:
102+
103+
* The steps are not necessarily performed in a linear sequence. Instead, they can be in a loop. For example,
104+
when a vector enters the access step, we can also go back to the vector mutation step, and then set value
105+
count, access vector, and so on.
106+
107+
* We should try to make sure the above steps are carried out in order. Otherwise, the vector
108+
may be in an undefined state, and some unexpected behavior may occur. However, this restriction
109+
is not strict. That means it is possible that we violates the order above, but still get
110+
correct results.
111+
112+
* When mutating vector values through set methods, we should prefer ``set(index, value)`` methods to
113+
``setSafe(index, value)`` methods whenever possible, to avoid unnecessary performance overhead of handling
114+
vector capacity.
35115

36116
Building ValueVector
37117
====================
@@ -41,50 +121,56 @@ Note that the current implementation doesn't enforce the rule that Arrow objects
41121
set/setSafe APIs and concrete subclasses of FieldWriter for populating values.
42122

43123
For example, the code below shows how to build a :class:`BigIntVector`, in this case, we build a
44-
vector of the range 0 to 7 where the element that should hold the fourth value is nulled::
45-
46-
BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
47-
48-
BigIntVector vector = new BigIntVector("vector", allocator);
49-
vector.allocateNew(8);
50-
vector.set(0, 1);
51-
vector.set(1, 2);
52-
vector.set(2, 3);
53-
vector.setNull(3);
54-
vector.set(4, 5);
55-
vector.set(5, 6);
56-
vector.set(6, 7);
57-
vector.set(7, 8);
58-
vector.setValueCount(8); // this will finalizes the vector by convention.
124+
vector of the range 0 to 7 where the element that should hold the fourth value is nulled
125+
126+
.. code-block:: Java
127+
128+
BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
129+
130+
BigIntVector vector = new BigIntVector("vector", allocator);
131+
vector.allocateNew(8);
132+
vector.set(0, 1);
133+
vector.set(1, 2);
134+
vector.set(2, 3);
135+
vector.setNull(3);
136+
vector.set(4, 5);
137+
vector.set(5, 6);
138+
vector.set(6, 7);
139+
vector.set(7, 8);
140+
vector.setValueCount(8); // this will finalizes the vector by convention.
59141
60142
The :class:`BigIntVector` holds two ArrowBufs. The first buffer holds the null bitmap, which consists
61143
here of a single byte with the bits 1|1|1|1|0|1|1|1 (the bit is 1 if the value is non-null).
62144
The second buffer contains all the above values. As the fourth entry is null, the value at that position
63145
in the buffer is undefined. Note compared with set API, setSafe API would check value capacity before setting
64146
values and reallocate buffers if necessary.
65147

66-
Here is how to build a vector using writer::
67-
68-
BigIntVector vector = new BigIntVector("vector", allocator);
69-
BigIntWriter writer = new BigIntWriterImpl(vector);
70-
writer.setPosition(0);
71-
writer.writeBigInt(1);
72-
writer.setPosition(1);
73-
writer.writeBigInt(2);
74-
writer.setPosition(2);
75-
writer.writeBigInt(3);
76-
// writer.setPosition(3) is not called which means the forth value is null.
77-
writer.setPosition(4);
78-
writer.writeBigInt(5);
79-
writer.setPosition(5);
80-
writer.writeBigInt(6);
81-
writer.setPosition(6);
82-
writer.writeBigInt(7);
83-
writer.setPosition(7);
84-
writer.writeBigInt(8);
148+
Here is how to build a vector using writer
149+
150+
.. code-block:: Java
151+
152+
BigIntVector vector = new BigIntVector("vector", allocator);
153+
BigIntWriter writer = new BigIntWriterImpl(vector);
154+
writer.setPosition(0);
155+
writer.writeBigInt(1);
156+
writer.setPosition(1);
157+
writer.writeBigInt(2);
158+
writer.setPosition(2);
159+
writer.writeBigInt(3);
160+
// writer.setPosition(3) is not called which means the forth value is null.
161+
writer.setPosition(4);
162+
writer.writeBigInt(5);
163+
writer.setPosition(5);
164+
writer.writeBigInt(6);
165+
writer.setPosition(6);
166+
writer.writeBigInt(7);
167+
writer.setPosition(7);
168+
writer.writeBigInt(8);
85169
86170
There are get API and concrete subclasses of :class:`FieldReader` for accessing vector values, what needs
87-
to be declared is that writer/reader is not as efficient as direct access::
171+
to be declared is that writer/reader is not as efficient as direct access
172+
173+
.. code-block:: Java
88174
89175
// access via get API
90176
for (int i = 0; i < vector.getValueCount(); i++) {
@@ -106,7 +192,9 @@ to be declared is that writer/reader is not as efficient as direct access::
106192
Slicing
107193
=======
108194
Similar with C++ implementation, it is possible to make zero-copy slices of vectors to obtain a vector
109-
referring to some logical sub-sequence of the data through :class:`TransferPair`::
195+
referring to some logical sub-sequence of the data through :class:`TransferPair`
196+
197+
.. code-block:: Java
110198
111199
IntVector vector = new IntVector("intVector", allocator);
112200
for (int i = 0; i < 10; i++) {

0 commit comments

Comments
 (0)