Browse Source

implement writing fk indexes

git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/mutateops@1019 f203690c-595d-4dc9-a70b-905162fa7fd2
tags/jackcess-2.1.5
James Ahlborn 7 years ago
parent
commit
7362dd2392

+ 14
- 6
src/main/java/com/healthmarketscience/jackcess/impl/RelationshipCreator.java View File

@@ -39,7 +39,11 @@ public class RelationshipCreator extends DBMutator
RelationshipImpl.CASCADE_DELETES_FLAG |
RelationshipImpl.CASCADE_UPDATES_FLAG |
RelationshipImpl.CASCADE_NULL_FLAG;

// for the purposes of choosing a backing index for a foreign key, there are
// certain index flags that can be ignored (we don't care how they are set)
private final static byte IGNORED_INDEX_FLAGS =
IndexData.IGNORE_NULLS_INDEX_FLAG | IndexData.REQUIRED_INDEX_FLAG;
private TableImpl _primaryTable;
private TableImpl _secondaryTable;
@@ -115,13 +119,13 @@ public class RelationshipCreator extends DBMutator
private void addPrimaryIndex() throws IOException {
TableUpdater updater = new TableUpdater(_primaryTable);
updater.setForeignKey(createFKReference(true));
updater.addIndex(createPrimaryIndex(), true);
updater.addIndex(createPrimaryIndex(), true, IGNORED_INDEX_FLAGS, (byte)0);
}

private void addSecondaryIndex() throws IOException {
TableUpdater updater = new TableUpdater(_secondaryTable);
updater.setForeignKey(createFKReference(false));
updater.addIndex(createSecondaryIndex(), true);
updater.addIndex(createSecondaryIndex(), true, IGNORED_INDEX_FLAGS, (byte)0);
}

private IndexImpl.ForeignKeyReference createFKReference(boolean isPrimary) {
@@ -131,11 +135,15 @@ public class RelationshipCreator extends DBMutator
if(isPrimary) {
tableType = IndexImpl.PRIMARY_TABLE_TYPE;
otherTableNum = _secondaryTable.getTableDefPageNumber();
// we create the primary index first, so the secondary index does not
// exist yet
otherIdxNum = _secondaryTable.getLogicalIndexCount();
} else {
tableType = IndexImpl.SECONDARY_TABLE_TYPE;
otherTableNum = _primaryTable.getTableDefPageNumber();
otherIdxNum = _primaryTable.getLogicalIndexCount();
// at this point, we've already created the primary index, it's the last
// one on the primary table
otherIdxNum = _primaryTable.getLogicalIndexCount() - 1;
}
boolean cascadeUpdates = ((_flags & RelationshipImpl.CASCADE_UPDATES_FLAG) != 0);
boolean cascadeDeletes = ((_flags & RelationshipImpl.CASCADE_DELETES_FLAG) != 0);
@@ -267,9 +275,9 @@ public class RelationshipCreator extends DBMutator
String baseName = null;
String suffix = null;
if(isPrimary) {
// primary naming scheme: ".rC", ".rD", "rE" ...
// primary naming scheme: ".rB", .rC", ".rD", "rE" ...
baseName = ".r";
suffix = "C";
suffix = "B";
} else {
// secondary naming scheme: "<t1><t2>", "<t1><t2>1", "<t1><t2>2"
baseName = _primaryTable.getName() + _secondaryTable.getName();

+ 9
- 6
src/main/java/com/healthmarketscience/jackcess/impl/TableUpdater.java View File

@@ -145,11 +145,13 @@ public class TableUpdater extends TableMutator
}

public IndexImpl addIndex(IndexBuilder index) throws IOException {
return addIndex(index, false);
return addIndex(index, false, (byte)0, (byte)0);
}

IndexImpl addIndex(IndexBuilder index, boolean isInternal) throws IOException {

IndexImpl addIndex(IndexBuilder index, boolean isInternal, byte ignoreIdxFlags,
byte ignoreColFlags)
throws IOException
{
_index = index;

if(!isInternal) {
@@ -161,7 +163,7 @@ public class TableUpdater extends TableMutator
_index.setIndexNumber(indexNumber);

// initialize backing index state
initIndexDataState();
initIndexDataState(ignoreIdxFlags, ignoreColFlags);

if(!isInternal) {
getPageChannel().startExclusiveWrite();
@@ -254,14 +256,15 @@ public class TableUpdater extends TableMutator
return idxNames;
}

private void initIndexDataState() {
private void initIndexDataState(byte ignoreIdxFlags, byte ignoreColFlags) {

_idxDataState = new IndexDataState();
_idxDataState.addIndex(_index);
// search for an existing index which matches the given index (in terms of
// the backing data)
IndexData idxData = findIndexData(_index, _table, (byte)0, (byte)0);
IndexData idxData = findIndexData(
_index, _table, ignoreIdxFlags, ignoreColFlags);

int idxDataNumber = ((idxData != null) ?
idxData.getIndexDataNumber() :

Loading…
Cancel
Save