github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/indexer/index/autoincrement_test.go (about) 1 // Copyright 2018-2022 CERN 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 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package index_test 20 21 import ( 22 "os" 23 "path/filepath" 24 "testing" 25 26 "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/index" 27 "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/option" 28 metadata "github.com/cs3org/reva/v2/pkg/storage/utils/metadata" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestNext(t *testing.T) { 33 scenarios := []struct { 34 name string 35 expected int 36 indexBy option.IndexBy 37 }{ 38 { 39 name: "get next value", 40 expected: 0, 41 indexBy: option.IndexByField("Number"), 42 }, 43 } 44 45 for _, scenario := range scenarios { 46 t.Run(scenario.name, func(t *testing.T) { 47 tmpDir, err := createTmpDirStr() 48 assert.NoError(t, err) 49 dataDir := filepath.Join(tmpDir, "data") 50 51 err = os.MkdirAll(dataDir, 0777) 52 assert.NoError(t, err) 53 54 storage, err := metadata.NewDiskStorage(dataDir) 55 assert.NoError(t, err) 56 57 i := index.NewAutoincrementIndex( 58 storage, 59 option.WithBounds(&option.Bound{ 60 Lower: 0, 61 Upper: 0, 62 }), 63 option.WithFilesDir(dataDir), 64 option.WithTypeName("LambdaType"), 65 option.WithIndexBy(scenario.indexBy), 66 ) 67 68 err = i.Init() 69 assert.NoError(t, err) 70 71 tmpFile, err := os.Create(filepath.Join(tmpDir, "data", "test-example")) 72 assert.NoError(t, err) 73 assert.NoError(t, tmpFile.Close()) 74 75 oldName, err := i.Add("test-example", "") 76 assert.NoError(t, err) 77 assert.Equal(t, "0", filepath.Base(oldName)) 78 79 oldName, err = i.Add("test-example", "") 80 assert.NoError(t, err) 81 assert.Equal(t, "1", filepath.Base(oldName)) 82 83 oldName, err = i.Add("test-example", "") 84 assert.NoError(t, err) 85 assert.Equal(t, "2", filepath.Base(oldName)) 86 t.Log(oldName) 87 88 _ = os.RemoveAll(tmpDir) 89 }) 90 } 91 } 92 93 func TestLowerBound(t *testing.T) { 94 scenarios := []struct { 95 name string 96 expected int 97 indexBy option.IndexBy 98 entity interface{} 99 }{ 100 { 101 name: "get next value with a lower bound specified", 102 expected: 0, 103 indexBy: option.IndexByField("Number"), 104 }, 105 } 106 107 for _, scenario := range scenarios { 108 t.Run(scenario.name, func(t *testing.T) { 109 tmpDir, err := createTmpDirStr() 110 assert.NoError(t, err) 111 dataDir := filepath.Join(tmpDir, "data") 112 113 err = os.MkdirAll(dataDir, 0777) 114 assert.NoError(t, err) 115 116 storage, err := metadata.NewDiskStorage(dataDir) 117 assert.NoError(t, err) 118 119 i := index.NewAutoincrementIndex( 120 storage, 121 option.WithBounds(&option.Bound{ 122 Lower: 1000, 123 }), 124 option.WithFilesDir(dataDir), 125 option.WithTypeName("LambdaType"), 126 option.WithIndexBy(scenario.indexBy), 127 ) 128 129 err = i.Init() 130 assert.NoError(t, err) 131 132 tmpFile, err := os.Create(filepath.Join(tmpDir, "data", "test-example")) 133 assert.NoError(t, err) 134 assert.NoError(t, tmpFile.Close()) 135 136 oldName, err := i.Add("test-example", "") 137 assert.NoError(t, err) 138 assert.Equal(t, "1000", filepath.Base(oldName)) 139 140 oldName, err = i.Add("test-example", "") 141 assert.NoError(t, err) 142 assert.Equal(t, "1001", filepath.Base(oldName)) 143 144 oldName, err = i.Add("test-example", "") 145 assert.NoError(t, err) 146 assert.Equal(t, "1002", filepath.Base(oldName)) 147 t.Log(oldName) 148 149 _ = os.RemoveAll(tmpDir) 150 }) 151 } 152 } 153 154 func TestAdd(t *testing.T) { 155 tmpDir, err := createTmpDirStr() 156 assert.NoError(t, err) 157 dataDir := filepath.Join(tmpDir, "data") 158 159 err = os.MkdirAll(dataDir, 0777) 160 assert.NoError(t, err) 161 162 storage, err := metadata.NewDiskStorage(dataDir) 163 assert.NoError(t, err) 164 165 tmpFile, err := os.Create(filepath.Join(tmpDir, "data", "test-example")) 166 assert.NoError(t, err) 167 assert.NoError(t, tmpFile.Close()) 168 169 i := index.NewAutoincrementIndex( 170 storage, 171 option.WithBounds(&option.Bound{ 172 Lower: 0, 173 Upper: 0, 174 }), 175 option.WithFilesDir(filepath.Join(tmpDir, "data")), 176 option.WithTypeName("owncloud.Account"), 177 option.WithIndexBy(option.IndexByField("UidNumber")), 178 ) 179 180 err = i.Init() 181 assert.NoError(t, err) 182 183 _, err = i.Add("test-example", "") 184 if err != nil { 185 t.Error(err) 186 } 187 } 188 189 func BenchmarkAdd(b *testing.B) { 190 tmpDir, err := createTmpDirStr() 191 assert.NoError(b, err) 192 dataDir := filepath.Join(tmpDir, "data") 193 194 err = os.MkdirAll(dataDir, 0777) 195 assert.NoError(b, err) 196 197 storage, err := metadata.NewDiskStorage(dataDir) 198 assert.NoError(b, err) 199 200 tmpFile, err := os.Create(filepath.Join(tmpDir, "data", "test-example")) 201 assert.NoError(b, err) 202 assert.NoError(b, tmpFile.Close()) 203 204 i := index.NewAutoincrementIndex( 205 storage, 206 option.WithBounds(&option.Bound{ 207 Lower: 0, 208 Upper: 0, 209 }), 210 option.WithFilesDir(filepath.Join(tmpDir, "data")), 211 option.WithTypeName("LambdaType"), 212 option.WithIndexBy(option.IndexByField("Number")), 213 ) 214 215 err = i.Init() 216 assert.NoError(b, err) 217 218 for n := 0; n < b.N; n++ { 219 _, err := i.Add("test-example", "") 220 if err != nil { 221 b.Error(err) 222 } 223 assert.NoError(b, err) 224 } 225 } 226 227 func createTmpDirStr() (string, error) { 228 name, err := os.MkdirTemp("/tmp", "testfiles-*") 229 if err != nil { 230 return "", err 231 } 232 233 return name, nil 234 }