-
-
Notifications
You must be signed in to change notification settings - Fork 91
feat: added new database.getSize() api
#3045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
engine/src/test/java/com/arcadedb/database/DatabaseGetSizeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /* | ||
| * Copyright © 2021-present Arcade Data Ltd ([email protected]) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd ([email protected]) | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.arcadedb.database; | ||
|
|
||
| import com.arcadedb.TestHelper; | ||
| import com.arcadedb.graph.MutableVertex; | ||
| import com.arcadedb.schema.DocumentType; | ||
| import com.arcadedb.schema.Type; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Test cases for Database.getSize() API | ||
| * | ||
| * @author Luca Garulli ([email protected]) | ||
| */ | ||
| class DatabaseGetSizeTest extends TestHelper { | ||
|
|
||
| @Test | ||
| void testGetSizeOnEmptyDatabase() { | ||
| // Even an empty database has metadata files (schema, configuration, etc.) | ||
| final long initialSize = database.getSize(); | ||
| assertThat(initialSize).isGreaterThan(0L); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSizeIncreasesWithDocuments() { | ||
| database.transaction(() -> { | ||
| // Create a document type | ||
| final DocumentType personType = database.getSchema().createDocumentType("Person"); | ||
| personType.createProperty("name", Type.STRING); | ||
| personType.createProperty("age", Type.INTEGER); | ||
| personType.createProperty("email", Type.STRING); | ||
| }); | ||
|
|
||
| final long sizeBeforeInsert = database.getSize(); | ||
|
|
||
| // Insert documents | ||
| database.transaction(() -> { | ||
| for (int i = 0; i < 1000; i++) { | ||
| final MutableDocument doc = database.newDocument("Person"); | ||
| doc.set("name", "Person" + i); | ||
| doc.set("age", 20 + (i % 50)); | ||
| doc.set("email", "person" + i + "@example.com"); | ||
| doc.save(); | ||
| } | ||
| }); | ||
|
|
||
| final long sizeAfterInsert = database.getSize(); | ||
|
|
||
| // Database size should increase after inserting documents | ||
| assertThat(sizeAfterInsert).isGreaterThan(sizeBeforeInsert); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSizeIncreasesWithVerticesAndEdges() { | ||
| database.transaction(() -> { | ||
| // Create vertex and edge types | ||
| database.getSchema().createVertexType("User"); | ||
| database.getSchema().createEdgeType("Follows"); | ||
| }); | ||
|
|
||
| final long sizeBeforeData = database.getSize(); | ||
|
|
||
| // Create vertices and edges | ||
| database.transaction(() -> { | ||
| MutableVertex user1 = database.newVertex("User"); | ||
| user1.set("name", "Alice"); | ||
| user1.save(); | ||
|
|
||
| MutableVertex user2 = database.newVertex("User"); | ||
| user2.set("name", "Bob"); | ||
| user2.save(); | ||
|
|
||
| MutableVertex user3 = database.newVertex("User"); | ||
| user3.set("name", "Charlie"); | ||
| user3.save(); | ||
|
|
||
| // Create edges (using explicit method to avoid ambiguity) | ||
| user1.newEdge("Follows", user2, true, (Object[]) null); | ||
| user2.newEdge("Follows", user3, true, (Object[]) null); | ||
| user1.newEdge("Follows", user3, true, (Object[]) null); | ||
| }); | ||
|
|
||
| final long sizeAfterData = database.getSize(); | ||
|
|
||
| // Database size should increase after inserting vertices and edges | ||
| assertThat(sizeAfterData).isGreaterThan(sizeBeforeData); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSizeWithIndex() { | ||
| database.transaction(() -> { | ||
| // Create a document type with an indexed property | ||
| final DocumentType productType = database.getSchema().createDocumentType("Product"); | ||
| productType.createProperty("sku", Type.STRING); | ||
| productType.createProperty("name", Type.STRING); | ||
| productType.createProperty("price", Type.DOUBLE); | ||
|
|
||
| // Create an index | ||
| database.getSchema().createTypeIndex(com.arcadedb.schema.Schema.INDEX_TYPE.LSM_TREE, true, "Product", "sku"); | ||
| }); | ||
|
|
||
| final long sizeBeforeInsert = database.getSize(); | ||
|
|
||
| // Insert documents that will be indexed | ||
| database.transaction(() -> { | ||
| for (int i = 0; i < 500; i++) { | ||
| final MutableDocument doc = database.newDocument("Product"); | ||
| doc.set("sku", "SKU-" + String.format("%05d", i)); | ||
| doc.set("name", "Product " + i); | ||
| doc.set("price", 10.0 + i); | ||
| doc.save(); | ||
| } | ||
| }); | ||
|
|
||
| final long sizeAfterInsert = database.getSize(); | ||
|
|
||
| // Database size should increase (includes both data and index files) | ||
| assertThat(sizeAfterInsert).isGreaterThan(sizeBeforeInsert); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSizeMultipleCalls() { | ||
| // Multiple calls to getSize() should return consistent results | ||
| final long size1 = database.getSize(); | ||
| final long size2 = database.getSize(); | ||
| final long size3 = database.getSize(); | ||
|
|
||
| assertThat(size1).isEqualTo(size2); | ||
| assertThat(size2).isEqualTo(size3); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSizeAfterDelete() { | ||
| database.transaction(() -> { | ||
| database.getSchema().createDocumentType("TempDoc"); | ||
| }); | ||
|
|
||
| // Insert documents | ||
| database.transaction(() -> { | ||
| for (int i = 0; i < 100; i++) { | ||
| final MutableDocument doc = database.newDocument("TempDoc"); | ||
| doc.set("value", "Data" + i); | ||
| doc.save(); | ||
| } | ||
| }); | ||
|
|
||
| final long sizeAfterInsert = database.getSize(); | ||
|
|
||
| // Delete some documents | ||
| database.transaction(() -> { | ||
| database.command("sql", "DELETE FROM TempDoc WHERE value LIKE 'Data5%'").close(); | ||
| }); | ||
|
|
||
| final long sizeAfterDelete = database.getSize(); | ||
|
|
||
| // Size might not decrease immediately due to space not being reclaimed until compaction | ||
| // But we can verify the size is still valid | ||
| assertThat(sizeAfterDelete).isGreaterThan(0L); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception handling in this block could be more consistent. An
IOExceptionthrown byFiles.walk(dir)is not caught here and would be handled by the outerexecuteInReadLockmethod, leading to a generic error message. In contrast, anIOExceptionfromFiles.size(p)is wrapped inUncheckedIOExceptionand caught, resulting in a more specific error message.To ensure all I/O errors during size calculation are handled uniformly with a clear message, I suggest catching both
IOExceptionandUncheckedIOExceptionwithin this block.