Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Collection.Count() method #27

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ func (c *Collection) AddConcurrently(ctx context.Context, ids []string, embeddin
return c.add(ctx, ids, documents, embeddings, metadatas, concurrency)
}

// Count returns the number of documents in the collection.
func (c *Collection) Count() int {
c.documentsLock.RLock()
defer c.documentsLock.RUnlock()
return len(c.documents)
}

// Performs a nearest neighbors query on a collection specified by UUID.
//
// - queryText: The text to search for.
Expand Down
31 changes: 31 additions & 0 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,34 @@ func TestCollection_Add(t *testing.T) {

// TODO: Check expectations when documents become accessible
}

func TestCollection_Count(t *testing.T) {
// Create collection
db := chromem.NewDB()
name := "test"
metadata := map[string]string{"foo": "bar"}
embeddingFunc := func(_ context.Context, _ string) ([]float32, error) {
return []float32{-0.1, 0.1, 0.2}, nil
}
c, err := db.CreateCollection(name, metadata, embeddingFunc)
if err != nil {
t.Error("expected no error, got", err)
}
if c == nil {
t.Error("expected collection, got nil")
}

// Add documents
ids := []string{"1", "2"}
metadatas := []map[string]string{{"foo": "bar"}, {"a": "b"}}
documents := []string{"hello world", "hallo welt"}
err = c.Add(context.Background(), ids, nil, metadatas, documents)
if err != nil {
t.Error("expected nil, got", err)
}

// Check count
if c.Count() != 2 {
t.Error("expected 2, got", c.Count())
}
}