-
Notifications
You must be signed in to change notification settings - Fork 18.9k
Use hashicorp/go-memdb instead of truncindex #43624
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ const ( | |
| memdbContainersTable = "containers" | ||
| memdbNamesTable = "names" | ||
| memdbIDIndex = "id" | ||
| memdbIDIndexPrefix = "id_prefix" | ||
| memdbContainerIDIndex = "containerid" | ||
| ) | ||
|
|
||
|
|
@@ -27,6 +28,24 @@ var ( | |
| ErrNameNotReserved = errors.New("name is not reserved") | ||
| ) | ||
|
|
||
| var ( | ||
| // ErrEmptyPrefix is an error returned if the prefix was empty. | ||
| ErrEmptyPrefix = errors.New("Prefix can't be empty") | ||
|
|
||
| // ErrNotExist is returned when ID or its prefix not found in index. | ||
| ErrNotExist = errors.New("ID does not exist") | ||
| ) | ||
|
|
||
| // ErrAmbiguousPrefix is returned if the prefix was ambiguous | ||
| // (multiple ids for the prefix). | ||
| type ErrAmbiguousPrefix struct { | ||
| prefix string | ||
| } | ||
|
|
||
| func (e ErrAmbiguousPrefix) Error() string { | ||
| return fmt.Sprintf("Multiple IDs found with provided prefix: %s", e.prefix) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same (also for a follow-up) should be lowercase, and implement |
||
| } | ||
|
|
||
| // Snapshot is a read only view for Containers. It holds all information necessary to serve container queries in a | ||
| // versioned ACID in-memory store. | ||
| type Snapshot struct { | ||
|
|
@@ -64,6 +83,8 @@ type ViewDB interface { | |
| Save(*Container) error | ||
| Delete(*Container) error | ||
|
|
||
| GetByPrefix(string) (string, error) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a look at the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at that; wondering if That's a bit of a weird construct, as effectively now it's using two separate interfaces to query the same data-source (once through the "read-only" Moving this method to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I did initially put The way I see it, My thinking is to remove the |
||
|
|
||
| ReserveName(name, containerID string) error | ||
| ReleaseName(name string) error | ||
| } | ||
|
|
@@ -131,6 +152,38 @@ func NewViewDB() (ViewDB, error) { | |
| return &memDB{store: store}, nil | ||
| } | ||
|
|
||
| func (db *memDB) GetByPrefix(s string) (string, error) { | ||
| if s == "" { | ||
| return "", ErrEmptyPrefix | ||
| } | ||
| txn := db.store.Txn(false) | ||
| iter, err := txn.Get(memdbContainersTable, memdbIDIndexPrefix, s) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| var ( | ||
| id string | ||
| ) | ||
|
|
||
| for { | ||
| item := iter.Next() | ||
| if item == nil { | ||
| break | ||
| } | ||
| if id != "" { | ||
| return "", ErrAmbiguousPrefix{prefix: s} | ||
| } | ||
| id = item.(*Container).ID | ||
| } | ||
|
|
||
| if id != "" { | ||
| return id, nil | ||
| } | ||
|
|
||
| return "", ErrNotExist | ||
| } | ||
|
|
||
| // Snapshot provides a consistent read-only View of the database | ||
| func (db *memDB) Snapshot() View { | ||
| return &memdbView{ | ||
|
|
@@ -441,6 +494,20 @@ func (e *containerByIDIndexer) FromArgs(args ...interface{}) ([]byte, error) { | |
| return []byte(arg), nil | ||
| } | ||
|
|
||
| func (e *containerByIDIndexer) PrefixFromArgs(args ...interface{}) ([]byte, error) { | ||
| val, err := e.FromArgs(args...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Strip the null terminator, the rest is a prefix | ||
| n := len(val) | ||
| if n > 0 { | ||
| return val[:n-1], nil | ||
| } | ||
| return val, nil | ||
| } | ||
|
|
||
| // namesByNameIndexer is used to index container name associations by name. | ||
| type namesByNameIndexer struct{} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not new, so ok to do in a follow-up, but this should be lowercase
prefix, and ideally, it would return an error that implementserrdefs.ErrInvalidParameter;moby/errdefs/defs.go
Lines 8 to 11 in 7d4b788