leveldb_open.go 863 B

1234567891011121314151617181920212223242526272829303132
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package backend
  7. import (
  8. "github.com/syndtr/goleveldb/leveldb"
  9. "github.com/syndtr/goleveldb/leveldb/opt"
  10. )
  11. const dbMaxOpenFiles = 100
  12. // OpenLevelDBRO attempts to open the database at the given location, read
  13. // only.
  14. func OpenLevelDBRO(location string) (Backend, error) {
  15. opts := &opt.Options{
  16. OpenFilesCacheCapacity: dbMaxOpenFiles,
  17. ReadOnly: true,
  18. }
  19. ldb, err := open(location, opts)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return newLeveldbBackend(ldb, location), nil
  24. }
  25. func open(location string, opts *opt.Options) (*leveldb.DB, error) {
  26. return leveldb.OpenFile(location, opts)
  27. }