gen.cpp
来自「此文件包含了在linux下实现tpr-tree索引的源代码」· C++ 代码 · 共 669 行 · 第 1/2 页
CPP
669 行
//--------------------------------------------------------------------// gen.cpp// ------- // Implemented: TGenerator//// Moving point workload generator v 1.1 // Copyright(c) 1999-2001, Aalborg University//#include "gen.h"#include "random.h"#include "mor_opcodes.h"#include <assert.h>#include <math.h>#include <stdlib.h>#include <string.h>#include <fstream.h>#include <stdio.h>#include <limits.h>// #include <macros.h>bool gen_binary = true;static char op_code; static inline double sign(double n) { return n < 0.0 ? -1 : 1;} template <class T>static inline void swap (T& a, T& b){ T temp = a; a = b; b = temp;}//------------------------------------------------TGenerator::TGenerator(TParams& params){ char speedn[10] = "Speed"; char altituden[15] = "Altitude"; updateInterval = params.getFloat("UpdateInterval"); points = params.getInt ("Points"); hubs = params.getInt ("Hubs"); threeD = params.getInt ("ThreeD"); spaceX = params.getFloat("SpaceX"); spaceY = threeD == 1 ? 0 : params.getFloat("SpaceY"); spaceZ = threeD == 3 ? params.getFloat("SpaceZ") : 0; qQuantity = params.getInt("QQuantity"); qWindow = params.getFloat("QWindow"); qSize = params.getFloat("QSize"); qTSize = params.getFloat("QTSize"); qType1 = params.getFloat("QType1"); qType2 = params.getFloat("QType2"); qType3 = params.getFloat("QType2"); char* tn = params.getString("Name"); while (*tn == ' ') tn++; strcpy (table_name, tn); strcat (table_name, ".dat"); if (params.Error()) { cout << "Aborting." << endl; exit(1); } TMovingPoint::Dims = threeD ? threeD : 2; params.Silent(); newObjects = params.getFloat("NewObjects"); if (params.Error()) { newObjects = 0; params.ResetError(); } totalOp = params.getInt("TotalOp"); if (params.Error()) { totalOp = 0; params.ResetError(); params.Silent(false); total = params.getFloat("Total"); params.Silent(); } qIntervalOp = params.getInt("QIntervalOp"); if (params.Error()) { qIntervalOp = 0; params.ResetError(); params.Silent(false); qInterval = params.getFloat("QInterval"); params.Silent(); } load = params.getInt("Load"); if (params.Error()) { load = 1; params.ResetError(); } for (numObjTypes = 0; !params.Error() && numObjTypes < GEN_MAXSPEEDS; numObjTypes++) { sprintf (speedn + 5, "%i", numObjTypes + 1); speeds[numObjTypes] = params.getFloat(speedn); } numObjTypes--; if (!numObjTypes) { cout << "GENERATOR Error: Possible speeds are not specified." << endl << "Aborting." << endl; exit(1); } params.Silent(false); if (load == 1) { datf.open(table_name); if (!datf) { cout << "GENERATOR Error: Unable to open output file " << table_name << "." << endl; exit (1); } } strcpy(table_name + strlen(table_name) - 4, ".sql"); sqlf.open(table_name); if (!sqlf) { cout << "GENERATOR Error: Unable to open output file " << table_name << "." << endl; exit (1); } // Compute how often new objects are introduced noIntOp = totalOp && newObjects > 0.0 ? (totalOp - points) / (points * newObjects) : INT_MAX; pool = new TPool(points); if (hubs) cities = new THub[hubs];}//------------------------------------------------TGenerator::~TGenerator (){ datf.close(); sqlf.close(); delete pool; delete[] cities;}//------------------------------------------------static void OutTime (ostream& os, float time){ if (gen_binary) { op_code = OPC_TX; os.write(&op_code, 1); os.write(reinterpret_cast<char*>(&time), sizeof(float)); } else os << "t " << time << endl;}//------------------------------------------------static void AdvTime (ostream& os, float time){ if (gen_binary) { op_code = time == 1 ? OPC_A : OPC_AX; os.write(&op_code, 1); if (time != 1) os.write(reinterpret_cast<char*>(&time), sizeof(float)); } else { os << "a"; if (time != 1) os << ' ' << time; os << endl; }}//------------------------------------------------static void OutStats (ostream& os, float time){ if (gen_binary) { cout << "GENERATOR Warning: Out Stats are currently supported only in text mode. Skipping." << endl; return; } os << "output current time is " << time << endl; os << "stats out" << endl;}//------------------------------------------------static void OutInsert (ostream& os, TMovingPoint& point){ if (gen_binary) { op_code = OPC_I; os.write(&op_code, 1); point.write(os); } else os << "i " << point << endl;}//------------------------------------------------static void OutSelect (ostream& os, float* beg, float* end, float tbeg, float tend, float* v = NULL){ if (gen_binary) { op_code = v ? OPC_S3 : OPC_S; os.write(&op_code, 1); os.write(reinterpret_cast<char*>(beg), TMovingPoint::Dims * sizeof(float)); os.write(reinterpret_cast<char*>(end), TMovingPoint::Dims * sizeof(float)); os.write(reinterpret_cast<char*>(&tbeg), sizeof(float)); os.write(reinterpret_cast<char*>(&tend), sizeof(float)); if (v) os.write(reinterpret_cast<char*>(v), TMovingPoint::Dims * sizeof(float)); } else { int i; os << "s &("; for (i = 0; i < TMovingPoint::Dims; i++) os << beg[i] << ','; for (i = 0; i < TMovingPoint::Dims; i++) os << end[i] << ','; os << tbeg << ',' << tend; if (v) for (i = 0; i < TMovingPoint::Dims; i++) os << ',' << v[i]; os << ')' << endl; }}//------------------------------------------------static void OutDelete (ostream& os, TMovingPoint& point){ if (gen_binary) { op_code = OPC_D; os.write(&op_code, 1); point.write(os); } else os << "d " << point << endl;}//------------------------------------------------void TGenerator::OutLoadPoint (TMovingPoint& key){ if (load == 1) key.write(datf); else OutInsert(sqlf, key); }//------------------------------------------------void TGenerator::Generate (){ int logOps, qOp; float noOp; int perc; float t, q; randomize(); if (gen_binary) sqlf << "bin" << endl; cout << "Generating the initial load file. " << endl; tref = 0; GenerateLoad(); cout << "Generating the updates and queries. " << endl; cout << "Progress : 0%"; OutTime (sqlf, 0); for (logOps = 0, perc = 0, t = 0, q = qInterval, qOp = qIntervalOp, noOp = noIntOp; totalOp ? logOps < totalOp : t < total; logOps++) { if (totalOp && logOps * 100 / totalOp > perc || !totalOp && (int) (t * 100 / total) > perc) { perc = totalOp ? logOps * 100 / totalOp : (int) (t * 100 / total); cout << "\rProgress : " << perc << "%"; cout.flush(); } while (!qIntervalOp && (q <= pool->Top()->utime || !updateInterval) && q <= total || qIntervalOp && logOps == qOp) { if (!qIntervalOp) { t = q - tref; OutTime (sqlf, t + tref); } GenerateQueries(t + tref); q += qInterval; qOp += qIntervalOp; if (!updateInterval || qIntervalOp) break; // We want to show the progress } if (!updateInterval) continue; if (t + tref < pool->Top()->utime) { t = pool->Top()->utime - tref; OutTime (sqlf, t + tref); } UpdatePoint (t + tref, logOps == (long) noOp); if (logOps == (long) noOp) noOp += noIntOp; } cout << "\rProgress : 100%" << endl; cout << "Number of updates performed: " << logOps << endl; if (gen_binary) { op_code = OPC_EOF; sqlf.write(&op_code, 1); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?