File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33namespace Upstash \Vector \Contracts ;
44
5+ use Upstash \Vector \Contracts \Transformers \ToDeletablePayloadInterface ;
56use Upstash \Vector \DataQuery ;
67use Upstash \Vector \DataQueryResult ;
78use Upstash \Vector \DataUpsert ;
@@ -51,9 +52,9 @@ public function queryMany(array $queries): VectorQueryManyResult;
5152 public function queryData (DataQuery $ query ): DataQueryResult ;
5253
5354 /**
54- * @param array<string|VectorIdentifierInterface> $ids
55+ * @param array<string|VectorIdentifierInterface>|ToDeletablePayloadInterface $ids
5556 */
56- public function delete (array $ ids ): VectorDeleteResult ;
57+ public function delete (array | ToDeletablePayloadInterface $ ids ): VectorDeleteResult ;
5758
5859 public function fetch (VectorFetch $ vectorFetch ): VectorFetchResult ;
5960
@@ -64,8 +65,4 @@ public function update(VectorUpdate $update): void;
6465 public function range (VectorRange $ range ): VectorRangeResult ;
6566
6667 public function rangeIterator (VectorRange $ range ): VectorRangeIterator ;
67-
68- public function deleteUsingIdPrefix (string $ prefix ): VectorDeleteResult ;
69-
70- public function deleteUsingMetadataFilter (string $ filter ): VectorDeleteResult ;
7168}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Upstash \Vector \Contracts \Transformers ;
4+
5+ interface ToDeletablePayloadInterface
6+ {
7+ /**
8+ * @return array<string, mixed>
9+ */
10+ public function toDeletablePayload (): array ;
11+ }
Original file line number Diff line number Diff line change 55use Http \Discovery \Psr18ClientDiscovery ;
66use Upstash \Vector \Contracts \IndexInterface ;
77use Upstash \Vector \Contracts \IndexNamespaceInterface ;
8+ use Upstash \Vector \Contracts \Transformers \ToDeletablePayloadInterface ;
89use Upstash \Vector \Contracts \TransporterInterface ;
910use Upstash \Vector \Exceptions \MissingEnvironmentVariableException ;
1011use Upstash \Vector \Iterators \VectorRangeIterator ;
@@ -113,7 +114,7 @@ public function queryData(DataQuery $query): DataQueryResult
113114 return $ this ->namespace ('' )->queryData ($ query );
114115 }
115116
116- public function delete (array $ ids ): VectorDeleteResult
117+ public function delete (array | ToDeletablePayloadInterface $ ids ): VectorDeleteResult
117118 {
118119 return $ this ->namespace ('' )->delete ($ ids );
119120 }
@@ -152,14 +153,4 @@ public function rangeIterator(VectorRange $range): VectorRangeIterator
152153 {
153154 return $ this ->namespace ('' )->rangeIterator ($ range );
154155 }
155-
156- public function deleteUsingIdPrefix (string $ prefix ): VectorDeleteResult
157- {
158- return $ this ->namespace ('' )->deleteUsingIdPrefix ($ prefix );
159- }
160-
161- public function deleteUsingMetadataFilter (string $ filter ): VectorDeleteResult
162- {
163- return $ this ->namespace ('' )->deleteUsingMetadataFilter ($ filter );
164- }
165156}
Original file line number Diff line number Diff line change 33namespace Upstash \Vector ;
44
55use Upstash \Vector \Contracts \IndexNamespaceInterface ;
6+ use Upstash \Vector \Contracts \Transformers \ToDeletablePayloadInterface ;
67use Upstash \Vector \Contracts \TransporterInterface ;
78use Upstash \Vector \Iterators \VectorRangeIterator ;
89use Upstash \Vector \Operations \DeleteNamespaceOperation ;
@@ -73,7 +74,7 @@ public function queryData(DataQuery $query): DataQueryResult
7374 return (new QueryDataOperation ($ this ->namespace , $ this ->transporter ))->query ($ query );
7475 }
7576
76- public function delete (array $ ids ): VectorDeleteResult
77+ public function delete (array | ToDeletablePayloadInterface $ ids ): VectorDeleteResult
7778 {
7879 return (new DeleteVectorsOperation ($ this ->namespace , $ this ->transporter ))
7980 ->delete ($ ids );
@@ -104,14 +105,4 @@ public function rangeIterator(VectorRange $range): VectorRangeIterator
104105 {
105106 return (new RangeVectorsOperation ($ this ->namespace , $ this ->transporter ))->rangeIterator ($ range );
106107 }
107-
108- public function deleteUsingIdPrefix (string $ prefix ): VectorDeleteResult
109- {
110- return (new DeleteVectorsOperation ($ this ->namespace , $ this ->transporter ))->deleteUsingIdPrefix ($ prefix );
111- }
112-
113- public function deleteUsingMetadataFilter (string $ filter ): VectorDeleteResult
114- {
115- return (new DeleteVectorsOperation ($ this ->namespace , $ this ->transporter ))->deleteUsingMetadataFilter ($ filter );
116- }
117108}
Original file line number Diff line number Diff line change 33namespace Upstash \Vector \Operations ;
44
55use InvalidArgumentException ;
6+ use Upstash \Vector \Contracts \Transformers \ToDeletablePayloadInterface ;
67use Upstash \Vector \Contracts \TransporterInterface ;
78use Upstash \Vector \Contracts \VectorIdentifierInterface ;
89use Upstash \Vector \Exceptions \OperationFailedException ;
2324 public function __construct (private string $ namespace , private TransporterInterface $ transporter ) {}
2425
2526 /**
26- * @param array<string|VectorIdentifierInterface> $ids
27+ * @param array<string|VectorIdentifierInterface>|ToDeletablePayloadInterface $ids
2728 */
28- public function delete (array $ ids ): VectorDeleteResult
29+ public function delete (array | ToDeletablePayloadInterface $ ids ): VectorDeleteResult
2930 {
31+ if ($ ids instanceof ToDeletablePayloadInterface) {
32+ return $ this ->sendDeleteRequest ($ ids ->toDeletablePayload ());
33+ }
34+
3035 return $ this ->sendDeleteRequest ([
3136 'ids ' => $ this ->mapIds ($ ids ),
3237 ]);
3338 }
3439
35- public function deleteUsingIdPrefix (string $ prefix ): VectorDeleteResult
36- {
37- return $ this ->sendDeleteRequest (compact ('prefix ' ));
38- }
39-
40- public function deleteUsingMetadataFilter (string $ filter ): VectorDeleteResult
41- {
42- return $ this ->sendDeleteRequest (compact ('filter ' ));
43- }
44-
4540 private function getPath (): string
4641 {
4742 $ namespace = trim ($ this ->namespace );
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Upstash \Vector ;
4+
5+ use Upstash \Vector \Contracts \Transformers \ToDeletablePayloadInterface ;
6+
7+ final readonly class VectorDelete implements ToDeletablePayloadInterface
8+ {
9+ /**
10+ * @var string[]
11+ */
12+ public array $ ids ;
13+
14+ /**
15+ * @param string[]|string $ids
16+ */
17+ public function __construct (array |string $ ids = [])
18+ {
19+ if (is_string ($ ids )) {
20+ $ this ->ids = [$ ids ];
21+ } else {
22+ $ this ->ids = array_values (array_unique ($ ids ));
23+ }
24+ }
25+
26+ /**
27+ * @param string[]|string $ids
28+ */
29+ public static function fromIds (array |string $ ids ): VectorDelete
30+ {
31+ return new VectorDelete ($ ids );
32+ }
33+
34+ public static function fromPrefix (string $ prefix ): VectorPrefixDelete
35+ {
36+ return new VectorPrefixDelete ($ prefix );
37+ }
38+
39+ public static function fromMetadataFilter (string $ filter ): VectorFilterDelete
40+ {
41+ return new VectorFilterDelete ($ filter );
42+ }
43+
44+ /**
45+ * @return array{
46+ * ids: string[]
47+ * }
48+ */
49+ public function toDeletablePayload (): array
50+ {
51+ return [
52+ 'ids ' => $ this ->ids ,
53+ ];
54+ }
55+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Upstash \Vector ;
4+
5+ use Upstash \Vector \Contracts \Transformers \ToDeletablePayloadInterface ;
6+
7+ final readonly class VectorFilterDelete implements ToDeletablePayloadInterface
8+ {
9+ public function __construct (
10+ public string $ filter ,
11+ ) {}
12+
13+ /**
14+ * @return array{
15+ * filter: string
16+ * }
17+ */
18+ public function toDeletablePayload (): array
19+ {
20+ return [
21+ 'filter ' => $ this ->filter ,
22+ ];
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Upstash \Vector ;
4+
5+ use Upstash \Vector \Contracts \Transformers \ToDeletablePayloadInterface ;
6+
7+ final readonly class VectorPrefixDelete implements ToDeletablePayloadInterface
8+ {
9+ public function __construct (public string $ prefix ) {}
10+
11+ /**
12+ * @return array{
13+ * prefix: string
14+ * }
15+ */
16+ public function toDeletablePayload (): array
17+ {
18+ return [
19+ 'prefix ' => $ this ->prefix ,
20+ ];
21+ }
22+ }
Original file line number Diff line number Diff line change 55use PHPUnit \Framework \TestCase ;
66use Upstash \Vector \Tests \Concerns \UsesDenseIndex ;
77use Upstash \Vector \Tests \Concerns \WaitsForIndex ;
8+ use Upstash \Vector \VectorDelete ;
89use Upstash \Vector \VectorQuery ;
910use Upstash \Vector \VectorUpsert ;
1011
@@ -63,7 +64,7 @@ public function test_delete_vectors_using_an_id_prefix(): void
6364 ]);
6465 $ this ->waitForIndex ($ this ->namespace );
6566
66- $ result = $ this ->namespace ->deleteUsingIdPrefix ( 'users:* ' );
67+ $ result = $ this ->namespace ->delete (VectorDelete:: fromPrefix ( 'users:* ' ) );
6768
6869 $ this ->assertEquals (2 , $ result ->deleted );
6970 $ this ->assertEquals (1 , $ this ->namespace ->getNamespaceInfo ()->vectorCount );
@@ -96,7 +97,7 @@ public function test_delete_vectors_using_a_metadata_filter(): void
9697 ]);
9798 $ this ->waitForIndex ($ this ->namespace );
9899
99- $ result = $ this ->namespace ->deleteUsingMetadataFilter ( 'salary < 3000 ' );
100+ $ result = $ this ->namespace ->delete (VectorDelete:: fromMetadataFilter ( 'salary < 3000 ' ) );
100101
101102 $ this ->assertEquals (2 , $ result ->deleted );
102103 $ this ->assertEquals (1 , $ this ->namespace ->getNamespaceInfo ()->vectorCount );
You can’t perform that action at this time.
0 commit comments