Writing in Dense Subarrays

This is applicable only to dense arrays (see Dense Writes for more details). You can write into a subarray of the array domain as follows:

C
C++
Python
R
Java
Go
C
#include <tiledb/tiledb.h>
// Create TileDB context
tiledb_ctx_t* ctx;
tiledb_ctx_alloc(NULL, &ctx);
// Open array for writing
tiledb_array_t* array;
tiledb_array_alloc(ctx, array_name, &array);
tiledb_array_open(ctx, array, TILEDB_WRITE);
// Prepare some data for the array
int data[] = {1, 2, 3, 4, 5, 6};
unsigned long long data_size = sizeof(data);
// Set the subarray to write into
int subarray[] = {1, 2, 2, 4};
// Create the query
tiledb_query_t* query;
tiledb_query_alloc(ctx, array, TILEDB_WRITE, &query);
tiledb_query_set_layout(ctx, query, TILEDB_ROW_MAJOR);
tiledb_query_set_subarray(ctx, query, subarray);
tiledb_query_set_buffer(ctx, query, "a", data, &data_size);
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with tiledb_query_set_buffer.
// You could have set the query layout to TILEDB_COL_MAJOR,
// and set the data vector to {1, 4, 2, 5, 3, 6},
// and the result would be the same
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the data vector to {INT_MIN, 1, INT_MIN, 4, 2, 3, 5, 6},
// and the result would be the same
// Submit query
tiledb_query_submit(ctx, query);
// Close array
tiledb_array_close(ctx, array);
// Clean up
tiledb_array_free(&array);
tiledb_query_free(&query);
tiledb_ctx_free(&ctx);
C++
#include <tiledb/tiledb>
// Create context
Context ctx;
// Prepare some data for the array
std::vector<int> data = {1, 2, 3, 4, 5, 6};
// Set the subarray to write into
std::vector<int> subarray = {1, 2, 2, 4};
// Open array for writing
Array array(ctx, array_name, TILEDB_WRITE);
// Create the query
Query query(ctx, array);
query.set_layout(TILEDB_ROW_MAJOR)
.set_buffer("a", data)
.set_subarray(subarray);
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with set_buffer.
// You could have set the query layout to TILEDB_COL_MAJOR,
// and set the data vector to {1, 4, 2, 5, 3, 6},
// and the result would be the same
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the data vector to {INT_MIN, 1, INT_MIN, 4, 2, 3, 5, 6},
// and the result would be the same
// Submit query
query.submit();
// Close the array
array.close();
Python
import tiledb
import numpy as np
data = np.array([[1,2],[3,4],[5,6]])
with tiledb.DenseArray(array_name, 'w') as A:
A[1:3,2:5] = data
R
# Prepare a 4x3 dense array
# Contrary to Python, R by default stores arrays in col-major order
data <- array(c(1L, 4L, 2L, 5L, 3L, 6L), dim=c(2,3))
# Prepare the [1,2] x [2,4] subarray to write to
I <- c(1:2)
J <- c(2:4)
# Open the array and write the data to it
A <- tiledb_dense(uri = array_name)
A[I, J] <- data
Java
// Create context, array and query
try(Context ctx = new Context(),
Array array = new Array(ctx, array_name, TILEDB_WRITE),
Query query = new Query(ctx, array);) {
// Prepare some data for the array
NativeArray subarray = new NativeArray(ctx, new int[] {1, 2, 2, 4}, Integer.class);
NativeArray data = new NativeArray(ctx, new int[] {1, 2, 3, 4, 5, 6}, Integer.class);
// Create the query
query.setLayout(TILEDB_ROW_MAJOR)
.setSubarray(subarray)
.setBuffer("a", data);
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with set_buffer.
// You could have set the query layout to TILEDB_COL_MAJOR,
// and set the data vector to {1, 4, 2, 5, 3, 6},
// and the result would be the same
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the data vector to {INT_MIN, 1, INT_MIN, 4, 2, 3, 5, 6},
// and the result would be the same
// Submit query
query.submit();
}
Go
import "github.com/TileDB-Inc/TileDB-Go"
// Create context
ctx, _ := tiledb.NewContext(nil)
// Prepare some data for the array
data := []int32{1, 2, 3, 4, 5, 6}
// Set the subarray to write into
subarray := []int32{1, 2, 2, 4}
// Open array for writing
array, _ := tiledb.NewArray(ctx, array_name)
_ = array.Open(tiledb.TILEDB_WRITE)
// Create the query
query, _ := tiledb.NewQuery(ctx, array)
query.SetLayout(tiledb.TILEDB_ROW_MAJOR)
query.SetBuffer("a", data)
query.SetSubArray(subarray)
// NOTE: If the array has more than one attributes, you should
// set the a buffer for every attribute with set_buffer.
// You could have set the query layout to TILEDB_COL_MAJOR,
// and set the data vector to {1, 4, 2, 5, 3, 6},
// and the result would be the same
// You could have set the query layout to TILEDB_GLOBAL_ORDER,
// and set the data vector to {INT_MIN, 1, INT_MIN, 4, 2, 3, 5, 6},
// and the result would be the same
// Submit query
query.Submit()
// Close the array
query.Finalize()
array.Close()

The code snippets above create the dense fragment shown in the figure below, assuming a 2D array with domain [1, 4] x [1, 4] , 2x2 space tiling, row-major tile and cell order, and a single int32 attribute called a. Observe that you can select the layout in which you can write the cells into. If you switch to column-major or global order, make sure to organize your cells properly before writing (see figure below).

Currently, if your array consists of more than one attributes, TileDB requires you to provide values for all the attributes in each write operation.