interfaces.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package ezdb
  2. // Collection is a key-value store for documents of any type.
  3. type Collection[T any] interface {
  4. Open() error // Open the collection.
  5. Close() error // Close the collection.
  6. Count() int // Count the number of documents in the collection.
  7. Delete(key string) error // Delete a document by key.
  8. DeleteAll() error // Delete all documents in the collection.
  9. Get(key string) (value T, err error) // Get a document by key.
  10. GetAll() (map[string]T, error) // Get all documents in the collection.
  11. GetAllKeys() []string // Get all keys in the document.
  12. Has(key string) (has bool, err error) // Check whether a document exists by key.
  13. Put(key string, value T) error // Put a document into the collection.
  14. Iter() Iterator[T] // Get an iterator for this collection.
  15. }
  16. // DocumentMarshaler facilitates conversion between two types - a document and its storage representation, depending on the implementation of the Collection.
  17. type DocumentMarshaler[T1 any, T2 any] interface {
  18. Factory() T1 // Create a new, empty document.
  19. Marshal(src T1) (T2, error) // Marshal a document to bytes.
  20. Unmarshal(src T2, dest T1) error // Unmarshal bytes into a document.
  21. }
  22. // FilterFunc processes a document as part of a filter operation.
  23. // This function returns true if the document passes all checks defined in the filter.
  24. type FilterFunc[T any] func(key string, value T) bool
  25. // Iterator provides functionality to explore a collection.
  26. //
  27. // Be mindful that the order of documents is not assured by any Collection implementation.
  28. // Use the Sort or SortKeys function before iterating over documents to ensure deterministic sort.
  29. type Iterator[T any] interface {
  30. First() bool // Move the iterator to the first document. Returns false if there is no first document.
  31. Last() bool // Move the iterator to the last document. Returns false if there is no last document.
  32. Next() bool // Move the iterator to the next document. Returns false if there is no next document.
  33. Prev() bool // Move the iterator to the previous document. Returns false if there is no previous document.
  34. Release() // Release the iterator and any associated resources, including those of previous iterators.
  35. Count() int // Count the number of documents in the iterator.
  36. Get() (key string, value T, err error) // Get the key and value of the current document.
  37. Key() string // Get the key of the current document.
  38. Value() (T, error) // Get the value of the current document.
  39. GetAll() (map[string]T, error) // Get all documents as a key-value map.
  40. GetAllKeys() []string // Get all document keys.
  41. Filter(f FilterFunc[T]) Iterator[T] // Create a new iterator with a subset of documents. The previous iterator will not be affected.
  42. Sort(f SortFunc[T]) Iterator[T] // Create a new iterator with sorted documents. The previous iterator will not be affected.
  43. SortKeys(f SortFunc[string]) Iterator[T] // Create a new iterator with documents sorted by key. The previous iterator will not be affected.
  44. }
  45. // SortFunc compares two documents as part of a sort operation.
  46. // This function returns false if a is less than b.
  47. type SortFunc[T any] func(a T, b T) bool