Fixed-length Attributes

Writing fixed-length attribute cells to an array involves populating a single buffer and serializing all values in that buffer according to the layout you are writing into (see Writing Dense Subarrays and Writing Sparse Cells). The same holds even if you defined the attribute to store any fixed number of values (>1) per cell. This is illustrated in the code block below with a sparse write, but it is applicable to dense writes as well.

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
long long coords[] = {1, 2, 2, 1, 3, 3, 4, 4};
unsigned long long coords_size = sizeof(coords);
float data[] = {1.1f, 1.2f, 2.1f, 2.2f, 3.1f, 3.2f, 4.1f, 4.2f};
unsigned long long data_size = sizeof(data);
// Create the query
tiledb_query_t* query;
tiledb_query_alloc(ctx, array, TILEDB_WRITE, &query);
tiledb_query_set_layout(ctx, query, TILEDB_UNORDERED);
tiledb_query_set_buffer(ctx, query, TILEDB_COORDS, coords, &coords_size);
tiledb_query_set_buffer(ctx, query, "a", data, &data_size);
// 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<int64_t> coords = {1, 2, 2, 1, 3, 3, 4, 4};
std::vector<float> data = {1.1f, 1.2f, 2.1f, 2.2f, 3.1f, 3.2f, 4.1f, 4.2f};
// 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_coords(coords)
.set_buffer("a", data);
// Submit query
query.submit();
// Close the array
array.close();
Python
# The Python API treats homogenous NumPy record arrays
# as fixed-length cells.
c1 = np.array([1,2,3,4])
c2 = np.array([2,1,3,2])
data = np.array([(1,2),(2,1),(3,3),(4,4)], dtype='i4,i4')
with tiledb.SparseArray(array_name, 'w') as A:
A[c1, c2] = data
R
d1 <- tiledb_dim(domain = c(1L, 4L))
d2 <- tiledb_dim(domain = c(1L, 4L))
dom <- tiledb_domain(c(d1, d2))
vec <- 1:32 * 10L
attr <- tiledb_attr("a", type = r_to_tiledb_type(vec))
## set to two values per cell
tiledb:::libtiledb_attribute_set_cell_val_num(attr@ptr, 2)
sch <- tiledb_array_schema(dom, attr)
tiledb_array_create(uridensefix, sch)
ctx <- tiledb_ctx()
arrptr <- tiledb:::libtiledb_array_open(ctx@ptr, uridensefix, "WRITE")
subarr <- c(1L,4L, 1L,4L)
qryptr <- tiledb:::libtiledb_query(ctx@ptr, arrptr, "WRITE")
qryptr <- tiledb:::libtiledb_query_set_subarray(qryptr, subarr)
qryptr <- tiledb:::libtiledb_query_set_layout(qryptr, "ROW_MAJOR")
qryptr <- tiledb:::libtiledb_query_set_buffer(qryptr, "a", vec)
qryptr <- tiledb:::libtiledb_query_submit(qryptr)
res <- tiledb:::libtiledb_array_close(arrptr)
#TODO Higher-level R support
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 coords = new NativeArray(ctx, new long[] {1l, 2l, 2l, 1l, 3l, 3l, 4l, 4l}, Long.class);
NativeArray coords = new NativeArray(ctx, new float[] {1.1f, 1.2f, 2.1f, 2.2f, 3.1f, 3.2f, 4.1f, 4.2f}, Float.class);
// Create the query
query.setLayout(TILEDB_ROW_MAJOR)
.setCoords(coords)
.setBuffer("a", data);
// Submit query
query.submit();
}
Go
import "github.com/TileDB-Inc/TileDB-Go"
// Create context
ctx, _ := tiledb.NewContext(nil)
// Prepare some data for the array
coords := []int32{1, 2, 2, 1, 3, 3, 4, 4}
data := []float32{1.1, 1.2, 2.1, 2.2, 3.1, 3.2, 4.1, 4.2}
// 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_UNORDERED)
_, err = query.SetCoordinates(coords)
_, err = query.SetBuffer("a", data)
// Submit query
query.Submit();
// Close the array
array.Close();

The code snippet above produces the following sparse fragment.