summaryrefslogtreecommitdiff
path: root/db/dbformat.cc
blob: 2664eb43fcda88d049f9cd244c43930ef2488167 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include <stdio.h>
#include "db/dbformat.h"
#include "port/port.h"
#include "util/coding.h"

namespace leveldb {

static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) {
  assert(seq <= kMaxSequenceNumber);
  assert(t <= kValueTypeForSeek);
  return (seq << 8) | t;
}

void AppendInternalKey(std::string* result, const ParsedInternalKey& key) {
  result->append(key.user_key.data(), key.user_key.size());
  PutFixed64(result, PackSequenceAndType(key.sequence, key.type));
}

std::string ParsedInternalKey::DebugString() const {
  char buf[50];
  snprintf(buf, sizeof(buf), "' @ %llu : %d",
           (unsigned long long) sequence,
           int(type));
  std::string result = "'";
  result += user_key.ToString();
  result += buf;
  return result;
}

const char* InternalKeyComparator::Name() const {
  return "leveldb.InternalKeyComparator";
}

int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const {
  // Order by:
  //    increasing user key (according to user-supplied comparator)
  //    decreasing sequence number
  //    decreasing type (though sequence# should be enough to disambiguate)
  int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey));
  if (r == 0) {
    const uint64_t anum = DecodeFixed64(akey.data() + akey.size() - 8);
    const uint64_t bnum = DecodeFixed64(bkey.data() + bkey.size() - 8);
    if (anum > bnum) {
      r = -1;
    } else if (anum < bnum) {
      r = +1;
    }
  }
  return r;
}

void InternalKeyComparator::FindShortestSeparator(
      std::string* start,
      const Slice& limit) const {
  // Attempt to shorten the user portion of the key
  Slice user_start = ExtractUserKey(*start);
  Slice user_limit = ExtractUserKey(limit);
  std::string tmp(user_start.data(), user_start.size());
  user_comparator_->FindShortestSeparator(&tmp, user_limit);
  if (user_comparator_->Compare(*start, tmp) < 0) {
    // User key has become larger.  Tack on the earliest possible
    // number to the shortened user key.
    PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek));
    assert(this->Compare(*start, tmp) < 0);
    assert(this->Compare(tmp, limit) < 0);
    start->swap(tmp);
  }
}

void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
  Slice user_key = ExtractUserKey(*key);
  std::string tmp(user_key.data(), user_key.size());
  user_comparator_->FindShortSuccessor(&tmp);
  if (user_comparator_->Compare(user_key, tmp) < 0) {
    // User key has become larger.  Tack on the earliest possible
    // number to the shortened user key.
    PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek));
    assert(this->Compare(*key, tmp) < 0);
    key->swap(tmp);
  }
}

LargeValueRef LargeValueRef::Make(const Slice& value, CompressionType ctype) {
  LargeValueRef result;
  port::SHA1_Hash(value.data(), value.size(), &result.data[0]);
  EncodeFixed64(&result.data[20], value.size());
  result.data[28] = static_cast<unsigned char>(ctype);
  return result;
}

std::string LargeValueRefToFilenameString(const LargeValueRef& h) {
  assert(sizeof(h.data) == LargeValueRef::ByteSize());
  assert(sizeof(h.data) == 29); // So we can hardcode the array size of buf
  static const char tohex[] = "0123456789abcdef";
  char buf[20*2];
  for (int i = 0; i < 20; i++) {
    buf[2*i] = tohex[(h.data[i] >> 4) & 0xf];
    buf[2*i+1] = tohex[h.data[i] & 0xf];
  }
  std::string result = std::string(buf, sizeof(buf));
  result += "-";
  result += NumberToString(h.ValueSize());
  result += "-";
  result += NumberToString(static_cast<uint64_t>(h.compression_type()));
  return result;
}

static uint32_t hexvalue(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  } else if (c >= 'A' && c <= 'F') {
    return 10 + c - 'A';
  } else {
    assert(c >= 'a' && c <= 'f');
    return 10 + c - 'a';
  }
}

bool FilenameStringToLargeValueRef(const Slice& s, LargeValueRef* h) {
  Slice in = s;
  if (in.size() < 40) {
    return false;
  }
  for (int i = 0; i < 20; i++) {
    if (!isxdigit(in[i*2]) || !isxdigit(in[i*2+1])) {
      return false;
    }
    unsigned char c = (hexvalue(in[i*2])<<4) | hexvalue(in[i*2+1]);
    h->data[i] = c;
  }
  in.remove_prefix(40);
  uint64_t value_size, ctype;

  if (ConsumeChar(&in, '-') &&
      ConsumeDecimalNumber(&in, &value_size) &&
      ConsumeChar(&in, '-') &&
      ConsumeDecimalNumber(&in, &ctype) &&
      in.empty() &&
      (ctype <= kSnappyCompression)) {
    EncodeFixed64(&h->data[20], value_size);
    h->data[28] = static_cast<unsigned char>(ctype);
    return true;
  } else {
    return false;
  }
}

}