inline BOOL ConfDB::UpdateVIPCData(u32 nId, VSCVIPCData &pData) { VSCConfVIPCKey sChKey(nId); leveldb::WriteOptions writeOptions; leveldb::Slice chKey((char *)&sChKey, sizeof(sChKey)); leveldb::Slice chValue((char *)&pData, sizeof(VSCDeviceData)); m_pDb->Put(writeOptions, chKey, chValue); return TRUE; }
void load(unsigned short bamIndex) { size_t n_reads=0; std::string value; bam1_t *b= bam_init1(); index2chromNames.resize(bamIndex+1); WHERE(in->header); for(int32_t i=0;i< in->header->n_targets;++i) { string target_name(in->header->target_name[i]); index2chromNames.back().push_back(target_name); } while(samread(this->in, b) > 0) /* loop over the records */ { std::auto_ptr<vector<Hit> > hits(0); Bam1Sequence seq(b); leveldb::Slice key1(seq.name()); value.clear(); WHERE(n_reads); leveldb::Status status = db->Get(this->read_options, key1, &value); if(!status.ok()) { hits.reset(new vector<Hit>()); n_reads++; if(n_reads%1000000UL==0) { clog << n_reads << endl; //break;//TODO } } else { hits=decode(value); } Hit hit; hit.bamIndex=bamIndex; hit.tid = seq.tid(); hit.pos = seq.pos(); hit.flag = seq.flag(); hits->push_back(hit); std::auto_ptr<string> encoded = this->encode(hits.get()); leveldb::Slice value1(encoded->data(),encoded->size()); status = db->Put(this->write_options, key1, value1); if(!status.ok()) { cerr << "Cannot insert into db" << endl; break; } } bam_destroy1(b); }
Try<Option<Entry> > LevelDBStorageProcess::read(const string& name) { CHECK_NONE(error); leveldb::ReadOptions options; string value; leveldb::Status status = db->Get(options, name, &value); if (status.IsNotFound()) { return None(); } else if (!status.ok()) { return Error(status.ToString()); } google::protobuf::io::ArrayInputStream stream(value.data(), value.size()); Entry entry; if (!entry.ParseFromZeroCopyStream(&stream)) { return Error("Failed to deserialize Entry"); } return Some(entry); }
inline BOOL ConfDB::GetVIPCData(u32 nId, VSCVIPCData &pData) { VSCConfVIPCKey sChKey(nId); leveldb::Slice key((char *)&sChKey, sizeof(sChKey)); leveldb::Iterator* it = m_pDb->NewIterator(leveldb::ReadOptions()); it->Seek(key); leveldb::Slice sysValue; if (it->Valid()) { sysValue = it->value(); } if (sysValue.size() != sizeof(VSCVIPCData)) { VDC_DEBUG( "Device Can not find !!!\n"); delete it; return FALSE; } memcpy(&pData, sysValue.data(), sizeof(VSCVIPCData)); // Check for any errors found during the scan assert(it->status().ok()); delete it; return TRUE; }
inline BOOL ConfDB::GetLicense(astring &strLicense) { VSCConfLicenseKey sLicKey; leveldb::Slice key((char *)&sLicKey, sizeof(sLicKey)); leveldb::Iterator* it = m_pDb->NewIterator(leveldb::ReadOptions()); it->Seek(key); leveldb::Slice sysValue; if (it->Valid()) { sysValue = it->value(); strLicense = sysValue.ToString(); } // Check for any errors found during the scan assert(it->status().ok()); delete it; return TRUE; }
Status Get(const std::string& key, std::string* value) { leveldb::Status s = db->Get(leveldb::ReadOptions(), key, value); if (s.ok()) { return Status::OK(); } return Status::NotFound(); }
inline BOOL ConfDB::SetLicense(astring &strLicense) { VSCConfLicenseKey sLic; leveldb::WriteOptions writeOptions; leveldb::Slice licKey((char *)&sLic, sizeof(sLic)); leveldb::Slice licValue(strLicense); m_pDb->Put(writeOptions, licKey, licValue); return true; }
Status Delete(const std::string& key) { leveldb::Status s = db->Delete(leveldb::WriteOptions(), key); if (s.ok()) { return Status::OK(); } return Status::NotFound(); }
Status Put(const std::string& key, const std::string& value) { leveldb::Status s = db->Put(leveldb::WriteOptions(), key, value); if (s.ok()) { return Status::OK(); } std::cerr << s.ToString() << std::endl; return Status::NotFound(); }
/* HDFS record */ inline s32 ConfDB::UpdateHdfsRecordData(VSCHdfsRecordData &pData) { VSCConfHdfsRecordKey sKey; leveldb::WriteOptions writeOptions; leveldb::Slice sysKey((char *)&sKey, sizeof(sKey)); leveldb::Slice sysValue((char *)&pData, sizeof(VSCHdfsRecordData)); m_pDb->Put(writeOptions, sysKey, sysValue); return TRUE; }
/* Camera Group */ inline s32 ConfDB::UpdateVGroupData(VSCVGroupData &pVGroupData) { VSCConfVGroupKey sVGroupKey; leveldb::WriteOptions writeOptions; leveldb::Slice sysKey((char *)&sVGroupKey, sizeof(sVGroupKey)); leveldb::Slice sysValue((char *)&pVGroupData, sizeof(VSCVGroupData)); m_pDb->Put(writeOptions, sysKey, sysValue); return TRUE; }
inline s32 ConfDB::UpdateSysData(VSCConfData &pSysData) { VSCConfSystemKey sSysKey; leveldb::WriteOptions writeOptions; leveldb::Slice sysKey((char *)&sSysKey, sizeof(sSysKey)); leveldb::Slice sysValue((char *)&pSysData, sizeof(VSCConfData)); m_pDb->Put(writeOptions, sysKey, sysValue); return TRUE; }
void LevelDBStorageProcess::initialize() { leveldb::Options options; options.create_if_missing = true; leveldb::Status status = leveldb::DB::Open(options, path, &db); if (!status.ok()) { // TODO(benh): Consider trying to repair the DB. error = status.ToString(); } else { // TODO(benh): Conditionally compact to avoid long recovery times? db->CompactRange(NULL, NULL); } }
Status Put(const std::vector<std::pair<std::string, std::string>>& writes) { leveldb::WriteBatch batch; for (auto kv : writes) { batch.Put(kv.first, kv.second); } leveldb::Status s = db->Write(leveldb::WriteOptions(), &batch); if (s.ok()) { return Status::OK(); } return Status::NotFound(); }
Status Put(const std::vector<std::pair<unsigned int, std::string>>& writes) { leveldb::WriteBatch batch; for (auto kv : writes) { std::string keystr; ZUtil::PutVarint64(keystr, kv.first); batch.Put(keystr, kv.second); } leveldb::Status s = db->Write(leveldb::WriteOptions(), &batch); if (s.ok()) { return Status::OK(); } return Status::NotFound(); }
Future<std::set<string> > LevelDBStorageProcess::names() { if (error.isSome()) { return Failure(error.get()); } std::set<string> results; leveldb::Iterator* iterator = db->NewIterator(leveldb::ReadOptions()); iterator->SeekToFirst(); while (iterator->Valid()) { results.insert(iterator->key().ToString()); iterator->Next(); } delete iterator; return results; }
Try<bool> LevelDBStorageProcess::write(const Entry& entry) { CHECK_NONE(error); leveldb::WriteOptions options; options.sync = true; string value; if (!entry.SerializeToString(&value)) { return Error("Failed to serialize Entry"); } leveldb::Status status = db->Put(options, entry.name(), value); if (!status.ok()) { return Error(status.ToString()); } return true; }
inline BOOL ConfDB::GetSystemConf(VSCConfData &pSys) { VSCConfSystemKey sSysKey; leveldb::Slice key((char *)&sSysKey, sizeof(sSysKey)); leveldb::Iterator* it = m_pDb->NewIterator(leveldb::ReadOptions()); it->Seek(key); leveldb::Slice sysValue; if (it->Valid()) { sysValue = it->value(); } if (sysValue.size() != sizeof(VSCConfData)) { VDC_DEBUG( "System Config is not init\n"); delete it; memset(&pSys, 0, sizeof(VSCConfData)); SysConfDataDefault(pSys); UpdateSysData(pSys); astring strLicense = " "; SetLicense(strLicense);//set the default license /* Call get system again */ return TRUE; } memcpy(&pSys, sysValue.data(), sizeof(VSCConfData)); // Check for any errors found during the scan assert(it->status().ok()); delete it; return TRUE; }
Future<bool> LevelDBStorageProcess::expunge(const Entry& entry) { if (error.isSome()) { return Failure(error.get()); } // We do a read first to make sure the version has not changed. This // could be optimized in the future, for now it will probably hit // the cache anyway. Try<Option<Entry> > option = read(entry.name()); if (option.isError()) { return Failure(option.error()); } if (option.get().isNone()) { return false; } if (UUID::fromBytes(option.get().get().uuid()) != UUID::fromBytes(entry.uuid())) { return false; } // Note that the read (i.e., DB::Get) and DB::Delete are inherently // "atomic" because only one db can be opened at a time, so there // can not be any writes that occur concurrently. leveldb::WriteOptions options; options.sync = true; leveldb::Status status = db->Delete(options, entry.name()); if (!status.ok()) { return Failure(status.ToString()); } return true; }
inline BOOL ConfDB::GetHdfsRecordConf(VSCHdfsRecordData &pData) { VSCConfHdfsRecordKey sKey; leveldb::Slice key((char *)&sKey, sizeof(sKey)); leveldb::Iterator* it = m_pDb->NewIterator(leveldb::ReadOptions()); it->Seek(key); leveldb::Slice sysValue; if (it->Valid()) { sysValue = it->value(); } if (sysValue.size() != sizeof(VSCHdfsRecordData)) { VDC_DEBUG( "Hdfs Record Config is not init\n"); delete it; memset(&pData, 0, sizeof(VSCHdfsRecordData)); VSCHdfsRecordDataItemDefault(pData.data.conf); UpdateHdfsRecordData(pData); /* Call get system again */ return TRUE; } memcpy(&pData, sysValue.data(), sizeof(VSCHdfsRecordData)); // Check for any errors found during the scan assert(it->status().ok()); delete it; return TRUE; }
void Compact() { db->CompactRange(NULL,NULL); }
void dump() { leveldb::Iterator* it=db->NewIterator(this->read_options); for (it->SeekToFirst(); it->Valid(); it->Next()) { std::auto_ptr<vector<Hit> > hits = decode(it->value().ToString()); size_t i0=0; while(i0 < hits->size()) { size_t i1=i0+1; while(i1<hits->size()) { if(hits->at(i0).bamIndex==hits->at(i1).bamIndex) {++i1; continue;} if(IS_FLAG_SET(hits->at(i0).flag,BAM_FREAD1)!=IS_FLAG_SET(hits->at(i1).flag,BAM_FREAD1)) { ++i1; continue; } if(IS_FLAG_SET(hits->at(i0).flag,BAM_FREAD2)!=IS_FLAG_SET(hits->at(i1).flag,BAM_FREAD2)) { ++i1; continue; } if(!hits->at(i0).mapped() && !hits->at(i1).mapped()) { break; } if(hits->at(i0).tid==-1 && hits->at(i1).tid==-1) { break; } else if(sameChromosome( index2chromNames[hits->at(i0).bamIndex][hits->at(i0).tid].c_str(), index2chromNames[hits->at(i1).bamIndex][hits->at(i1).tid].c_str() )) { if(hits->at(i0).pos==hits->at(i1).pos) { break; } } ++i1; } if(i1!= hits->size()) { cout << it->key().ToString() << "\t"; if(IS_FLAG_SET(hits->at(i0).flag,BAM_FREAD1)) { cout << "1\t"; } else if(IS_FLAG_SET(hits->at(i0).flag,BAM_FREAD2)) { cout << "2\t";} else cout << "?\t"; print(hits->at(i0)); cout << "\t"; print(hits->at(i1)); cout << "\t="; cout << endl; hits->erase(hits->begin()+i1);//that one before hits->erase(hits->begin()+i0); } else { ++i0; } } for(i0=0;i0<hits->size();++i0) { cout << it->key().ToString() << "\t"; if(IS_FLAG_SET(hits->at(i0).flag,BAM_FREAD1)) cout << "1\t"; else if(IS_FLAG_SET(hits->at(i0).flag,BAM_FREAD2)) cout << "2\t"; else cout << "?\t"; if(hits->at(i0).bamIndex==1) cout << ".\t"; print(hits->at(i0)); if(hits->at(i0).bamIndex==0) cout << "\t."; cout << endl; } } delete it; }