Changeset 19146 for trunk/FACT++/src


Ignore:
Timestamp:
08/10/18 22:54:57 (6 years ago)
Author:
tbretz
Message:
Added support for a user defined column with a constant number, allow to extract that number from the file name; fixed a problem with deletion (when the vector gets re-allocated - the memory must not be re-allocated; added support for leaves of arrays of basic types analogeously to arrays in fits tables.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/root2sql.cc

    r19139 r19146  
    6969        ("duplicate",      vars<string>(),            "Specifies an assignment_list for an 'ON DUPLICATE KEY UPDATE' expression")
    7070        ("ignore-errors",  po_switch(),               "Adds the IGNORE keyword to the INSERT query (turns errors into warnings, ignores rows with errors)")
     71        ("const.*",        var<string>(),             "Insert a constant number into the given column (--const.mycolumn=5). A special case is `/..../N/`")
    7172        ;
    7273
     
    183184        "are marked as (-ignored-).\n"
    184185        "\n"
     186        "A constant value for the given file can be inserted by using the --const directive. "
     187        "For example --const.mycolumn=42 would insert 42 into a column called mycolumn. "
     188        "The column is created as INT UNSIGNED as default which can be altered by "
     189        "--sql-type. A special case is a value of the form `/regex/N/`. Here, the given "
     190        "regular expression is applied to the filename and N specifies the N-th "
     191        "sub-sequence which matches. To debug what matches, verbosity can be set to 3.\n"
     192        "\n"
    185193        "If a query failed, the query is printed to stderr together with the error message. "
    186194        "For the main INSERT query, this is only true if the verbosity level is at least 2 "
     
    198206{
    199207    kNone = 0,
     208    kConst,
    200209    kFloat,
    201210    kDouble,
     
    222231struct Container
    223232{
     233    static map<void*, size_t> counter;
     234
    224235    string branch; // branch name
    225236    string column; // column name
    226237    BasicType_t type;
     238    size_t num;
    227239    void *ptr;
    228240
    229     Container(const string &b, const string &c, const BasicType_t &t) : branch(b), column(c), type(t), ptr(0)
     241    Container(const string &b, const string &c, const BasicType_t &t, const size_t &n) : branch(b), column(c), type(t), num(n), ptr(0)
    230242    {
    231243        switch (t)
    232244        {
    233         case kFloat:  ptr = new Float_t;   break;
    234         case kDouble: ptr = new Double_t;  break;
    235         case kInt16:  ptr = new Short_t;   break;
    236         case kUInt16: ptr = new UShort_t;  break;
    237         case kInt32:  ptr = new Int_t;     break;
    238         case kUInt32: ptr = new UInt_t;    break;
    239         case kInt64:  ptr = new Long64_t;  break;
    240         case kUInt64: ptr = new ULong64_t; break;
     245        case kFloat:  ptr = new Float_t[n];   break;
     246        case kDouble: ptr = new Double_t[n];  break;
     247        case kInt16:  ptr = new Short_t[n];   break;
     248        case kUInt16: ptr = new UShort_t[n];  break;
     249        case kInt32:  ptr = new Int_t[n];     break;
     250        case kUInt32: ptr = new UInt_t[n];    break;
     251        case kInt64:  ptr = new Long64_t[n];  break;
     252        case kUInt64: ptr = new ULong64_t[n]; break;
     253        case kConst:
    241254        case kNone:
    242255            break;
    243256        }
    244     }
     257        counter[ptr]++;
     258    }
     259    Container(const string &c, const string &value) : branch(value), column(c), type(kConst), num(1), ptr(0)
     260    {
     261    }
     262
     263    Container(const Container &c) : branch(c.branch), column(c.column), type(c.type), num(c.num), ptr(c.ptr)
     264    {
     265        counter[ptr]++;
     266    }
     267
    245268    ~Container()
    246269    {
    247         //::operator delete(ptr); // It seems root is deleting it already
    248     }
    249 
    250     string fmt() const
     270        counter[ptr]--;
     271        if (counter[ptr]==0)
     272            ::operator delete[](ptr); // It seems root is deleting it already
     273    }
     274
     275    string fmt(const size_t &index) const
    251276    {
    252277        ostringstream str;
     
    254279        switch (type)
    255280        {
    256         case kFloat:   str << setprecision(8) << *reinterpret_cast<Float_t*>(ptr);  break;
    257         case kDouble:  str << setprecision(16) << *reinterpret_cast<Double_t*>(ptr); break;
    258         case kInt16:   str << *reinterpret_cast<Short_t*>(ptr); break;
    259         case kUInt16:  str << *reinterpret_cast<UShort_t*>(ptr); break;
    260         case kInt32:   str << *reinterpret_cast<Int_t*>(ptr); break;
    261         case kUInt32:  str << *reinterpret_cast<UInt_t*>(ptr); break;
    262         case kInt64:   str << *reinterpret_cast<Long64_t*>(ptr); break;
    263         case kUInt64:  str << *reinterpret_cast<ULong64_t*>(ptr); break;
     281        case kFloat:   str << setprecision(8) << reinterpret_cast<Float_t*>(ptr)[index];  break;
     282        case kDouble:  str << setprecision(16) << reinterpret_cast<Double_t*>(ptr)[index]; break;
     283        case kInt16:   str << reinterpret_cast<Short_t*>(ptr)[index]; break;
     284        case kUInt16:  str << reinterpret_cast<UShort_t*>(ptr)[index]; break;
     285        case kInt32:   str << reinterpret_cast<Int_t*>(ptr)[index]; break;
     286        case kUInt32:  str << reinterpret_cast<UInt_t*>(ptr)[index]; break;
     287        case kInt64:   str << reinterpret_cast<Long64_t*>(ptr)[index]; break;
     288        case kUInt64:  str << reinterpret_cast<ULong64_t*>(ptr)[index]; break;
     289        case kConst:   str << branch; break;
    264290        case kNone:
    265291            break;
     
    272298    }
    273299};
     300
     301map<void*, size_t> Container::counter;
    274302
    275303void ErrorHandlerAll(Int_t level, Bool_t abort, const char *location, const char *msg)
     
    387415    vector<Container> vec;
    388416
     417    const auto fixed = conf.GetWildcardOptions("const.*");
     418
     419    for (auto it=fixed.cbegin(); it!=fixed.cend(); it++)
     420    {
     421        const string name = it->substr(6);
     422        string val  = conf.Get<string>(*it);
     423
     424        boost::smatch match;
     425        if (boost::regex_match(val, match, boost::regex("\\/(.+)\\/([0-9]*)\\/")))
     426        {
     427            string s = match[1];
     428            size_t r = atoi(match[2].str().c_str());
     429
     430            if (boost::regex_search(file, match, boost::regex(s)))
     431            {
     432                if (verbose>2)
     433                    for (size_t i=0; i<match.size(); i++)
     434                        cout << "Regex match " << setw(2) << i << ": `" << match[i] << "`" << endl;
     435
     436                val = match[r];
     437            }
     438        }
     439
     440        if (verbose>2)
     441            cout << "\n" << val << " [-const-]";
     442        if (verbose>1)
     443            cout << " (" << name << ")";
     444
     445        string sqltype = "INT UNSIGNED";
     446
     447        for (auto m=sqltypes.cbegin(); m!=sqltypes.cend(); m++)
     448            if (m->first==name)
     449                sqltype = m->second;
     450
     451        if (!vec.empty())
     452            query += ",\n";
     453        query += "   `"+name+"` "+sqltype+" NOT NULL COMMENT '--user--'";
     454
     455        vec.emplace_back(name, val);
     456    }
     457
     458    const size_t nvec = vec.size();
    389459
    390460    TIter Next(leaves);
     
    395465
    396466        if (verbose>2)
    397             cout << '\n' << o->GetName() << " [" << L->GetTypeName() << "]";
     467            cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}";
     468
     469        if (L->GetLenStatic()=!L->GetLen())
     470        {
     471            if (verbose>2)
     472                cout << " (-skipped-)";
     473            continue;
     474        }
    398475
    399476        string name = o->GetName();
    400 
    401477
    402478        bool found = false;
     
    425501
    426502        if (verbose==2)
    427             cout << '\n' << o->GetName() << " [" << L->GetTypeName() << "]";
     503            cout << '\n' << L->GetTitle() << " {" << L->GetTypeName() << "}";
    428504
    429505        for (auto m=mymap.cbegin(); m!=mymap.cend(); m++)
     
    441517        if (!vec.empty())
    442518            query += ",\n";
    443         query += "   `"+name+"` "+sqltype+" NOT NULL COMMENT '"+o->GetName()+"'";
    444 
    445         vec.emplace_back(o->GetName(), name, it->second.first);
    446         T->SetBranchAddress(o->GetName(), vec.back().ptr);
     519
     520        const size_t N = L->GetLenStatic();
     521        for (size_t i=0; i<N; i++)
     522        {
     523            query += "   `"+name;
     524            if (N>1)
     525                query += "["+to_string(i)+"]";
     526            query += "` "+sqltype+" NOT NULL COMMENT '"+o->GetTitle()+"'";
     527            if (N>1 && i!=N-1)
     528                query += ",\n";
     529        }
     530
     531        vec.emplace_back(o->GetTitle(), name, it->second.first, L->GetLenStatic());
     532        T->SetBranchAddress(o->GetTitle(), vec.back().ptr);
    447533    }
    448534
     
    450536        cout << "\n\n";
    451537    if (verbose>0)
    452         cout << vec.size() << " leaves setup for reading." << endl;
     538    {
     539        if (nvec>0)
     540            cout << nvec << " constant value column(s) configured." << endl;
     541        cout << vec.size()-nvec << " leaf/leaves setup for reading." << endl;
     542    }
    453543
    454544    UInt_t datatype = 0;
     
    458548    T->SetBranchStatus("*", 0);
    459549    for (auto c=vec.cbegin(); c!=vec.cend(); c++)
    460         T->SetBranchStatus(c->branch.c_str(), 1);
     550        if (c->type!=kConst)
     551            T->SetBranchStatus(c->branch.c_str(), 1);
    461552
    462553    if (has_datatype)
     
    467558    }
    468559
     560
    469561    // -------------------------------------------------------------------------
    470562    // Checking for database connection
     
    513605
    514606    if (!primary.empty())
    515         query += ",\n   PRIMARY KEY USING BTREE (`"+boost::algorithm::join(primary, "`, `")+"`)\n";
     607        query += ",\n   PRIMARY KEY USING BTREE (`"+boost::algorithm::join(primary, "`, `")+"`)";
    516608
    517609    query +=
    518         ")\n"
     610        "\n)\n"
    519611        "DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci\n";
    520612    if (!engine.empty())
     
    578670        if (c!=vec.cbegin())
    579671            query += ",\n";
    580         query += "   `"+c->column+"`";
     672
     673        const size_t N = c->num;
     674        for (size_t i=0; i<N; i++)
     675        {
     676            if (N==1)
     677                query += "   `"+c->column+"`";
     678            else
     679                query += "   `"+c->column+"["+to_string(i)+"]`";
     680
     681            if (N>1 && i!=N-1)
     682                query += ",\n";
     683        }
    581684    }
    582685
     
    604707                query += ",\n";
    605708
    606             query += "   "+c->fmt();
    607 
    608             if (print_insert)
    609                 query += " /* "+c->column+" -> "+c->branch+" */";
     709            const size_t N = c->num;
     710            for (size_t i=0; i<N; i++)
     711            {
     712                query += "   "+c->fmt(i);
     713
     714                if (print_insert && i==0)
     715                    query += " /* "+c->column+" -> "+c->branch+" */";
     716
     717                if (N>1 && i!=N-1)
     718                    query += ",\n";
     719            }
    610720        }
    611721        query += "\n)";
Note: See TracChangeset for help on using the changeset viewer.