blob: 7bcc2e1c3145e363df2c349ee05e662ebf7c3f1f [file] [log] [blame]
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 "RocksDbStream.h"
#include <fstream>
#include <utility>
#include <vector>
#include <memory>
#include <string>
#include <Exception.h>
#include "io/validation.h"
namespace org {
namespace apache {
namespace nifi {
namespace minifi {
namespace io {
RocksDbStream::RocksDbStream(std::string path, rocksdb::DB *db, bool write_enable)
: BaseStream(),
path_(std::move(path)),
write_enable_(write_enable),
db_(db),
logger_(logging::LoggerFactory<RocksDbStream>::getLogger()) {
exists_ = db_->Get(rocksdb::ReadOptions(), path_, &value_).ok();
offset_ = 0;
size_ = value_.size();
}
void RocksDbStream::closeStream() {
}
void RocksDbStream::seek(uint64_t offset) {
// noop
}
int RocksDbStream::writeData(std::vector<uint8_t> &buf, int buflen) {
if (buflen < 0) {
throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
}
if (buf.size() < static_cast<size_t>(buflen)) {
return -1;
}
return writeData(buf.data(), buflen);
}
// data stream overrides
int RocksDbStream::writeData(uint8_t *value, int size) {
if (!IsNullOrEmpty(value) && write_enable_) {
rocksdb::Slice slice_value((const char *) value, size);
rocksdb::Status status;
size_ += size;
rocksdb::WriteOptions opts;
opts.sync = true;
db_->Merge(opts, path_, slice_value);
if (status.ok()) {
return 0;
} else {
return -1;
}
} else {
return -1;
}
}
template<typename T>
inline int RocksDbStream::readBuffer(std::vector<uint8_t>& buf, const T& t) {
buf.resize(sizeof t);
return readData(reinterpret_cast<uint8_t *>(&buf[0]), sizeof(t));
}
int RocksDbStream::readData(std::vector<uint8_t> &buf, int buflen) {
if (buflen < 0) {
throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
}
if (buf.size() < buflen) {
buf.resize(buflen);
}
int ret = readData(buf.data(), buflen);
if (ret < buflen) {
buf.resize((std::max)(ret, 0));
}
return ret;
}
int RocksDbStream::readData(uint8_t *buf, int buflen) {
if (!IsNullOrEmpty(buf) && exists_) {
int amtToRead = buflen;
if (offset_ >= value_.size()) {
return 0;
}
if (buflen > value_.size() - offset_) {
amtToRead = value_.size() - offset_;
}
std::memcpy(buf, value_.data() + offset_, amtToRead);
offset_ += amtToRead;
return amtToRead;
} else {
return -1;
}
}
} /* namespace io */
} /* namespace minifi */
} /* namespace nifi */
} /* namespace apache */
} /* namespace org */