/** * 更新小型大对象 * * @param session 会话对象 * @param lobId 大对象ID * @param lob 对应的大对象字节流 * @param size 大对象长度 * @param orgLen 原有长度 */ void SmallLobStorage::update(Session *session, LobId lobId, const byte *lob, uint size, u32 orgLen) { ftrace(ts.lob, tout << session << lobId << lob << size << orgLen); // 先设置第二位的是否压缩的为0 SubRecord subRec; byte data[Limits::MAX_REC_SIZE] ; u16 cols[2] = {0, 1}; subRec.m_columns = cols; subRec.m_numCols = 2; subRec.m_data = data; subRec.m_size = Limits::MAX_REC_SIZE; // 设置压缩位为0 SubRecord *reTSubRec = RecordOper::createSlobSubRecordR(m_vtableDef, &subRec, lob, size, (size_t)orgLen); reTSubRec->m_rowId = lobId; reTSubRec->m_format = REC_REDUNDANT; // 先更新MMS MmsRecord *mRecord = NULL; if (m_useMms) mRecord = m_mtable->getByRid(session, lobId, true, NULL, None); if (mRecord != NULL) { u16 recSize; if (m_mtable->canUpdate(mRecord, reTSubRec, &recSize)) m_mtable->update(session, mRecord, reTSubRec, recSize); else { Session *sessionNew = m_db->getSessionManager()->allocSession("SmallLobStorage::update", session->getConnection()); m_mtable->flushAndDel(sessionNew, mRecord); m_db->getSessionManager()->freeSession(sessionNew); NTSE_ASSERT(m_heap->update(session, lobId, reTSubRec)); } } else { // 如果不在MMS,则更新DRS NTSE_ASSERT(m_heap->update(session, lobId, reTSubRec)); } }
/* - use fopen to open the file, and fclose when you're done */ void frender(camera c, frame f, char *filename) //void render(stage g) { char name[32]; int i, j; for (i=0; i<32; i++) name[i]='\0'; sprintf(name, "%s.ppm", filename); FILE *fp = fopen (name, "a"); fprintf(fp, "P3\n"); fprintf(fp, "%d %d\n", c.w, c.h); fprintf(fp, "255\n"); for(i=0; i < c.h; i++) { for(j=0; j < c.w; j++) { vec p = {j,i,0}; vec loc = logical_loc(c, p); vec dir = vec_sub(loc, c.loc); vec normdir = vec_norm(dir); ray r = {c.loc, normdir}; rgb col = ftrace(r, f); //rgb_print_bytes(col); fprintf(fp, "%d %d %d\n",bytify(col.r),bytify(col.g),bytify(col.b)); } } fclose(fp); }
/** * 删除一个小型大对象,用于crash恢复 * * @param session 会话 * @param lid 大对象ID * @return 是否成功删除 * @throw NtseException 文件操作出错等 */ bool SmallLobStorage::delAtCrash(Session *session, LobId lid) throw(NtseException) { ftrace(ts.lob, tout << session << lid); delInMms(session, lid); //然后到DRS中寻找 bool isSucuss = m_heap->del(session, lid); return isSucuss; }
/** * 在MMS查找相应lobId的大对象记录,如果存在就删除 * * @param session 会话对象 * @param lobId 大对象ID */ void SmallLobStorage::delInMms(Session *session, LobId lobId) { ftrace(ts.lob, tout << lobId); // 先设置第二位的是否压缩的为0 MmsRecord *mRecord = NULL; if (m_useMms) mRecord = m_mtable->getByRid(session, lobId, false, NULL, None); if (mRecord != NULL) { m_mtable->del(session, mRecord); } }
/** * 读取一个小型大对象 * * @param session 会话 * @param mc 内存上下文 * @param lid 大对象ID * @param size out 大对象的长度 * @param intoMms 是否写入MMS * @param orgLen 原有长度 * @return 大对象的字节流 */ byte* SmallLobStorage::read(Session *session, MemoryContext *mc, LobId lid, u32 *size, bool intoMms, u32 *orgLen) { ftrace(ts.lob, tout << session << lid); MmsRecord *mRecord = NULL; if (m_useMms) mRecord = m_mtable->getByRid(session, lid, true, NULL, None); // 首先判断是否在MMS中 if (mRecord) { Record record; // 上层保证,这里可以不加行锁 // 进行内存分配 byte *data = (byte *)mc->alloc(Limits::MAX_REC_SIZE); record.m_data = data; record.m_size = Limits::MAX_REC_SIZE; m_mtable->getRecord(mRecord, &record); // 释放pin和行锁 m_mtable->unpinRecord(session, mRecord); size_t len; size_t orgSize; byte *retData = RecordOper::extractSlobData(m_vtableDef, &record, &len, &orgSize); *size = (u32)len; *orgLen = (u32)orgSize; return retData; } // 假如不在则到Drs中找 Record record; // 分配内存 byte *data = (byte *)mc->alloc(Limits::MAX_REC_SIZE); record.m_data = data; record.m_format = REC_VARLEN; bool isExist = m_heap->getRecord(session, lid, &record); // 假如记录不存在 if (!isExist) { *size = 0; return NULL; } if (intoMms) { assert(m_useMms); // 同时插入MMS中 mRecord = m_mtable->putIfNotExist(session, &record); // 释放PIN if (mRecord) m_mtable->unpinRecord(session, mRecord); } size_t len; size_t orgSize; byte *retData = RecordOper::extractSlobData(m_vtableDef, &record, &len, &orgSize); *size = (u32)len; *orgLen = (u32)orgSize; return retData; }
/** * 插入小型大对象 * * @param session 会话对象 * @param slob 对应的大对象字节流 * @param size 大对象长度 * @param orgLen 原始长度 * @return 大对象ID */ LobId SmallLobStorage::insert(Session *session, const byte *slob, uint size, u32 orgLen) { ftrace(ts.lob, tout << session << size << orgLen); Record tempRec; byte temp[Limits::MAX_REC_SIZE] ; tempRec.m_data = temp; tempRec.m_size = Limits::MAX_REC_SIZE; Record *rec = RecordOper::createSlobRecord(m_vtableDef, &tempRec, slob, (size_t)size, (size_t)orgLen); try { rec->m_rowId = m_heap->insert(session, rec, NULL); } catch (NtseException &){ // 大对象插入不需要加TNT行锁,因此不可能抛错 assert(false); } nftrace(ts.lob, tout << "finish lob insert small lob" << rec->m_rowId); return rec->m_rowId; }
/** * 打开大对象存储 * * @param db 数据库对象 * @param session 会话对象 * @param path 路径 * @param useMms 是否启用MMS * @throw NtseException 文件操作出错等 * @return 小型大对象存储对象 */ SmallLobStorage* SmallLobStorage::open(Database *db, Session *session, const char *basePath, bool useMms) throw(NtseException) { ftrace(ts.lob, tout << basePath); DrsHeap *heap = NULL; string heapPath = string(basePath) + Limits::NAME_SOBH_EXT; string vtblDefPath = string(basePath) + Limits::NAME_SOBH_TBLDEF_EXT; // 读取tableDef TableDef *vtableDef = TableDef::open(vtblDefPath.c_str()); heap = DrsHeap::open(db, session, heapPath.c_str(), vtableDef); heap->getDBObjStats()->m_type = DBO_Slob; // 建立MMSTable MmsTable *lobtable = NULL; if (useMms) { Mms *lobMms = db->getMms(); lobtable = new MmsTable(lobMms, db, heap, vtableDef, false, 0); lobMms->registerMmsTable(session, lobtable); } nftrace(ts.lob, tout << "open finish"); return new SmallLobStorage(db, heap, System::strdup(basePath), lobtable, vtableDef, useMms); }
void Garnet::Drivers::executeTrees(StringMap& args) { Garnet::Configuration conf; //----------------------------------------------------- // Determine project location and name. //----------------------------------------------------- resolveProjectInformation(args, &conf); //----------------------------------------------------- // 0. Load arguments //----------------------------------------------------- std::string infilename; { // Load from configuration file. std::ifstream fin; fin.open(conf.prj.c_str()); if ( fin.fail() ) { throw std::string("[" __FUNCTION__ "] E2: Failed to open a project file '") + conf.prj + "'."; } conf.load(fin, 0); } if ( args.find("_Input") != args.end() ) { infilename = args["_Input"]; } // Override if parameters are specified by command line agruments. if ( args.find("_RandomSeed") != args.end() ) { conf.randomSeed = strtoul(args["_RandomSeed"].c_str(), 0, 0); } if ( args.find("_SizeX") != args.end() ) { conf.imageWidth = strtol(args["_SizeX"].c_str(), 0, 0); } if ( args.find("_SizeY") != args.end() ) { conf.imageHeight = strtol(args["_SizeY"].c_str(), 0, 0); } if ( args.find("_Dictionaries") != args.end() ) { strings dics = Text::split(args["_Dictionaries"], ';'); for (auto it = dics.begin(); it != dics.end(); it++) { std::ifstream fin; fin.open(it->c_str()); if ( fin.fail() ) { throw std::string("[" __FUNCTION__ "] E2: Failed to open an alias dictionary file '") + *it + "'."; } conf.load(fin, 0); } } { conf.numThreads.clear(); conf.numThreads.push_back(1); } { std::cout << boost::format("Image size: %d x %d\n") % conf.imageWidth % conf.imageHeight << std::endl; std::cout << "----- ----- ----- -----" << std::endl; } //----------------------------------------------------- // 1. Create GA Engine //----------------------------------------------------- Garnet::GPEnginePtr ga; { std::ofstream ftrace("trace.------.txt"); setTraceStream(&ftrace); ga = Garnet::GPEngine::create(conf); if ( !ga ) { throw "Invalid GA Engine Type."; } setTraceStream(0); ftrace.close(); } //----------------------------------------------------- // 2. Prepare trees as a generation. //----------------------------------------------------- Garnet::Generation generation; if ( !infilename.empty() ) { std::ifstream fin(infilename.c_str()); generation = Generation::createFromFile(fin); } else { std::vector<std::string> treeExps; treeExps = Text::split(args["_Trees"], ';'); for (auto it = treeExps.cbegin(); it != treeExps.cend(); it++) { Individual ind; ind.chromosome.push_back(Tree(*it)); generation.individuals.push_back(ind); } } //----------------------------------------------------- // 3. Evaluate. //----------------------------------------------------- ga->evaluate(generation); //----------------------------------------------------- // 4. Show result. //----------------------------------------------------- // 4.1 Show numbers. if ( args.find("_Number") != args.end() ) { std::cout << "\nResults:"; for (auto i = 0u; i < generation.individuals.size(); i++) { std::cout << "\n Tree " << i << " " << print_range( generation.individuals[i].fitness.cbegin(), generation.individuals[i].fitness.cend(), ", ", double_digit_putter(9, 6)); } std::cout << "\n----- ----- ----- -----" << std::endl; } // 4.2 Save tree images. if ( args.find("_TreeImage") != args.end() ) { std::vector<size_t> targets; targets.push_back(0); std::cout << "\nSave Tree Images."; ga->saveLastImages("", generation.individuals, targets); std::cout << "\n----- ----- ----- -----" << std::endl; } // 4.2 Display images. #if 0 if ( args.find("_Window") != args.end() ) { for (auto i = 0u; i < generation.individuals.size(); i++) { for (auto f = 0u; f < generation.individuals[i].fitness.size(); f++) { std::string key = (boost::format("Tree#%d, Fitness#%d") % i).str(); cv::namedWindow(key, CV_WINDOW_AUTOSIZE); cv::imshow(key, ga->saveLastImages()); } } cv::waitKey(); std::cout << "----- ----- ----- -----" << std::endl; } // A. Show information. { std::cout << boost::format("[%s] Trees to IVGraph %.6fs\n") % __FUNCTION__ % watches[0].get(); std::cout << boost::format("[%s] IVGraph to PPEGraph %.6fs\n") % __FUNCTION__ % watches[1].get(); std::cout << boost::format("[%s] PPEGraph to Script %.6fs\n") % __FUNCTION__ % watches[2].get(); std::cout << boost::format("[%s] Compilation %.6fs\n") % __FUNCTION__ % watches[3].get(); std::cout << boost::format("[%s] Execution %.6fs\n") % __FUNCTION__ % watches[4].get(); std::cout << boost::format("[%s] Number of Operators %.6u\n") % __FUNCTION__ % executable->getNumberOfOperators(); std::cout << boost::format("[%s] Number of Data %.6u\n") % __FUNCTION__ % executable->getNumberOfData(); std::cout << std::flush; } #endif }