Commit 4a04246b authored by Paul Asmuth's avatar Paul Asmuth
Browse files

fix a bug where duplicate labels were inserted

parent 5a3b0ef4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -115,7 +115,7 @@ std::shared_ptr<MetricSnapshot> Metric::getOrCreateSnapshot() {
  return head_;
}

void Metric::insertSample(
void Metric::insertSampleImpl(
    double value,
    const std::vector<std::pair<std::string, std::string>>& labels) {
  SampleWriter writer(&token_index_);
+4 −4
Original line number Diff line number Diff line
@@ -40,10 +40,6 @@ public:
      io::FileRepository* file_repo,
      std::vector<std::unique_ptr<TableRef>>&& tables);

  void insertSample(
      double value,
      const std::vector<std::pair<std::string, std::string>>& labels) override;

  void scanSamples(
      const fnord::util::DateTime& time_begin,
      const fnord::util::DateTime& time_end,
@@ -61,6 +57,10 @@ public:
  bool hasLabel(const std::string& label) const override;

protected:
  void insertSampleImpl(
      double value,
      const std::vector<std::pair<std::string, std::string>>& labels) override;

  std::shared_ptr<MetricSnapshot> getSnapshot() const;
  std::shared_ptr<MetricSnapshot> getOrCreateSnapshot();
  std::shared_ptr<MetricSnapshot> createSnapshot(bool writable);
+26 −8
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ AbstractSampleReader::AbstractSampleReader(
const std::vector<std::pair<std::string, std::string>>&
    AbstractSampleReader::labels() {
  if (!labels_read_) {
    seekTo(label_offset_);
    labels_read_ = true;

    while (pos_ < size_) {
@@ -38,10 +39,32 @@ const std::vector<std::pair<std::string, std::string>>&
  return labels_;
}

const std::vector<std::pair<uint32_t, std::string>>&
std::vector<std::pair<uint32_t, std::string>>
    AbstractSampleReader::tokenDefinitions() {
  labels();
  return token_definitions_;
  seekTo(label_offset_);

  std::vector<std::pair<uint32_t, std::string>> token_definitions;
  while (pos_ < size_) {
    auto token_ref = *readUInt32();
    uint32_t string_len;
    uint32_t token_def = 0;

    if (token_ref == 0xffffffff) {
      token_def = *readUInt32();
      string_len = *readUInt32();
    } else if (token_ref >= TokenIndex::kMinTokenID) {
      continue;
    } else {
      string_len = token_ref;
    }

    auto str = std::string(readString(string_len), string_len);
    if (token_def > 0) {
      token_definitions.emplace_back( token_def, str);
    }
  }

  return token_definitions;
}

std::string AbstractSampleReader::readToken() {
@@ -59,11 +82,6 @@ std::string AbstractSampleReader::readToken() {
  }

  auto token = std::string(readString(string_len), string_len);

  if (token_def > 0) {
    token_definitions_.emplace_back(token_def, token);
  }

  return token;
}

+2 −3
Original line number Diff line number Diff line
@@ -28,14 +28,13 @@ public:
      TokenIndex* token_index);

  const std::vector<std::pair<std::string, std::string>>& labels();
  const std::vector<std::pair<uint32_t, std::string>>& tokenDefinitions();
  std::vector<std::pair<uint32_t, std::string>> tokenDefinitions();

protected:
  std::string readToken();

  size_t label_offset_;
  TokenIndex* token_index_;
  std::vector<std::pair<std::string, std::string>> labels_;
  std::vector<std::pair<uint32_t, std::string>> token_definitions_;
  bool labels_read_;
};

+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ SampleReader<T>::SampleReader(
    TokenIndex* token_index) :
    AbstractSampleReader(data, size, token_index) {
  value_ = readValue();
  label_offset_ = pos_;
}

template <typename T>
Loading