github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/example_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package fileservice 16 17 import ( 18 "context" 19 "io" 20 "testing" 21 22 "github.com/matrixorigin/matrixone/pkg/fileservice/memorycache" 23 "github.com/matrixorigin/matrixone/pkg/util/toml" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestCacheExample(t *testing.T) { 28 ctx := context.Background() 29 30 dir := t.TempDir() 31 fs, err := NewLocalFS( 32 ctx, 33 "rc", 34 dir, 35 CacheConfig{ 36 MemoryCapacity: ptrTo[toml.ByteSize](32 << 20), 37 }, 38 nil, 39 ) 40 assert.Nil(t, err) 41 42 // write 43 err = fs.Write(ctx, IOVector{ 44 FilePath: "foo", 45 Entries: []IOEntry{ 46 { 47 Data: []byte("42"), 48 Size: 2, 49 }, 50 }, 51 }) 52 assert.Nil(t, err) 53 54 // read 55 vec := &IOVector{ 56 FilePath: "foo", 57 Entries: []IOEntry{ 58 { 59 Offset: 0, 60 Size: 2, 61 ToCacheData: func(_ io.Reader, data []byte, allocator CacheDataAllocator) (memorycache.CacheData, error) { 62 cacheData := allocator.Alloc(len(data)) 63 copy(cacheData.Bytes(), data) 64 return cacheData, nil 65 }, 66 }, 67 }, 68 } 69 err = fs.Read(ctx, vec) 70 assert.Nil(t, err) 71 72 value := vec.Entries[0].CachedData 73 assert.Equal(t, []byte("42"), value.Bytes()) 74 75 vec.Release() 76 fs.FlushCache() 77 }