Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
478 lines
12 KiB
Go
478 lines
12 KiB
Go
package needle_map
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
)
|
|
|
|
func TestSegmentBsearchKey(t *testing.T) {
|
|
testSegment := &CompactMapSegment{
|
|
list: []CompactNeedleValue{
|
|
CompactNeedleValue{key: 10},
|
|
CompactNeedleValue{key: 20},
|
|
CompactNeedleValue{key: 21},
|
|
CompactNeedleValue{key: 26},
|
|
CompactNeedleValue{key: 30},
|
|
},
|
|
firstKey: 10,
|
|
lastKey: 30,
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
cs *CompactMapSegment
|
|
key types.NeedleId
|
|
wantIndex int
|
|
wantFound bool
|
|
}{
|
|
{
|
|
name: "empty segment",
|
|
cs: newCompactMapSegment(0),
|
|
key: 123,
|
|
wantIndex: 0,
|
|
wantFound: false,
|
|
},
|
|
{
|
|
name: "new key, insert at beginning",
|
|
cs: testSegment,
|
|
key: 5,
|
|
wantIndex: 0,
|
|
wantFound: false,
|
|
},
|
|
{
|
|
name: "new key, insert at end",
|
|
cs: testSegment,
|
|
key: 100,
|
|
wantIndex: 5,
|
|
wantFound: false,
|
|
},
|
|
{
|
|
name: "new key, insert second",
|
|
cs: testSegment,
|
|
key: 12,
|
|
wantIndex: 1,
|
|
wantFound: false,
|
|
},
|
|
{
|
|
name: "new key, insert in middle",
|
|
cs: testSegment,
|
|
key: 23,
|
|
wantIndex: 3,
|
|
wantFound: false,
|
|
},
|
|
{
|
|
name: "key #1",
|
|
cs: testSegment,
|
|
key: 10,
|
|
wantIndex: 0,
|
|
wantFound: true,
|
|
},
|
|
{
|
|
name: "key #2",
|
|
cs: testSegment,
|
|
key: 20,
|
|
wantIndex: 1,
|
|
wantFound: true,
|
|
},
|
|
{
|
|
name: "key #3",
|
|
cs: testSegment,
|
|
key: 21,
|
|
wantIndex: 2,
|
|
wantFound: true,
|
|
},
|
|
{
|
|
name: "key #4",
|
|
cs: testSegment,
|
|
key: 26,
|
|
wantIndex: 3,
|
|
wantFound: true,
|
|
},
|
|
{
|
|
name: "key #5",
|
|
cs: testSegment,
|
|
key: 30,
|
|
wantIndex: 4,
|
|
wantFound: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
index, found := tc.cs.bsearchKey(tc.key)
|
|
if got, want := index, tc.wantIndex; got != want {
|
|
t.Errorf("expected %v, got %v", want, got)
|
|
}
|
|
if got, want := found, tc.wantFound; got != want {
|
|
t.Errorf("expected %v, got %v", want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSegmentSet(t *testing.T) {
|
|
testSegment := &CompactMapSegment{
|
|
list: []CompactNeedleValue{
|
|
CompactNeedleValue{key: 10, offset: OffsetToCompact(types.Uint32ToOffset(0)), size: 100},
|
|
CompactNeedleValue{key: 20, offset: OffsetToCompact(types.Uint32ToOffset(100)), size: 200},
|
|
CompactNeedleValue{key: 30, offset: OffsetToCompact(types.Uint32ToOffset(300)), size: 300},
|
|
},
|
|
firstKey: 10,
|
|
lastKey: 30,
|
|
}
|
|
|
|
if got, want := testSegment.len(), 3; got != want {
|
|
t.Errorf("got starting size %d, want %d", got, want)
|
|
}
|
|
if got, want := testSegment.cap(), 3; got != want {
|
|
t.Errorf("got starting capacity %d, want %d", got, want)
|
|
}
|
|
|
|
testSets := []struct {
|
|
name string
|
|
key types.NeedleId
|
|
offset types.Offset
|
|
size types.Size
|
|
wantOffset types.Offset
|
|
wantSize types.Size
|
|
}{
|
|
{
|
|
name: "insert at beginning",
|
|
key: 5, offset: types.Uint32ToOffset(1000), size: 123,
|
|
wantOffset: types.Uint32ToOffset(0), wantSize: 0,
|
|
},
|
|
{
|
|
name: "insert at end",
|
|
key: 51, offset: types.Uint32ToOffset(7000), size: 456,
|
|
wantOffset: types.Uint32ToOffset(0), wantSize: 0,
|
|
},
|
|
{
|
|
name: "insert in middle",
|
|
key: 25, offset: types.Uint32ToOffset(8000), size: 789,
|
|
wantOffset: types.Uint32ToOffset(0), wantSize: 0,
|
|
},
|
|
{
|
|
name: "update existing",
|
|
key: 30, offset: types.Uint32ToOffset(9000), size: 999,
|
|
wantOffset: types.Uint32ToOffset(300), wantSize: 300,
|
|
},
|
|
}
|
|
|
|
for _, ts := range testSets {
|
|
offset, size := testSegment.set(ts.key, ts.offset, ts.size)
|
|
if offset != ts.wantOffset {
|
|
t.Errorf("%s: got offset %v, want %v", ts.name, offset, ts.wantOffset)
|
|
}
|
|
if size != ts.wantSize {
|
|
t.Errorf("%s: got size %v, want %v", ts.name, size, ts.wantSize)
|
|
}
|
|
}
|
|
|
|
wantSegment := &CompactMapSegment{
|
|
list: []CompactNeedleValue{
|
|
CompactNeedleValue{key: 5, offset: OffsetToCompact(types.Uint32ToOffset(1000)), size: 123},
|
|
CompactNeedleValue{key: 10, offset: OffsetToCompact(types.Uint32ToOffset(0)), size: 100},
|
|
CompactNeedleValue{key: 20, offset: OffsetToCompact(types.Uint32ToOffset(100)), size: 200},
|
|
CompactNeedleValue{key: 25, offset: OffsetToCompact(types.Uint32ToOffset(8000)), size: 789},
|
|
CompactNeedleValue{key: 30, offset: OffsetToCompact(types.Uint32ToOffset(9000)), size: 999},
|
|
CompactNeedleValue{key: 51, offset: OffsetToCompact(types.Uint32ToOffset(7000)), size: 456},
|
|
},
|
|
firstKey: 5,
|
|
lastKey: 51,
|
|
}
|
|
if !reflect.DeepEqual(testSegment, wantSegment) {
|
|
t.Errorf("got result segment %v, want %v", testSegment, wantSegment)
|
|
}
|
|
|
|
if got, want := testSegment.len(), 6; got != want {
|
|
t.Errorf("got result size %d, want %d", got, want)
|
|
}
|
|
if got, want := testSegment.cap(), 6; got != want {
|
|
t.Errorf("got result capacity %d, want %d", got, want)
|
|
}
|
|
}
|
|
|
|
func TestSegmentSetOrdering(t *testing.T) {
|
|
keys := []types.NeedleId{}
|
|
for i := 0; i < SegmentChunkSize; i++ {
|
|
keys = append(keys, types.NeedleId(i))
|
|
}
|
|
|
|
r := rand.New(rand.NewSource(123456789))
|
|
r.Shuffle(len(keys), func(i, j int) { keys[i], keys[j] = keys[j], keys[i] })
|
|
|
|
cs := newCompactMapSegment(0)
|
|
for _, k := range keys {
|
|
_, _ = cs.set(k, types.Uint32ToOffset(123), 456)
|
|
}
|
|
if got, want := cs.len(), SegmentChunkSize; got != want {
|
|
t.Errorf("expected size %d, got %d", want, got)
|
|
}
|
|
for i := 1; i < cs.len(); i++ {
|
|
if ka, kb := cs.list[i-1].key, cs.list[i].key; ka >= kb {
|
|
t.Errorf("found out of order entries at (%d, %d) = (%d, %d)", i-1, i, ka, kb)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSegmentGet(t *testing.T) {
|
|
testSegment := &CompactMapSegment{
|
|
list: []CompactNeedleValue{
|
|
CompactNeedleValue{key: 10, offset: OffsetToCompact(types.Uint32ToOffset(0)), size: 100},
|
|
CompactNeedleValue{key: 20, offset: OffsetToCompact(types.Uint32ToOffset(100)), size: 200},
|
|
CompactNeedleValue{key: 30, offset: OffsetToCompact(types.Uint32ToOffset(300)), size: 300},
|
|
},
|
|
firstKey: 10,
|
|
lastKey: 30,
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
key types.NeedleId
|
|
wantValue *CompactNeedleValue
|
|
wantFound bool
|
|
}{
|
|
{
|
|
name: "invalid key",
|
|
key: 99,
|
|
wantValue: nil,
|
|
wantFound: false,
|
|
},
|
|
{
|
|
name: "key #1",
|
|
key: 10,
|
|
wantValue: &testSegment.list[0],
|
|
wantFound: true,
|
|
},
|
|
{
|
|
name: "key #2",
|
|
key: 20,
|
|
wantValue: &testSegment.list[1],
|
|
wantFound: true,
|
|
},
|
|
{
|
|
name: "key #3",
|
|
key: 30,
|
|
wantValue: &testSegment.list[2],
|
|
wantFound: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
value, found := testSegment.get(tc.key)
|
|
if got, want := value, tc.wantValue; got != want {
|
|
t.Errorf("got %v, want %v", got, want)
|
|
}
|
|
if got, want := found, tc.wantFound; got != want {
|
|
t.Errorf("got %v, want %v", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSegmentDelete(t *testing.T) {
|
|
testSegment := &CompactMapSegment{
|
|
list: []CompactNeedleValue{
|
|
CompactNeedleValue{key: 10, offset: OffsetToCompact(types.Uint32ToOffset(0)), size: 100},
|
|
CompactNeedleValue{key: 20, offset: OffsetToCompact(types.Uint32ToOffset(100)), size: 200},
|
|
CompactNeedleValue{key: 30, offset: OffsetToCompact(types.Uint32ToOffset(300)), size: 300},
|
|
CompactNeedleValue{key: 40, offset: OffsetToCompact(types.Uint32ToOffset(600)), size: 400},
|
|
},
|
|
firstKey: 10,
|
|
lastKey: 40,
|
|
}
|
|
|
|
testDeletes := []struct {
|
|
name string
|
|
key types.NeedleId
|
|
want types.Size
|
|
}{
|
|
{
|
|
name: "invalid key",
|
|
key: 99,
|
|
want: 0,
|
|
},
|
|
{
|
|
name: "delete key #2",
|
|
key: 20,
|
|
want: 200,
|
|
},
|
|
{
|
|
name: "delete key #4",
|
|
key: 40,
|
|
want: 400,
|
|
},
|
|
}
|
|
|
|
for _, td := range testDeletes {
|
|
size := testSegment.delete(td.key)
|
|
if got, want := size, td.want; got != want {
|
|
t.Errorf("%s: got %v, want %v", td.name, got, want)
|
|
}
|
|
}
|
|
|
|
wantSegment := &CompactMapSegment{
|
|
list: []CompactNeedleValue{
|
|
CompactNeedleValue{key: 10, offset: OffsetToCompact(types.Uint32ToOffset(0)), size: 100},
|
|
CompactNeedleValue{key: 20, offset: OffsetToCompact(types.Uint32ToOffset(100)), size: -200},
|
|
CompactNeedleValue{key: 30, offset: OffsetToCompact(types.Uint32ToOffset(300)), size: 300},
|
|
CompactNeedleValue{key: 40, offset: OffsetToCompact(types.Uint32ToOffset(600)), size: -400},
|
|
},
|
|
firstKey: 10,
|
|
lastKey: 40,
|
|
}
|
|
if !reflect.DeepEqual(testSegment, wantSegment) {
|
|
t.Errorf("got result segment %v, want %v", testSegment, wantSegment)
|
|
}
|
|
}
|
|
|
|
func TestSegmentForKey(t *testing.T) {
|
|
testMap := NewCompactMap()
|
|
|
|
tests := []struct {
|
|
name string
|
|
key types.NeedleId
|
|
want *CompactMapSegment
|
|
}{
|
|
{
|
|
name: "first segment",
|
|
key: 12,
|
|
want: &CompactMapSegment{
|
|
list: []CompactNeedleValue{},
|
|
chunk: 0,
|
|
firstKey: MaxCompactKey,
|
|
lastKey: 0,
|
|
},
|
|
},
|
|
{
|
|
name: "second segment, gapless",
|
|
key: SegmentChunkSize + 34,
|
|
want: &CompactMapSegment{
|
|
list: []CompactNeedleValue{},
|
|
chunk: 1,
|
|
firstKey: MaxCompactKey,
|
|
lastKey: 0,
|
|
},
|
|
},
|
|
{
|
|
name: "gapped segment",
|
|
key: (5 * SegmentChunkSize) + 56,
|
|
want: &CompactMapSegment{
|
|
list: []CompactNeedleValue{},
|
|
chunk: 5,
|
|
firstKey: MaxCompactKey,
|
|
lastKey: 0,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cs := testMap.segmentForKey(tc.key)
|
|
if !reflect.DeepEqual(cs, tc.want) {
|
|
t.Errorf("got segment %v, want %v", cs, tc.want)
|
|
}
|
|
})
|
|
}
|
|
|
|
wantMap := &CompactMap{
|
|
segments: map[Chunk]*CompactMapSegment{
|
|
0: &CompactMapSegment{
|
|
list: []CompactNeedleValue{},
|
|
chunk: 0,
|
|
firstKey: MaxCompactKey,
|
|
lastKey: 0,
|
|
},
|
|
1: &CompactMapSegment{
|
|
list: []CompactNeedleValue{},
|
|
chunk: 1,
|
|
firstKey: MaxCompactKey,
|
|
lastKey: 0,
|
|
},
|
|
5: &CompactMapSegment{
|
|
list: []CompactNeedleValue{},
|
|
chunk: 5,
|
|
firstKey: MaxCompactKey,
|
|
lastKey: 0,
|
|
},
|
|
},
|
|
}
|
|
if !reflect.DeepEqual(testMap, wantMap) {
|
|
t.Errorf("got map %v, want %v", testMap, wantMap)
|
|
}
|
|
}
|
|
|
|
func TestAscendingVisit(t *testing.T) {
|
|
cm := NewCompactMap()
|
|
for _, nid := range []types.NeedleId{20, 7, 40000, 300000, 0, 100, 500, 10000, 200000} {
|
|
cm.Set(nid, types.Uint32ToOffset(123), 456)
|
|
}
|
|
|
|
got := []NeedleValue{}
|
|
err := cm.AscendingVisit(func(nv NeedleValue) error {
|
|
got = append(got, nv)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Errorf("got error %v, expected none", err)
|
|
}
|
|
|
|
want := []NeedleValue{
|
|
NeedleValue{Key: 0, Offset: types.Uint32ToOffset(123), Size: 456},
|
|
NeedleValue{Key: 7, Offset: types.Uint32ToOffset(123), Size: 456},
|
|
NeedleValue{Key: 20, Offset: types.Uint32ToOffset(123), Size: 456},
|
|
NeedleValue{Key: 100, Offset: types.Uint32ToOffset(123), Size: 456},
|
|
NeedleValue{Key: 500, Offset: types.Uint32ToOffset(123), Size: 456},
|
|
NeedleValue{Key: 10000, Offset: types.Uint32ToOffset(123), Size: 456},
|
|
NeedleValue{Key: 40000, Offset: types.Uint32ToOffset(123), Size: 456},
|
|
NeedleValue{Key: 200000, Offset: types.Uint32ToOffset(123), Size: 456},
|
|
NeedleValue{Key: 300000, Offset: types.Uint32ToOffset(123), Size: 456},
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("got values %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestRandomInsert(t *testing.T) {
|
|
count := 8 * SegmentChunkSize
|
|
keys := []types.NeedleId{}
|
|
for i := 0; i < count; i++ {
|
|
keys = append(keys, types.NeedleId(i))
|
|
}
|
|
|
|
r := rand.New(rand.NewSource(123456789))
|
|
r.Shuffle(len(keys), func(i, j int) { keys[i], keys[j] = keys[j], keys[i] })
|
|
|
|
cm := NewCompactMap()
|
|
for _, k := range keys {
|
|
_, _ = cm.Set(k, types.Uint32ToOffset(123), 456)
|
|
}
|
|
if got, want := cm.Len(), count; got != want {
|
|
t.Errorf("expected size %d, got %d", want, got)
|
|
}
|
|
|
|
last := -1
|
|
err := cm.AscendingVisit(func(nv NeedleValue) error {
|
|
key := int(nv.Key)
|
|
if key <= last {
|
|
return fmt.Errorf("found out of order entries (%d vs %d)", key, last)
|
|
}
|
|
last = key
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Errorf("got error %v, expected none", err)
|
|
}
|
|
|
|
// Given that we've written a integer multiple of SegmentChunkSize, all
|
|
// segments should be fully utilized and capacity-adjusted.
|
|
if l, c := cm.Len(), cm.Cap(); l != c {
|
|
t.Errorf("map length (%d) doesn't match capacity (%d)", l, c)
|
|
}
|
|
}
|