|
| 1 | +import pytest |
| 2 | +from arcadedb_embedded import ArcadeDBError, create_database |
| 3 | + |
| 4 | + |
| 5 | +@pytest.fixture |
| 6 | +def test_db(tmp_path): |
| 7 | + """Create a temporary test database.""" |
| 8 | + db_path = str(tmp_path / "test_vector_params_db") |
| 9 | + db = create_database(db_path) |
| 10 | + yield db |
| 11 | + db.drop() |
| 12 | + |
| 13 | + |
| 14 | +class TestVectorParams: |
| 15 | + """Verify that vector index parameters are correctly passed to Java.""" |
| 16 | + |
| 17 | + def test_quantization_param(self, test_db): |
| 18 | + """Test sending quantization parameter.""" |
| 19 | + test_db.schema.create_vertex_type("QuantDoc") |
| 20 | + test_db.schema.create_property("QuantDoc", "embedding", "ARRAY_OF_FLOATS") |
| 21 | + |
| 22 | + # Create with INT8 |
| 23 | + index = test_db.create_vector_index( |
| 24 | + "QuantDoc", "embedding", dimensions=3, quantization="INT8" |
| 25 | + ) |
| 26 | + |
| 27 | + # Verify via wrapper convenience method |
| 28 | + assert index.get_quantization() == "INT8" |
| 29 | + |
| 30 | + # Verify by inspecting Java object directly |
| 31 | + # Accessing the underlying Java object's metadata |
| 32 | + java_index = index._java_index |
| 33 | + |
| 34 | + # Depending on if it's a TypeIndex or LSMVectorIndex directly |
| 35 | + idx_to_check = java_index |
| 36 | + if "TypeIndex" in java_index.getClass().getName(): |
| 37 | + idx_to_check = java_index.getSubIndexes().get(0) |
| 38 | + |
| 39 | + # In Java: index.getMetadata().quantizationType |
| 40 | + # JPype allows attribute access for getters or fields |
| 41 | + # Note: the exact field name depends on the Java class implementation of metadata |
| 42 | + # Given bindings/python/src/arcadedb_embedded/vector.py uses .quantizationType: |
| 43 | + assert str(idx_to_check.getMetadata().quantizationType) == "INT8" |
| 44 | + |
| 45 | + def test_store_vectors_in_graph_param(self, test_db): |
| 46 | + """Test sending store_vectors_in_graph parameter.""" |
| 47 | + test_db.schema.create_vertex_type("StoreDoc") |
| 48 | + test_db.schema.create_property("StoreDoc", "embedding", "ARRAY_OF_FLOATS") |
| 49 | + |
| 50 | + # Create with store_vectors_in_graph=True |
| 51 | + index = test_db.create_vector_index( |
| 52 | + "StoreDoc", "embedding", dimensions=3, store_vectors_in_graph=True |
| 53 | + ) |
| 54 | + |
| 55 | + # Accessing the underlying Java object's metadata |
| 56 | + java_index = index._java_index |
| 57 | + idx_to_check = java_index |
| 58 | + if "TypeIndex" in java_index.getClass().getName(): |
| 59 | + idx_to_check = java_index.getSubIndexes().get(0) |
| 60 | + |
| 61 | + metadata = idx_to_check.getMetadata() |
| 62 | + |
| 63 | + # We need to find where "storeVectorsInGraph" is stored. |
| 64 | + # It might be a field, or it might be in map-like structure if it was passed via JSON. |
| 65 | + # Let's inspect what we can. |
| 66 | + |
| 67 | + print(f"\nMetadata Class: {metadata.getClass().getName()}") |
| 68 | + print(f"Metadata String: {metadata.toString()}") |
| 69 | + |
| 70 | + # Attempt to check property directly if it's exposed as a field matching the JSON key |
| 71 | + # Or check via getter if available |
| 72 | + |
| 73 | + val = None |
| 74 | + try: |
| 75 | + # Try field access |
| 76 | + val = metadata.storeVectorsInGraph |
| 77 | + except Exception: |
| 78 | + try: |
| 79 | + # Try getter |
| 80 | + val = metadata.isStoreVectorsInGraph() |
| 81 | + except Exception: |
| 82 | + pass |
| 83 | + |
| 84 | + if val is None: |
| 85 | + # Try inspecting the string representation as a fallback for verification |
| 86 | + assert ( |
| 87 | + "storeVectorsInGraph" in metadata.toString() |
| 88 | + or "storeVectorsInGraph=true" in metadata.toString() |
| 89 | + ) |
| 90 | + else: |
| 91 | + assert val is True |
| 92 | + |
| 93 | + def test_params_persistence(self, tmp_path): |
| 94 | + """Verify parameters persist after reload.""" |
| 95 | + db_path = str(tmp_path / "test_vector_params_persist") |
| 96 | + |
| 97 | + # 1. Create and Configure |
| 98 | + with create_database(db_path) as db: |
| 99 | + db.schema.create_vertex_type("Doc") |
| 100 | + db.schema.create_property("Doc", "embedding", "ARRAY_OF_FLOATS") |
| 101 | + |
| 102 | + db.create_vector_index( |
| 103 | + "Doc", |
| 104 | + "embedding", |
| 105 | + dimensions=3, |
| 106 | + quantization="INT8", |
| 107 | + store_vectors_in_graph=True, |
| 108 | + ) |
| 109 | + |
| 110 | + # 2. Reopen and Check |
| 111 | + from arcadedb_embedded import open_database |
| 112 | + |
| 113 | + with open_database(db_path) as db: |
| 114 | + index = db.schema.get_vector_index("Doc", "embedding") |
| 115 | + |
| 116 | + # Check Quantization |
| 117 | + assert index.get_quantization() == "INT8" |
| 118 | + |
| 119 | + # Check Graph Storage |
| 120 | + java_index = index._java_index |
| 121 | + idx_to_check = java_index |
| 122 | + if "TypeIndex" in java_index.getClass().getName(): |
| 123 | + idx_to_check = java_index.getSubIndexes().get(0) |
| 124 | + |
| 125 | + metadata = idx_to_check.getMetadata() |
| 126 | + print(f"\nReloaded Metadata: {metadata.toString()}") |
| 127 | + |
| 128 | + # Verification (similar strategy as above) |
| 129 | + try: |
| 130 | + assert metadata.storeVectorsInGraph is True |
| 131 | + except AttributeError: |
| 132 | + assert ( |
| 133 | + "storeVectorsInGraph=true" in metadata.toString() |
| 134 | + or "storeVectorsInGraph: true" in metadata.toString() |
| 135 | + ) |
| 136 | + |
| 137 | + def test_quantization_none(self, test_db): |
| 138 | + """Test sending quantization parameter NONE.""" |
| 139 | + test_db.schema.create_vertex_type("QuantNoneDoc") |
| 140 | + test_db.schema.create_property("QuantNoneDoc", "embedding", "ARRAY_OF_FLOATS") |
| 141 | + |
| 142 | + index = test_db.create_vector_index( |
| 143 | + "QuantNoneDoc", "embedding", dimensions=3, quantization="NONE" |
| 144 | + ) |
| 145 | + |
| 146 | + assert index.get_quantization() == "NONE" |
| 147 | + |
| 148 | + java_index = index._java_index |
| 149 | + idx_to_check = java_index |
| 150 | + if "TypeIndex" in java_index.getClass().getName(): |
| 151 | + idx_to_check = java_index.getSubIndexes().get(0) |
| 152 | + |
| 153 | + assert str(idx_to_check.getMetadata().quantizationType) == "NONE" |
| 154 | + |
| 155 | + def test_quantization_binary(self, test_db): |
| 156 | + """Test sending quantization parameter BINARY.""" |
| 157 | + test_db.schema.create_vertex_type("QuantBinaryDoc") |
| 158 | + test_db.schema.create_property("QuantBinaryDoc", "embedding", "ARRAY_OF_FLOATS") |
| 159 | + |
| 160 | + index = test_db.create_vector_index( |
| 161 | + "QuantBinaryDoc", "embedding", dimensions=128, quantization="BINARY" |
| 162 | + ) |
| 163 | + |
| 164 | + assert index.get_quantization() == "BINARY" |
| 165 | + |
| 166 | + java_index = index._java_index |
| 167 | + idx_to_check = java_index |
| 168 | + if "TypeIndex" in java_index.getClass().getName(): |
| 169 | + idx_to_check = java_index.getSubIndexes().get(0) |
| 170 | + |
| 171 | + assert str(idx_to_check.getMetadata().quantizationType) == "BINARY" |
| 172 | + |
| 173 | + def test_jvm_heap_check(self): |
| 174 | + """Verify JVM memory settings from Java level.""" |
| 175 | + import jpype |
| 176 | + |
| 177 | + runtime = jpype.JPackage("java.lang").Runtime.getRuntime() |
| 178 | + max_memory = runtime.maxMemory() |
| 179 | + total_memory = runtime.totalMemory() |
| 180 | + free_memory = runtime.freeMemory() |
| 181 | + |
| 182 | + print(f"\n=== JVM Memory Stats ===") |
| 183 | + print(f"Max Memory: {max_memory / (1024**3):.2f} GB ({max_memory} bytes)") |
| 184 | + print(f"Total Memory: {total_memory / (1024**2):.2f} MB") |
| 185 | + print(f"Free Memory: {free_memory / (1024**2):.2f} MB") |
| 186 | + |
| 187 | + # Verify it's a reasonable size (at least 1GB, reflecting -Xmx4g default) |
| 188 | + assert max_memory > 1 * 1024 * 1024 * 1024 |
0 commit comments