summaryrefslogtreecommitdiff
path: root/chromium/components/leveldb/public/cpp/util.cc
blob: 3d60ea2e971d1e404333626883c8d04ea3b7c6d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/leveldb/public/cpp/util.h"

#include "third_party/leveldatabase/src/include/leveldb/status.h"

namespace leveldb {

DatabaseError LeveldbStatusToError(const leveldb::Status& s) {
  if (s.ok())
    return DatabaseError::OK;
  if (s.IsNotFound())
    return DatabaseError::NOT_FOUND;
  if (s.IsCorruption())
    return DatabaseError::CORRUPTION;
  if (s.IsNotSupportedError())
    return DatabaseError::NOT_SUPPORTED;
  if (s.IsIOError())
    return DatabaseError::IO_ERROR;
  return DatabaseError::INVALID_ARGUMENT;
}

leveldb::Status DatabaseErrorToStatus(DatabaseError e,
                                      const Slice& msg,
                                      const Slice& msg2) {
  switch (e) {
    case DatabaseError::OK:
      return leveldb::Status::OK();
    case DatabaseError::NOT_FOUND:
      return leveldb::Status::NotFound(msg, msg2);
    case DatabaseError::CORRUPTION:
      return leveldb::Status::Corruption(msg, msg2);
    case DatabaseError::NOT_SUPPORTED:
      return leveldb::Status::NotSupported(msg, msg2);
    case DatabaseError::INVALID_ARGUMENT:
      return leveldb::Status::InvalidArgument(msg, msg2);
    case DatabaseError::IO_ERROR:
      return leveldb::Status::IOError(msg, msg2);
  }

  // This will never be reached, but we still have configurations which don't
  // do switch enum checking.
  return leveldb::Status::InvalidArgument(msg, msg2);
}

leveldb::Slice GetSliceFor(const mojo::Array<uint8_t>& key) {
  if (key.size() == 0)
    return leveldb::Slice();
  return leveldb::Slice(reinterpret_cast<const char*>(&key.front()),
                        key.size());
}

mojo::Array<uint8_t> GetArrayFor(const leveldb::Slice& s) {
  if (s.size() == 0)
    return mojo::Array<uint8_t>();
  return mojo::Array<uint8_t>(std::vector<uint8_t>(
      reinterpret_cast<const uint8_t*>(s.data()),
      reinterpret_cast<const uint8_t*>(s.data() + s.size())));
}

}  // namespace leveldb