| 1 | // Copyright 2023 LanceDB Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import { describe } from 'mocha' |
| 16 | import * as chai from 'chai' |
| 17 | import { assert } from 'chai' |
| 18 | import * as chaiAsPromised from 'chai-as-promised' |
| 19 | import { v4 as uuidv4 } from 'uuid' |
| 20 | |
| 21 | import * as lancedb from '../index' |
| 22 | import { tmpdir } from 'os' |
| 23 | import * as fs from 'fs' |
| 24 | import * as path from 'path' |
| 25 | |
| 26 | chai.use(chaiAsPromised) |
| 27 | |
| 28 | describe('LanceDB AWS Integration test', function () { |
| 29 | it('s3+ddb schema is processed correctly', async function () { |
| 30 | this.timeout(15000) |
| 31 | |
| 32 | // WARNING: specifying engine is NOT a publicly supported feature in lancedb yet |
| 33 | // THE API WILL CHANGE |
| 34 | const conn = await lancedb.connect('s3://lancedb-integtest?engine=ddb&ddbTableName=lancedb-integtest') |
| 35 | const data = [{ vector: Array(128).fill(1.0) }] |
| 36 | |
| 37 | const tableName = uuidv4() |
| 38 | let table = await conn.createTable(tableName, data, { writeMode: lancedb.WriteMode.Overwrite }) |
| 39 | |
| 40 | const futs = [table.add(data), table.add(data), table.add(data), table.add(data), table.add(data)] |
| 41 | await Promise.allSettled(futs) |
| 42 | |
| 43 | table = await conn.openTable(tableName) |
| 44 | assert.equal(await table.countRows(), 6) |
| 45 | }) |
| 46 | }) |
| 47 | |
| 48 | describe('LanceDB Mirrored Store Integration test', function () { |
| 49 | it('s3://...?mirroredStore=... param is processed correctly', async function () { |
| 50 | this.timeout(600000) |
| 51 | |
| 52 | const dir = tmpdir() |
| 53 | console.log(dir) |
| 54 | const conn = await lancedb.connect({ uri: `s3://lancedb-integtest?mirroredStore=${dir}`, storageOptions: { allowHttp: 'true' } }) |
| 55 | const data = Array(200).fill({ vector: Array(128).fill(1.0), id: 0 }) |
| 56 | data.push(...Array(200).fill({ vector: Array(128).fill(1.0), id: 1 })) |
| 57 | data.push(...Array(200).fill({ vector: Array(128).fill(1.0), id: 2 })) |
| 58 | data.push(...Array(200).fill({ vector: Array(128).fill(1.0), id: 3 })) |
| 59 | |
| 60 | const tableName = uuidv4() |
| 61 | |
| 62 | // try create table and check if it's mirrored |
| 63 | const t = await conn.createTable(tableName, data, { writeMode: lancedb.WriteMode.Overwrite }) |
| 64 | |
| 65 | const mirroredPath = path.join(dir, `${tableName}.lance`) |
| 66 | fs.readdir(mirroredPath, { withFileTypes: true }, (err, files) => { |
| 67 | if (err != null) throw err |
| 68 | // there should be three dirs |
| 69 | assert.equal(files.length, 3) |
| 70 | assert.isTrue(files[0].isDirectory()) |
| 71 | assert.isTrue(files[1].isDirectory()) |
| 72 | |
| 73 | fs.readdir(path.join(mirroredPath, '_transactions'), { withFileTypes: true }, (err, files) => { |
| 74 | if (err != null) throw err |
| 75 | assert.equal(files.length, 1) |
| 76 | assert.isTrue(files[0].name.endsWith('.txn')) |
| 77 | }) |
| 78 | |
| 79 | fs.readdir(path.join(mirroredPath, '_versions'), { withFileTypes: true }, (err, files) => { |
| 80 | if (err != null) throw err |
| 81 | assert.equal(files.length, 1) |
| 82 | assert.isTrue(files[0].name.endsWith('.manifest')) |
| 83 | }) |
| 84 | |
| 85 | fs.readdir(path.join(mirroredPath, 'data'), { withFileTypes: true }, (err, files) => { |
| 86 | if (err != null) throw err |
| 87 | assert.equal(files.length, 1) |
| 88 | assert.isTrue(files[0].name.endsWith('.lance')) |
| 89 | }) |
| 90 | }) |
| 91 | |
| 92 | // try create index and check if it's mirrored |
| 93 | await t.createIndex({ column: 'vector', type: 'ivf_pq' }) |
| 94 | |
| 95 | fs.readdir(mirroredPath, { withFileTypes: true }, (err, files) => { |
| 96 | if (err != null) throw err |
| 97 | // there should be four dirs |
| 98 | assert.equal(files.length, 4) |
| 99 | assert.isTrue(files[0].isDirectory()) |
| 100 | assert.isTrue(files[1].isDirectory()) |
| 101 | assert.isTrue(files[2].isDirectory()) |
| 102 | |
| 103 | // Two TXs now |
| 104 | fs.readdir(path.join(mirroredPath, '_transactions'), { withFileTypes: true }, (err, files) => { |
| 105 | if (err != null) throw err |
| 106 | assert.equal(files.length, 2) |
| 107 | assert.isTrue(files[0].name.endsWith('.txn')) |
| 108 | assert.isTrue(files[1].name.endsWith('.txn')) |
| 109 | }) |
| 110 | |
| 111 | fs.readdir(path.join(mirroredPath, 'data'), { withFileTypes: true }, (err, files) => { |
| 112 | if (err != null) throw err |
| 113 | assert.equal(files.length, 1) |
| 114 | assert.isTrue(files[0].name.endsWith('.lance')) |
| 115 | }) |
| 116 | |
| 117 | fs.readdir(path.join(mirroredPath, '_indices'), { withFileTypes: true }, (err, files) => { |
| 118 | if (err != null) throw err |
| 119 | assert.equal(files.length, 1) |
| 120 | assert.isTrue(files[0].isDirectory()) |
| 121 | |
| 122 | fs.readdir(path.join(mirroredPath, '_indices', files[0].name), { withFileTypes: true }, (err, files) => { |
| 123 | if (err != null) throw err |
| 124 | |
| 125 | assert.equal(files.length, 1) |
| 126 | assert.isTrue(files[0].isFile()) |
| 127 | assert.isTrue(files[0].name.endsWith('.idx')) |
| 128 | }) |
| 129 | }) |
| 130 | }) |
| 131 | |
| 132 | // try delete and check if it's mirrored |
| 133 | await t.delete('id = 0') |
| 134 | |
| 135 | fs.readdir(mirroredPath, { withFileTypes: true }, (err, files) => { |
| 136 | if (err != null) throw err |
| 137 | // there should be five dirs |
| 138 | assert.equal(files.length, 5) |
| 139 | assert.isTrue(files[0].isDirectory()) |
| 140 | assert.isTrue(files[1].isDirectory()) |
| 141 | assert.isTrue(files[2].isDirectory()) |
| 142 | assert.isTrue(files[3].isDirectory()) |
| 143 | assert.isTrue(files[4].isDirectory()) |
| 144 | |
| 145 | // Three TXs now |
| 146 | fs.readdir(path.join(mirroredPath, '_transactions'), { withFileTypes: true }, (err, files) => { |
| 147 | if (err != null) throw err |
| 148 | assert.equal(files.length, 3) |
| 149 | assert.isTrue(files[0].name.endsWith('.txn')) |
| 150 | assert.isTrue(files[1].name.endsWith('.txn')) |
| 151 | }) |
| 152 | |
| 153 | fs.readdir(path.join(mirroredPath, 'data'), { withFileTypes: true }, (err, files) => { |
| 154 | if (err != null) throw err |
| 155 | assert.equal(files.length, 1) |
| 156 | assert.isTrue(files[0].name.endsWith('.lance')) |
| 157 | }) |
| 158 | |
| 159 | fs.readdir(path.join(mirroredPath, '_indices'), { withFileTypes: true }, (err, files) => { |
| 160 | if (err != null) throw err |
| 161 | assert.equal(files.length, 1) |
| 162 | assert.isTrue(files[0].isDirectory()) |
| 163 | |
| 164 | fs.readdir(path.join(mirroredPath, '_indices', files[0].name), { withFileTypes: true }, (err, files) => { |
| 165 | if (err != null) throw err |
| 166 | |
| 167 | assert.equal(files.length, 1) |
| 168 | assert.isTrue(files[0].isFile()) |
| 169 | assert.isTrue(files[0].name.endsWith('.idx')) |
| 170 | }) |
| 171 | }) |
| 172 | |
| 173 | fs.readdir(path.join(mirroredPath, '_deletions'), { withFileTypes: true }, (err, files) => { |
| 174 | if (err != null) throw err |
| 175 | assert.equal(files.length, 1) |
| 176 | assert.isTrue(files[0].name.endsWith('.arrow')) |
| 177 | }) |
| 178 | }) |
| 179 | }) |
| 180 | }) |