Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,10 @@ private static void buildIndex(final int maxAttempts, Database database, Index.B
// answers the current page size unless it is not legal to create with, in which case it repairs it.
final int pageSize = ((IndexInternal) idx).getPageSizeForNewFile();
final LSMTreeIndexAbstract.NULL_STRATEGY nullStrategy = idx.getNullStrategy();
// Get index metadata (includes vector-specific settings like dimensions, similarity, etc.), reconstructing
// FULL_TEXT/GEOSPATIAL from the index's persisted JSON so the rebuild preserves their type-specific
// settings (issue #4732/#5478) instead of the generic IndexMetadata getMetadata() would otherwise return.
// Get index metadata (includes vector-specific settings like dimensions, similarity, etc.) through the
// accessor that answers with the type-specific configuration a WRAPPER index keeps outside the underlying
// LSM-Tree, so the rebuild preserves the analyzers, the geohash resolution and the sparse dimensionality
// (issues #4732/#5478/#5742) instead of the generic IndexMetadata getMetadata() would otherwise return.
// Shared with DatabaseChecker's auto-fix rebuild path (issue #5934).
final IndexMetadata rebuildMetadata = IndexMetadata.reconstructForRebuild((IndexInternal) idx, typeName,
propertyNames.toArray(new String[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
import com.arcadedb.query.sql.executor.ResultSet;
import com.arcadedb.schema.DocumentType;
import com.arcadedb.schema.IndexMetadata;
import com.arcadedb.schema.LSMVectorIndexMetadata;
import com.arcadedb.schema.Schema;
import com.arcadedb.schema.TypeIndexBuilder;
import com.arcadedb.schema.TypeLSMVectorIndexBuilder;

import java.util.*;

Expand Down Expand Up @@ -233,14 +231,17 @@ private void recreateIndex(final Schema schema, final IndexDefinition def) {
builder.withPageSize(def.pageSize);
builder.withNullStrategy(def.nullStrategy);

// Type-specific metadata (e.g. LSM_VECTOR dimensions/similarity) must be carried over,
// otherwise the recreated index loses its configuration and inserts fail with "index
// dimension 0" (issue #4359). The type-specific builder's withMetadata(IndexMetadata)
// override reinstates the vector-specific fields.
if (def.indexType == Schema.INDEX_TYPE.LSM_VECTOR && def.metadata instanceof LSMVectorIndexMetadata vectorMeta)
// A copy - rather than the original reference - keeps the dropped index's per-index runtime state (notably
// buildState) off the rebuilt one and resets associatedBucketId so the per-bucket builder sets it during create().
((TypeLSMVectorIndexBuilder) builder).withMetadata(vectorMeta.copy(def.typeName, def.propertyNames, -1));
// Type-specific metadata must be carried over, otherwise the recreated index loses its configuration: a vector
// index comes back with dimensions 0 and rejects every insert (issue #4359), a full-text one with a different
// analyzer and a different ranking, a geospatial one with a different cell size (issue #5742). withType() above
// has already swapped in the builder subclass that owns those settings, so the polymorphic withMetadata() call
// lands on the override that reinstates them - no per-index-type branch here to forget one.
//
// A copy - rather than the original reference - keeps the dropped index's per-index runtime state (notably the
// vector index's buildState and the full-text corpus counters, which describe records that are about to be gone)
// off the rebuilt one, and resets associatedBucketId so the per-bucket builder sets it during create().
if (def.metadata != null)
builder.withMetadata(def.metadata.copy(def.typeName, def.propertyNames, -1));

builder.create();
}
Expand Down Expand Up @@ -284,10 +285,13 @@ private IndexDefinition(final String name, final String typeName, final String[]

static IndexDefinition from(final TypeIndex index) {
final List<String> props = index.getPropertyNames();
// getPageSizeForNewFile(), not getPageSize(): this definition is replayed through the creation path after the
// index has been dropped, so a page size creation refuses would leave the type without the index it had (#5713).
// getPageSizeForNewFile()/getMetadataForNewFile(), not getPageSize()/getMetadata(): this definition is replayed
// through the creation path after the index has been dropped, so the page size carried over has to be one that
// path accepts (#5713), and the configuration has to be the one the WRAPPER index types keep outside the
// underlying LSM-Tree - analyzers, geohash resolution, sparse dimensionality (#5742).
return new IndexDefinition(index.getName(), index.getTypeName(), props.toArray(new String[0]),
index.getType(), index.isUnique(), index.getPageSizeForNewFile(), index.getNullStrategy(), index.getMetadata());
index.getType(), index.isUnique(), index.getPageSizeForNewFile(), index.getNullStrategy(),
index.getMetadataForNewFile());
}
}

Expand Down
68 changes: 46 additions & 22 deletions engine/src/main/java/com/arcadedb/schema/IndexMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ protected final <T extends IndexMetadata> T copyCommonTo(final T copy) {
return copy;
}

/**
* The in-place form of {@link #copyCommonTo}: takes the base-class settings from the definition of the index this one
* sits on top of.
* <p>
* A WRAPPER index type - full-text, geospatial, sparse-vector - splits its definition in two: the base settings
* (collations, the user-supplied {@code TypeIndex} name) belong to the underlying LSM-Tree's plain
* {@link IndexMetadata}, while the type-specific ones live in a subclass instance the wrapper holds. Creation
* populates a single instance carrying both halves, but a schema RELOAD builds the subclass instance from the
* bucket-level index JSON, which carries no {@code typeName} key and therefore skips {@link #fromJSON}'s base-field
* read entirely. Without this hop, {@link com.arcadedb.index.IndexInternal#getMetadataForNewFile()} would answer with
* the type-specific settings and NO collations and NO manual index name after a restart, and every carry-over site
* reading it would trade one silent loss for another (issue #5742).
*
* @param source the underlying index's definition, or {@code null} when there is none to inherit from
*/
public final void inheritCommonSettingsFrom(final IndexMetadata source) {
if (source == null || source == this)
return;
collations = source.collations;
typeIndexName = source.typeIndexName;
}

public void fromJSON(final JSONObject metadata) {
typeName = metadata.getString("typeName");
propertyNames = metadata.getJSONArray("properties").toListOfStrings();
Expand Down Expand Up @@ -331,15 +353,21 @@ public boolean hasAnyCaseInsensitive() {
}

/**
* Reconstructs the metadata an index rebuild should carry over, for the two index types whose configuration
* {@link com.arcadedb.index.IndexInternal#getMetadata()} does not capture. {@code FULL_TEXT} and {@code GEOSPATIAL}
* keep their type-specific settings (analyzer, BM25 parameters, GeoHash precision) in a subtype the generic
* {@code IndexMetadata} does not carry, so a rebuild that passed {@code getMetadata()} straight through silently
* reset them to defaults (issue #4732). Reconstructing from the index's persisted JSON recovers them. FULL_TEXT's
* BM25 corpus counters are reset to zero so the re-index pass recomputes them from scratch instead of doubling the
* persisted totals; GEOSPATIAL's tokenization is reset to {@link GeoIndexMetadata#DEFAULT_TOKENIZATION}, which is
* also the one safe moment (a full re-read of every record) to publish the compact FRONTIER layout on an index
* created before 26.8.1 (#5478).
* The metadata an index rebuild has to carry over into the new index file: everything
* {@link com.arcadedb.index.IndexInternal#getMetadataForNewFile()} answers, retargeted to the type and properties the
* rebuilt index is created against, with the two adjustments a rebuild - and only a rebuild - is entitled to make.
* <p>
* FULL_TEXT's BM25 corpus counters are left behind (which {@link FullTextIndexMetadata#copy} already does) so the
* re-index pass recomputes them from scratch instead of doubling the persisted totals. GEOSPATIAL's tokenization is
* reset to {@link GeoIndexMetadata#DEFAULT_TOKENIZATION}: a rebuild is a full re-read of every record, which is the
* one safe moment to publish the compact FRONTIER layout on an index created before 26.8.1 (#5478).
* <p>
* Reads through the accessor rather than round-tripping the index's persisted JSON, which is what it used to do for
* FULL_TEXT and GEOSPATIAL and could not do at all for LSM_SPARSE_VECTOR: falling through to {@code getMetadata()}
* there answered with the underlying LSM-Tree's plain metadata, and the sparse builder refuses it outright, so
* {@code REBUILD INDEX} and {@code CHECK DATABASE FIX} on a sparse-vector index failed with "requires
* LSMSparseVectorIndexMetadata but got IndexMetadata" (issue #5742). One accessor answers for every index type, so an
* index type added later cannot be forgotten here.
* <p>
* Shared by {@code RebuildIndexStatement.buildIndex()} and {@code DatabaseChecker}'s auto-fix rebuild path, which
* both drop and recreate a corrupted or explicitly-rebuilt index the same way (issue #5934).
Expand All @@ -350,18 +378,14 @@ public boolean hasAnyCaseInsensitive() {
*/
public static IndexMetadata reconstructForRebuild(final IndexInternal idx, final String typeName,
final String[] propertyNames) {
final Schema.INDEX_TYPE type = idx.getType();
if (type == Schema.INDEX_TYPE.FULL_TEXT) {
final FullTextIndexMetadata ftMeta = new FullTextIndexMetadata(typeName, propertyNames, -1);
ftMeta.fromJSON(idx.toJSON());
ftMeta.setCounters(0L, 0L);
return ftMeta;
} else if (type == Schema.INDEX_TYPE.GEOSPATIAL) {
final GeoIndexMetadata geoMeta = new GeoIndexMetadata(typeName, propertyNames, -1);
geoMeta.fromJSON(idx.toJSON());
geoMeta.setTokenization(GeoIndexMetadata.DEFAULT_TOKENIZATION);
return geoMeta;
}
return idx.getMetadata();
final IndexMetadata source = idx.getMetadataForNewFile();
if (source == null)
return null;

// bucketId -1: the per-bucket builder binds each sub-index during create().
final IndexMetadata metadata = source.copy(typeName, propertyNames, -1);
if (metadata instanceof GeoIndexMetadata geoMetadata)
geoMetadata.setTokenization(GeoIndexMetadata.DEFAULT_TOKENIZATION);
return metadata;
}
}
16 changes: 13 additions & 3 deletions engine/src/main/java/com/arcadedb/schema/LocalDocumentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1554,11 +1554,19 @@ protected void addBucketInternal(final Bucket bucket) {
// getPageSizeForNewFile(), not getPageSize(): this creates a NEW index file, so the page size carried over has
// to be one the creation path accepts. A HASH index predating #5713 can hold an unaddressable one, and
// adding a bucket to its type must not fail because of it (#5713).
//
// getMetadataForNewFile(), not getMetadata(), for the same reason applied to the configuration: on a WRAPPER
// index type the latter answers with the underlying LSM-Tree's plain metadata, so the sub-index minted here
// for the new bucket came up with the DEFAULT analyzer, the DEFAULT geohash resolution or - for a sparse
// vector index, which refuses a plain IndexMetadata outright - not at all (issue #5742). The instance is
// handed over as-is rather than copied: this is one more bucket of the SAME logical index, which is exactly
// what index creation does when it passes one metadata instance to every bucket sub-index, and what keeps
// the full-text corpus counters type-wide instead of restarting them at zero for the new bucket.
schema.createBucketIndex(this, idx.getKeyTypes(), bucket, name, idx.getType(), idx.isUnique(),
idx.getPageSizeForNewFile(),
idx.getNullStrategy(), null, idx.getPropertyNames().toArray(new String[idx.getPropertyNames().size()]), idx,
IndexBuilder.BUILD_BATCH_SIZE,
idx.getMetadata());
idx.getMetadataForNewFile());
}
});
}
Expand Down Expand Up @@ -2262,11 +2270,13 @@ private void createSubIndexesFor(final TypeIndex index, final boolean sharesCall

// Inherit the page size of the index being propagated, like the createBucket() path above does. Hardcoding the
// LSM default here gave a HASH index a page size it cannot address (#5713), and getPageSizeForNewFile() is the
// accessor that guarantees the value is legal to create with.
// accessor that guarantees the value is legal to create with. getMetadataForNewFile() is its counterpart for
// everything that is not the page size, and is handed over as-is, for the reasons spelled out in
// addBucketInternal(): the sub-index created here belongs to the SAME logical index being propagated (#5742).
final Index subIndex = schema.createBucketIndex(this, index.getKeyTypes(), bucket, name, index.getType(),
index.isUnique(), index.getPageSizeForNewFile(), index.getNullStrategy(), null,
index.getPropertyNames().toArray(new String[index.getPropertyNames().size()]), index,
IndexBuilder.BUILD_BATCH_SIZE, index.getMetadata(), !sharesCallerTransaction);
IndexBuilder.BUILD_BATCH_SIZE, index.getMetadataForNewFile(), !sharesCallerTransaction);

created.add(subIndex);
if (sharesCallerTransaction)
Expand Down
17 changes: 17 additions & 0 deletions engine/src/main/java/com/arcadedb/schema/LocalSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,15 @@ public void setEncoding(final String encoding) {
* either side of it is the {@code catch} below: it drops {@code newTypeName}, and with it the buckets and records
* already committed, so a failed copy leaves the schema as it found it rather than a half-built type. The SOURCE type
* is only ever read, so it is unaffected either way.
* <p>
* What the safety net cannot cover is a hard CRASH inside that same window - after the records commit, before or
* during the index build - because nothing runs to drop the copy. What survives is a populated type whose indexes are
* empty or partial, which reads as a working type that silently answers nothing to an indexed query, and
* {@code CHECK DATABASE} does not report it: index checking here is STRUCTURAL ({@code IndexInternal.checkIntegrity}
* walks the key order of the index's own pages), so an index that is merely missing entries looks healthy. The repair
* is {@code REBUILD INDEX <name>} on the copy's indexes, or simply dropping the copy and running {@code copyType()}
* again; both are cheap next to making the operation atomic, which would mean holding every copied record in one
* transaction (issue #5742).
*
* @param typeName type to copy from, left untouched
* @param newTypeName type to create, which must not exist yet
Expand Down Expand Up @@ -2030,6 +2039,12 @@ protected synchronized void readConfiguration() {
// its getAssociatedBucketId(); this metadata only carries the full-text/BM25 configuration, not the binding.
final FullTextIndexMetadata ftMeta = new FullTextIndexMetadata(typeName, properties, -1);
ftMeta.fromJSON(indexJSON);
// The bucket-level index JSON carries no "typeName" key, so fromJSON() above skipped the base-field
// read: take the collations and the manual TypeIndex name from the underlying definition, which
// setMetadata(indexJSON) has just populated. Without this the full-text metadata comes back from a
// restart missing both, and every site that carries the definition into a new index file through
// getMetadataForNewFile() loses them (issue #5742).
ftMeta.inheritCommonSettingsFrom(index.getMetadata());
// Same reserved-name guard as the creation path, in case a hand-edited/restored schema reintroduced a property
// colliding with the query parser's default-field sentinel.
LSMTreeFullTextIndex.checkReservedPropertyNames(ftMeta.propertyNames);
Expand All @@ -2044,6 +2059,8 @@ protected synchronized void readConfiguration() {
} else if (configuredIndexType.equalsIgnoreCase(Schema.INDEX_TYPE.LSM_SPARSE_VECTOR.toString())) {
final LSMSparseVectorIndexMetadata sparseMeta = new LSMSparseVectorIndexMetadata(typeName, properties, -1);
sparseMeta.fromJSON(indexJSON);
// Same reason as the full-text branch above (issue #5742).
sparseMeta.inheritCommonSettingsFrom(index.getMetadata());
index = new LSMSparseVectorIndex((LSMTreeIndex) index, sparseMeta);
indexMap.put(indexName, index);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ void truncatingAManuallyNamedVectorIndexKeepsItsName() {

assertThat(database.getSchema().existsIndex("myVectorIndex")).isTrue();

database.transaction(() -> database.command("sql", "TRUNCATE TYPE Doc"));
// NOT inside a transaction: with one active TRUNCATE TYPE joins it and deletes record by record, which never
// drops the index at all - so the carry-over this test is about would not run (issue #6220).
database.command("sql", "TRUNCATE TYPE Doc UNSAFE").close();

assertThat(database.getSchema().existsIndex("myVectorIndex")).as("the truncated index must keep its manual name")//
.isTrue();
Expand Down
Loading
Loading