Skip to content

Commit

Permalink
metadata: add created_at field to namespace metadata (#23)
Browse files Browse the repository at this point in the history
* metadata: add created_at field to namespace metadata

* remove metadata when listing namespaces
  • Loading branch information
sirupsen authored May 21, 2024
1 parent f964c83 commit 536c68a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions tests/test_vectors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import uuid
import turbopuffer as tpuf
import tests
from datetime import datetime


def test_upsert_rows():
Expand Down Expand Up @@ -297,6 +298,7 @@ def test_read_metadata():
assert ns.exists()
assert ns.dimensions() == 2
assert ns.approx_count() == 98
assert type(ns.created_at()) == type(datetime.now())

all_ns = tpuf.namespaces()
assert ns in list(all_ns)
Expand Down
15 changes: 11 additions & 4 deletions turbopuffer/namespace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import iso8601
from datetime import datetime
from turbopuffer.error import APIError
from turbopuffer.vectors import Cursor, VectorResult, VectorColumns, VectorRow, batch_iter
from turbopuffer.backend import Backend
Expand Down Expand Up @@ -51,12 +52,14 @@ def refresh_metadata(self):
'exists': dimensions != 0,
'dimensions': dimensions,
'approx_count': approx_count,
'created_at': iso8601.parse_date(headers.get('x-turbopuffer-created-at')),
}
elif status_code == 404:
self.metadata = {
'exists': False,
'dimensions': 0,
'approx_count': 0,
'created_at': None,
}
else:
raise APIError(response.status_code, 'Unexpected status code', response.get('content'))
Expand Down Expand Up @@ -85,6 +88,14 @@ def approx_count(self) -> int:
self.refresh_metadata()
return self.metadata.pop('approx_count', 0)

def created_at(self) -> Optional[datetime]:
"""
Returns the creation date of this namespace.
"""
if self.metadata is None or 'created_at' not in self.metadata:
self.refresh_metadata()
return self.metadata.pop('created_at', None)

@overload
def upsert(self,
ids: Union[List[int], List[str]],
Expand Down Expand Up @@ -366,10 +377,6 @@ def load_namespaces(api_key: Optional[str], initial_set: List[dict]) -> List[Nam
ns = tpuf.Namespace(input['id'], api_key=api_key)
ns.metadata = {
'exists': True,
'dimensions': input['dimensions'],
'approx_count': input['approx_count'],
# rfc3339 returned by the server is compatible with iso8601
'created_at': iso8601.parse_date(input['created_at']),
}
output.append(ns)

Expand Down

0 comments on commit 536c68a

Please sign in to comment.