Skip to content

Commit d724db9

Browse files
committed
wip
1 parent a8f5dd4 commit d724db9

10 files changed

+76
-122
lines changed

src/Contracts/IndexNamespaceInterface.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace Upstash\Vector\Contracts;
44

5-
use Upstash\Vector\Contracts\Transformers\ToDeletablePayloadInterface;
65
use Upstash\Vector\DataQuery;
76
use Upstash\Vector\DataQueryResult;
87
use Upstash\Vector\DataUpsert;
98
use Upstash\Vector\Iterators\VectorRangeIterator;
109
use Upstash\Vector\NamespaceInfo;
10+
use Upstash\Vector\VectorDeleteByMetadataFilter;
11+
use Upstash\Vector\VectorDeleteByPrefix;
1112
use Upstash\Vector\VectorDeleteResult;
1213
use Upstash\Vector\VectorFetch;
1314
use Upstash\Vector\VectorFetchResult;
@@ -52,9 +53,9 @@ public function queryMany(array $queries): VectorQueryManyResult;
5253
public function queryData(DataQuery $query): DataQueryResult;
5354

5455
/**
55-
* @param array<string|VectorIdentifierInterface>|ToDeletablePayloadInterface $ids
56+
* @param array<string|VectorIdentifierInterface>|string|VectorDeleteByPrefix|VectorDeleteByMetadataFilter $ids
5657
*/
57-
public function delete(array|ToDeletablePayloadInterface $ids): VectorDeleteResult;
58+
public function delete(array|string|VectorDeleteByPrefix|VectorDeleteByMetadataFilter $ids): VectorDeleteResult;
5859

5960
public function fetch(VectorFetch $vectorFetch): VectorFetchResult;
6061

src/Contracts/Transformers/ToDeletablePayloadInterface.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Index.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Http\Discovery\Psr18ClientDiscovery;
66
use Upstash\Vector\Contracts\IndexInterface;
77
use Upstash\Vector\Contracts\IndexNamespaceInterface;
8-
use Upstash\Vector\Contracts\Transformers\ToDeletablePayloadInterface;
98
use Upstash\Vector\Contracts\TransporterInterface;
109
use Upstash\Vector\Exceptions\MissingEnvironmentVariableException;
1110
use Upstash\Vector\Iterators\VectorRangeIterator;
@@ -114,7 +113,7 @@ public function queryData(DataQuery $query): DataQueryResult
114113
return $this->namespace('')->queryData($query);
115114
}
116115

117-
public function delete(array|ToDeletablePayloadInterface $ids): VectorDeleteResult
116+
public function delete(array|string|VectorDeleteByPrefix|VectorDeleteByMetadataFilter $ids): VectorDeleteResult
118117
{
119118
return $this->namespace('')->delete($ids);
120119
}

src/IndexNamespace.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Upstash\Vector;
44

55
use Upstash\Vector\Contracts\IndexNamespaceInterface;
6-
use Upstash\Vector\Contracts\Transformers\ToDeletablePayloadInterface;
76
use Upstash\Vector\Contracts\TransporterInterface;
87
use Upstash\Vector\Iterators\VectorRangeIterator;
98
use Upstash\Vector\Operations\DeleteNamespaceOperation;
@@ -74,7 +73,10 @@ public function queryData(DataQuery $query): DataQueryResult
7473
return (new QueryDataOperation($this->namespace, $this->transporter))->query($query);
7574
}
7675

77-
public function delete(array|ToDeletablePayloadInterface $ids): VectorDeleteResult
76+
/**
77+
* @param string[]|string|VectorDeleteByPrefix|VectorDeleteByMetadataFilter $ids
78+
*/
79+
public function delete(array|string|VectorDeleteByPrefix|VectorDeleteByMetadataFilter $ids): VectorDeleteResult
7880
{
7981
return (new DeleteVectorsOperation($this->namespace, $this->transporter))
8082
->delete($ids);

src/Operations/DeleteVectorsOperation.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Upstash\Vector\Operations;
44

55
use InvalidArgumentException;
6-
use Upstash\Vector\Contracts\Transformers\ToDeletablePayloadInterface;
6+
use Upstash\Vector\Contracts\Arrayable;
77
use Upstash\Vector\Contracts\TransporterInterface;
88
use Upstash\Vector\Contracts\VectorIdentifierInterface;
99
use Upstash\Vector\Exceptions\OperationFailedException;
@@ -12,6 +12,8 @@
1212
use Upstash\Vector\Transporter\Method;
1313
use Upstash\Vector\Transporter\TransporterRequest;
1414
use Upstash\Vector\Transporter\TransporterResponse;
15+
use Upstash\Vector\VectorDeleteByMetadataFilter;
16+
use Upstash\Vector\VectorDeleteByPrefix;
1517
use Upstash\Vector\VectorDeleteResult;
1618

1719
/**
@@ -24,12 +26,20 @@
2426
public function __construct(private string $namespace, private TransporterInterface $transporter) {}
2527

2628
/**
27-
* @param array<string|VectorIdentifierInterface>|ToDeletablePayloadInterface $ids
29+
* @param string[]|string|VectorDeleteByPrefix|VectorDeleteByMetadataFilter $ids
30+
*
31+
* @throws OperationFailedException
2832
*/
29-
public function delete(array|ToDeletablePayloadInterface $ids): VectorDeleteResult
33+
public function delete(array|string|VectorDeleteByPrefix|VectorDeleteByMetadataFilter $ids): VectorDeleteResult
3034
{
31-
if ($ids instanceof ToDeletablePayloadInterface) {
32-
return $this->sendDeleteRequest($ids->toDeletablePayload());
35+
if ($ids instanceof Arrayable) {
36+
return $this->sendDeleteRequest($ids->toArray());
37+
}
38+
39+
if (is_string($ids)) {
40+
return $this->sendDeleteRequest([
41+
'ids' => [$ids],
42+
]);
3343
}
3444

3545
return $this->sendDeleteRequest([

src/VectorDelete.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/VectorFilterDelete.php renamed to src/VectorDeleteByMetadataFilter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
namespace Upstash\Vector;
44

5-
use Upstash\Vector\Contracts\Transformers\ToDeletablePayloadInterface;
5+
use Upstash\Vector\Contracts\Arrayable;
66

7-
final readonly class VectorFilterDelete implements ToDeletablePayloadInterface
7+
final readonly class VectorDeleteByMetadataFilter implements Arrayable
88
{
99
public function __construct(
1010
public string $filter,
1111
) {}
1212

1313
/**
1414
* @return array{
15-
* filter: string
15+
* filter: string,
1616
* }
1717
*/
18-
public function toDeletablePayload(): array
18+
public function toArray(): array
1919
{
2020
return [
2121
'filter' => $this->filter,

src/VectorDeleteByPrefix.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Upstash\Vector;
4+
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class VectorDeleteByPrefix implements Arrayable
8+
{
9+
public function __construct(
10+
public string $prefix,
11+
) {}
12+
13+
/**
14+
* @return array{
15+
* prefix: string,
16+
* }
17+
*/
18+
public function toArray(): array
19+
{
20+
return [
21+
'prefix' => $this->prefix,
22+
];
23+
}
24+
}

src/VectorPrefixDelete.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/Dense/Operations/DeleteVectorsOperationTest.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use PHPUnit\Framework\TestCase;
66
use Upstash\Vector\Tests\Concerns\UsesDenseIndex;
77
use Upstash\Vector\Tests\Concerns\WaitsForIndex;
8-
use Upstash\Vector\VectorDelete;
8+
use Upstash\Vector\VectorDeleteByMetadataFilter;
9+
use Upstash\Vector\VectorDeleteByPrefix;
910
use Upstash\Vector\VectorQuery;
1011
use Upstash\Vector\VectorUpsert;
1112

@@ -35,6 +36,22 @@ public function test_delete_vectors(): void
3536
$this->assertSame(1, $info->vectorCount);
3637
}
3738

39+
public function test_delete_single_vector(): void
40+
{
41+
$this->namespace->upsertMany([
42+
new VectorUpsert('id-1', vector: createRandomVector(2)),
43+
new VectorUpsert('id-2', vector: createRandomVector(2)),
44+
new VectorUpsert('id-3', vector: createRandomVector(2)),
45+
]);
46+
$this->waitForIndex($this->namespace);
47+
48+
$result = $this->namespace->delete('id-1');
49+
50+
$this->assertEquals(1, $result->deleted);
51+
$info = $this->namespace->getNamespaceInfo();
52+
$this->assertSame(2, $info->vectorCount);
53+
}
54+
3855
public function test_delete_vectors_from_a_query_result_results(): void
3956
{
4057
$vector = createRandomVector(2);
@@ -55,21 +72,6 @@ public function test_delete_vectors_from_a_query_result_results(): void
5572
$this->assertEquals(2, $result->deleted);
5673
}
5774

58-
public function test_delete_vectors_using_builder_pattern(): void
59-
{
60-
$this->namespace->upsertMany([
61-
new VectorUpsert('users:1', vector: createRandomVector(2)),
62-
new VectorUpsert('users:2', vector: createRandomVector(2)),
63-
new VectorUpsert('posts:1', vector: createRandomVector(2)),
64-
]);
65-
$this->waitForIndex($this->namespace);
66-
67-
$result = $this->namespace->delete(VectorDelete::fromIds(['users:1', 'users:2']));
68-
69-
$this->assertEquals(2, $result->deleted);
70-
$this->assertEquals(1, $this->namespace->getNamespaceInfo()->vectorCount);
71-
}
72-
7375
public function test_delete_vectors_using_an_id_prefix(): void
7476
{
7577
$this->namespace->upsertMany([
@@ -79,7 +81,9 @@ public function test_delete_vectors_using_an_id_prefix(): void
7981
]);
8082
$this->waitForIndex($this->namespace);
8183

82-
$result = $this->namespace->delete(VectorDelete::fromPrefix('users:'));
84+
$result = $this->namespace->delete(new VectorDeleteByPrefix(
85+
prefix: 'users:',
86+
));
8387

8488
$this->assertEquals(2, $result->deleted);
8589
$this->assertEquals(1, $this->namespace->getNamespaceInfo()->vectorCount);
@@ -112,7 +116,9 @@ public function test_delete_vectors_using_a_metadata_filter(): void
112116
]);
113117
$this->waitForIndex($this->namespace);
114118

115-
$result = $this->namespace->delete(VectorDelete::fromMetadataFilter('salary < 3000'));
119+
$result = $this->namespace->delete(new VectorDeleteByMetadataFilter(
120+
filter: 'salary > 1000',
121+
));
116122

117123
$this->assertEquals(2, $result->deleted);
118124
$this->assertEquals(1, $this->namespace->getNamespaceInfo()->vectorCount);

0 commit comments

Comments
 (0)