00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <eyedbconfig.h>
00026
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030 #include <stdio.h>
00031
00032 #include <eyedbsm/eyedbsm.h>
00033 #include "BIdxBTree.h"
00034 #include "eyedbsm_p.h"
00035 #include <eyedbsm/xdr.h>
00036 #include <assert.h>
00037
00038 #include <eyedblib/machtypes.h>
00039 #include <eyedblib/rpc_lib.h>
00040 #include <eyedblib/thread.h>
00041
00042 #include <eyedblib/log.h>
00043
00044 namespace eyedbsm {
00045
00046 Status
00047 BIdx::readBTree(BTree &btree) const
00048 {
00049 Status s = objectRead(dbh, sizeof btree, &btree, &treeOid);
00050 if (!s)
00051 x2h_btree(btree);
00052 return s;
00053 }
00054
00055 Status
00056 BIdx::writeBTree(const BTree &hbtree) const
00057 {
00058 BTree xbtree;
00059 h2x_btree(xbtree, hbtree);
00060 return objectWrite(dbh, sizeof xbtree, &xbtree, &treeOid);
00061 }
00062
00063 Status
00064 BIdx::readKeyType(KeyType *&_keyType, unsigned int _nkeys,
00065 const Oid &keytype_oid) const
00066 {
00067 _keyType = new KeyType[_nkeys];
00068 Status s = objectRead(dbh, _nkeys*sizeof(KeyType), _keyType,
00069 &keytype_oid);
00070 if (!s)
00071 x2h_keytype(_keyType, _nkeys);
00072 return s;
00073 }
00074
00075 Status
00076 BIdx::createKeyType(const KeyType *hkeyType, unsigned int _nkeys,
00077 Oid *keytype_oid) const
00078 {
00079 KeyType *xkeyType = new KeyType[_nkeys];
00080 h2x_keytype(xkeyType, hkeyType, _nkeys);
00081 Status s = objectCreate(dbh, xkeyType, _nkeys*sizeof(KeyType),
00082 dspid, keytype_oid);
00083 delete [] xkeyType;
00084 return s;
00085 }
00086
00087 Status
00088 BIdx::readNode(Node *node, const Oid &node_oid) const
00089 {
00090 Status s = objectRead(dbh, Node::nodeSize(this), node, &node_oid);
00091 if (!s)
00092 x2h_node(*node, maxchildren);
00093 return s;
00094 }
00095
00096 static eyedblib::Mutex tmpnode_mt;
00097
00098 Status
00099 BIdx::writeNode(const Node *node, const Oid &node_oid) const
00100 {
00101 eyedblib::MutexLocker _(tmpnode_mt);
00102 h2x_node(*tmpnode, *node, maxchildren, True);
00103 return objectWrite(dbh, Node::nodeSize(this), tmpnode, &node_oid);
00104 }
00105
00106 Status
00107 BIdx::createNode(const Node *node, Oid *node_oid) const
00108 {
00109 eyedblib::MutexLocker _(tmpnode_mt);
00110 h2x_node(*tmpnode, *node, maxchildren, False);
00111 return objectCreate(dbh, tmpnode, Node::nodeSize(this), dspid, node_oid);
00112 }
00113
00114
00115 void x2h_keytype(Idx::KeyType *keytype, unsigned int nkeys)
00116 {
00117 static bool checked = false;
00118 if (!checked) {assert(sizeof(Idx::Type) == sizeof(eyedblib::int32)); checked = true;}
00119
00120 for (int i = 0; i < nkeys; i++) {
00121 keytype[i].type = (Idx::Type)x2h_u32(keytype[i].type);
00122 keytype[i].count = x2h_u32(keytype[i].count);
00123 keytype[i].offset = x2h_u32(keytype[i].offset);
00124 }
00125 }
00126
00127 void h2x_keytype(Idx::KeyType *xkeytype,
00128 const Idx::KeyType *hkeytype, unsigned int nkeys)
00129 {
00130 for (int i = 0; i < nkeys; i++) {
00131 xkeytype[i].type = (Idx::Type)h2x_u32(hkeytype[i].type);
00132 xkeytype[i].count = h2x_u32(hkeytype[i].count);
00133 xkeytype[i].offset = h2x_u32(hkeytype[i].offset);
00134 }
00135 }
00136
00137 void x2h_btree(BIdx::BTree &btree)
00138 {
00139 btree.idxtype = x2h_u32(btree.idxtype);
00140 btree.count = x2h_u32(btree.count);
00141 btree.dspid = x2h_16(btree.dspid);
00142 btree.version = x2h_u32(btree.version);
00143 btree.degree = x2h_u32(btree.degree);
00144 btree.maxchildren = x2h_u32(btree.maxchildren);
00145 btree.dataSize = x2h_u32(btree.dataSize);
00146 btree.keySize = x2h_u32(btree.keySize);
00147 for (int i = 0; i < BIDX_IMPL_HINTS_CNT; i++)
00148
00149 btree.impl_hints[i] = x2h_u32(btree.impl_hints[i]);
00150 x2h_oid(&btree.root, &btree.root);
00151 x2h_oid(&btree.type, &btree.type);
00152 }
00153
00154 void h2x_btree(BIdx::BTree &xbtree, const BIdx::BTree &hbtree)
00155 {
00156 xbtree.idxtype = h2x_u32(hbtree.idxtype);
00157 xbtree.count = h2x_u32(hbtree.count);
00158 xbtree.dspid = h2x_16(hbtree.dspid);
00159 xbtree.version = h2x_u32(hbtree.version);
00160 xbtree.degree = h2x_u32(hbtree.degree);
00161 xbtree.maxchildren = h2x_u32(hbtree.maxchildren);
00162 xbtree.dataSize = h2x_u32(hbtree.dataSize);
00163 xbtree.keySize = h2x_u32(hbtree.keySize);
00164 for (int i = 0; i < BIDX_IMPL_HINTS_CNT; i++)
00165
00166 xbtree.impl_hints[i] = h2x_u32(hbtree.impl_hints[i]);
00167 h2x_oid(&xbtree.root, &hbtree.root);
00168 h2x_oid(&xbtree.type, &hbtree.type);
00169 }
00170
00171 void x2h_node(BIdx::Node &node, unsigned int maxchildren)
00172 {
00173 node.leaf = x2h_32(node.leaf);
00174 node.n = x2h_u32(node.n);
00175 x2h_oid(&node.keys, &node.keys);
00176 x2h_oid(&node.data, &node.data);
00177
00178 for (int i = 0; i <= maxchildren; i++)
00179 x2h_oid(&node.c[i], &node.c[i]);
00180 }
00181
00182 void h2x_node(BIdx::Node &xnode, const BIdx::Node &hnode,
00183 unsigned int maxchildren, Boolean complete)
00184 {
00185 xnode.leaf = h2x_32(hnode.leaf);
00186 xnode.n = h2x_u32(hnode.n);
00187 h2x_oid(&xnode.keys, &hnode.keys);
00188 h2x_oid(&xnode.data, &hnode.data);
00189
00190 if (complete)
00191 for (int i = 0; i <= maxchildren; i++)
00192 h2x_oid(&xnode.c[i], &hnode.c[i]);
00193 }
00194 }