|
| 1 | +/* |
| 2 | + * Copyright © 2021-present Arcade Data Ltd ([email protected]) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd ([email protected]) |
| 17 | + * SPDX-License-Identifier: Apache-2.0 |
| 18 | + */ |
| 19 | +package com.arcadedb.database; |
| 20 | + |
| 21 | +import com.arcadedb.TestHelper; |
| 22 | +import com.arcadedb.graph.MutableVertex; |
| 23 | +import com.arcadedb.schema.DocumentType; |
| 24 | +import com.arcadedb.schema.Type; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * Test cases for Database.getSize() API |
| 31 | + * |
| 32 | + * @author Luca Garulli ([email protected]) |
| 33 | + */ |
| 34 | +class DatabaseGetSizeTest extends TestHelper { |
| 35 | + |
| 36 | + @Test |
| 37 | + void testGetSizeOnEmptyDatabase() { |
| 38 | + // Even an empty database has metadata files (schema, configuration, etc.) |
| 39 | + final long initialSize = database.getSize(); |
| 40 | + assertThat(initialSize).isGreaterThan(0L); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testGetSizeIncreasesWithDocuments() { |
| 45 | + database.transaction(() -> { |
| 46 | + // Create a document type |
| 47 | + final DocumentType personType = database.getSchema().createDocumentType("Person"); |
| 48 | + personType.createProperty("name", Type.STRING); |
| 49 | + personType.createProperty("age", Type.INTEGER); |
| 50 | + personType.createProperty("email", Type.STRING); |
| 51 | + }); |
| 52 | + |
| 53 | + final long sizeBeforeInsert = database.getSize(); |
| 54 | + |
| 55 | + // Insert documents |
| 56 | + database.transaction(() -> { |
| 57 | + for (int i = 0; i < 1000; i++) { |
| 58 | + final MutableDocument doc = database.newDocument("Person"); |
| 59 | + doc.set("name", "Person" + i); |
| 60 | + doc.set("age", 20 + (i % 50)); |
| 61 | + doc.set("email", "person" + i + "@example.com"); |
| 62 | + doc.save(); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + final long sizeAfterInsert = database.getSize(); |
| 67 | + |
| 68 | + // Database size should increase after inserting documents |
| 69 | + assertThat(sizeAfterInsert).isGreaterThan(sizeBeforeInsert); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testGetSizeIncreasesWithVerticesAndEdges() { |
| 74 | + database.transaction(() -> { |
| 75 | + // Create vertex and edge types |
| 76 | + database.getSchema().createVertexType("User"); |
| 77 | + database.getSchema().createEdgeType("Follows"); |
| 78 | + }); |
| 79 | + |
| 80 | + final long sizeBeforeData = database.getSize(); |
| 81 | + |
| 82 | + // Create vertices and edges |
| 83 | + database.transaction(() -> { |
| 84 | + MutableVertex user1 = database.newVertex("User"); |
| 85 | + user1.set("name", "Alice"); |
| 86 | + user1.save(); |
| 87 | + |
| 88 | + MutableVertex user2 = database.newVertex("User"); |
| 89 | + user2.set("name", "Bob"); |
| 90 | + user2.save(); |
| 91 | + |
| 92 | + MutableVertex user3 = database.newVertex("User"); |
| 93 | + user3.set("name", "Charlie"); |
| 94 | + user3.save(); |
| 95 | + |
| 96 | + // Create edges (using explicit method to avoid ambiguity) |
| 97 | + user1.newEdge("Follows", user2, true, (Object[]) null); |
| 98 | + user2.newEdge("Follows", user3, true, (Object[]) null); |
| 99 | + user1.newEdge("Follows", user3, true, (Object[]) null); |
| 100 | + }); |
| 101 | + |
| 102 | + final long sizeAfterData = database.getSize(); |
| 103 | + |
| 104 | + // Database size should increase after inserting vertices and edges |
| 105 | + assertThat(sizeAfterData).isGreaterThan(sizeBeforeData); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testGetSizeWithIndex() { |
| 110 | + database.transaction(() -> { |
| 111 | + // Create a document type with an indexed property |
| 112 | + final DocumentType productType = database.getSchema().createDocumentType("Product"); |
| 113 | + productType.createProperty("sku", Type.STRING); |
| 114 | + productType.createProperty("name", Type.STRING); |
| 115 | + productType.createProperty("price", Type.DOUBLE); |
| 116 | + |
| 117 | + // Create an index |
| 118 | + database.getSchema().createTypeIndex(com.arcadedb.schema.Schema.INDEX_TYPE.LSM_TREE, true, "Product", "sku"); |
| 119 | + }); |
| 120 | + |
| 121 | + final long sizeBeforeInsert = database.getSize(); |
| 122 | + |
| 123 | + // Insert documents that will be indexed |
| 124 | + database.transaction(() -> { |
| 125 | + for (int i = 0; i < 500; i++) { |
| 126 | + final MutableDocument doc = database.newDocument("Product"); |
| 127 | + doc.set("sku", "SKU-" + String.format("%05d", i)); |
| 128 | + doc.set("name", "Product " + i); |
| 129 | + doc.set("price", 10.0 + i); |
| 130 | + doc.save(); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + final long sizeAfterInsert = database.getSize(); |
| 135 | + |
| 136 | + // Database size should increase (includes both data and index files) |
| 137 | + assertThat(sizeAfterInsert).isGreaterThan(sizeBeforeInsert); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testGetSizeMultipleCalls() { |
| 142 | + // Multiple calls to getSize() should return consistent results |
| 143 | + final long size1 = database.getSize(); |
| 144 | + final long size2 = database.getSize(); |
| 145 | + final long size3 = database.getSize(); |
| 146 | + |
| 147 | + assertThat(size1).isEqualTo(size2); |
| 148 | + assertThat(size2).isEqualTo(size3); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void testGetSizeAfterDelete() { |
| 153 | + database.transaction(() -> { |
| 154 | + database.getSchema().createDocumentType("TempDoc"); |
| 155 | + }); |
| 156 | + |
| 157 | + // Insert documents |
| 158 | + database.transaction(() -> { |
| 159 | + for (int i = 0; i < 100; i++) { |
| 160 | + final MutableDocument doc = database.newDocument("TempDoc"); |
| 161 | + doc.set("value", "Data" + i); |
| 162 | + doc.save(); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + final long sizeAfterInsert = database.getSize(); |
| 167 | + |
| 168 | + // Delete some documents |
| 169 | + database.transaction(() -> { |
| 170 | + database.command("sql", "DELETE FROM TempDoc WHERE value LIKE 'Data5%'").close(); |
| 171 | + }); |
| 172 | + |
| 173 | + final long sizeAfterDelete = database.getSize(); |
| 174 | + |
| 175 | + // Size might not decrease immediately due to space not being reclaimed until compaction |
| 176 | + // But we can verify the size is still valid |
| 177 | + assertThat(sizeAfterDelete).isGreaterThan(0L); |
| 178 | + } |
| 179 | +} |
0 commit comments