Skip to content

Commit

Permalink
Improved usage API.
Browse files Browse the repository at this point in the history
  • Loading branch information
EdDuarte committed Jul 2, 2016
1 parent 296464c commit da081b6
Show file tree
Hide file tree
Showing 36 changed files with 1,530 additions and 559 deletions.
150 changes: 81 additions & 69 deletions README.md

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions vokter-cassandra/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2014 Eduardo Duarte
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.edduarte</groupId>
<artifactId>vokter</artifactId>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>vokter-cassandra</artifactId>
<version>1.4.1</version>

<name>vokter-cassandra</name>

<developers>
<developer>
<id>edduarte</id>
<name>Eduardo Duarte</name>
<email>hello@edduarte.com</email>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
</developer>
</developers>

<build>
<directory>../target</directory>
<outputDirectory>../target/vokter-cassandra</outputDirectory>
<finalName>${project.artifactId}</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
</build>

<dependencies>

<dependency>
<groupId>com.edduarte</groupId>
<artifactId>vokter-core</artifactId>
<version>1.4.1</version>
</dependency>


<!-- db -->

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2015 Eduardo Duarte
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.edduarte.vokter.persistence.cassandra;

import com.edduarte.vokter.diff.DiffDetector;
import com.edduarte.vokter.diff.DiffEvent;
import com.edduarte.vokter.persistence.Diff;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

import java.io.Serializable;

/**
* A difference represents a addition or a removal of an occurrence from a document.
*
* @author Eduardo Duarte (<a href="mailto:hello@edduarte.com">hello@edduarte.com</a>)
* @version 1.3.2
* @since 1.0.0
*/
public class CassandraDiff extends BasicDBObject implements Diff, Serializable {

public static final String DIFF_EVENT = "diff_event";

public static final String TEXT = "text";

public static final String START_INDEX = "start_index";

public static final String END_INDEX = "end_index";

private static final long serialVersionUID = 1L;


public CassandraDiff(final DiffEvent action,
final String occurrenceText,
final int startIndex) {
super(DIFF_EVENT, action.toString());
append(TEXT, occurrenceText);
append(START_INDEX, startIndex);
append(END_INDEX, startIndex + occurrenceText.length());
}


public CassandraDiff(DiffDetector.Result r) {
this(r.getEvent(), r.getText(), r.getStartIndex());
}


public CassandraDiff(DBObject mongoObject) {
super(mongoObject.toMap());
}


/**
* Returns the status of this difference.
*/
@Override
public DiffEvent getEvent() {
String event = getString(DIFF_EVENT);
return DiffEvent.valueOf(event);
}


/**
* Returns the text of the occurrence contained within this difference.
*/
@Override
public String getText() {
return getString(TEXT);
}


@Override
public int getStartIndex() {
return getInt(START_INDEX);
}


@Override
public int getEndIndex() {
return getInt(END_INDEX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.edduarte.vokter.persistence.cassandra;

import com.edduarte.vokter.diff.DiffDetector;
import com.edduarte.vokter.persistence.Diff;
import com.edduarte.vokter.persistence.DiffCollection;
import com.mongodb.BulkWriteOperation;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* @author Eduardo Duarte (<a href="mailto:hello@edduarte.com">hello@edduarte.com</a>)
* @version 1.0.0
* @since 1.0.0
*/
public class CassandraDiffCollection implements DiffCollection {

private final DB db;


/**
* Instantiate the Collection object, which represents the core access to
* diffs using MongoDB persistence mechanisms.
*/
public CassandraDiffCollection(DB db) {
this.db = db;
}


private static String getDiffCollectionName(String url, String contentType) {
return
// "diffs|" +
url + "|" + contentType;
}


@Override
public void addDifferences(String documentUrl, String documentContentType,
List<DiffDetector.Result> diffs) {
DBCollection diffColl = db.getCollection(
getDiffCollectionName(documentUrl, documentContentType));
BulkWriteOperation bulkOp = diffColl.initializeUnorderedBulkOperation();
diffs.parallelStream()
.map(CassandraDiff::new)
.forEach(bulkOp::insert);
bulkOp.execute();
bulkOp = null;
diffColl = null;
}


@Override
public List<Diff> getDifferences(String documentUrl, String documentContentType) {
// check diffs stored on the database
DBCollection diffColl = db.getCollection(
getDiffCollectionName(documentUrl, documentContentType));
long count = diffColl.count();
if (count <= 0) {
return Collections.emptyList();
}

Iterable<DBObject> cursor = diffColl.find();
return StreamSupport.stream(cursor.spliterator(), true)
.map(CassandraDiff::new)
.collect(Collectors.toList());
}


@Override
public void removeDifferences(String documentUrl, String documentContentType) {
DBCollection diffColl = db.getCollection(
getDiffCollectionName(documentUrl, documentContentType));
diffColl.drop();
}
}
Loading

0 comments on commit da081b6

Please sign in to comment.