github.com/ledgerwatch/erigon-lib@v1.0.0/kv/mdbx/kv_mdbx_temporary.go (about) 1 /* 2 Copyright 2021 Erigon contributors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mdbx 18 19 import ( 20 "context" 21 "os" 22 23 "github.com/ledgerwatch/erigon-lib/kv" 24 "github.com/ledgerwatch/log/v3" 25 ) 26 27 type TemporaryMdbx struct { 28 db kv.RwDB 29 path string 30 } 31 32 func NewTemporaryMdbx(tempdir string) (kv.RwDB, error) { 33 path, err := os.MkdirTemp(tempdir, "mdbx-temp") 34 if err != nil { 35 return &TemporaryMdbx{}, err 36 } 37 38 db, err := Open(path, log.Root(), false) 39 if err != nil { 40 return &TemporaryMdbx{}, err 41 } 42 43 return &TemporaryMdbx{ 44 db: db, 45 path: path, 46 }, nil 47 } 48 49 func (t *TemporaryMdbx) ReadOnly() bool { return t.db.ReadOnly() } 50 func (t *TemporaryMdbx) Update(ctx context.Context, f func(kv.RwTx) error) error { 51 return t.db.Update(ctx, f) 52 } 53 54 func (t *TemporaryMdbx) UpdateNosync(ctx context.Context, f func(kv.RwTx) error) error { 55 return t.db.UpdateNosync(ctx, f) 56 } 57 58 func (t *TemporaryMdbx) BeginRw(ctx context.Context) (kv.RwTx, error) { 59 return t.db.BeginRw(ctx) 60 } 61 func (t *TemporaryMdbx) BeginRwNosync(ctx context.Context) (kv.RwTx, error) { 62 return t.db.BeginRwNosync(ctx) 63 } 64 65 func (t *TemporaryMdbx) View(ctx context.Context, f func(kv.Tx) error) error { 66 return t.db.View(ctx, f) 67 } 68 69 func (t *TemporaryMdbx) BeginRo(ctx context.Context) (kv.Tx, error) { 70 return t.db.BeginRo(ctx) 71 } 72 73 func (t *TemporaryMdbx) AllTables() kv.TableCfg { 74 return t.db.AllTables() 75 } 76 77 func (t *TemporaryMdbx) PageSize() uint64 { 78 return t.db.PageSize() 79 } 80 81 func (t *TemporaryMdbx) Close() { 82 t.db.Close() 83 os.RemoveAll(t.path) 84 }