|
| 1 | +/** |
| 2 | + * @file rerading_range_test.go |
| 3 | + * |
| 4 | + * @section LICENSE |
| 5 | + * |
| 6 | + * The MIT License |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2019 TileDB, Inc. |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * in the Software without restriction, including without limitation the rights |
| 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + * copies of the Software, and to permit persons to whom the Software is |
| 15 | + * furnished to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be included in |
| 18 | + * all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | + * THE SOFTWARE. |
| 27 | + * |
| 28 | + * @section DESCRIPTION |
| 29 | + * |
| 30 | + * This is a part of the TileDB reading_range tutorial: |
| 31 | + * https://docs.tiledb.io/en/latest/reading_range.html |
| 32 | + * |
| 33 | + * When run, this program will create a simple 2D dense array, write some data |
| 34 | + * to it, and read a slice of the data back in the layout of the user's choice |
| 35 | + * (passed as an argument to the program: "row", "col", or "global"). |
| 36 | + * |
| 37 | + */ |
| 38 | + |
| 39 | +package examples |
| 40 | + |
| 41 | +import ( |
| 42 | + "fmt" |
| 43 | + "os" |
| 44 | + |
| 45 | + tiledb "github.com/TileDB-Inc/TileDB-Go" |
| 46 | +) |
| 47 | + |
| 48 | +// Name of array. |
| 49 | +var readRangeArrayName = "read_range_array" |
| 50 | + |
| 51 | +func createReadRangeArray() { |
| 52 | + // Create a TileDB context. |
| 53 | + ctx, err := tiledb.NewContext(nil) |
| 54 | + checkError(err) |
| 55 | + |
| 56 | + // The array will be 4x4 with dimensions "rows" and "cols", with domain [1,4]. |
| 57 | + domain, err := tiledb.NewDomain(ctx) |
| 58 | + checkError(err) |
| 59 | + rowDim, err := tiledb.NewDimension(ctx, "rows", []int32{1, 4}, int32(4)) |
| 60 | + checkError(err) |
| 61 | + colDim, err := tiledb.NewDimension(ctx, "cols", []int32{1, 4}, int32(4)) |
| 62 | + checkError(err) |
| 63 | + err = domain.AddDimensions(rowDim, colDim) |
| 64 | + checkError(err) |
| 65 | + |
| 66 | + // The array will be dense. |
| 67 | + schema, err := tiledb.NewArraySchema(ctx, tiledb.TILEDB_DENSE) |
| 68 | + err = schema.SetDomain(domain) |
| 69 | + checkError(err) |
| 70 | + err = schema.SetCellOrder(tiledb.TILEDB_ROW_MAJOR) |
| 71 | + checkError(err) |
| 72 | + err = schema.SetTileOrder(tiledb.TILEDB_ROW_MAJOR) |
| 73 | + checkError(err) |
| 74 | + |
| 75 | + // Add a single attribute "a" so each (i,j) cell can store an integer. |
| 76 | + a, err := tiledb.NewAttribute(ctx, "a", tiledb.TILEDB_INT32) |
| 77 | + checkError(err) |
| 78 | + err = schema.AddAttributes(a) |
| 79 | + checkError(err) |
| 80 | + |
| 81 | + // Create the (empty) array on disk. |
| 82 | + array, err := tiledb.NewArray(ctx, readRangeArrayName) |
| 83 | + checkError(err) |
| 84 | + err = array.Create(schema) |
| 85 | + checkError(err) |
| 86 | +} |
| 87 | + |
| 88 | +func writeRearRangeArray() { |
| 89 | + ctx, err := tiledb.NewContext(nil) |
| 90 | + checkError(err) |
| 91 | + |
| 92 | + // Prepare some data for the array |
| 93 | + data := []int32{ |
| 94 | + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} |
| 95 | + |
| 96 | + // Open the array for writing and create the query. |
| 97 | + array, err := tiledb.NewArray(ctx, readRangeArrayName) |
| 98 | + checkError(err) |
| 99 | + err = array.Open(tiledb.TILEDB_WRITE) |
| 100 | + checkError(err) |
| 101 | + query, err := tiledb.NewQuery(ctx, array) |
| 102 | + checkError(err) |
| 103 | + err = query.SetLayout(tiledb.TILEDB_ROW_MAJOR) |
| 104 | + checkError(err) |
| 105 | + _, err = query.SetBuffer("a", data) |
| 106 | + checkError(err) |
| 107 | + |
| 108 | + // Perform the write and close the array. |
| 109 | + err = query.Submit() |
| 110 | + checkError(err) |
| 111 | + err = array.Close() |
| 112 | + checkError(err) |
| 113 | +} |
| 114 | + |
| 115 | +func readReadRangeArray(dimIdx uint32) { |
| 116 | + ctx, err := tiledb.NewContext(nil) |
| 117 | + checkError(err) |
| 118 | + |
| 119 | + // Prepare the array for reading |
| 120 | + array, err := tiledb.NewArray(ctx, readRangeArrayName) |
| 121 | + checkError(err) |
| 122 | + err = array.Open(tiledb.TILEDB_READ) |
| 123 | + checkError(err) |
| 124 | + |
| 125 | + // Prepare the vector that will hold the result (of size 6 elements) |
| 126 | + data := make([]int32, 12) |
| 127 | + |
| 128 | + // Prepare the query |
| 129 | + query, err := tiledb.NewQuery(ctx, array) |
| 130 | + checkError(err) |
| 131 | + |
| 132 | + err = query.AddRange(dimIdx, int32(1), int32(1)) |
| 133 | + checkError(err) |
| 134 | + err = query.AddRange(dimIdx, int32(3), int32(4)) |
| 135 | + checkError(err) |
| 136 | + |
| 137 | + numOfRanges, err := query.GetRangeNum(dimIdx) |
| 138 | + checkError(err) |
| 139 | + fmt.Printf("Num of Ranges: %d\n", *numOfRanges) |
| 140 | + |
| 141 | + var I uint64 |
| 142 | + for I = 0; I < *numOfRanges; I++ { |
| 143 | + start, end, err := query.GetRange(dimIdx, I) |
| 144 | + checkError(err) |
| 145 | + fmt.Printf("Range for dimension: %d, start: %v, end: %v\n", dimIdx, start, end) |
| 146 | + } |
| 147 | + |
| 148 | + ranges, err := query.GetRanges() |
| 149 | + checkError(err) |
| 150 | + |
| 151 | + fmt.Printf("Ranges: %v\n", ranges) |
| 152 | + |
| 153 | + _, err = query.SetBuffer("a", data) |
| 154 | + checkError(err) |
| 155 | + |
| 156 | + // Submit the query and close the array. |
| 157 | + err = query.Submit() |
| 158 | + checkError(err) |
| 159 | + err = array.Close() |
| 160 | + checkError(err) |
| 161 | + |
| 162 | + // Print out the results. |
| 163 | + fmt.Println(data) |
| 164 | +} |
| 165 | + |
| 166 | +// ExampleReadRangeArray shows and example creation, writing and range reading |
| 167 | +// of a dense array |
| 168 | +func ExampleReadRangeArray() { |
| 169 | + createReadRangeArray() |
| 170 | + writeRearRangeArray() |
| 171 | + // Rows |
| 172 | + readReadRangeArray(0) |
| 173 | + // Columns |
| 174 | + readReadRangeArray(1) |
| 175 | + |
| 176 | + // Cleanup example so unit tests are clean |
| 177 | + if _, err := os.Stat(readRangeArrayName); err == nil { |
| 178 | + err = os.RemoveAll(readRangeArrayName) |
| 179 | + checkError(err) |
| 180 | + } |
| 181 | + |
| 182 | + // Output: Num of Ranges: 2 |
| 183 | + // Range for dimension: 0, start: 1, end: 1 |
| 184 | + // Range for dimension: 0, start: 3, end: 4 |
| 185 | + // Ranges: [[{1 1} {3 4}] [{1 4}]] |
| 186 | + // [1 2 3 4 9 10 11 12 13 14 15 16] |
| 187 | + // Num of Ranges: 2 |
| 188 | + // Range for dimension: 1, start: 1, end: 1 |
| 189 | + // Range for dimension: 1, start: 3, end: 4 |
| 190 | + // Ranges: [[{1 4}] [{1 1} {3 4}]] |
| 191 | + // [1 3 4 5 7 8 9 11 12 13 15 16] |
| 192 | +} |
| 193 | + |
| 194 | +// 1 2 3 4 |
| 195 | +// 5 6 7 8 |
| 196 | +// 9 10 11 12 |
| 197 | +// 13 14 15 16 |
0 commit comments