interfaces.go 2.9 KB

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