Skip to content

Commit

Permalink
Merge pull request #1 from sirixdb/master
Browse files Browse the repository at this point in the history
updating my fork
  • Loading branch information
Fulton Browne committed Sep 27, 2019
2 parents 44ef4bc + c841269 commit 910cadc
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -38,6 +40,7 @@
import org.sirix.api.PageReadOnlyTrx;
import org.sirix.api.PageTrx;
import org.sirix.api.ResourceManager;
import org.sirix.api.RevisionInfo;
import org.sirix.api.xml.XmlNodeTrx;
import org.sirix.cache.BufferManager;
import org.sirix.exception.SirixException;
Expand Down Expand Up @@ -186,6 +189,25 @@ public PageTrx<Long, Record, UnorderedKeyValuePage> createPageWriteTransaction(f
writer, id, representRevision, storedRevision, lastCommitedRev, isBoundToNodeTrx);
}

@Override
public List<RevisionInfo> getHistory() {
final int lastCommittedRevision = mLastCommittedUberPage.get().getRevisionNumber();

final var revisionInfos = new ArrayList<RevisionInfo>();

// TODO: Do this in parallel but maybe using Kotlin Coroutines, if we switch to Kotlin.
for (int revision = lastCommittedRevision; revision > 0; revision--) {
try (final NodeReadOnlyTrx rtx = beginNodeReadOnlyTrx(revision)) {
final CommitCredentials commitCredentials = rtx.getCommitCredentials();

revisionInfos.add(new RevisionInfo(commitCredentials.getUser(), rtx.getRevisionNumber(),
rtx.getRevisionTimestamp(), commitCredentials.getMessage()));
}
}

return revisionInfos;
}

@Override
public Path getResourcePath() {
return mResourceConfig.resourcePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,27 @@
*/
package org.sirix.access.trx.node;

import org.sirix.access.User;

/**
* @author Johannes Lichtenberger <lichtenberger.johannes@gmail.com>
*
*/
public final class CommitCredentials {
private final String mUserName;
private final User user;

private final String mCommitMessage;
private final String commitMessage;

public CommitCredentials(final String userName, final String commitMessage) {
mUserName = userName;
mCommitMessage = commitMessage;
public CommitCredentials(final User user, final String commitMessage) {
this.user = user;
this.commitMessage = commitMessage;
}

public String getUserName() {
return mUserName;
public User getUser() {
return user;
}

public String getMessage() {
return mCommitMessage;
return commitMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.nio.file.Path;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnegative;
Expand Down Expand Up @@ -72,6 +73,13 @@ public interface ResourceManager<R extends NodeReadOnlyTrx & NodeCursor, W exten
*/
Path getResourcePath();

/**
* Get the history, that is the metadata informations about the revisions.
*
* @return the history
*/
List<RevisionInfo> getHistory();

/**
* Get the single node writer if available, wrapped in an {@link Optional}.
*
Expand Down
78 changes: 78 additions & 0 deletions bundles/sirix-core/src/main/java/org/sirix/api/RevisionInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.sirix.api;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;
import org.sirix.access.User;
import com.google.common.base.MoreObjects;

public final class RevisionInfo {
private final User user;

private final int revision;

private final Instant revisionTimestamp;

private final String commitMessage;

private int hash;

public RevisionInfo(final User user, final int revision, final Instant revisionTimestamp,
final String commitMessage) {
this.user = checkNotNull(user);

checkArgument(revision >= 0);

this.revision = revision;
this.revisionTimestamp = checkNotNull(revisionTimestamp);
this.commitMessage = commitMessage;
}

public User getUser() {
return user;
}

public int getRevision() {
return revision;
}

public Instant getRevisionTimestamp() {
return revisionTimestamp;
}

public Optional<String> getCommitMessage() {
return Optional.ofNullable(commitMessage);
}

@Override
public int hashCode() {
if (hash == 0) {
hash = Objects.hash(user, revision, revisionTimestamp, commitMessage);
}
return hash;
}

@Override
public boolean equals(final Object other) {
if (!(other instanceof RevisionInfo))
return false;

final RevisionInfo otherRevisionInfo = (RevisionInfo) other;

return this.user == otherRevisionInfo.user && this.revision == otherRevisionInfo.revision
&& revisionTimestamp.equals(otherRevisionInfo.revisionTimestamp)
&& Objects.equals(this.commitMessage, otherRevisionInfo.commitMessage);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("user", user)
.add("revision", revision)
.add("revisionTimestamp", revisionTimestamp)
.add("commitMessage", commitMessage)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public void setUser(final User user) {
}

public CommitCredentials getCommitCredentials() {
return new CommitCredentials(null, mCommitMessage);
return new CommitCredentials(mUser, mCommitMessage);
}

public Optional<User> getUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ protected void emitRevisionEndNode(final @Nonnull JsonNodeReadOnlyTrx rtx) {
}

private boolean hasMoreRevisionsToSerialize(final JsonNodeReadOnlyTrx rtx) {
return rtx.getRevisionNumber() < mRevisions[mRevisions.length - 1] || (mRevisions.length == 1
&& mRevisions[0] == -1 && rtx.getRevisionNumber() < rtx.getResourceManager().getMostRecentRevisionNumber());
return rtx.getRevisionNumber() < mRevisions[mRevisions.length - 1] || (mRevisions.length == 1 && mRevisions[0] == -1
&& rtx.getRevisionNumber() < rtx.getResourceManager().getMostRecentRevisionNumber());
}

/**
Expand Down Expand Up @@ -458,7 +458,7 @@ public Builder(final JsonResourceManager resourceMgr, final Appendable stream, f
*/
public Builder(final JsonResourceManager resourceMgr, final @Nonnegative long nodeKey, final Writer stream,
final JsonSerializerProperties properties, final int... revisions) {
checkArgument(nodeKey >= 0, "pNodeKey must be >= 0!");
checkArgument(nodeKey >= 0, "nodeKey must be >= 0!");
mResourceMgr = checkNotNull(resourceMgr);
mNodeKey = nodeKey;
mStream = checkNotNull(stream);
Expand Down
82 changes: 82 additions & 0 deletions bundles/sirix-core/src/test/java/org/sirix/access/UpdateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Iterator;
import java.util.Optional;
Expand Down Expand Up @@ -119,6 +120,87 @@ public void testUserNameRetrievalWhenReverting() {
}
}

@Test
public void testGettingHistory() {
final var user = new User("Johannes Lichtenberger", UUID.randomUUID());
try (final var database = XmlTestHelper.getDatabase(XmlTestHelper.PATHS.PATH1.getFile(), user);
final var manager = database.openResourceManager(XmlTestHelper.RESOURCE);
final var wtx = manager.beginNodeTrx()) {
XmlDocumentCreator.createVersioned(wtx);
}

try (final var database = Databases.openXmlDatabase(XmlTestHelper.PATHS.PATH1.getFile(), user);
final var manager = database.openResourceManager(XmlTestHelper.RESOURCE)) {
final var history = manager.getHistory();

assertEquals(3, history.size());

assertEquals(3, history.get(0).getRevision());
assertNotNull(history.get(0).getRevisionTimestamp());
assertTrue(history.get(0).getCommitMessage().isEmpty());
assertEquals("Johannes Lichtenberger", history.get(0).getUser().getName());

assertEquals(2, history.get(1).getRevision());
assertNotNull(history.get(1).getRevisionTimestamp());
assertTrue(history.get(1).getCommitMessage().isEmpty());
assertEquals("Johannes Lichtenberger", history.get(1).getUser().getName());

assertEquals(1, history.get(2).getRevision());
assertNotNull(history.get(2).getRevisionTimestamp());
assertTrue(history.get(2).getCommitMessage().isEmpty());
assertEquals("Johannes Lichtenberger", history.get(2).getUser().getName());
}
}

@Test
public void testGettingHistoryWithCommitMessageAndDifferentUser() {
final var user = new User("Johannes Lichtenberger", UUID.randomUUID());
try (final var database = XmlTestHelper.getDatabase(XmlTestHelper.PATHS.PATH1.getFile(), user);
final var manager = database.openResourceManager(XmlTestHelper.RESOURCE);
final var wtx = manager.beginNodeTrx()) {
XmlDocumentCreator.create(wtx);
wtx.commit();
wtx.moveToDocumentRoot();
wtx.moveToFirstChild();
wtx.insertElementAsFirstChild(new QNm("ns", "p", "a"));
wtx.insertTextAsFirstChild("OOPS4!");
wtx.commit("Insert element and text nodes");
}

final var newUser = new User("Marc Kramis", UUID.randomUUID());

try (final var database = Databases.openXmlDatabase(XmlTestHelper.PATHS.PATH1.getFile(), newUser);
final var manager = database.openResourceManager(XmlTestHelper.RESOURCE);
final var wtx = manager.beginNodeTrx()) {
wtx.moveToFirstChild();
wtx.insertElementAsFirstChild(new QNm("ns", "p", "a"));
wtx.insertTextAsFirstChild("OOPS4!");
wtx.commit("Insert a second element and text node");
}

try (final var database = Databases.openXmlDatabase(XmlTestHelper.PATHS.PATH1.getFile(), user);
final var manager = database.openResourceManager(XmlTestHelper.RESOURCE)) {
final var history = manager.getHistory();

assertEquals(3, history.size());

assertEquals(3, history.get(0).getRevision());
assertNotNull(history.get(0).getRevisionTimestamp());
assertEquals("Insert a second element and text node", history.get(0).getCommitMessage().get());
assertEquals("Marc Kramis", history.get(0).getUser().getName());

assertEquals(2, history.get(1).getRevision());
assertNotNull(history.get(1).getRevisionTimestamp());
assertEquals("Insert element and text nodes", history.get(1).getCommitMessage().get());
assertEquals("Johannes Lichtenberger", history.get(1).getUser().getName());

assertEquals(1, history.get(2).getRevision());
assertNotNull(history.get(2).getRevisionTimestamp());
assertTrue(history.get(2).getCommitMessage().isEmpty());
assertEquals("Johannes Lichtenberger", history.get(2).getUser().getName());
}
}

@Test
public void testDelete() {
try (final XmlNodeTrx wtx = holder.getResourceManager().beginNodeTrx()) {
Expand Down
Loading

0 comments on commit 910cadc

Please sign in to comment.