-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from ipfs/fix/ktds
fix the keytransform datastore's query implementation
- Loading branch information
Showing
12 changed files
with
422 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package keytransform | ||
|
||
import ds "github.com/ipfs/go-datastore" | ||
|
||
// Pair is a convince struct for constructing a key transform. | ||
type Pair struct { | ||
Convert KeyMapping | ||
Invert KeyMapping | ||
} | ||
|
||
func (t *Pair) ConvertKey(k ds.Key) ds.Key { | ||
return t.Convert(k) | ||
} | ||
|
||
func (t *Pair) InvertKey(k ds.Key) ds.Key { | ||
return t.Invert(k) | ||
} | ||
|
||
var _ KeyTransform = (*Pair)(nil) | ||
|
||
// PrefixTransform constructs a KeyTransform with a pair of functions that | ||
// add or remove the given prefix key. | ||
// | ||
// Warning: will panic if prefix not found when it should be there. This is | ||
// to avoid insidious data inconsistency errors. | ||
type PrefixTransform struct { | ||
Prefix ds.Key | ||
} | ||
|
||
// ConvertKey adds the prefix. | ||
func (p PrefixTransform) ConvertKey(k ds.Key) ds.Key { | ||
return p.Prefix.Child(k) | ||
} | ||
|
||
// InvertKey removes the prefix. panics if prefix not found. | ||
func (p PrefixTransform) InvertKey(k ds.Key) ds.Key { | ||
if p.Prefix.String() == "/" { | ||
return k | ||
} | ||
|
||
if !p.Prefix.IsAncestorOf(k) { | ||
panic("expected prefix not found") | ||
} | ||
|
||
s := k.String()[len(p.Prefix.String()):] | ||
return ds.RawKey(s) | ||
} | ||
|
||
var _ KeyTransform = (*PrefixTransform)(nil) |
Oops, something went wrong.