aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core/src/firebase/firestore/core/target_id_generator.cc
blob: 473d083208445fcb42678723492d49d9c07aac2a (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
/*
 * Copyright 2018 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Firestore/core/src/firebase/firestore/core/target_id_generator.h"

using firebase::firestore::model::TargetId;

namespace firebase {
namespace firestore {
namespace core {

TargetIdGenerator::TargetIdGenerator(const TargetIdGenerator& value)
    : generator_id_(value.generator_id_), previous_id_(value.previous_id_) {
}

TargetIdGenerator::TargetIdGenerator(TargetIdGeneratorId generator_id,
                                     TargetId after)
    : generator_id_(generator_id) {
  const TargetId after_without_generator = (after >> kReservedBits)
                                           << kReservedBits;
  const TargetId after_generator = after - after_without_generator;
  const TargetId generator = static_cast<TargetId>(generator_id);
  if (after_generator >= generator) {
    // For example, if:
    //   self.generatorID = 0b0000
    //   after = 0b1011
    //   afterGenerator = 0b0001
    // Then:
    //   previous = 0b1010
    //   next = 0b1100
    previous_id_ = after_without_generator | generator;
  } else {
    // For example, if:
    //   self.generatorID = 0b0001
    //   after = 0b1010
    //   afterGenerator = 0b0000
    // Then:
    //   previous = 0b1001
    //   next = 0b1011
    previous_id_ = (after_without_generator | generator) - (1 << kReservedBits);
  }
}

TargetId TargetIdGenerator::NextId() {
  previous_id_ += 1 << kReservedBits;
  return previous_id_;
}

}  // namespace core
}  // namespace firestore
}  // namespace firebase