char* Namespace::getNextRecordData(int &recordSize) { Record *rec; if (extentCursor.isNull()) // the first time after connection the extent cursor will be null { extentCursor = firstExtentLoc; // so, get the first extent of the colection if (_DEBUG) LOGI("getting first extent"); } if (extentCursor.isNull()) // if first extent is null, then no extents exist return NULL; // no extents Extent* extent = db->getExtent(extentCursor); if (recordCursor == -1) // the first time the record cursor will be null as well { recordCursor = extent->firstRecord; // get the first record of the extent if (recordCursor == -1) // if first record is null, then no records exist return NULL; // no records rec = extent->getRecord(recordCursor); } else // else, the cursor shows the previous record, should take the next one and return its data { rec = extent->getRecord(recordCursor); if (rec->nextRecLoc == -1) // if its next record is null, then go to next extent { if (extent->nextExtent.isNull()) // if the next extent is null, then no more extents { return NULL; // no more records } else // proceed with the next extent { extentCursor = extent->nextExtent; recordCursor = -1; return getNextRecordData(recordSize); } } else { recordCursor = rec->nextRecLoc; // next record exists, so put the cursor on it if (_DEBUG) LOGI("getting next record"); rec = extent->getRecord(recordCursor); } } int bsonSize = 0; bson_little_endian32( &bsonSize, rec->data); char *val = (char*) malloc((bsonSize + 128) * sizeof(char)); int cur = 0; return bson_to_json(val, rec->data, 0, true, 0, recordSize, cur); }
Record* Namespace::getNextRecord() { Record *rec; if (extentCursor.isNull()) // the first time after connection the extent cursor will be null { extentCursor = firstExtentLoc; // so, get the first extent of the colection if (_DEBUG) LOGI("getting first extent"); } if (extentCursor.isNull()) // if first extent is null, then no extents exist return NULL; // no extents Extent* extent = db->getExtent(extentCursor); if (recordCursor == -1) // the first time the record cursor will be null as well { recordCursor = extent->firstRecord; // get the first record of the extent if (recordCursor == -1) // if first record is null, then no records exist return NULL; // no records rec = extent->getRecord(recordCursor); } else // else, the cursor shows the previous record, should take the next one and return its data { rec = extent->getRecord(recordCursor); if (rec->nextRecLoc == -1) // if its next record is null, then go to next extent { if (extent->nextExtent.isNull()) // if the next extent is null, then no more extents { return NULL; // no more records } else // proceed with the next extent { extentCursor = extent->nextExtent; recordCursor = -1; return getNextRecord(); } } else { recordCursor = rec->nextRecLoc; // next record exists, so put the cursor on it if (_DEBUG) LOGI("getting next record"); rec = extent->getRecord(recordCursor); } } return rec; }
string Namespace::getData() { string data = "{\"docs\":["; DiskLoc nextExtentLoc = firstExtentLoc; while (!nextExtentLoc.isNull()) { Extent* extent = db->getExtent(nextExtentLoc); int nextRecordOffset = extent->firstRecord; while (nextRecordOffset != -1) { Record* record = extent->getRecord(nextRecordOffset); //LOGI(record->data); data.append(record->data); nextRecordOffset = record->nextRecLoc; if (nextRecordOffset != -1) data.append(","); } nextExtentLoc = extent->nextExtent; } data.append("]}"); return data; }
char* Namespace::getNextBatch(int &count) { if (_DEBUG) LOGI("get next batch"); int batchSize = 0; int sizes[count]; int jsonSize = 0; char *records[count]; //= (char**)malloc(count*sizeof(char*)); int lastRetrievedRecord = -1, recordSize, skip = 0; int i = 0; Extent *extent = getFirstExtent(); while (extent != NULL) { lastRetrievedRecord = -1; Record *record = extent->getFirstRecord(); while (record != NULL && i < count) { recordSize = 0; int bsonSize = 0; bson_little_endian32( &bsonSize, record->data); //FORCE_LOG_INT("bsonSize: ", bsonSize); char *val = (char*) malloc((bsonSize + 100) * sizeof(char)); int cur = 0; val = bson_to_json(val, record->data, 0, true, bsonSize, recordSize, cur); records[i] = val; batchSize += recordSize; sizes[i] = recordSize; jsonSize += recordSize; record = extent->getRecord(record->nextRecLoc); i++; } extent = db->getExtent(extent->nextExtent); } count = i; FORCE_LOG_INT("count: ", i); return serializeJSON(records, jsonSize, count, sizes); }
void Namespace::printData() { DiskLoc nextExtentLoc = firstExtentLoc; while (!nextExtentLoc.isNull()) { Extent* extent = db->getExtent(nextExtentLoc); int nextRecordOffset = extent->firstRecord; while (nextRecordOffset != -1) { LOG_INT("print record num: ", nextRecordOffset); Record* record = extent->getRecord(nextRecordOffset); int jsonSize = 0; // bson_to_json(((bson*)record->data)->data, 0, true, 0, jsonSize); nextRecordOffset = record->nextRecLoc; } nextExtentLoc = extent->nextExtent; } }