2015-01-17 19:53:33 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-01-17 19:53:33 +00:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNamespacedInt(t *testing.T) {
|
2020-12-21 11:59:22 +00:00
|
|
|
ldb := newLowlevelMemory(t)
|
2020-02-11 13:31:43 +00:00
|
|
|
defer ldb.Close()
|
2015-01-17 19:53:33 +00:00
|
|
|
|
|
|
|
n1 := NewNamespacedKV(ldb, "foo")
|
|
|
|
n2 := NewNamespacedKV(ldb, "bar")
|
|
|
|
|
|
|
|
// Key is missing to start with
|
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.Int64("test"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != 0 || ok {
|
2015-01-17 19:53:33 +00:00
|
|
|
t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:11:52 +00:00
|
|
|
if err := n1.PutInt64("test", 42); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-01-17 19:53:33 +00:00
|
|
|
|
|
|
|
// It should now exist in n1
|
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.Int64("test"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != 42 || !ok {
|
2015-01-17 19:53:33 +00:00
|
|
|
t.Errorf("Incorrect return v %v != 42 || ok %v != true", v, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... but not in n2, which is in a different namespace
|
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n2.Int64("test"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != 0 || ok {
|
2015-01-17 19:53:33 +00:00
|
|
|
t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:11:52 +00:00
|
|
|
if err := n1.Delete("test"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-01-17 19:53:33 +00:00
|
|
|
|
|
|
|
// It should no longer exist
|
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.Int64("test"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != 0 || ok {
|
2015-01-17 19:53:33 +00:00
|
|
|
t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNamespacedTime(t *testing.T) {
|
2020-12-21 11:59:22 +00:00
|
|
|
ldb := newLowlevelMemory(t)
|
2020-02-11 13:31:43 +00:00
|
|
|
defer ldb.Close()
|
2015-01-17 19:53:33 +00:00
|
|
|
|
|
|
|
n1 := NewNamespacedKV(ldb, "foo")
|
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.Time("test"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if !v.IsZero() || ok {
|
2015-01-17 19:53:33 +00:00
|
|
|
t.Errorf("Incorrect return v %v != %v || ok %v != false", v, time.Time{}, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
2019-11-29 08:11:52 +00:00
|
|
|
if err := n1.PutTime("test", now); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-01-17 19:53:33 +00:00
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.Time("test"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if !v.Equal(now) || !ok {
|
2015-01-17 19:53:33 +00:00
|
|
|
t.Errorf("Incorrect return v %v != %v || ok %v != true", v, now, ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNamespacedString(t *testing.T) {
|
2020-12-21 11:59:22 +00:00
|
|
|
ldb := newLowlevelMemory(t)
|
2020-02-11 13:31:43 +00:00
|
|
|
defer ldb.Close()
|
2015-01-17 19:53:33 +00:00
|
|
|
|
|
|
|
n1 := NewNamespacedKV(ldb, "foo")
|
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.String("test"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != "" || ok {
|
2015-01-17 19:53:33 +00:00
|
|
|
t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:11:52 +00:00
|
|
|
if err := n1.PutString("test", "yo"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-01-17 19:53:33 +00:00
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.String("test"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != "yo" || !ok {
|
2015-01-17 19:53:33 +00:00
|
|
|
t.Errorf("Incorrect return v %q != \"yo\" || ok %v != true", v, ok)
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 15:37:47 +00:00
|
|
|
|
|
|
|
func TestNamespacedReset(t *testing.T) {
|
2020-12-21 11:59:22 +00:00
|
|
|
ldb := newLowlevelMemory(t)
|
2020-02-11 13:31:43 +00:00
|
|
|
defer ldb.Close()
|
2015-05-12 15:37:47 +00:00
|
|
|
|
|
|
|
n1 := NewNamespacedKV(ldb, "foo")
|
|
|
|
|
2019-11-29 08:11:52 +00:00
|
|
|
if err := n1.PutString("test1", "yo1"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := n1.PutString("test2", "yo2"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := n1.PutString("test3", "yo3"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-12 15:37:47 +00:00
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.String("test1"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != "yo1" || !ok {
|
2015-05-12 15:37:47 +00:00
|
|
|
t.Errorf("Incorrect return v %q != \"yo1\" || ok %v != true", v, ok)
|
|
|
|
}
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.String("test2"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != "yo2" || !ok {
|
2015-05-12 15:37:47 +00:00
|
|
|
t.Errorf("Incorrect return v %q != \"yo2\" || ok %v != true", v, ok)
|
|
|
|
}
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.String("test3"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != "yo3" || !ok {
|
2015-05-12 15:37:47 +00:00
|
|
|
t.Errorf("Incorrect return v %q != \"yo3\" || ok %v != true", v, ok)
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:11:52 +00:00
|
|
|
reset(n1)
|
2015-05-12 15:37:47 +00:00
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.String("test1"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != "" || ok {
|
2015-05-12 15:37:47 +00:00
|
|
|
t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
|
|
|
|
}
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.String("test2"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != "" || ok {
|
2015-05-12 15:37:47 +00:00
|
|
|
t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
|
|
|
|
}
|
2019-11-30 12:03:24 +00:00
|
|
|
if v, ok, err := n1.String("test3"); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
} else if v != "" || ok {
|
2015-05-12 15:37:47 +00:00
|
|
|
t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 08:11:52 +00:00
|
|
|
|
|
|
|
// reset removes all entries in this namespace.
|
|
|
|
func reset(n *NamespacedKV) {
|
|
|
|
tr, err := n.db.NewWriteTransaction()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer tr.Release()
|
|
|
|
|
2020-03-31 12:32:24 +00:00
|
|
|
it, err := tr.NewPrefixIterator([]byte(n.prefix))
|
2019-11-29 08:11:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for it.Next() {
|
|
|
|
_ = tr.Delete(it.Key())
|
|
|
|
}
|
|
|
|
it.Release()
|
|
|
|
_ = tr.Commit()
|
|
|
|
}
|