Chris Mumford | 66df99e | 2017-09-13 22:23:32 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 4 | |
| 5 | #ifndef THIRD_PARTY_LEVELDATABASE_LEVELDB_CHROME_H_ |
| 6 | #define THIRD_PARTY_LEVELDATABASE_LEVELDB_CHROME_H_ |
| 7 | |
Chris Mumford | 8d26d10a | 2018-04-20 17:07:58 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
Chris Mumford | a884aa1f | 2017-10-24 22:46:32 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
Chris Mumford | 66df99e | 2017-09-13 22:23:32 | [diff] [blame] | 12 | #include "leveldb/cache.h" |
Chris Mumford | c38afb6 | 2017-10-09 17:55:08 | [diff] [blame] | 13 | #include "leveldb/env.h" |
Chris Mumford | 2a0c74d4 | 2017-10-14 00:49:20 | [diff] [blame] | 14 | #include "leveldb/export.h" |
Chris Mumford | da3afc3b | 2018-06-05 22:10:23 | [diff] [blame] | 15 | #include "leveldb/options.h" |
Chris Mumford | 2a0c74d4 | 2017-10-14 00:49:20 | [diff] [blame] | 16 | #include "third_party/leveldatabase/src/db/filename.h" |
Chris Mumford | 66df99e | 2017-09-13 22:23:32 | [diff] [blame] | 17 | |
Chris Mumford | 8d26d10a | 2018-04-20 17:07:58 | [diff] [blame] | 18 | namespace base { |
| 19 | namespace trace_event { |
| 20 | class MemoryAllocatorDump; |
| 21 | class ProcessMemoryDump; |
| 22 | } // namespace trace_event |
| 23 | } // namespace base |
| 24 | |
Chris Mumford | 66df99e | 2017-09-13 22:23:32 | [diff] [blame] | 25 | namespace leveldb_chrome { |
| 26 | |
| 27 | // Return the shared leveldb block cache for web APIs. The caller *does not* |
| 28 | // own the returned instance. |
Chris Mumford | 2a0c74d4 | 2017-10-14 00:49:20 | [diff] [blame] | 29 | LEVELDB_EXPORT leveldb::Cache* GetSharedWebBlockCache(); |
Chris Mumford | 66df99e | 2017-09-13 22:23:32 | [diff] [blame] | 30 | |
| 31 | // Return the shared leveldb block cache for browser (non web) APIs. The caller |
| 32 | // *does not* own the returned instance. |
Chris Mumford | 2a0c74d4 | 2017-10-14 00:49:20 | [diff] [blame] | 33 | LEVELDB_EXPORT leveldb::Cache* GetSharedBrowserBlockCache(); |
Chris Mumford | 66df99e | 2017-09-13 22:23:32 | [diff] [blame] | 34 | |
Chris Mumford | a46073f | 2017-10-12 21:54:12 | [diff] [blame] | 35 | // Return the shared leveldb block cache for in-memory Envs. The caller *does |
| 36 | // not* own the returned instance. |
Chris Mumford | 2a0c74d4 | 2017-10-14 00:49:20 | [diff] [blame] | 37 | LEVELDB_EXPORT leveldb::Cache* GetSharedInMemoryBlockCache(); |
Chris Mumford | a46073f | 2017-10-12 21:54:12 | [diff] [blame] | 38 | |
Chris Mumford | 1dc497c0 | 2017-10-09 22:03:22 | [diff] [blame] | 39 | // Determine if a leveldb::Env stores the file data in RAM. |
Chris Mumford | 2a0c74d4 | 2017-10-14 00:49:20 | [diff] [blame] | 40 | LEVELDB_EXPORT bool IsMemEnv(const leveldb::Env* env); |
Chris Mumford | 1dc497c0 | 2017-10-09 22:03:22 | [diff] [blame] | 41 | |
Chris Mumford | c38afb6 | 2017-10-09 17:55:08 | [diff] [blame] | 42 | // Creates an in-memory Env for which all files are stored in the heap. |
Chris Mumford | 8d26d10a | 2018-04-20 17:07:58 | [diff] [blame] | 43 | // This wraps leveldb::NewMemEnv to add memory-infra logging. |
| 44 | // if |base_env| is null then leveldb::Env::Default() will be used. |
| 45 | LEVELDB_EXPORT std::unique_ptr<leveldb::Env> NewMemEnv( |
| 46 | const std::string& name, |
| 47 | leveldb::Env* base_env = nullptr); |
Chris Mumford | 2a0c74d4 | 2017-10-14 00:49:20 | [diff] [blame] | 48 | |
| 49 | // If filename is a leveldb file, store the type of the file in *type. |
Chris Mumford | 8d26d10a | 2018-04-20 17:07:58 | [diff] [blame] | 50 | // The number encoded in the filename is stored in *number. |
Chris Mumford | 2a0c74d4 | 2017-10-14 00:49:20 | [diff] [blame] | 51 | // Returns true if the filename was successfully parsed. |
| 52 | LEVELDB_EXPORT bool ParseFileName(const std::string& filename, |
| 53 | uint64_t* number, |
| 54 | leveldb::FileType* type); |
Chris Mumford | c38afb6 | 2017-10-09 17:55:08 | [diff] [blame] | 55 | |
Chris Mumford | a46073f | 2017-10-12 21:54:12 | [diff] [blame] | 56 | // Report leveldb UMA values. |
Chris Mumford | 2a0c74d4 | 2017-10-14 00:49:20 | [diff] [blame] | 57 | LEVELDB_EXPORT void UpdateHistograms(); |
Chris Mumford | a46073f | 2017-10-12 21:54:12 | [diff] [blame] | 58 | |
Chris Mumford | a884aa1f | 2017-10-24 22:46:32 | [diff] [blame] | 59 | // Corrupt a closed database for testing purposes. After calling this function |
| 60 | // leveldb::OpenDB(...) will return a status where IsCorruption() returns true. |
| 61 | // Returns true if the database was successfully corrupted, false if not. |
| 62 | // Note: This function will fail if |db_path| does not exist. |
| 63 | LEVELDB_EXPORT bool CorruptClosedDBForTesting(const base::FilePath& db_path); |
| 64 | |
Chris Mumford | da3afc3b | 2018-06-05 22:10:23 | [diff] [blame] | 65 | // Check that the database path in |db_path| "appears" to be a valid leveldb |
| 66 | // database using the provided |env|. This function *does not* open or verify |
| 67 | // that the database referred to by |db_path| is a valid database, only that it |
| 68 | // appears to be one. |
| 69 | LEVELDB_EXPORT bool PossiblyValidDB(const base::FilePath& db_path, |
| 70 | leveldb::Env* env); |
| 71 | |
| 72 | // Fully delete the leveldb database specified by |db_path|. leveldb::DestroyDB |
| 73 | // will only delete files that it creates. Other files, if present, will be |
| 74 | // ignored and left behind after leveldb::DestroyDB returns. This function will |
| 75 | // delete the entire database directory. |
| 76 | // |
| 77 | // Note: Can be used with in-memory Env's. |
| 78 | LEVELDB_EXPORT leveldb::Status DeleteDB(const base::FilePath& db_path, |
| 79 | const leveldb::Options& options); |
| 80 | |
Chris Mumford | 8d26d10a | 2018-04-20 17:07:58 | [diff] [blame] | 81 | // Returns the memory-infra dump for |tracked_memenv|. |
| 82 | // Do not call this function, instead call |
| 83 | // leveldb_env::DBTracker::GetOrCreateAllocatorDump(). |
| 84 | // TODO(crbug.com/762598) Can be made private as part of leveldb cleanup. |
| 85 | base::trace_event::MemoryAllocatorDump* GetEnvAllocatorDump( |
| 86 | base::trace_event::ProcessMemoryDump* pmd, |
| 87 | leveldb::Env* tracked_memenv); |
| 88 | |
| 89 | // Dump all tracked in-memory env's to the |pmd|. Do not call - this is a |
| 90 | // private function for leveldb_env::DBTracker. |
| 91 | // TODO(crbug.com/762598) Can be made private as part of leveldb cleanup. |
| 92 | void DumpAllTrackedEnvs(base::trace_event::ProcessMemoryDump* pmd); |
| 93 | |
Chris Mumford | 66df99e | 2017-09-13 22:23:32 | [diff] [blame] | 94 | } // namespace leveldb_chrome |
| 95 | |
| 96 | #endif // THIRD_PARTY_LEVELDATABASE_LEVELDB_CHROME_H_ |