-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,530 additions
and
559 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
96 changes: 96 additions & 0 deletions
96
vokter-cassandra/src/main/java/com/edduarte/vokter/persistence/cassandra/CassandraDiff.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...ndra/src/main/java/com/edduarte/vokter/persistence/cassandra/CassandraDiffCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.