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
This commit is part of merge request !67. Comments created here will be created in the context of that merge request.
...@@ -114,36 +114,46 @@ func (ls *LocalStorage) Clear() { ...@@ -114,36 +114,46 @@ func (ls *LocalStorage) Clear() {
ls.clear() ls.clear()
} }
// ClearPrefix clears all keys with the given prefix. // ClearPrefix clears all keys with the given prefix. Returns the number of
func (ls *LocalStorage) ClearPrefix(prefix string) { // keys cleared.
func (ls *LocalStorage) ClearPrefix(prefix string) int {
// Get a copy of all key names at once // Get a copy of all key names at once
keys := ls.keys() keys := ls.keys()
// Loop through each key // Loop through each key
var n int
for i := 0; i < keys.Length(); i++ { for i := 0; i < keys.Length(); i++ {
if v := keys.Index(i); !v.IsNull() { if v := keys.Index(i); !v.IsNull() {
keyName := strings.TrimPrefix(v.String(), ls.prefix) keyName := strings.TrimPrefix(v.String(), ls.prefix)
if strings.HasPrefix(keyName, prefix) { if strings.HasPrefix(keyName, prefix) {
ls.removeItem(v.String()) ls.removeItem(v.String())
n++
} }
} }
} }
return n
} }
// ClearWASM clears all the keys in storage created by WASM. // ClearWASM clears all the keys in storage created by WASM. Returns the number
func (ls *LocalStorage) ClearWASM() { // of keys cleared.
func (ls *LocalStorage) ClearWASM() int {
// Get a copy of all key names at once // Get a copy of all key names at once
keys := ls.keys() keys := ls.keys()
// Loop through each key // Loop through each key
var n int
for i := 0; i < keys.Length(); i++ { for i := 0; i < keys.Length(); i++ {
if v := keys.Index(i); !v.IsNull() { if v := keys.Index(i); !v.IsNull() {
keyName := v.String() keyName := v.String()
if strings.HasPrefix(keyName, ls.prefix) { if strings.HasPrefix(keyName, ls.prefix) {
ls.RemoveItem(strings.TrimPrefix(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 // Key returns the name of the nth key in localStorage. Return os.ErrNotExist if
......
...@@ -91,10 +91,11 @@ func TestLocalStorage_ClearPrefix(t *testing.T) { ...@@ -91,10 +91,11 @@ func TestLocalStorage_ClearPrefix(t *testing.T) {
s := newLocalStorage("") s := newLocalStorage("")
s.clear() s.clear()
prng := rand.New(rand.NewSource(11)) prng := rand.New(rand.NewSource(11))
const numKeys = 10
var yesPrefix, noPrefix []string var yesPrefix, noPrefix []string
prefix := "keyNamePrefix/" prefix := "keyNamePrefix/"
for i := 0; i < 10; i++ { for i := 0; i < numKeys; i++ {
keyName := "keyNum" + strconv.Itoa(i) keyName := "keyNum" + strconv.Itoa(i)
if prng.Intn(2) == 0 { if prng.Intn(2) == 0 {
keyName = prefix + keyName keyName = prefix + keyName
...@@ -106,7 +107,11 @@ func TestLocalStorage_ClearPrefix(t *testing.T) { ...@@ -106,7 +107,11 @@ func TestLocalStorage_ClearPrefix(t *testing.T) {
s.SetItem(keyName, []byte(strconv.Itoa(i))) 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 { for _, keyName := range noPrefix {
if _, err := s.GetItem(keyName); err != nil { if _, err := s.GetItem(keyName); err != nil {
...@@ -126,8 +131,10 @@ func TestLocalStorage_ClearPrefix(t *testing.T) { ...@@ -126,8 +131,10 @@ func TestLocalStorage_ClearPrefix(t *testing.T) {
func TestLocalStorage_ClearWASM(t *testing.T) { func TestLocalStorage_ClearWASM(t *testing.T) {
jsStorage.clear() jsStorage.clear()
prng := rand.New(rand.NewSource(11)) prng := rand.New(rand.NewSource(11))
const numKeys = 10
var yesPrefix, noPrefix []string var yesPrefix, noPrefix []string
for i := 0; i < 10; i++ {
for i := 0; i < numKeys; i++ {
keyName := "keyNum" + strconv.Itoa(i) keyName := "keyNum" + strconv.Itoa(i)
if prng.Intn(2) == 0 { if prng.Intn(2) == 0 {
yesPrefix = append(yesPrefix, keyName) yesPrefix = append(yesPrefix, keyName)
...@@ -138,7 +145,11 @@ func TestLocalStorage_ClearWASM(t *testing.T) { ...@@ -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 { for _, keyName := range noPrefix {
if v := jsStorage.getItem(keyName); v.IsNull() { if v := jsStorage.getItem(keyName); v.IsNull() {
......
...@@ -12,6 +12,7 @@ package storage ...@@ -12,6 +12,7 @@ package storage
import ( import (
"github.com/hack-pad/go-indexeddb/idb" "github.com/hack-pad/go-indexeddb/idb"
"github.com/pkg/errors" "github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/v4/storage/utility" "gitlab.com/elixxir/client/v4/storage/utility"
"gitlab.com/elixxir/xxdk-wasm/utils" "gitlab.com/elixxir/xxdk-wasm/utils"
"sync/atomic" "sync/atomic"
...@@ -61,11 +62,11 @@ func Purge(_ js.Value, args []js.Value) any { ...@@ -61,11 +62,11 @@ func Purge(_ js.Value, args []js.Value) any {
} }
// Verify all Cmix followers are stopped // Verify all Cmix followers are stopped
// if n := atomic.LoadUint64(&numClientsRunning); n != 0 { if n := atomic.LoadUint64(&numClientsRunning); n != 0 {
// utils.Throw(utils.TypeError, errors.Errorf( utils.Throw(utils.TypeError, errors.Errorf(
// "%d cMix followers running; all need to be stopped", n)) "%d cMix followers running; all need to be stopped", n))
// return nil return nil
// } }
// Get all indexedDb database names // Get all indexedDb database names
databaseList, err := GetIndexedDbList() databaseList, err := GetIndexedDbList()
...@@ -74,6 +75,8 @@ func Purge(_ js.Value, args []js.Value) any { ...@@ -74,6 +75,8 @@ func Purge(_ js.Value, args []js.Value) any {
"failed to get list of indexedDb database names: %+v", err)) "failed to get list of indexedDb database names: %+v", err))
return nil return nil
} }
jww.DEBUG.Printf("[PURGE] Found %d databases to delete: %s",
len(databaseList), databaseList)
// Delete each database // Delete each database
for dbName := range databaseList { for dbName := range databaseList {
...@@ -89,13 +92,18 @@ func Purge(_ js.Value, args []js.Value) any { ...@@ -89,13 +92,18 @@ func Purge(_ js.Value, args []js.Value) any {
ls := GetLocalStorage() ls := GetLocalStorage()
// Clear all local storage saved by this WASM project // 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 // 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 // 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 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