Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrDocExists is returned when attempting to insert a document that already exists. ErrDocExists = errors.New("any-store: document already exists") // ErrDocNotFound is returned when a document cannot be found by its ID. ErrDocNotFound = errors.New("any-store: document not found") // ErrDocWithoutId is returned when a document is provided without a required ID. ErrDocWithoutId = errors.New("any-store: document missing ID") // ErrCollectionExists is returned when attempting to create a collection that already exists. ErrCollectionExists = errors.New("any-store: collection already exists") // ErrCollectionNotFound is returned when a collection cannot be found. ErrCollectionNotFound = errors.New("any-store: collection not found") // ErrIndexExists is returned when attempting to create an index that already exists. ErrIndexExists = errors.New("any-store: index already exists") // ErrIndexNotFound is returned when an index cannot be found. ErrIndexNotFound = errors.New("any-store: index not found") // ErrTxIsReadOnly is returned when a write operation is attempted in a read-only transaction. ErrTxIsReadOnly = errors.New("any-store: transaction is read-only") // ErrTxIsUsed is returned when an operation is attempted on a transaction that has already been committed or rolled back. ErrTxIsUsed = errors.New("any-store: transaction has already been used") // ErrTxOtherInstance is returned when an operation is attempted using a transaction from a different database instance. ErrTxOtherInstance = errors.New("any-store: transaction belongs to another database instance") // ErrUniqueConstraint is returned when a unique constraint violation occurs. ErrUniqueConstraint = errors.New("any-store: unique constraint violation") // ErrIterClosed is returned when operations are attempted on a closed iterator. ErrIterClosed = errors.New("any-store: iterator is closed") )
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection interface {
// Name returns the name of the collection.
Name() string
// FindId finds a document by its ID.
// Returns the document or an error if the document is not found.
FindId(ctx context.Context, id any) (Doc, error)
// FindIdWithParser finds a document by its ID. Uses provided fastjson parser.
// Returns the document or an error if the document is not found.
FindIdWithParser(ctx context.Context, p *fastjson.Parser, id any) (Doc, error)
// Find returns a new Query object with given filter
Find(filter any) Query
// InsertOne inserts a single document into the collection.
// Returns the ID of the inserted document or an error if the insertion fails.
InsertOne(ctx context.Context, doc any) (id any, err error)
// Insert inserts multiple documents into the collection.
// Returns an error if the insertion fails.
Insert(ctx context.Context, docs ...any) (err error)
// UpdateOne updates a single document in the collection.
// Provided document must contain an id field
// Returns an error if the update fails.
UpdateOne(ctx context.Context, doc any) (err error)
// UpdateId updates a single document in the collection with provided modifier
// Returns a modify result or error.
UpdateId(ctx context.Context, id any, mod query.Modifier) (res ModifyResult, err error)
// UpsertOne inserts a document if it does not exist, or updates it if it does.
// Returns the ID of the upserted document or an error if the operation fails.
UpsertOne(ctx context.Context, doc any) (id any, err error)
// UpsertId updates a single document or creates new one
// Returns a modify result or error.
UpsertId(ctx context.Context, id any, mod query.Modifier) (res ModifyResult, err error)
// DeleteId deletes a single document by its ID.
// Returns an error if the deletion fails.
DeleteId(ctx context.Context, id any) (err error)
// Count returns the number of documents in the collection.
// Returns the count of documents or an error if the operation fails.
Count(ctx context.Context) (count int, err error)
// EnsureIndex ensures an index exists on the specified fields.
// Returns an error if the operation fails.
EnsureIndex(ctx context.Context, info ...IndexInfo) (err error)
// DropIndex drops an index by its name.
// Returns an error if the operation fails.
DropIndex(ctx context.Context, indexName string) (err error)
// GetIndexes returns a list of indexes on the collection.
GetIndexes() (indexes []Index)
// Rename renames the collection.
// Returns an error if the operation fails.
Rename(ctx context.Context, newName string) (err error)
// Drop drops the collection.
// Returns an error if the operation fails.
Drop(ctx context.Context) (err error)
// ReadTx starts a new read-only transaction. It's just a proxy to db object.
// Returns a ReadTx or an error if there is an issue starting the transaction.
ReadTx(ctx context.Context) (ReadTx, error)
// WriteTx starts a new read-write transaction. It's just a proxy to db object.
// Returns a WriteTx or an error if there is an issue starting the transaction.
WriteTx(ctx context.Context) (WriteTx, error)
// Close closes the collection.
// Returns an error if the operation fails.
Close() error
}
Collection represents a collection of documents.
type Config ¶
type Config struct {
// Namespace is a prefix for all created tables and indexes by any-store,
// helping to isolate tables and indexes within the same database file.
Namespace string
// ReadConnections specifies the number of read connections to the database,
// optimizing read operations by allowing multiple concurrent read connections.
ReadConnections int
// SQLiteConnectionOptions provides additional options for SQLite connections,
// corresponding to SQLite pragmas or other connection settings.
SQLiteConnectionOptions map[string]string
}
Config provides the configuration options for the database.
type DB ¶
type DB interface {
// CreateCollection creates a new collection with the specified name.
// Returns the created Collection or an error if the collection already exists.
// Possible errors:
// - ErrCollectionExists: if the collection already exists.
CreateCollection(ctx context.Context, collectionName string) (Collection, error)
// OpenCollection opens an existing collection with the specified name.
// Returns the opened Collection or an error if the collection does not exist.
// Possible errors:
// - ErrCollectionNotFound: if the collection does not exist.
OpenCollection(ctx context.Context, collectionName string) (Collection, error)
// Collection is a convenience method to get or create a collection.
// It first attempts to open the collection, and if it does not exist, it creates the collection.
// Returns the Collection or an error if there is an issue creating or opening the collection.
Collection(ctx context.Context, collectionName string) (Collection, error)
// GetCollectionNames returns a list of all collection names in the database.
// Returns a slice of collection names or an error if there is an issue retrieving the names.
GetCollectionNames(ctx context.Context) ([]string, error)
// Stats returns the statistics of the database.
// Returns a DBStats struct containing the database statistics or an error if there is an issue retrieving the stats.
Stats(ctx context.Context) (DBStats, error)
// ReadTx starts a new read-only transaction.
// Returns a ReadTx or an error if there is an issue starting the transaction.
ReadTx(ctx context.Context) (ReadTx, error)
// WriteTx starts a new read-write transaction.
// Returns a WriteTx or an error if there is an issue starting the transaction.
WriteTx(ctx context.Context) (WriteTx, error)
// Close closes the database connection.
// Returns an error if there is an issue closing the connection.
Close() error
}
DB represents a document-oriented database.
type DBStats ¶
type DBStats struct {
// CollectionsCount is the total number of collections in the database.
CollectionsCount int
// IndexesCount is the total number of indexes across all collections in the database.
IndexesCount int
// TotalSizeBytes is the total size of the database in bytes.
TotalSizeBytes int
// DataSizeBytes is the total size of the data stored in the database in bytes, excluding free space.
DataSizeBytes int
}
DBStats represents the statistics of the database.
type Doc ¶
type Doc interface {
// Decode decodes the document into the provided variable.
// The variable should be a pointer to the appropriate type.
// Returns an error if decoding fails.
Decode(v any) error
// Value returns the document as a *fastjson.Value.
// Important: When used in an iterator, the returned value is valid only until the next call to Next.
Value() *fastjson.Value
}
Doc represents a document in the collection.
type Explain ¶
type Explain struct {
Sql string
SqliteExplain []string
Indexes []IndexExplain
}
type Index ¶
type Index interface {
// Info returns the IndexInfo for this index.
Info() IndexInfo
// Len returns the length of the index.
Len(ctx context.Context) (int, error)
}
Index represents an index on a collection.
type IndexExplain ¶
type IndexInfo ¶
type IndexInfo struct {
// Name is the name of the index. If empty, it will be generated
// based on the fields (e.g., "name,-createdDate").
Name string
// Fields are the fields included in the index. Each field can specify
// ascending (e.g., "name") or descending (e.g., "-createdDate") order.
Fields []string
// Unique indicates whether the index enforces a unique constraint.
Unique bool
// Sparse indicates whether the index is sparse, indexing only documents
// with the specified fields.
Sparse bool
}
IndexInfo provides information about an index.
type Iterator ¶
type Iterator interface {
// Next advances the iterator to the next document.
// Returns false if there are no more documents.
Next() bool
// Doc returns the current document.
// Returns an error if there is an issue retrieving the document.
Doc() (Doc, error)
// Err returns any error encountered during the lifetime of the iterator,
Err() error
// Close closes the iterator and releases any associated resources.
// Returns an error if there is an issue closing the iterator or any other error encountered during its lifetime.
Close() error
}
Iterator represents an iterator over query results.
Example of usage:
for iter.Next() {
doc, err := iter.Doc()
if err != nil {
log.Fatalf("error retrieving document: %v", err)
}
fmt.Println("Document:", doc.Value().String())
}
if err := iter.Close(); err != nil {
log.Fatalf("iteration error: %v", err)
}
type ModifyResult ¶
type ModifyResult struct {
// Matched is the number of documents matched by the query.
Matched int
// Modified is the number of documents that were actually modified.
Modified int
}
ModifyResult represents the result of a modification operation.
type Query ¶
type Query interface {
// Limit sets the maximum number of documents to return.
Limit(limit uint) Query
// Offset sets the number of documents to skip before starting to return results.
Offset(offset uint) Query
// Sort sets the sort order for the query results.
Sort(sort ...any) Query
// IndexHint adds or removes boost for some indexes
IndexHint(hints ...IndexHint) Query
// Iter executes the query and returns an Iterator for the results.
Iter(ctx context.Context) (Iterator, error)
// Count returns the number of documents matching the query.
Count(ctx context.Context) (count int, err error)
// Update modifies documents matching the query.
Update(ctx context.Context, modifier any) (res ModifyResult, err error)
// Delete removes documents matching the query.
Delete(ctx context.Context) (res ModifyResult, err error)
// Explain provides the query execution plan.
Explain(ctx context.Context) (explain Explain, err error)
}
Query represents a query on a collection.
type ReadTx ¶
type ReadTx interface {
// Context returns the context associated with the transaction.
Context() context.Context
// Commit commits the transaction.
// Returns an error if the commit fails.
Commit() error
// Done returns true if the transaction is completed (committed or rolled back).
Done() bool
// contains filtered or unexported methods
}
ReadTx represents a read-only transaction.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.