1
0

IndexRecord.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. IndexRecord Class
  8. --------------------------------------------------------------------------- */
  9. #include "IndexRecord.h"
  10. #include "../nde.h"
  11. #include <stdio.h>
  12. IndexRecord::IndexRecord(int RecordPos, int insertionPoint, VFILE *TableHandle, Table *ParentTable)
  13. {
  14. InsertionPoint = insertionPoint;
  15. if (RecordPos != 0)
  16. {
  17. int n=0;
  18. uint32_t ThisPos = RecordPos;
  19. while (ThisPos)
  20. {
  21. if (n >= 128)
  22. break;
  23. Vfseek(TableHandle, ThisPos, SEEK_SET);
  24. Field Entry (ThisPos);
  25. Field *TypedEntry = Entry.ReadField(ParentTable, ThisPos, &ThisPos);
  26. if (!TypedEntry) break; // some db error?
  27. AddField(TypedEntry);
  28. // ThisPos = TypedEntry->GetNextFieldPos();
  29. n++;
  30. }
  31. }
  32. }
  33. void IndexRecord::BuildCollaboration()
  34. {
  35. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  36. {
  37. IndexField *p = (IndexField *)*itr;
  38. if (p->next)
  39. p->index->Colaborate((IndexField *)p->next);
  40. else
  41. p->index->Colaborate((IndexField *)*Fields.begin());
  42. }
  43. }
  44. bool IndexRecord::NeedFix()
  45. {
  46. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  47. {
  48. IndexField *p = (IndexField *)*itr;
  49. if (p->index->NeedFix())
  50. return true;
  51. }
  52. return false;
  53. }
  54. IndexField *IndexRecord::GetIndexByName(const char *name)
  55. {
  56. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  57. {
  58. IndexField *p = (IndexField *)*itr;
  59. if (!strcasecmp(p->GetIndexName(), name))
  60. return p;
  61. }
  62. return NULL;
  63. }
  64. bool IndexRecord::CheckIndexing(int v)
  65. {
  66. int i = v;
  67. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  68. {
  69. IndexField *p = (IndexField *)*itr;
  70. v = p->index->GetCooperative(v);
  71. }
  72. return v == i;
  73. }
  74. Field *RecordBase::GetField(unsigned char ID)
  75. {
  76. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  77. {
  78. IndexField *p = (IndexField *)*itr;
  79. if (p->GetFieldId() == ID)
  80. return p;
  81. }
  82. return NULL;
  83. }
  84. void RecordBase::AddField(Field *field)
  85. {
  86. if (!field) return;
  87. if (GetField(field->ID))
  88. return;
  89. Fields.push_back(field);
  90. }
  91. int IndexRecord::WriteFields(Table *ParentTable)
  92. {
  93. Field *previous = 0;
  94. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  95. {
  96. IndexField *p = (IndexField *)*itr;
  97. p->WriteField(ParentTable, previous, (Field *)p->next);
  98. previous = p;
  99. }
  100. return WriteIndex(ParentTable);
  101. }
  102. int IndexRecord::WriteIndex(Table *ParentTable)
  103. {
  104. int P=0;
  105. if (!Fields.empty())
  106. P=(*Fields.begin())->GetFieldPos();
  107. return ParentTable->index->Update(INDEX_RECORD_NUM, P, this, false);
  108. }
  109. void RecordBase::RemoveField(Field *field)
  110. {
  111. if (!field)
  112. return;
  113. Fields.erase(field);
  114. delete field;
  115. }
  116. void IndexRecord::WalkFields(FieldsWalker callback, void *context)
  117. {
  118. if (callback)
  119. {
  120. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  121. {
  122. if (!callback(this, *itr, context))
  123. break;
  124. }
  125. }
  126. }