Skip to content

Commit 07f1a15

Browse files
committed
wip
1 parent 9423770 commit 07f1a15

9 files changed

Lines changed: 129 additions & 42 deletions

File tree

src/Contracts/IndexNamespaceInterface.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Upstash\Vector\Contracts;
44

5+
use Upstash\Vector\Contracts\Transformers\ToDeletablePayloadInterface;
56
use Upstash\Vector\DataQuery;
67
use Upstash\Vector\DataQueryResult;
78
use 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
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

src/Index.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Discovery\Psr18ClientDiscovery;
66
use Upstash\Vector\Contracts\IndexInterface;
77
use Upstash\Vector\Contracts\IndexNamespaceInterface;
8+
use Upstash\Vector\Contracts\Transformers\ToDeletablePayloadInterface;
89
use Upstash\Vector\Contracts\TransporterInterface;
910
use Upstash\Vector\Exceptions\MissingEnvironmentVariableException;
1011
use 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
}

src/IndexNamespace.php

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

55
use Upstash\Vector\Contracts\IndexNamespaceInterface;
6+
use Upstash\Vector\Contracts\Transformers\ToDeletablePayloadInterface;
67
use Upstash\Vector\Contracts\TransporterInterface;
78
use Upstash\Vector\Iterators\VectorRangeIterator;
89
use 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
}

src/Operations/DeleteVectorsOperation.php

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

55
use InvalidArgumentException;
6+
use Upstash\Vector\Contracts\Transformers\ToDeletablePayloadInterface;
67
use Upstash\Vector\Contracts\TransporterInterface;
78
use Upstash\Vector\Contracts\VectorIdentifierInterface;
89
use Upstash\Vector\Exceptions\OperationFailedException;
@@ -23,25 +24,19 @@
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);

src/VectorDelete.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

src/VectorFilterDelete.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\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+
}

src/VectorPrefixDelete.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

tests/Dense/Operations/DeleteVectorsOperationTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Upstash\Vector\Tests\Concerns\UsesDenseIndex;
77
use Upstash\Vector\Tests\Concerns\WaitsForIndex;
8+
use Upstash\Vector\VectorDelete;
89
use Upstash\Vector\VectorQuery;
910
use 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);

0 commit comments

Comments
 (0)