Skip to content

Commit e4f007a

Browse files
committed
add delete_all
1 parent bae0a94 commit e4f007a

4 files changed

Lines changed: 137 additions & 36 deletions

File tree

nbs/00_vector.ipynb

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@
182182
" \n",
183183
" def drop_embedding_index_query(self):\n",
184184
" return \"DROP INDEX IF EXISTS {index_name};\".format(index_name=self._get_embedding_index_name())\n",
185-
" \n",
185+
"\n",
186+
" def delete_all_query(self):\n",
187+
" return \"TRUNCATE {table_name};\".format(table_name=self._quote_ident(self.table_name))\n",
188+
" \n",
186189
" def create_ivfflat_index_query(self, num_records):\n",
187190
" \"\"\"\n",
188191
" Generates an ivfflat index creation query.\n",
@@ -385,6 +388,19 @@
385388
" async with await self.connect() as pool:\n",
386389
" await pool.execute(query)\n",
387390
"\n",
391+
" async def delete_all(self, drop_index=True):\n",
392+
" \"\"\"\n",
393+
" Deletes all data. Also drops the index if `drop_index` is true.\n",
394+
"\n",
395+
" Returns:\n",
396+
" None\n",
397+
" \"\"\"\n",
398+
" if drop_index:\n",
399+
" await self.drop_embedding_index();\n",
400+
" query = self.builder.delete_all_query()\n",
401+
" async with await self.connect() as pool:\n",
402+
" await pool.execute(query)\n",
403+
"\n",
388404
" async def _get_approx_count(self):\n",
389405
" \"\"\"\n",
390406
" Retrieves an approximate count of records in the table.\n",
@@ -662,7 +678,11 @@
662678
" ])\n",
663679
" assert False\n",
664680
"except BaseException as e:\n",
665-
" pass\n"
681+
" pass\n",
682+
"\n",
683+
"\n",
684+
"await vec.delete_all()\n",
685+
"assert await vec.table_is_empty()"
666686
]
667687
},
668688
{
@@ -810,6 +830,20 @@
810830
" with conn.cursor() as cur:\n",
811831
" cur.execute(query)\n",
812832
"\n",
833+
" def delete_all(self, drop_index=True):\n",
834+
" \"\"\"\n",
835+
" Deletes all data. Also drops the index if `drop_index` is true.\n",
836+
"\n",
837+
" Returns:\n",
838+
" None\n",
839+
" \"\"\"\n",
840+
" if drop_index:\n",
841+
" self.drop_embedding_index();\n",
842+
" query = self.builder.delete_all_query()\n",
843+
" with self.connect() as conn:\n",
844+
" with conn.cursor() as cur:\n",
845+
" cur.execute(query)\n",
846+
"\n",
813847
" def _get_approx_count(self):\n",
814848
" \"\"\"\n",
815849
" Retrieves an approximate count of records in the table.\n",
@@ -1120,7 +1154,10 @@
11201154
"rec = vec.search([1.0, 2.0], filter={\"key_1\":\"val_1\", \"key_2\":\"val_2\"})\n",
11211155
"assert rec[0][SEARCH_RESULT_CONTENTS_IDX] == 'the brown fox'\n",
11221156
"assert rec[0][SEARCH_RESULT_METADATA_IDX] == {'key_1': 'val_1', 'key_2': 'val_2'}\n",
1123-
"assert rec[0][SEARCH_RESULT_DISTANCE_IDX] == 0.0009438353921149556"
1157+
"assert rec[0][SEARCH_RESULT_DISTANCE_IDX] == 0.0009438353921149556\n",
1158+
"\n",
1159+
"vec.delete_all()\n",
1160+
"assert vec.table_is_empty()"
11241161
]
11251162
},
11261163
{

nbs/index.ipynb

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
{
5050
"cell_type": "code",
51-
"execution_count": null,
51+
"execution_count": 1,
5252
"metadata": {},
5353
"outputs": [],
5454
"source": [
@@ -58,7 +58,7 @@
5858
},
5959
{
6060
"cell_type": "code",
61-
"execution_count": null,
61+
"execution_count": 2,
6262
"metadata": {},
6363
"outputs": [],
6464
"source": [
@@ -83,7 +83,7 @@
8383
},
8484
{
8585
"cell_type": "code",
86-
"execution_count": null,
86+
"execution_count": 3,
8787
"metadata": {},
8888
"outputs": [],
8989
"source": [
@@ -93,7 +93,7 @@
9393
},
9494
{
9595
"cell_type": "code",
96-
"execution_count": null,
96+
"execution_count": 4,
9797
"metadata": {},
9898
"outputs": [],
9999
"source": [
@@ -105,7 +105,7 @@
105105
},
106106
{
107107
"cell_type": "code",
108-
"execution_count": null,
108+
"execution_count": 5,
109109
"metadata": {},
110110
"outputs": [],
111111
"source": [
@@ -114,7 +114,7 @@
114114
},
115115
{
116116
"cell_type": "code",
117-
"execution_count": null,
117+
"execution_count": 6,
118118
"metadata": {},
119119
"outputs": [],
120120
"source": [
@@ -130,7 +130,7 @@
130130
},
131131
{
132132
"cell_type": "code",
133-
"execution_count": null,
133+
"execution_count": 7,
134134
"metadata": {},
135135
"outputs": [],
136136
"source": [
@@ -153,7 +153,7 @@
153153
},
154154
{
155155
"cell_type": "code",
156-
"execution_count": null,
156+
"execution_count": 8,
157157
"metadata": {},
158158
"outputs": [],
159159
"source": [
@@ -162,7 +162,7 @@
162162
},
163163
{
164164
"cell_type": "code",
165-
"execution_count": null,
165+
"execution_count": 9,
166166
"metadata": {},
167167
"outputs": [],
168168
"source": [
@@ -181,17 +181,17 @@
181181
},
182182
{
183183
"cell_type": "code",
184-
"execution_count": null,
184+
"execution_count": 10,
185185
"metadata": {},
186186
"outputs": [
187187
{
188188
"data": {
189189
"text/plain": [
190-
"[<Record id=UUID('0c2aceca-9375-4c04-b5b4-01db19eea3c8') metadata='{\"action\": \"jump\", \"animal\": \"fox\"}' contents='jumped over the' embedding=array([ 1. , 10.8], dtype=float32) distance=0.00016793422934946456>,\n",
191-
" <Record id=UUID('4954646e-2677-49c3-91e8-fdc38b8eee75') metadata='{\"animal\": \"fox\"}' contents='the brown fox' embedding=array([1. , 1.3], dtype=float32) distance=0.14489260377438218>]"
190+
"[<Record id=UUID('b4b4d144-fedf-4377-a9ce-645f4555af45') metadata='{\"action\": \"jump\", \"animal\": \"fox\"}' contents='jumped over the' embedding=array([ 1. , 10.8], dtype=float32) distance=0.00016793422934946456>,\n",
191+
" <Record id=UUID('33373a06-1f22-4980-8095-256116215a81') metadata='{\"animal\": \"fox\"}' contents='the brown fox' embedding=array([1. , 1.3], dtype=float32) distance=0.14489260377438218>]"
192192
]
193193
},
194-
"execution_count": null,
194+
"execution_count": 10,
195195
"metadata": {},
196196
"output_type": "execute_result"
197197
}
@@ -209,16 +209,16 @@
209209
},
210210
{
211211
"cell_type": "code",
212-
"execution_count": null,
212+
"execution_count": 11,
213213
"metadata": {},
214214
"outputs": [
215215
{
216216
"data": {
217217
"text/plain": [
218-
"[<Record id=UUID('0c2aceca-9375-4c04-b5b4-01db19eea3c8') metadata='{\"action\": \"jump\", \"animal\": \"fox\"}' contents='jumped over the' embedding=array([ 1. , 10.8], dtype=float32) distance=0.00016793422934946456>]"
218+
"[<Record id=UUID('b4b4d144-fedf-4377-a9ce-645f4555af45') metadata='{\"action\": \"jump\", \"animal\": \"fox\"}' contents='jumped over the' embedding=array([ 1. , 10.8], dtype=float32) distance=0.00016793422934946456>]"
219219
]
220220
},
221-
"execution_count": null,
221+
"execution_count": 11,
222222
"metadata": {},
223223
"output_type": "execute_result"
224224
}
@@ -236,16 +236,16 @@
236236
},
237237
{
238238
"cell_type": "code",
239-
"execution_count": null,
239+
"execution_count": 12,
240240
"metadata": {},
241241
"outputs": [
242242
{
243243
"data": {
244244
"text/plain": [
245-
"[<Record id=UUID('0c2aceca-9375-4c04-b5b4-01db19eea3c8') metadata='{\"action\": \"jump\", \"animal\": \"fox\"}' contents='jumped over the' embedding=array([ 1. , 10.8], dtype=float32) distance=0.00016793422934946456>]"
245+
"[<Record id=UUID('b4b4d144-fedf-4377-a9ce-645f4555af45') metadata='{\"action\": \"jump\", \"animal\": \"fox\"}' contents='jumped over the' embedding=array([ 1. , 10.8], dtype=float32) distance=0.00016793422934946456>]"
246246
]
247247
},
248-
"execution_count": null,
248+
"execution_count": 12,
249249
"metadata": {},
250250
"output_type": "execute_result"
251251
}
@@ -263,16 +263,16 @@
263263
},
264264
{
265265
"cell_type": "code",
266-
"execution_count": null,
266+
"execution_count": 13,
267267
"metadata": {},
268268
"outputs": [
269269
{
270270
"data": {
271271
"text/plain": [
272-
"UUID('0c2aceca-9375-4c04-b5b4-01db19eea3c8')"
272+
"UUID('b4b4d144-fedf-4377-a9ce-645f4555af45')"
273273
]
274274
},
275-
"execution_count": null,
275+
"execution_count": 13,
276276
"metadata": {},
277277
"output_type": "execute_result"
278278
}
@@ -284,7 +284,7 @@
284284
},
285285
{
286286
"cell_type": "code",
287-
"execution_count": null,
287+
"execution_count": 14,
288288
"metadata": {},
289289
"outputs": [
290290
{
@@ -293,7 +293,7 @@
293293
"'{\"action\": \"jump\", \"animal\": \"fox\"}'"
294294
]
295295
},
296-
"execution_count": null,
296+
"execution_count": 14,
297297
"metadata": {},
298298
"output_type": "execute_result"
299299
}
@@ -304,7 +304,7 @@
304304
},
305305
{
306306
"cell_type": "code",
307-
"execution_count": null,
307+
"execution_count": 15,
308308
"metadata": {},
309309
"outputs": [
310310
{
@@ -313,7 +313,7 @@
313313
"'jumped over the'"
314314
]
315315
},
316-
"execution_count": null,
316+
"execution_count": 15,
317317
"metadata": {},
318318
"output_type": "execute_result"
319319
}
@@ -324,7 +324,7 @@
324324
},
325325
{
326326
"cell_type": "code",
327-
"execution_count": null,
327+
"execution_count": 16,
328328
"metadata": {},
329329
"outputs": [
330330
{
@@ -333,7 +333,7 @@
333333
"array([ 1. , 10.8], dtype=float32)"
334334
]
335335
},
336-
"execution_count": null,
336+
"execution_count": 16,
337337
"metadata": {},
338338
"output_type": "execute_result"
339339
}
@@ -344,7 +344,7 @@
344344
},
345345
{
346346
"cell_type": "code",
347-
"execution_count": null,
347+
"execution_count": 17,
348348
"metadata": {},
349349
"outputs": [
350350
{
@@ -353,7 +353,7 @@
353353
"0.00016793422934946456"
354354
]
355355
},
356-
"execution_count": null,
356+
"execution_count": 17,
357357
"metadata": {},
358358
"output_type": "execute_result"
359359
}
@@ -362,6 +362,22 @@
362362
"records[0][client.SEARCH_RESULT_DISTANCE_IDX]"
363363
]
364364
},
365+
{
366+
"cell_type": "markdown",
367+
"metadata": {},
368+
"source": [
369+
"To delete all records use: "
370+
]
371+
},
372+
{
373+
"cell_type": "code",
374+
"execution_count": 21,
375+
"metadata": {},
376+
"outputs": [],
377+
"source": [
378+
"await vec.delete_all()"
379+
]
380+
},
365381
{
366382
"cell_type": "markdown",
367383
"metadata": {},
@@ -384,7 +400,7 @@
384400
},
385401
{
386402
"cell_type": "code",
387-
"execution_count": null,
403+
"execution_count": 19,
388404
"metadata": {},
389405
"outputs": [],
390406
"source": [
@@ -402,7 +418,7 @@
402418
},
403419
{
404420
"cell_type": "code",
405-
"execution_count": null,
421+
"execution_count": 20,
406422
"metadata": {},
407423
"outputs": [],
408424
"source": [
@@ -429,6 +445,18 @@
429445
"display_name": "nbdev_env",
430446
"language": "python",
431447
"name": "nbdev_env"
448+
},
449+
"language_info": {
450+
"codemirror_mode": {
451+
"name": "ipython",
452+
"version": 3
453+
},
454+
"file_extension": ".py",
455+
"mimetype": "text/x-python",
456+
"name": "python",
457+
"nbconvert_exporter": "python",
458+
"pygments_lexer": "ipython3",
459+
"version": "3.11.4"
432460
}
433461
},
434462
"nbformat": 4,

timescale_vector/_modidx.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
'timescale_vector/client.py'),
1919
'timescale_vector.client.Async.create_tables': ( 'vector.html#async.create_tables',
2020
'timescale_vector/client.py'),
21+
'timescale_vector.client.Async.delete_all': ( 'vector.html#async.delete_all',
22+
'timescale_vector/client.py'),
2123
'timescale_vector.client.Async.drop_embedding_index': ( 'vector.html#async.drop_embedding_index',
2224
'timescale_vector/client.py'),
2325
'timescale_vector.client.Async.search': ('vector.html#async.search', 'timescale_vector/client.py'),
@@ -33,6 +35,8 @@
3335
'timescale_vector/client.py'),
3436
'timescale_vector.client.QueryBuilder.create_ivfflat_index_query': ( 'vector.html#querybuilder.create_ivfflat_index_query',
3537
'timescale_vector/client.py'),
38+
'timescale_vector.client.QueryBuilder.delete_all_query': ( 'vector.html#querybuilder.delete_all_query',
39+
'timescale_vector/client.py'),
3640
'timescale_vector.client.QueryBuilder.drop_embedding_index_query': ( 'vector.html#querybuilder.drop_embedding_index_query',
3741
'timescale_vector/client.py'),
3842
'timescale_vector.client.QueryBuilder.get_approx_count_query': ( 'vector.html#querybuilder.get_approx_count_query',
@@ -59,6 +63,8 @@
5963
'timescale_vector/client.py'),
6064
'timescale_vector.client.Sync.create_tables': ( 'vector.html#sync.create_tables',
6165
'timescale_vector/client.py'),
66+
'timescale_vector.client.Sync.delete_all': ( 'vector.html#sync.delete_all',
67+
'timescale_vector/client.py'),
6268
'timescale_vector.client.Sync.drop_embedding_index': ( 'vector.html#sync.drop_embedding_index',
6369
'timescale_vector/client.py'),
6470
'timescale_vector.client.Sync.search': ('vector.html#sync.search', 'timescale_vector/client.py'),

0 commit comments

Comments
 (0)