|
8 | 8 | /** Represents a queue for uploading documents using a specified upload strategy */ |
9 | 9 | class DocumentUploadQueue { |
10 | 10 | private static final Logger logger = LogManager.getLogger(DocumentUploadQueue.class); |
11 | | - protected final UploadStrategy uploader; |
12 | | - protected final int maxQueueSize = 5 * 1024 * 1024; |
| 11 | + |
| 12 | + /** Maximum allowed queue size based on Stream API limit (256 MB) */ |
| 13 | + protected static final int MAX_ALLOWED_QUEUE_SIZE = 256 * 1024 * 1024; |
| 14 | + |
| 15 | + /** Default queue size (5 MB) */ |
| 16 | + protected static final int DEFAULT_QUEUE_SIZE = 5 * 1024 * 1024; |
| 17 | + |
| 18 | + /** System property name for configuring the default batch size */ |
| 19 | + public static final String BATCH_SIZE_PROPERTY = "coveo.push.batchSize"; |
| 20 | + |
| 21 | + protected UploadStrategy uploader; |
| 22 | + protected final int maxQueueSize; |
13 | 23 | protected ArrayList<DocumentBuilder> documentToAddList; |
14 | 24 | protected ArrayList<DeleteDocument> documentToDeleteList; |
15 | 25 | protected int size; |
16 | 26 |
|
17 | 27 | /** |
18 | | - * Constructs a new DocumentUploadQueue object with a default maximum queue size limit of 5MB. |
| 28 | + * Validates batch size against constraints (> 0 and <= 256MB). Used by getConfiguredBatchSize and |
| 29 | + * constructors to ensure consistent validation logic. |
| 30 | + * |
| 31 | + * @param sizeBytes The batch size in bytes to validate |
| 32 | + * @throws IllegalArgumentException if size exceeds MAX_ALLOWED_QUEUE_SIZE or is <= 0 |
| 33 | + */ |
| 34 | + protected static void validateBatchSize(int sizeBytes) { |
| 35 | + if (sizeBytes > MAX_ALLOWED_QUEUE_SIZE) { |
| 36 | + throw new IllegalArgumentException( |
| 37 | + String.format( |
| 38 | + "Batch size (%d bytes) exceeds the Stream API limit of %d bytes (%d MB)", |
| 39 | + sizeBytes, MAX_ALLOWED_QUEUE_SIZE, MAX_ALLOWED_QUEUE_SIZE / (1024 * 1024))); |
| 40 | + } |
| 41 | + if (sizeBytes <= 0) { |
| 42 | + throw new IllegalArgumentException("Batch size must be greater than 0"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the configured batch size from system properties, or returns the default if not set. |
| 48 | + * |
| 49 | + * <p>The system property is read as bytes. When not set, returns DEFAULT_QUEUE_SIZE (5 MB). |
| 50 | + * |
| 51 | + * <p>Example: Set a 50 MB batch size via system property: |
| 52 | + * |
| 53 | + * <pre> |
| 54 | + * java -Dcoveo.push.batchSize=52428800 -jar app.jar // 50 * 1024 * 1024 bytes |
| 55 | + * </pre> |
| 56 | + * |
| 57 | + * @return The configured batch size in bytes (e.g., 52428800 for 50 MB) |
| 58 | + * @throws IllegalArgumentException if the configured value exceeds 256MB or is invalid |
| 59 | + */ |
| 60 | + public static int getConfiguredBatchSize() { |
| 61 | + String propertyValue = System.getProperty(BATCH_SIZE_PROPERTY); |
| 62 | + if (propertyValue == null || propertyValue.trim().isEmpty()) { |
| 63 | + return DEFAULT_QUEUE_SIZE; |
| 64 | + } |
| 65 | + |
| 66 | + int configuredSize; |
| 67 | + try { |
| 68 | + configuredSize = Integer.parseInt(propertyValue.trim()); |
| 69 | + } catch (NumberFormatException e) { |
| 70 | + throw new IllegalArgumentException( |
| 71 | + String.format( |
| 72 | + "Invalid value for system property %s: '%s'. Must be a valid integer.", |
| 73 | + BATCH_SIZE_PROPERTY, propertyValue), |
| 74 | + e); |
| 75 | + } |
| 76 | + |
| 77 | + validateBatchSize(configuredSize); |
| 78 | + |
| 79 | + logger.info( |
| 80 | + String.format( |
| 81 | + "Using configured batch size from system property %s: %d bytes (%.2f MB)", |
| 82 | + BATCH_SIZE_PROPERTY, configuredSize, configuredSize / (1024.0 * 1024.0))); |
| 83 | + return configuredSize; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Constructs a new DocumentUploadQueue with the default batch size. |
| 88 | + * |
| 89 | + * <p>Uses the configured batch size from system property "coveo.push.batchSize" if set, otherwise |
| 90 | + * defaults to DEFAULT_QUEUE_SIZE (5 MB = 5242880 bytes). |
19 | 91 | * |
20 | 92 | * @param uploader The upload strategy to be used for document uploads. |
| 93 | + * @throws IllegalArgumentException if the system property value exceeds 256MB or is invalid. |
21 | 94 | */ |
22 | 95 | public DocumentUploadQueue(UploadStrategy uploader) { |
| 96 | + this(uploader, getConfiguredBatchSize()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Constructs a new DocumentUploadQueue object with a configurable maximum queue size limit. |
| 101 | + * |
| 102 | + * @param uploader The upload strategy to be used for document uploads. |
| 103 | + * @param maxQueueSize The maximum queue size in bytes (e.g., 52428800 for 50 MB). Must not exceed |
| 104 | + * 256MB (Stream API limit). |
| 105 | + * @throws IllegalArgumentException if maxQueueSize exceeds the API limit of 256MB. |
| 106 | + */ |
| 107 | + public DocumentUploadQueue(UploadStrategy uploader, int maxQueueSize) { |
| 108 | + validateBatchSize(maxQueueSize); |
23 | 109 | this.documentToAddList = new ArrayList<>(); |
24 | 110 | this.documentToDeleteList = new ArrayList<>(); |
25 | 111 | this.uploader = uploader; |
| 112 | + this.maxQueueSize = maxQueueSize; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Default constructor for testing purposes (used by Mockito @InjectMocks). Initializes with |
| 117 | + * default batch size; uploader is injected by Mockito. |
| 118 | + */ |
| 119 | + public DocumentUploadQueue() { |
| 120 | + this.documentToAddList = new ArrayList<>(); |
| 121 | + this.documentToDeleteList = new ArrayList<>(); |
| 122 | + this.maxQueueSize = DEFAULT_QUEUE_SIZE; |
26 | 123 | } |
27 | 124 |
|
28 | 125 | /** |
|
0 commit comments