Skip to content

Commit

Permalink
Merge pull request #1 from sirixdb/master
Browse files Browse the repository at this point in the history
Merge from upstream
  • Loading branch information
mosheduminer committed Jul 29, 2020
2 parents 519cde0 + a6a826e commit 574c575
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.google.common.base.MoreObjects;
import org.sirix.access.json.JsonResourceStore;
import org.sirix.access.trx.node.AfterCommitState;
import org.sirix.api.Database;
import org.sirix.api.ResourceManager;
import org.sirix.api.json.JsonNodeTrx;
Expand Down Expand Up @@ -127,7 +128,7 @@ protected boolean bootstrapResource(ResourceConfiguration resConfig) {
try (final JsonResourceManager resourceTrxManager = openResourceManager(resConfig.getResource()
.getFileName()
.toString());
final JsonNodeTrx wtx = resourceTrxManager.beginNodeTrx()) {
final JsonNodeTrx wtx = resourceTrxManager.beginNodeTrx(AfterCommitState.Close)) {
wtx.commit();
} catch (final SirixException e) {
LOGWRAPPER.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static int compareStructure(final Path file) {

// FIXED STANDARD FIELDS
/** Standard storage. */
private static final StorageType STORAGE = StorageType.MEMORY_MAPPED;
private static final StorageType STORAGE = StorageType.FILE;

/** Standard versioning approach. */
private static final VersioningType VERSIONING = VersioningType.SLIDING_SNAPSHOT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,10 @@ public abstract class AbstractResourceManager<R extends NodeReadOnlyTrx & NodeCu
*/
final User user;

public enum AfterCommitState {
KeepOpen,

Close
}

/**
* Package private constructor.
*
* @param database {@link LocalXmlDatabase} for centralized operations on related sessions
* @param database {@link Database} for centralized operations on related sessions
* @param resourceStore the resource store with which this manager has been created
* @param resourceConf {@link DatabaseConfiguration} for general setting about the storage
* @param bufferManager the cache of in-memory pages shared amongst all resource managers and transactions
Expand Down Expand Up @@ -346,44 +340,45 @@ public Path getCommitFile() {

@Override
public W beginNodeTrx() {
return beginNodeTrx(0, TimeUnit.MINUTES, 0, AfterCommitState.KeepOpen);
return beginNodeTrx(0, 0, TimeUnit.MILLISECONDS, AfterCommitState.KeepOpen);
}

@Override
public W beginNodeTrx(final @Nonnegative int maxNodeCount) {
return beginNodeTrx(maxNodeCount, TimeUnit.MINUTES, 0, AfterCommitState.KeepOpen);
return beginNodeTrx(maxNodeCount, 0, TimeUnit.MILLISECONDS, AfterCommitState.KeepOpen);
}

@Override
public W beginNodeTrx(final @Nonnull TimeUnit timeUnit, final @Nonnegative int maxTime) {
return beginNodeTrx(0, timeUnit, maxTime, AfterCommitState.KeepOpen);
public W beginNodeTrx(final @Nonnegative int maxTime, final @Nonnull TimeUnit timeUnit) {
return beginNodeTrx(0, maxTime, timeUnit, AfterCommitState.KeepOpen);
}

public W beginNodeTrx(final @Nonnegative int maxNodeCount, final @Nonnull TimeUnit timeUnit,
final @Nonnegative int maxTime) {
return beginNodeTrx(maxNodeCount, timeUnit, maxTime, AfterCommitState.KeepOpen);
@Override
public W beginNodeTrx(final @Nonnegative int maxNodeCount, final @Nonnegative int maxTime,
final @Nonnull TimeUnit timeUnit) {
return beginNodeTrx(maxNodeCount, maxTime, timeUnit, AfterCommitState.KeepOpen);
}

@Override
public W beginNodeTrx(final @Nonnull AfterCommitState afterCommitState) {
return beginNodeTrx(0, TimeUnit.MINUTES, 0, afterCommitState);
return beginNodeTrx(0, 0, TimeUnit.MILLISECONDS, afterCommitState);
}

@Override
public W beginNodeTrx(final @Nonnegative int maxNodeCount, final @Nonnull AfterCommitState afterCommitState) {
return beginNodeTrx(maxNodeCount, TimeUnit.MINUTES, 0);
return beginNodeTrx(maxNodeCount, 0, TimeUnit.MILLISECONDS);
}

@Override
public W beginNodeTrx(final @Nonnull TimeUnit timeUnit, final @Nonnegative int maxTime,
public W beginNodeTrx(final @Nonnegative int maxTime, final @Nonnull TimeUnit timeUnit,
final @Nonnull AfterCommitState afterCommitState) {
return beginNodeTrx(0, timeUnit, maxTime, afterCommitState);
return beginNodeTrx(0, maxTime, timeUnit, afterCommitState);
}

@SuppressWarnings("unchecked")
@Override
public synchronized W beginNodeTrx(final @Nonnegative int maxNodeCount, final @Nonnull TimeUnit timeUnit,
final @Nonnegative int maxTime, final @Nonnull AfterCommitState afterCommitState) {
public synchronized W beginNodeTrx(final @Nonnegative int maxNodeCount, final @Nonnegative int maxTime,
final @Nonnull TimeUnit timeUnit, final @Nonnull AfterCommitState afterCommitState) {
// Checks.
assertAccess(lastCommittedUberPage.get().getRevision());
if (maxNodeCount < 0 || maxTime < 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.sirix.access.trx.node;

public enum AfterCommitState {
KeepOpen,

Close
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.brackit.xquery.atomic.QNm;
import org.sirix.access.ResourceConfiguration;
import org.sirix.access.User;
import org.sirix.access.trx.node.AbstractResourceManager;
import org.sirix.access.trx.node.AfterCommitState;
import org.sirix.access.trx.node.CommitCredentials;
import org.sirix.access.trx.node.HashType;
import org.sirix.access.trx.node.InternalResourceManager;
Expand Down Expand Up @@ -128,7 +128,7 @@ final class JsonNodeTrxImpl extends AbstractForwardingJsonNodeReadOnlyTrx implem
/**
* After commit state: keep open or close.
*/
private final AbstractResourceManager.AfterCommitState afterCommitState;
private final AfterCommitState afterCommitState;

/**
* Hashes nodes.
Expand Down Expand Up @@ -232,7 +232,7 @@ final class JsonNodeTrxImpl extends AbstractForwardingJsonNodeReadOnlyTrx implem

private final boolean isAutoCommitting;

private State state;
private volatile State state;

private enum State {
Running,
Expand Down Expand Up @@ -262,7 +262,7 @@ private enum State {
final InternalJsonNodeReadOnlyTrx nodeReadTrx, final PathSummaryWriter<JsonNodeReadOnlyTrx> pathSummaryWriter,
final @Nonnegative int maxNodeCount, final TimeUnit timeUnit, final @Nonnegative int maxTime,
final @Nonnull JsonNodeHashing nodeHashing, final JsonNodeFactory nodeFactory,
final @Nonnull AbstractResourceManager.AfterCommitState afterCommitState) {
final @Nonnull AfterCommitState afterCommitState) {
// Do not accept negative values.
Preconditions.checkArgument(maxNodeCount >= 0 && maxTime >= 0,
"Negative arguments for maxNodeCount and maxTime are not accepted.");
Expand Down Expand Up @@ -348,32 +348,46 @@ private void unLockIfNecessary() {

@Override
public JsonNodeTrx insertSubtreeAsFirstChild(final JsonReader reader) {
return insertSubtree(reader, InsertPosition.AS_FIRST_CHILD, true);
return insertSubtree(reader, InsertPosition.AS_FIRST_CHILD, Commit.Implicit, CheckParentNode.Yes);
}

@Override
public JsonNodeTrx insertSubtreeAsFirstChild(final JsonReader reader, boolean doImplicitCommit) {
return insertSubtree(reader, InsertPosition.AS_FIRST_CHILD, doImplicitCommit);
public JsonNodeTrx insertSubtreeAsFirstChild(final JsonReader reader, Commit commit) {
return insertSubtree(reader, InsertPosition.AS_FIRST_CHILD, commit, CheckParentNode.Yes);
}

@Override
public JsonNodeTrx insertSubtreeAsFirstChild(final JsonReader reader, Commit commit,
CheckParentNode checkParentNode) {
return insertSubtree(reader, InsertPosition.AS_FIRST_CHILD, commit, checkParentNode);
}

@Override
public JsonNodeTrx insertSubtreeAsRightSibling(final JsonReader reader) {
return insertSubtree(reader, InsertPosition.AS_RIGHT_SIBLING, true);
return insertSubtree(reader, InsertPosition.AS_RIGHT_SIBLING, Commit.Implicit, CheckParentNode.Yes);
}

@Override
public JsonNodeTrx insertSubtreeAsRightSibling(final JsonReader reader, Commit commit) {
return insertSubtree(reader, InsertPosition.AS_RIGHT_SIBLING, commit, CheckParentNode.Yes);
}

@Override
public JsonNodeTrx insertSubtreeAsRightSibling(final JsonReader reader, boolean doImplicitCommit) {
return insertSubtree(reader, InsertPosition.AS_RIGHT_SIBLING, false);
public JsonNodeTrx insertSubtreeAsRightSibling(final JsonReader reader, Commit commit,
CheckParentNode checkParentNode) {
return insertSubtree(reader, InsertPosition.AS_RIGHT_SIBLING, commit, checkParentNode);
}

private JsonNodeTrx insertSubtree(final JsonReader reader, final InsertPosition insertionPosition,
boolean doImplicitCommit) {
private JsonNodeTrx insertSubtree(final JsonReader reader, final InsertPosition insertionPosition, Commit commit,
CheckParentNode checkParentNode) {
nodeReadOnlyTrx.assertNotClosed();
checkState();
checkNotNull(reader);
assert insertionPosition != null;

acquireLockIfNecessary();

try {
checkState();
final var peekedJsonToken = reader.peek();

if (peekedJsonToken != JsonToken.BEGIN_OBJECT && peekedJsonToken != JsonToken.BEGIN_ARRAY)
Expand Down Expand Up @@ -403,9 +417,11 @@ private JsonNodeTrx insertSubtree(final JsonReader reader, final InsertPosition
}
}
case AS_RIGHT_SIBLING -> {
final NodeKind parentKind = getParentKind();
if (parentKind != NodeKind.ARRAY) {
throw new IllegalStateException("Current parent node must be an array node.");
if (checkParentNode == CheckParentNode.Yes) {
final NodeKind parentKind = getParentKind();
if (parentKind != NodeKind.ARRAY) {
throw new IllegalStateException("Current parent node must be an array node.");
}
}
}
default -> throw new UnsupportedOperationException();
Expand Down Expand Up @@ -448,11 +464,13 @@ private JsonNodeTrx insertSubtree(final JsonReader reader, final InsertPosition

nodeHashing.setBulkInsert(false);

if (doImplicitCommit) {
if (commit == Commit.Implicit) {
commit();
}
} catch (final IOException e) {
throw new UncheckedIOException(e);
} finally {
unLockIfNecessary();
}
return this;
}
Expand Down Expand Up @@ -515,8 +533,10 @@ public JsonNodeTrx insertObjectAsRightSibling() {
try {
checkAccessAndCommit();

if (getParentKind() != NodeKind.ARRAY) {
throw new SirixUsageException("Insert is not allowed if parent node is not an array node!");
if (!nodeHashing.isBulkInsert()) {
if (getParentKind() != NodeKind.ARRAY) {
throw new SirixUsageException("Insert is not allowed if parent node is not an array node!");
}
}

final StructNode currentNode = nodeReadOnlyTrx.getStructuralNode();
Expand Down Expand Up @@ -846,7 +866,7 @@ private long getPathNodeKey(StructNode structNode) {
private void adaptNodesAndHashesForInsertAsFirstChild(final ImmutableJsonNode node) {
// Adapt local nodes and hashes.
nodeReadOnlyTrx.setCurrentNode(node);
adaptForInsert((StructNode) node, InsertPos.ASFIRSTCHILD);
adaptForInsert((StructNode) node);
nodeReadOnlyTrx.setCurrentNode(node);
nodeHashing.adaptHashesWithAdd();
}
Expand Down Expand Up @@ -956,14 +976,15 @@ public JsonNodeTrx insertBooleanValueAsRightSibling(boolean value) {
}

private void checkPrecondition() {
if (getParentKind() != NodeKind.ARRAY)
if (!nodeHashing.isBulkInsert() && getParentKind() != NodeKind.ARRAY) {
throw new SirixUsageException("Insert is not allowed if parent node is not an array node!");
}
}

private void insertAsRightSibling(final ImmutableJsonNode node) {
// Adapt local nodes and hashes.
nodeReadOnlyTrx.setCurrentNode(node);
adaptForInsert((StructNode) node, InsertPos.ASRIGHTSIBLING);
adaptForInsert((StructNode) node);
nodeReadOnlyTrx.setCurrentNode(node);
nodeHashing.adaptHashesWithAdd();

Expand Down Expand Up @@ -1642,12 +1663,10 @@ private void checkAccessAndCommit() {
* Adapting everything for insert operations.
*
* @param structNode pointer of the new node to be inserted
* @param insertPos determines the position where to insert
* @throws SirixIOException if anything weird happens
*/
private void adaptForInsert(final StructNode structNode, final InsertPos insertPos) {
private void adaptForInsert(final StructNode structNode) {
assert structNode != null;
assert insertPos != null;

final StructNode parent =
(StructNode) pageWriteTrx.prepareEntryForModification(structNode.getParentKey(), PageKind.RECORDPAGE, -1);
Expand Down Expand Up @@ -1831,11 +1850,12 @@ public CommitCredentials getCommitCredentials() {
@Override
public JsonNodeTrx commit(final String commitMessage) {
nodeReadOnlyTrx.assertNotClosed();
state = State.Committing;

// Optionally lock while commiting and assigning new instances.
acquireLockIfNecessary();
try {
state = State.Committing;

// Execute pre-commit hooks.
for (final PreCommitHook hook : preCommitHooks) {
hook.preCommit(this);
Expand All @@ -1854,7 +1874,7 @@ public JsonNodeTrx commit(final String commitMessage) {
}

// Reinstantiate everything.
if (afterCommitState == AbstractResourceManager.AfterCommitState.KeepOpen) {
if (afterCommitState == AfterCommitState.KeepOpen) {
reInstantiate(getId(), getRevisionNumber());
state = State.Running;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.sirix.access.User;
import org.sirix.access.json.JsonResourceStore;
import org.sirix.access.trx.node.AbstractResourceManager;
import org.sirix.access.trx.node.AfterCommitState;
import org.sirix.access.trx.node.InternalResourceManager;
import org.sirix.access.trx.node.xml.XmlIndexController;
import org.sirix.api.Database;
import org.sirix.api.PageReadOnlyTrx;
import org.sirix.api.PageTrx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.google.common.base.Preconditions;
import org.brackit.xquery.atomic.QNm;
import org.sirix.access.User;
import org.sirix.access.trx.node.AbstractResourceManager;
import org.sirix.access.trx.node.AfterCommitState;
import org.sirix.access.trx.node.CommitCredentials;
import org.sirix.access.trx.node.HashType;
import org.sirix.access.trx.node.InternalResourceManager;
Expand Down Expand Up @@ -114,7 +114,7 @@ final class XmlNodeTrxImpl extends AbstractForwardingXmlNodeReadOnlyTrx implemen
*/
private final XmlDeweyIDManager deweyIDManager;

private final AbstractResourceManager.AfterCommitState afterCommitState;
private final AfterCommitState afterCommitState;

/**
* Modification counter.
Expand Down Expand Up @@ -220,7 +220,7 @@ final class XmlNodeTrxImpl extends AbstractForwardingXmlNodeReadOnlyTrx implemen
final InternalXmlNodeReadOnlyTrx nodeReadOnlyTrx, final PathSummaryWriter<XmlNodeReadOnlyTrx> pathSummaryWriter,
final @Nonnegative int maxNodeCount, final TimeUnit timeUnit, final @Nonnegative int maxTime,
final @Nonnull XmlNodeHashing nodeHashing, final XmlNodeFactory nodeFactory,
final AbstractResourceManager.AfterCommitState afterCommitState) {
final @Nonnull AfterCommitState afterCommitState) {
// Do not accept negative values.
Preconditions.checkArgument(maxNodeCount >= 0 && maxTime >= 0,
"Negative arguments for maxNodeCount and maxTime are not accepted.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.sirix.access.ResourceConfiguration;
import org.sirix.access.User;
import org.sirix.access.trx.node.AbstractResourceManager;
import org.sirix.access.trx.node.AfterCommitState;
import org.sirix.access.trx.node.InternalResourceManager;
import org.sirix.access.xml.XmlResourceStore;
import org.sirix.api.Database;
Expand Down
Loading

0 comments on commit 574c575

Please sign in to comment.