1 /* 2 * Copyright 2006-2008 The FLWOR Foundation. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 #ifndef ZORBA_BINARY_SERIALIZATION_BINARY_ARCHIVER 18 #define ZORBA_BINARY_SERIALIZATION_BINARY_ARCHIVER 19 20 #include <iostream> 21 22 #include "zorbaserialization/archiver.h" 23 24 #include "zorbautils/hashmap_zstring.h" 25 26 #include "store/api/shared_types.h" 27 28 namespace zorba 29 { 30 31 namespace serialization 32 { 33 34 /******************************************************************************* 35 theStrings : 36 ------------ 37 Stores the set of distinct strings that are found in theValue data member of 38 all the fields. During serialization, theStrings is populated after the full 39 tree of fields has been constructed. Then, the order in which the strings are 40 going to be written to disk is determined and the strings are written 41 consecutively according to this order. theDiskPos data member reflects this 42 order: the 1st string in disk will have theDiskPos == 1, the 2nd string will 43 have theDiskPos == 2, etc. 44 45 theStringPool : 46 ------------- 47 Maps a string to its position within theStrings vector. It is used during 48 serialization only, to enforce the uniqueness of the strings in theStrings. 49 50 theOrderedStrings : 51 ------------------- 52 This vector stores an ordering of theStrings, based on their use counts. 53 Specifically, theOrderedStrings[0] points to the string with the highest 54 string count, theOrderedStrings[1] points to the string with the 2nd highest 55 string count, etc. The string are written to disk in this order. 56 57 ********************************************************************************/ 58 class BinArchiver : public Archiver 59 { 60 protected: 61 typedef struct 62 { 63 zstring str; 64 bool binary; 65 csize count; 66 csize theDiskPos;//1 based 67 } StringInfo; 68 69 ZSTRING_HASH_MAP(csize, StringPoolMap); 70 71 protected: 72 std::istream * is; 73 74 std::ostream * os; 75 76 StringPoolMap theStringPool; 77 78 std::vector<StringInfo> theStrings; 79 std::vector<csize> theOrderedStrings; 80 csize theFirstBinaryString; 81 82 unsigned int theLastId; 83 unsigned char theCurrentByte; 84 unsigned char theBitfill; 85 86 unsigned char * theBuffer; 87 unsigned char * theCurrentBytePtr; 88 size_t size_read; 89 90 #ifdef ZORBA_PLAN_SERIALIZER_STATISTICS 91 unsigned int bytes_saved; 92 unsigned int objects_saved; 93 unsigned int nr_ptrs; 94 unsigned int strings_saved; 95 #endif 96 97 public: 98 BinArchiver(std::istream* is); 99 100 BinArchiver(std::ostream* os); 101 102 virtual ~BinArchiver(); 103 104 void read_next_compound_field_impl( 105 bool is_class, 106 ArchiveFieldKind& field_kind, 107 TypeCode& type, 108 int& id, 109 int& referencing); 110 111 void read_next_simple_temp_field_impl(TypeCode type, void* obj); 112 113 void read_next_simple_ptr_field_impl(TypeCode type, void** obj); 114 115 void read_end_current_level_impl(); 116 117 void serialize_out(); 118 119 protected: 120 //writing 121 void serialize_out_string_pool(); 122 123 void collect_strings(archive_field* parent_field); 124 125 int add_to_string_pool(const zstring& str); 126 127 void write_string(const StringInfo& info); 128 129 void serialize_compound_fields(archive_field* parent_field); 130 131 void write_int64(int64_t intval); 132 133 void write_uint64(uint64_t intval); 134 135 void write_int32(int32_t intval); 136 137 void write_uint32(uint32_t intval); 138 139 void write_int_exp(unsigned int intval); 140 141 void write_int_exp2(unsigned int intval); 142 143 void write_enum(unsigned int intval); 144 145 void write_bits(unsigned int value, unsigned int bits); 146 147 void write_bit(unsigned char bit); 148 149 //reading 150 void read_string_pool(); 151 152 void read_string(zstring& str); 153 154 void read_binary_string(zstring& str); 155 156 int64_t read_int64(); 157 158 uint64_t read_uint64(); 159 160 int32_t read_int32(); 161 162 uint32_t read_uint32(); 163 164 unsigned int read_int_exp(); 165 166 unsigned int read_int_exp2(); 167 168 unsigned int read_enum(); 169 170 unsigned char read_bit(); 171 172 unsigned int read_bits(unsigned int bits); 173 }; 174 175 }} 176 177 #endif 178 /* vim:set et sw=2 ts=2: */ 179