From 5528c264d38f4821bd3e4d29c2e507242bb4e142 Mon Sep 17 00:00:00 2001 From: jdl Date: Tue, 21 Nov 2023 18:35:37 +0100 Subject: [PATCH] Added count functionality --- mdb/collection.go | 4 ++++ mdb/index.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/mdb/collection.go b/mdb/collection.go index 2571ccf..d044286 100644 --- a/mdb/collection.go +++ b/mdb/collection.go @@ -274,6 +274,10 @@ func (c *Collection[T]) delete(tx *Snapshot, itemID uint64) error { return c.deleteItem(tx, itemID) } +func (c *Collection[T]) Count(tx *Snapshot) int { + return c.ByID.Count(tx) +} + func (c *Collection[T]) getByID(tx *Snapshot, itemID uint64) (*T, bool) { x := new(T) c.setID(x, itemID) diff --git a/mdb/index.go b/mdb/index.go index a370fa2..f28ac7a 100644 --- a/mdb/index.go +++ b/mdb/index.go @@ -192,6 +192,11 @@ func (i *Index[T]) List(tx *Snapshot, args *ListArgs[T], out []*T) []*T { return items } +func (i *Index[T]) Count(tx *Snapshot) int { + tx = i.ensureSnapshot(tx) + return i.btree(tx).Len() +} + // ---------------------------------------------------------------------------- func (i *Index[T]) insertConflict(tx *Snapshot, item *T) bool {