Validator.hh

00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 #ifndef avro_Validating_hh__
00020 #define avro_Validating_hh__
00021 
00022 #include <boost/noncopyable.hpp>
00023 #include <vector>
00024 #include <stdint.h>
00025 
00026 #include "Types.hh"
00027 #include "ValidSchema.hh"
00028 
00029 namespace avro {
00030 
00031 class NullValidator : private boost::noncopyable
00032 {
00033   public:
00034 
00035     explicit NullValidator(const ValidSchema &schema) {}
00036     NullValidator() {}
00037 
00038     void setCount(int64_t val) {}
00039 
00040     bool typeIsExpected(Type type) const {
00041         return true;
00042     }
00043 
00044     Type nextTypeExpected() const {
00045         return AVRO_UNKNOWN;
00046     }
00047 
00048     int nextSizeExpected() const {
00049         return 0;
00050     }
00051 
00052     bool getCurrentRecordName(std::string &name) const {
00053         return true;
00054     }
00055 
00056     bool getNextFieldName(std::string &name) const {
00057         return true;
00058     }
00059 
00060     void checkTypeExpected(Type type) { }
00061     void checkFixedSizeExpected(int size) { }
00062 
00063 
00064 };
00065 
00071 
00072 class Validator : private boost::noncopyable
00073 {
00074   public:
00075 
00076     explicit Validator(const ValidSchema &schema);
00077 
00078     void setCount(int64_t val);
00079 
00080     bool typeIsExpected(Type type) const {
00081         return (expectedTypesFlag_ & typeToFlag(type));
00082     }
00083 
00084     Type nextTypeExpected() const {
00085         return nextType_;
00086     }
00087 
00088     int nextSizeExpected() const;
00089 
00090     bool getCurrentRecordName(std::string &name) const;
00091     bool getNextFieldName(std::string &name) const;
00092 
00093     void checkTypeExpected(Type type) {
00094         if(! typeIsExpected(type)) {
00095             throw Exception(
00096                 boost::format("Type %1% does not match schema %2%") 
00097                     % type % nextType_
00098             );
00099         }
00100         advance();
00101     }
00102 
00103     void checkFixedSizeExpected(int size) { 
00104         if( nextSizeExpected() != size) {
00105             throw Exception(
00106                 boost::format("Wrong size for fixed, got %1%, expected %2%") 
00107                     % size % nextSizeExpected()
00108             );
00109         }
00110         checkTypeExpected(AVRO_FIXED);
00111     }
00112 
00113   private:
00114 
00115     typedef uint32_t flag_t;
00116 
00117     flag_t typeToFlag(Type type) const {
00118         flag_t flag = (1L << type);
00119         return flag;
00120     }
00121 
00122     void setupOperation(const NodePtr &node);
00123 
00124     void setWaitingForCount();
00125 
00126     void advance();
00127     void doAdvance();
00128 
00129     void enumAdvance();
00130     bool countingSetup();
00131     void countingAdvance();
00132     void unionAdvance();
00133     void fixedAdvance();
00134 
00135     void setupFlag(Type type);
00136 
00137     const ValidSchema schema_;
00138 
00139     Type nextType_; 
00140     flag_t expectedTypesFlag_;
00141     bool compoundStarted_;
00142     bool waitingForCount_;
00143     int64_t count_;
00144 
00145     struct CompoundType {
00146         explicit CompoundType(const NodePtr &n) :
00147             node(n), pos(0)
00148         {}
00149         NodePtr node;  
00150         size_t  pos;   
00151     };
00152 
00153     std::vector<CompoundType> compoundStack_;
00154     std::vector<size_t> counters_;
00155 
00156 };
00157 
00158 } // namespace avro
00159 
00160 #endif