-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c6f042
commit b22e006
Showing
12 changed files
with
271 additions
and
121 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,55 @@ | ||
package indexutil | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/garethgeorge/resticui/internal/database/serializationutil" | ||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
func IndexByteValue(b *bolt.Bucket, value []byte, recordId int64) error { | ||
key := serializationutil.BytesToKey(value) | ||
key = append(key, serializationutil.Itob(recordId)...) | ||
return b.Put(key, []byte{}) | ||
} | ||
|
||
func IndexSearchByteValue(b *bolt.Bucket, value []byte) *IndexSearchIterator { | ||
return newSearchIterator(b, serializationutil.BytesToKey(value)) | ||
} | ||
|
||
func IndexSearchIntValue(b *bolt.Bucket, value int64) *IndexSearchIterator { | ||
return newSearchIterator(b, serializationutil.Itob(value)) | ||
} | ||
|
||
type IndexSearchIterator struct { | ||
c *bolt.Cursor | ||
k []byte | ||
prefix []byte | ||
} | ||
|
||
func newSearchIterator(b *bolt.Bucket, prefix []byte) *IndexSearchIterator { | ||
c := b.Cursor() | ||
k, _ := c.Seek(prefix) | ||
return &IndexSearchIterator{ | ||
c: c, | ||
k: k, | ||
prefix: prefix, | ||
} | ||
} | ||
|
||
func (i *IndexSearchIterator) Next() (int64, bool) { | ||
if i.k == nil || !bytes.HasPrefix(i.k, i.prefix) { | ||
return 0, false | ||
} | ||
id := serializationutil.Btoi(i.k[len(i.prefix):]) | ||
i.k, _ = i.c.Next() | ||
return id, true | ||
} | ||
|
||
func (i *IndexSearchIterator) ToSlice() []int64 { | ||
var ids []int64 | ||
for id, ok := i.Next(); ok; id, ok = i.Next() { | ||
ids = append(ids, id) | ||
} | ||
return ids | ||
} |
Oops, something went wrong.