Skip to content

Commit 51de146

Browse files
authored
Merge pull request #1182 from weaviate/batch_ref_fix
Dont add references if the .to object has not been added yet
2 parents 0ff35d5 + 88eca40 commit 51de146

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

integration/test_batch_v4.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from _pytest.fixtures import SubRequest
77

88
import weaviate
9+
import weaviate.classes as wvc
910
from integration.conftest import _sanitize_collection_name
1011
from weaviate.collections.classes.batch import Shard
1112
from weaviate.collections.classes.config import (
@@ -587,3 +588,37 @@ def test_uuids_keys_and_original_index(client_factory: ClientFactory) -> None:
587588
assert [objs[k][0] for k in client.batch.results.objs.uuids.keys()] == list(
588589
client.batch.results.objs.uuids.values()
589590
)
591+
592+
593+
def test_references_with_to_uuids(client_factory: ClientFactory) -> None:
594+
"""Test that batch waits until the to object is created."""
595+
client, name = client_factory()
596+
597+
client.collections.delete(["target", "source"])
598+
target = client.collections.create(
599+
"target", multi_tenancy_config=wvc.config.Configure.multi_tenancy(enabled=True)
600+
)
601+
source = client.collections.create(
602+
"source",
603+
references=[wvc.config.ReferenceProperty(name="to", target_collection="target")],
604+
multi_tenancy_config=wvc.config.Configure.multi_tenancy(enabled=True),
605+
)
606+
607+
target.tenants.create("tenant-1")
608+
source.tenants.create("tenant-1")
609+
from_uuid = source.with_tenant("tenant-1").data.insert(properties={})
610+
objs = 20
611+
612+
with client.batch.fixed_size(batch_size=10, concurrent_requests=1) as batch:
613+
for _ in range(objs):
614+
to = batch.add_object(collection="target", properties={}, tenant="tenant-1")
615+
batch.add_reference(
616+
from_uuid=from_uuid,
617+
from_property="to",
618+
to=to,
619+
from_collection="source",
620+
tenant="tenant-1",
621+
)
622+
623+
assert len(client.batch.failed_references) == 0, client.batch.failed_references
624+
client.collections.delete(["target", "source"])

weaviate/collections/batch/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ def pop_items(self, pop_amount: int, uuid_lookup: Set[str]) -> List[_BatchRefere
100100
i = 0
101101
self._lock.acquire()
102102
while len(ret) < pop_amount and len(self._items) > 0 and i < len(self._items):
103-
if self._items[i].from_uuid not in uuid_lookup:
103+
if self._items[i].from_uuid not in uuid_lookup and (
104+
self._items[i].to_uuid is None or self._items[i].to_uuid not in uuid_lookup
105+
):
104106
ret.append(self._items.pop(i))
105107
else:
106108
i += 1

weaviate/collections/classes/batch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class _BatchReference:
3131
to: str
3232
tenant: Optional[str]
3333
from_uuid: str
34+
to_uuid: Optional[str] = None
3435

3536

3637
class BatchObject(BaseModel):
@@ -131,6 +132,7 @@ def _to_internal(self) -> _BatchReference:
131132
from_uuid=str(self.from_object_uuid),
132133
from_=f"{BEACON}{self.from_object_collection}/{self.from_object_uuid}/{self.from_property_name}",
133134
to=f"{BEACON}{self.to_object_collection}{str(self.to_object_uuid)}",
135+
to_uuid=str(self.to_object_uuid),
134136
tenant=self.tenant,
135137
)
136138

weaviate/collections/data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def _reference_add_many(self, refs: List[DataReferences]) -> BatchReferenceRetur
215215
to=beacon,
216216
tenant=self._tenant,
217217
from_uuid=str(ref.from_uuid),
218+
to_uuid=None, # not relevant here, this entry is only needed for the batch module
218219
)
219220
for ref in refs
220221
for beacon in ref._to_beacons()

0 commit comments

Comments
 (0)