Skip to content
Snippets Groups Projects
Commit f2b32423 authored by Jono Wenger's avatar Jono Wenger
Browse files

Merge branch 'XX-4448/purgeBug' into 'release'

Xx 4448/purge bug

See merge request !64
parents fea2826e b0e6dae2
No related branches found
No related tags found
2 merge requests!67fix for latest client release,!64Xx 4448/purge bug
......@@ -114,36 +114,46 @@ func (ls *LocalStorage) Clear() {
ls.clear()
}
// ClearPrefix clears all keys with the given prefix.
func (ls *LocalStorage) ClearPrefix(prefix string) {
// ClearPrefix clears all keys with the given prefix. Returns the number of
// keys cleared.
func (ls *LocalStorage) ClearPrefix(prefix string) int {
// Get a copy of all key names at once
keys := ls.keys()
// Loop through each key
var n int
for i := 0; i < keys.Length(); i++ {
if v := keys.Index(i); !v.IsNull() {
keyName := strings.TrimPrefix(v.String(), ls.prefix)
if strings.HasPrefix(keyName, prefix) {
ls.removeItem(v.String())
n++
}
}
}
return n
}
// ClearWASM clears all the keys in storage created by WASM.
func (ls *LocalStorage) ClearWASM() {
// ClearWASM clears all the keys in storage created by WASM. Returns the number
// of keys cleared.
func (ls *LocalStorage) ClearWASM() int {
// Get a copy of all key names at once
keys := ls.keys()
// Loop through each key
var n int
for i := 0; i < keys.Length(); i++ {
if v := keys.Index(i); !v.IsNull() {
keyName := v.String()
if strings.HasPrefix(keyName, ls.prefix) {
ls.RemoveItem(strings.TrimPrefix(keyName, ls.prefix))
n++
}
}
}
return n
}
// Key returns the name of the nth key in localStorage. Return os.ErrNotExist if
......
......@@ -91,10 +91,11 @@ func TestLocalStorage_ClearPrefix(t *testing.T) {
s := newLocalStorage("")
s.clear()
prng := rand.New(rand.NewSource(11))
const numKeys = 10
var yesPrefix, noPrefix []string
prefix := "keyNamePrefix/"
for i := 0; i < 10; i++ {
for i := 0; i < numKeys; i++ {
keyName := "keyNum" + strconv.Itoa(i)
if prng.Intn(2) == 0 {
keyName = prefix + keyName
......@@ -106,7 +107,11 @@ func TestLocalStorage_ClearPrefix(t *testing.T) {
s.SetItem(keyName, []byte(strconv.Itoa(i)))
}
s.ClearPrefix(prefix)
n := s.ClearPrefix(prefix)
if n != numKeys/2 {
t.Errorf("Incorrect number of keys.\nexpected: %d\nreceived: %d",
numKeys/2, n)
}
for _, keyName := range noPrefix {
if _, err := s.GetItem(keyName); err != nil {
......@@ -126,8 +131,10 @@ func TestLocalStorage_ClearPrefix(t *testing.T) {
func TestLocalStorage_ClearWASM(t *testing.T) {
jsStorage.clear()
prng := rand.New(rand.NewSource(11))
const numKeys = 10
var yesPrefix, noPrefix []string
for i := 0; i < 10; i++ {
for i := 0; i < numKeys; i++ {
keyName := "keyNum" + strconv.Itoa(i)
if prng.Intn(2) == 0 {
yesPrefix = append(yesPrefix, keyName)
......@@ -138,7 +145,11 @@ func TestLocalStorage_ClearWASM(t *testing.T) {
}
}
jsStorage.ClearWASM()
n := jsStorage.ClearWASM()
if n != numKeys/2 {
t.Errorf("Incorrect number of keys.\nexpected: %d\nreceived: %d",
numKeys/2, n)
}
for _, keyName := range noPrefix {
if v := jsStorage.getItem(keyName); v.IsNull() {
......
......@@ -12,6 +12,7 @@ package storage
import (
"github.com/hack-pad/go-indexeddb/idb"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/v4/storage/utility"
"gitlab.com/elixxir/xxdk-wasm/utils"
"sync/atomic"
......@@ -61,11 +62,11 @@ func Purge(_ js.Value, args []js.Value) any {
}
// Verify all Cmix followers are stopped
// if n := atomic.LoadUint64(&numClientsRunning); n != 0 {
// utils.Throw(utils.TypeError, errors.Errorf(
// "%d cMix followers running; all need to be stopped", n))
// return nil
// }
if n := atomic.LoadUint64(&numClientsRunning); n != 0 {
utils.Throw(utils.TypeError, errors.Errorf(
"%d cMix followers running; all need to be stopped", n))
return nil
}
// Get all indexedDb database names
databaseList, err := GetIndexedDbList()
......@@ -74,6 +75,8 @@ func Purge(_ js.Value, args []js.Value) any {
"failed to get list of indexedDb database names: %+v", err))
return nil
}
jww.DEBUG.Printf("[PURGE] Found %d databases to delete: %s",
len(databaseList), databaseList)
// Delete each database
for dbName := range databaseList {
......@@ -89,13 +92,18 @@ func Purge(_ js.Value, args []js.Value) any {
ls := GetLocalStorage()
// Clear all local storage saved by this WASM project
ls.ClearWASM()
n := ls.ClearWASM()
jww.DEBUG.Printf("[PURGE] Cleared %d WASM keys in local storage", n)
// Clear all EKV from local storage
ls.ClearPrefix(storageDirectory)
n = ls.ClearPrefix(storageDirectory)
jww.DEBUG.Printf("[PURGE] Cleared %d keys with the prefix %q (for EKV)",
n, storageDirectory)
// Clear all NDFs saved to local storage
ls.ClearPrefix(utility.NdfStorageKeyNamePrefix)
n = ls.ClearPrefix(utility.NdfStorageKeyNamePrefix)
jww.DEBUG.Printf("[PURGE] Cleared %d keys with the prefix %q (for NDF)",
n, utility.NdfStorageKeyNamePrefix)
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment