Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cursor.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. **
  3. ** Copyright (C) 1995-2011 Opera Software ASA. All rights reserved.
  4. **
  5. ** This file is part of the Opera web browser.
  6. ** It may not be distributed under any circumstances.
  7. */
  8. #ifndef CURSOR_H
  9. #define CURSOR_H
  10. #include "modules/search_engine/BlockStorage.h"
  11. #define MAX_FIELD_NAME 8
  12. class BSCursor;
  13. // workaround for MSVC 6 error C2893 (bodies of the template methods cannot be defined separately from declaration)
  14. class FieldBase
  15. {
  16. protected:
  17. BSCursor *cursor;
  18. int size;
  19. BOOL variable_length;
  20. FieldBase *next;
  21. char name[MAX_FIELD_NAME]; /* ARRAY OK 2010-09-24 roarl */
  22. public:
  23. FieldBase()
  24. {
  25. cursor = NULL;
  26. size = 0;
  27. variable_length = FALSE;
  28. next = NULL;
  29. name[0] = 0;
  30. }
  31. };
  32. class ConstField;
  33. class Field;
  34. /**
  35. * @brief BSCursor built on top of BlockStorage.
  36. * @author Pavel Studeny <pavels@opera.com>
  37. *
  38. * Cursor provides an access to structured data on top of BlockStorage.
  39. * It manipulates with data blocks from BlockStorage like rows of a table.
  40. * You can setup named fields of static/variable length and acces them separately.
  41. */
  42. class BSCursor : public NonCopyable
  43. {
  44. public:
  45. typedef OpFileLength RowID;
  46. /**
  47. * empty constructor, use SetStorage later
  48. * @param autocreate_transaction automatically enter a transaction when creating new rows
  49. * or modifying a size of the existing ones
  50. */
  51. BSCursor(BOOL autocreate_transaction = FALSE);
  52. /**
  53. * creates BSCursor on given table
  54. * @param table opened BlockStorage
  55. * @param autocreate_transaction automatically enter a transaction when creating new rows
  56. * or modifying a size of the existing ones
  57. */
  58. BSCursor(BlockStorage *table, BOOL autocreate_transaction = FALSE);
  59. /**
  60. * doesn't Flush unsaved data
  61. */
  62. ~BSCursor(void);
  63. /**
  64. * sets a table if it hadn't been already set
  65. */
  66. void SetStorage(BlockStorage *table);
  67. /**
  68. * create a new field
  69. * @param name name limited to MAX_FIELD_NAME - 1 characters (rest is ignored)
  70. * @param size size of static field, 0 for variable length field
  71. */
  72. CHECK_RESULT(OP_STATUS AddField(const char *name, int size));
  73. /**
  74. * reserve internal buffer to avoid future resizing, doesn't decrease the size
  75. * @param size new size of internal buffer
  76. */
  77. CHECK_RESULT(OP_STATUS Reserve(int size));
  78. /**
  79. * @return true if the current row has been modified
  80. */
  81. BOOL Modified() const
  82. {
  83. return modified;
  84. }
  85. /**
  86. * create a new row, it will have its ID after first Flush
  87. */
  88. CHECK_RESULT(OP_STATUS Create(void));
  89. /**
  90. * select a row with given id, flushes modified data; the first RowID is 1
  91. */
  92. CHECK_RESULT(OP_STATUS Goto(RowID id));
  93. /**
  94. * delete current row, Cursor doesn't contatin valid data after successfull operation
  95. */
  96. CHECK_RESULT(OP_STATUS Delete(void));
  97. /**
  98. * write modified data to disk
  99. * @return current ID
  100. */
  101. CHECK_RESULT(OP_STATUS Flush(void));
  102. /**
  103. * get current size of internal buffer
  104. */
  105. int Capacity(void) const
  106. {
  107. return bufsize;
  108. }
  109. /**
  110. * @return number of valid elements
  111. */
  112. int Size(void) const
  113. {
  114. return datasize;
  115. }
  116. /**
  117. * @return current ID
  118. */
  119. RowID GetID(void) const
  120. {
  121. return filepos / table->GetBlockSize();
  122. }
  123. /**
  124. * return a pointer to data
  125. */
  126. const unsigned char *CPtr(void) const
  127. {
  128. return buf;
  129. }
  130. /**
  131. * @param name name of previously created field
  132. * @return read-only field
  133. */
  134. const ConstField &GetField(const char *name) const;
  135. /**
  136. * @param pos position of previously created field counted from 0
  137. * @return read-only field
  138. */
  139. const ConstField &GetField(int pos) const;
  140. /**
  141. * @param name name of previously created field
  142. * @return read/write field
  143. */
  144. Field &GetField(const char *name);
  145. /**
  146. * @param pos position of previously created field counted from 0
  147. * @return read/write field
  148. */
  149. Field &GetField(int pos);
  150. /** wrapper for GetField */
  151. const ConstField &operator[](const char *name) const {return GetField(name);}
  152. /** wrapper for GetField */
  153. const ConstField &operator[](int pos) const {return GetField(pos);}
  154. /** wrapper for GetField */
  155. Field &operator[](const char *name) {return GetField(name);}
  156. /** wrapper for GetField */
  157. Field &operator[](int pos) {return GetField(pos);}
  158. protected:
  159. friend class ConstField;
  160. friend class Field;
  161. CHECK_RESULT(OP_STATUS ModifyField(Field *field, const unsigned char *value, int new_size));
  162. unsigned char *GetPos(const ConstField *field) const;
  163. BlockStorage *table;
  164. unsigned char *buf;
  165. int bufsize;
  166. int datasize;
  167. BOOL autocreate_transaction;
  168. OpFileLength filepos;
  169. ConstField *fields;
  170. BOOL modified;
  171. ConstField *current_field;
  172. unsigned char *current_pos;
  173. FieldBase null_field;
  174. };
  175. /** @brief Read-only BSCursor field. */
  176. class ConstField : public FieldBase
  177. {
  178. protected:
  179. friend class BSCursor;
  180. ConstField(int size, const char *name, BSCursor *cursor)
  181. {
  182. this->cursor = cursor;
  183. this->size = size;
  184. this->variable_length = size == 0;
  185. this->next = NULL;
  186. op_strncpy(this->name, name, MAX_FIELD_NAME);
  187. this->name[MAX_FIELD_NAME - 1] = 0;
  188. }
  189. public:
  190. ConstField(void)
  191. {
  192. cursor = NULL;
  193. }
  194. /**
  195. * @return size of current field, doesn't count in the ending 0 for strings
  196. */
  197. int GetSize(void) const
  198. {
  199. return size;
  200. }
  201. /**
  202. * @return whether this field has a variable length
  203. */
  204. BOOL IsVariableLength(void) const
  205. {
  206. return variable_length;
  207. }
  208. /**
  209. * get value of a static-sized field
  210. * @param val variable to store the value
  211. * @return reference to val
  212. */
  213. template<typename T>T &GetValue(T *val) const
  214. {
  215. int maxlen;
  216. if (variable_length || cursor == NULL) // variable length fields shouldn't use this method
  217. {
  218. *val = 0;
  219. return *val;
  220. }
  221. maxlen = (int)sizeof(T) > size ? size : sizeof(T);
  222. if ((int)sizeof(T) > maxlen)
  223. op_memset(val, 0, sizeof(T));
  224. #ifndef OPERA_BIG_ENDIAN
  225. op_memcpy(val, cursor->GetPos(this), maxlen);
  226. #else
  227. op_memcpy(((char *)val) + sizeof(T) - maxlen, (char *)cursor->GetPos(this) + size - maxlen, maxlen);
  228. #endif
  229. return *val;
  230. }
  231. /**
  232. * get value of a field as a string, maxlen must be one character longer than GetSize() should the string include the ending 0
  233. * @param val buffer at least maxlen characters long
  234. * @param maxlen length val or 0 not to check
  235. * @return val
  236. */
  237. char *GetStringValue(char *val, int maxlen = 0) const
  238. {
  239. int copy_len;
  240. if (cursor == NULL)
  241. {
  242. val[0] = 0;
  243. return val;
  244. }
  245. copy_len = maxlen;
  246. if (copy_len > size || copy_len <= 0)
  247. copy_len = size;
  248. if (variable_length)
  249. op_strncpy(val, (char *)(cursor->GetPos(this) + 4), copy_len);
  250. else op_strncpy(val, (char *)cursor->GetPos(this), copy_len);
  251. if ((size < maxlen || size == 0 || maxlen == 0) && (size == 0 || val[size - 1] != 0))
  252. val[size] = 0;
  253. return val;
  254. }
  255. /**
  256. * get value of a field as a string, maxlen must be one character longer than GetSize() should the string include the ending 0
  257. * @param val buffer at least maxlen characters long
  258. * @param maxlen length val or 0 not to check
  259. * @return val
  260. */
  261. uni_char *GetStringValue(uni_char *val, int maxlen = 0) const
  262. {
  263. int copy_len;
  264. if (cursor == NULL)
  265. {
  266. val[0] = 0;
  267. return val;
  268. }
  269. copy_len = maxlen * 2;
  270. if (copy_len > size || copy_len <= 0)
  271. copy_len = size + 1;
  272. if (variable_length)
  273. op_memcpy(val, cursor->GetPos(this) + 4, copy_len);
  274. else op_memcpy(val, cursor->GetPos(this), copy_len);
  275. if ((size < maxlen || size <= 1 || maxlen == 0) && (size <= 1 || val[size / 2 - 1] != 0))
  276. val[size / 2] = 0;
  277. return val;
  278. }
  279. /**
  280. * get value of a filed as OpString8
  281. */
  282. CHECK_RESULT(OP_STATUS GetStringValue(OpString8& val) const)
  283. {
  284. char* char_val;
  285. if ((char_val = val.Reserve(size)) == NULL)
  286. return OpStatus::ERR_NO_MEMORY;
  287. GetStringValue(char_val);
  288. return OpStatus::OK;
  289. }
  290. /**
  291. * get value of a filed as OpString
  292. */
  293. CHECK_RESULT(OP_STATUS GetStringValue(OpString& val) const)
  294. {
  295. uni_char* uni_val;
  296. if ((uni_val = val.Reserve(size / 2)) == NULL)
  297. return OpStatus::ERR_NO_MEMORY;
  298. GetStringValue(uni_val);
  299. return OpStatus::OK;
  300. }
  301. /**
  302. * allocate a buffer and copy a string value into it
  303. * @return NULL on out of memory
  304. */
  305. char *CopyStringValue(char **val) const
  306. {
  307. if (cursor == NULL)
  308. {
  309. return NULL;
  310. }
  311. if ((*val = OP_NEWA(char, size + 1)) == NULL)
  312. return NULL;
  313. if (variable_length)
  314. op_strncpy(*val, (char *)(cursor->GetPos(this) + 4), size);
  315. else op_strncpy(*val, (char *)cursor->GetPos(this), size);
  316. (*val)[size] = 0;
  317. return *val;
  318. }
  319. /**
  320. * allocate a buffer and copy a string value into it
  321. * @return NULL on out of memory
  322. */
  323. uni_char *CopyStringValue(uni_char **val) const
  324. {
  325. int maxlen;
  326. if (cursor == NULL)
  327. {
  328. return NULL;
  329. }
  330. maxlen = size / 2;
  331. if ((*val = OP_NEWA(uni_char, maxlen + 1)) == NULL)
  332. return NULL;
  333. if (variable_length)
  334. op_memcpy(*val, cursor->GetPos(this) + 4, maxlen * 2);
  335. else op_memcpy(*val, cursor->GetPos(this), maxlen * 2);
  336. (*val)[maxlen] = 0;
  337. return *val;
  338. }
  339. /**
  340. * get value of a general variable-length field
  341. */
  342. template<typename T>T *GetValue(T *val, int maxsize) const
  343. {
  344. if (cursor == NULL)
  345. {
  346. op_memset(val, 0, maxsize);
  347. return val;
  348. }
  349. if (maxsize > GetSize())
  350. maxsize = GetSize();
  351. if (variable_length)
  352. op_memcpy(val, cursor->GetPos(this) + 4, maxsize);
  353. else op_memcpy(val, cursor->GetPos(this), maxsize);
  354. return val;
  355. }
  356. /**
  357. * typecasting to anything else than char * might cause a bus error on sparc solaris
  358. * because of byte alignment
  359. * @return address of the field's data
  360. */
  361. const void *GetAddress() const
  362. {
  363. if (cursor == NULL)
  364. return NULL;
  365. return variable_length && size > 0 ? cursor->GetPos(this) + 4 : cursor->GetPos(this);
  366. }
  367. };
  368. /** @brief Read/write BSCursor field. */
  369. class Field : public ConstField
  370. {
  371. protected:
  372. friend class BSCursor;
  373. Field(int size, const char *name, BSCursor *cursor) : ConstField(size, name, cursor) {};
  374. public:
  375. Field(void) : ConstField() {};
  376. /**
  377. * set value of a static-sized field, resizes integers automatically
  378. */
  379. template<typename T>OP_STATUS SetValue(const T &val)
  380. {
  381. if (cursor == NULL)
  382. {
  383. return OpStatus::ERR;
  384. }
  385. if ((int)sizeof(T) > size)
  386. #ifndef OPERA_BIG_ENDIAN
  387. return cursor->ModifyField(this, (const unsigned char *)&val, size);
  388. #else
  389. return cursor->ModifyField(this, ((const unsigned char *)&val) + sizeof(T) - size, size);
  390. #endif
  391. if ((int)sizeof(T) < size)
  392. {
  393. OP_STATUS rv;
  394. unsigned char *tmpbuf = OP_NEWA(unsigned char, size);
  395. if (tmpbuf == NULL)
  396. return OpStatus::ERR_NO_MEMORY;
  397. op_memset(tmpbuf, 0, size);
  398. #ifndef OPERA_BIG_ENDIAN
  399. op_memcpy(tmpbuf, &val, sizeof(T)); // sizeof(T) < size here
  400. #else
  401. op_memcpy(tmpbuf + size - sizeof(T), &val, sizeof(T));
  402. #endif
  403. rv = cursor->ModifyField(this, tmpbuf, size);
  404. OP_DELETEA(tmpbuf);
  405. return rv;
  406. }
  407. return cursor->ModifyField(this, (const unsigned char *)&val, size);
  408. }
  409. /**
  410. * set value of variable-length field as a string
  411. */
  412. CHECK_RESULT(OP_STATUS SetStringValue(const char *val))
  413. {
  414. if (cursor == NULL)
  415. return OpStatus::ERR;
  416. if (val == NULL)
  417. return cursor->ModifyField(this, (const unsigned char *)"", 1);
  418. // it would be better not to include the ending 0 in the data,
  419. // but then the string wouldn't be finished when accessed by GetAddress()
  420. return cursor->ModifyField(this, (const unsigned char *)val, (int)op_strlen(val) + 1);
  421. }
  422. /**
  423. * set value of variable-length field as a string
  424. */
  425. CHECK_RESULT(OP_STATUS SetStringValue(const uni_char *val))
  426. {
  427. if (cursor == NULL)
  428. return OpStatus::ERR;
  429. if (val == NULL)
  430. return cursor->ModifyField(this, (const unsigned char *)(UNI_L("")), 2);
  431. // it would be better not to include the ending 0 in the data,
  432. // but then the string wouldn't be finished when accessed by GetAddress()
  433. return cursor->ModifyField(this, (const unsigned char *)val, (int)uni_strlen(val) * 2 + 2);
  434. }
  435. /**
  436. * get value of a general variable-length field
  437. */
  438. CHECK_RESULT(OP_STATUS SetValue(const void *val, int len))
  439. {
  440. if (cursor == NULL)
  441. {
  442. return OpStatus::ERR;
  443. }
  444. return cursor->ModifyField(this, (const unsigned char *)val, len);
  445. }
  446. };
  447. #endif // CURSOR_H