| 1438 | } |
| 1439 | |
| 1440 | int FlatStreamerEntity::alloc_segment() { |
| 1441 | // add()/add_vector_with_id() serialize allocation with mutex_. Keep the |
| 1442 | // cache lock out of storage allocation, which may wait for reader pins. |
| 1443 | size_t index; |
| 1444 | { |
| 1445 | std::lock_guard<std::mutex> lock(segments_mutex_); |
| 1446 | index = segments_.size(); |
| 1447 | } |
| 1448 | if (index == kMaxSegmentId) { |
| 1449 | LOG_ERROR("Failed to alloc new segment, exceed max count %zu", |
| 1450 | kMaxSegmentId); |
| 1451 | return IndexError_IndexFull; |
| 1452 | } |
| 1453 | |
| 1454 | std::string segment_id = |
| 1455 | ailego::StringHelper::Concat(FLAT_SEGMENT_FEATURES_SEG_ID, index); |
| 1456 | size_t size = |
| 1457 | ailego_align(meta_.segment_size, ailego::MemoryHelper::PageSize()); |
| 1458 | auto segment = storage_->get(segment_id); |
| 1459 | if (segment) { |
| 1460 | if (segment->padding_size() < linear_block_size()) { |
| 1461 | LOG_ERROR( |
| 1462 | "Unexpect segment, index=%zu, data_size=%zu " |
| 1463 | "padding_size=%zu block_size=%zu", |
| 1464 | index, segment->data_size(), segment->padding_size(), |
| 1465 | linear_block_size()); |
| 1466 | return IndexError_Runtime; |
| 1467 | } |
| 1468 | LOG_WARN("Alloc an existing segment=%s capacity=%zu", segment_id.c_str(), |
| 1469 | segment->capacity()); |
| 1470 | } else { |
| 1471 | int ret = storage_->append(segment_id, size); |
| 1472 | if (ailego_unlikely(ret != 0)) { |
| 1473 | LOG_ERROR("Failed to alloc segment from storage"); |
| 1474 | return ret; |
| 1475 | } |
| 1476 | segment = storage_->get(segment_id); |
| 1477 | if (ailego_unlikely(!segment)) { |
| 1478 | LOG_ERROR("Failed to get segment %s", segment_id.c_str()); |
| 1479 | return IndexError_Runtime; |
| 1480 | } |
| 1481 | } |
| 1482 | meta_.segment_count += 1; |
| 1483 | meta_.header.linear_body_size += size; |
| 1484 | { |
| 1485 | std::lock_guard<std::mutex> lock(segments_mutex_); |
| 1486 | segments_.emplace_back(std::move(segment)); |
| 1487 | } |
| 1488 | *stats_.mutable_index_size() += size; |
| 1489 | |
| 1490 | // Update meta information |
| 1491 | auto meta_segment = storage_->get(FLAT_LINEAR_META_SEG_ID); |
| 1492 | if (ailego_unlikely(!meta_segment)) { |
| 1493 | LOG_ERROR("Failed to get segment %s", FLAT_LINEAR_META_SEG_ID.c_str()); |
| 1494 | return IndexError_Runtime; |
| 1495 | } |
| 1496 | if (meta_segment->write(0, &meta_, sizeof(meta_)) != sizeof(meta_)) { |
| 1497 | LOG_ERROR("Failed to write meta segment"); |
no test coverage detected