1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
package foundationdb
import (
"context"
"sort"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// MockFoundationDBStore provides a simple mock implementation for testing
type MockFoundationDBStore struct {
data map[string][]byte
kvStore map[string][]byte
inTransaction bool
}
func NewMockFoundationDBStore() *MockFoundationDBStore {
return &MockFoundationDBStore{
data: make(map[string][]byte),
kvStore: make(map[string][]byte),
}
}
func (store *MockFoundationDBStore) GetName() string {
return "foundationdb_mock"
}
func (store *MockFoundationDBStore) Initialize(configuration util.Configuration, prefix string) error {
return nil
}
func (store *MockFoundationDBStore) BeginTransaction(ctx context.Context) (context.Context, error) {
store.inTransaction = true
return ctx, nil
}
func (store *MockFoundationDBStore) CommitTransaction(ctx context.Context) error {
store.inTransaction = false
return nil
}
func (store *MockFoundationDBStore) RollbackTransaction(ctx context.Context) error {
store.inTransaction = false
return nil
}
func (store *MockFoundationDBStore) InsertEntry(ctx context.Context, entry *filer.Entry) error {
return store.UpdateEntry(ctx, entry)
}
func (store *MockFoundationDBStore) UpdateEntry(ctx context.Context, entry *filer.Entry) error {
key := string(entry.FullPath)
value, err := entry.EncodeAttributesAndChunks()
if err != nil {
return err
}
store.data[key] = value
return nil
}
func (store *MockFoundationDBStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
key := string(fullpath)
data, exists := store.data[key]
if !exists {
return nil, filer_pb.ErrNotFound
}
entry = &filer.Entry{
FullPath: fullpath,
}
err = entry.DecodeAttributesAndChunks(data)
return entry, err
}
func (store *MockFoundationDBStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
key := string(fullpath)
delete(store.data, key)
return nil
}
func (store *MockFoundationDBStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
prefix := string(fullpath)
if !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
for key := range store.data {
if strings.HasPrefix(key, prefix) {
delete(store.data, key)
}
}
return nil
}
func (store *MockFoundationDBStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
return store.ListDirectoryPrefixedEntries(ctx, dirPath, startFileName, includeStartFile, limit, "", eachEntryFunc)
}
func (store *MockFoundationDBStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
dirPrefix := string(dirPath)
if !strings.HasSuffix(dirPrefix, "/") {
dirPrefix += "/"
}
var entries []string
for key := range store.data {
if strings.HasPrefix(key, dirPrefix) {
relativePath := strings.TrimPrefix(key, dirPrefix)
// Only direct children (no subdirectories)
if !strings.Contains(relativePath, "/") && strings.HasPrefix(relativePath, prefix) {
entries = append(entries, key)
}
}
}
// Sort entries for consistent ordering
sort.Strings(entries)
// Apply startFileName filter
startIndex := 0
if startFileName != "" {
for i, entryPath := range entries {
fileName := strings.TrimPrefix(entryPath, dirPrefix)
if fileName == startFileName {
if includeStartFile {
startIndex = i
} else {
startIndex = i + 1
}
break
} else if fileName > startFileName {
startIndex = i
break
}
}
}
// Iterate through sorted entries with limit
count := int64(0)
for i := startIndex; i < len(entries) && count < limit; i++ {
entryPath := entries[i]
data := store.data[entryPath]
entry := &filer.Entry{
FullPath: util.FullPath(entryPath),
}
if err := entry.DecodeAttributesAndChunks(data); err != nil {
continue
}
if !eachEntryFunc(entry) {
break
}
lastFileName = entry.Name()
count++
}
return lastFileName, nil
}
func (store *MockFoundationDBStore) KvPut(ctx context.Context, key []byte, value []byte) error {
store.kvStore[string(key)] = value
return nil
}
func (store *MockFoundationDBStore) KvGet(ctx context.Context, key []byte) ([]byte, error) {
value, exists := store.kvStore[string(key)]
if !exists {
return nil, filer.ErrKvNotFound
}
return value, nil
}
func (store *MockFoundationDBStore) KvDelete(ctx context.Context, key []byte) error {
delete(store.kvStore, string(key))
return nil
}
func (store *MockFoundationDBStore) Shutdown() {
// Nothing to do for mock
}
// TestMockFoundationDBStore_BasicOperations tests basic store operations with mock
func TestMockFoundationDBStore_BasicOperations(t *testing.T) {
store := NewMockFoundationDBStore()
defer store.Shutdown()
ctx := context.Background()
// Test InsertEntry
entry := &filer.Entry{
FullPath: "/test/file1.txt",
Attr: filer.Attr{
Mode: 0644,
Uid: 1000,
Gid: 1000,
Mtime: time.Now(),
},
}
err := store.InsertEntry(ctx, entry)
if err != nil {
t.Fatalf("InsertEntry failed: %v", err)
}
t.Log("✅ InsertEntry successful")
// Test FindEntry
foundEntry, err := store.FindEntry(ctx, "/test/file1.txt")
if err != nil {
t.Fatalf("FindEntry failed: %v", err)
}
if foundEntry.FullPath != entry.FullPath {
t.Errorf("Expected path %s, got %s", entry.FullPath, foundEntry.FullPath)
}
t.Log("✅ FindEntry successful")
// Test UpdateEntry
foundEntry.Attr.Mode = 0755
err = store.UpdateEntry(ctx, foundEntry)
if err != nil {
t.Fatalf("UpdateEntry failed: %v", err)
}
t.Log("✅ UpdateEntry successful")
// Test DeleteEntry
err = store.DeleteEntry(ctx, "/test/file1.txt")
if err != nil {
t.Fatalf("DeleteEntry failed: %v", err)
}
t.Log("✅ DeleteEntry successful")
// Test entry is deleted
_, err = store.FindEntry(ctx, "/test/file1.txt")
if err == nil {
t.Error("Expected entry to be deleted, but it was found")
}
if err != filer_pb.ErrNotFound {
t.Errorf("Expected ErrNotFound, got %v", err)
}
t.Log("✅ Entry deletion verified")
}
// TestMockFoundationDBStore_TransactionOperations tests transaction handling
func TestMockFoundationDBStore_TransactionOperations(t *testing.T) {
store := NewMockFoundationDBStore()
defer store.Shutdown()
ctx := context.Background()
// Test transaction workflow
txCtx, err := store.BeginTransaction(ctx)
if err != nil {
t.Fatalf("BeginTransaction failed: %v", err)
}
t.Log("✅ BeginTransaction successful")
if !store.inTransaction {
t.Error("Expected to be in transaction")
}
// Insert entry in transaction
entry := &filer.Entry{
FullPath: "/test/tx_file.txt",
Attr: filer.Attr{
Mode: 0644,
Uid: 1000,
Gid: 1000,
Mtime: time.Now(),
},
}
err = store.InsertEntry(txCtx, entry)
if err != nil {
t.Fatalf("InsertEntry in transaction failed: %v", err)
}
t.Log("✅ InsertEntry in transaction successful")
// Commit transaction
err = store.CommitTransaction(txCtx)
if err != nil {
t.Fatalf("CommitTransaction failed: %v", err)
}
t.Log("✅ CommitTransaction successful")
if store.inTransaction {
t.Error("Expected to not be in transaction after commit")
}
// Test rollback
txCtx2, err := store.BeginTransaction(ctx)
if err != nil {
t.Fatalf("BeginTransaction for rollback test failed: %v", err)
}
err = store.RollbackTransaction(txCtx2)
if err != nil {
t.Fatalf("RollbackTransaction failed: %v", err)
}
t.Log("✅ RollbackTransaction successful")
if store.inTransaction {
t.Error("Expected to not be in transaction after rollback")
}
}
// TestMockFoundationDBStore_KVOperations tests key-value operations
func TestMockFoundationDBStore_KVOperations(t *testing.T) {
store := NewMockFoundationDBStore()
defer store.Shutdown()
ctx := context.Background()
// Test KvPut
key := []byte("test_key")
value := []byte("test_value")
err := store.KvPut(ctx, key, value)
if err != nil {
t.Fatalf("KvPut failed: %v", err)
}
t.Log("✅ KvPut successful")
// Test KvGet
retrievedValue, err := store.KvGet(ctx, key)
if err != nil {
t.Fatalf("KvGet failed: %v", err)
}
if string(retrievedValue) != string(value) {
t.Errorf("Expected value %s, got %s", value, retrievedValue)
}
t.Log("✅ KvGet successful")
// Test KvDelete
err = store.KvDelete(ctx, key)
if err != nil {
t.Fatalf("KvDelete failed: %v", err)
}
t.Log("✅ KvDelete successful")
// Verify key is deleted
_, err = store.KvGet(ctx, key)
if err == nil {
t.Error("Expected key to be deleted")
}
if err != filer.ErrKvNotFound {
t.Errorf("Expected ErrKvNotFound, got %v", err)
}
t.Log("✅ Key deletion verified")
}
// TestMockFoundationDBStore_DirectoryOperations tests directory operations
func TestMockFoundationDBStore_DirectoryOperations(t *testing.T) {
store := NewMockFoundationDBStore()
defer store.Shutdown()
ctx := context.Background()
// Create multiple entries in a directory
testDir := util.FullPath("/test/dir/")
files := []string{"file1.txt", "file2.txt", "file3.txt"}
for _, fileName := range files {
entry := &filer.Entry{
FullPath: util.NewFullPath(string(testDir), fileName),
Attr: filer.Attr{
Mode: 0644,
Uid: 1000,
Gid: 1000,
Mtime: time.Now(),
},
}
err := store.InsertEntry(ctx, entry)
if err != nil {
t.Fatalf("InsertEntry failed for %s: %v", fileName, err)
}
}
t.Log("✅ Directory entries created")
// Test ListDirectoryEntries
var listedFiles []string
lastFileName, err := store.ListDirectoryEntries(ctx, testDir, "", true, 100, func(entry *filer.Entry) bool {
listedFiles = append(listedFiles, entry.Name())
return true
})
if err != nil {
t.Fatalf("ListDirectoryEntries failed: %v", err)
}
t.Logf("✅ ListDirectoryEntries successful, last file: %s", lastFileName)
t.Logf("Listed files: %v", listedFiles)
// Test DeleteFolderChildren
err = store.DeleteFolderChildren(ctx, testDir)
if err != nil {
t.Fatalf("DeleteFolderChildren failed: %v", err)
}
t.Log("✅ DeleteFolderChildren successful")
// Verify children are deleted
var remainingFiles []string
_, err = store.ListDirectoryEntries(ctx, testDir, "", true, 100, func(entry *filer.Entry) bool {
remainingFiles = append(remainingFiles, entry.Name())
return true
})
if err != nil {
t.Fatalf("ListDirectoryEntries after delete failed: %v", err)
}
if len(remainingFiles) != 0 {
t.Errorf("Expected no files after DeleteFolderChildren, got %d: %v", len(remainingFiles), remainingFiles)
}
t.Log("✅ Folder children deletion verified")
}
|