From 4a0b176dbe5aa182e03ae03700203e763e6d6a57 Mon Sep 17 00:00:00 2001 From: DerLinkshaender Date: Wed, 5 Dec 2018 00:01:55 +0100 Subject: [PATCH 1/2] added function to retrieve a slice of all keys --- skv.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/skv.go b/skv.go index e8fd7cc..7bbf35a 100644 --- a/skv.go +++ b/skv.go @@ -141,6 +141,28 @@ func (kvs *KVStore) Delete(key string) error { }) } +// Iterate over all existing keys and return a slice of the keys. +// If no keys are found, return an empty slice. +// +// store.GetKeys() +func (kvs *KVStore) GetKeys() ([][]byte, error) { + var kl [][]byte + + err := kvs.db.View(func(tx *bolt.Tx) error { + var err error + b := tx.Bucket(bucketName) + + err = b.ForEach(func(k, v []byte) error { + kopie := make([]byte, len(k)) + copy(kopie, k) + kl = append(kl, kopie) + return nil + }) + return err + }) + return kl, err +} + // Close closes the key-value store file. func (kvs *KVStore) Close() error { return kvs.db.Close() From 10c54400421f9a96e8bc23ff732241602e0e1a11 Mon Sep 17 00:00:00 2001 From: DerLinkshaender Date: Wed, 5 Dec 2018 00:36:08 +0100 Subject: [PATCH 2/2] retrieve keys as string, added test --- skv-test.db | Bin 0 -> 65536 bytes skv.go | 11 +++++------ skv_test.go | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 skv-test.db diff --git a/skv-test.db b/skv-test.db new file mode 100644 index 0000000000000000000000000000000000000000..41a6619a277c5733a2da949c9d89f2d825658d74 GIT binary patch literal 65536 zcmeI)F;2rU6aY|Dpioqa4s0yFLdC|w$UPW3m6b{ygA*{q%)$*27l45~lrs=F{!)<= zlmV%!^ddF394B#Krk`&orFuIkC7LY5IEl9Q~98 z2oNAZfB*pk1PBlyK!8Ah1WNdx+T#E9yMT@1?w5MR{~zT7muGM9k1yXB8(mci5FkK+ z009C72oNAZfB=D>2t<58NOd5e_c`^S+pntMVp;7Pw>Tpo%xB~`2>GqRVtzjxjni;> zb9*n1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAVA