Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2013 eBuddy B.V.
*
* 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.ebuddy.cassandra;
import static com.ebuddy.cassandra.HectorUtils.bytes;
import static com.ebuddy.cassandra.HectorUtils.getSlice;
import static com.ebuddy.cassandra.HectorUtils.string;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.Mutation;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SuperColumn;
import org.apache.commons.collections.KeyValue;
import org.apache.commons.collections.keyvalue.DefaultKeyValue;
import com.ebuddy.cassandra.property.PropertyValue;
import com.ebuddy.cassandra.property.PropertyValueFactory;
/**
* An NestedBatchMutation encapsulates a set of updates/insertions/deletions all submitted
* at the same time to Cassandra.
*
* Similar to the Hector BatchMutation, but this implementation also supports a delta
* insertion to an embedded Map object, to support hierarchically nested properties.
*
* A NestedBatchMutation is also limited to a single row and single column family.
*
* @author Eric Zoerner [email protected]
* @deprecated use DAO objects and ExtendedNetworkId instead
*
*/
@Deprecated
class NestedBatchMutation {
private final CassandraTemplate cassandraTemplate;
private final String keySpace;
private final String rowKey;
private final String columnFamily;
private final String superColumnName; // may be null
/** map of mutations, new values keyed by column name. */
private final Map mutations = new HashMap();
/** map of partial mutations, keyed by column name. */
private final Map partials = new HashMap();
private boolean partialsApplied = false;
/**
* Construct a NestedBatchMutation.
*
* @param cassandraTemplate
* @param keySpace
* @param rowKey the row key
* @param columnFamily the column family
* @param superColumnName the super column name, or null if this is a regular column family
*/
NestedBatchMutation(CassandraTemplate cassandraTemplate,
String keySpace,
String rowKey,
String columnFamily,
String superColumnName) {
this.cassandraTemplate = cassandraTemplate;
this.keySpace = keySpace;
if(rowKey == null) {
throw new IllegalArgumentException("rowKey should not be null");
}
if (columnFamily == null) {
throw new IllegalArgumentException("columnFamily should not be null");
}
this.rowKey = rowKey;
this.columnFamily = columnFamily;
this.superColumnName = superColumnName;
}
/**
* Add an Column insertion (or update) to the batch mutation request.
*
* @param path the property name, can be hierarchical using delimiter
* @param value the new String value for the property
* @return the receiver
*/
NestedBatchMutation addInsertion(String path, String value, String delimiter) {
if (partialsApplied) {
throw new IllegalStateException("cannot insert after getMutationMap() is called");
}
if (path.indexOf(delimiter) >= 0) {
// first part of path is the column name, extract rest for the relative path
String[] parts = splitFastAndURLDecode(path,delimiter);
int restLen = parts.length - 1;
String[] rest = new String[restLen];
System.arraycopy(parts, 1, rest, 0, restLen);
insertPartialMutation(parts[0], rest, value);
} else {
mutations.put(urlDecode(path), bytes(value));
}
return this;
}
/**
* Calculate and return the mutation map for updating Cassandra.
* For hierarchical properties, reads the current value from Cassandra, then applies the nested properties to it.
*
* @return the mutation map
*/
Map>> getMutationMap() {
if (!partials.isEmpty()) {
fillInOldValues();
buildMutationsFromPartials();
}
return Collections.unmodifiableMap(createMutationMapIgnoringPartials());
}
private void fillInOldValues() {
// incorporate the partial mutations into the list of mutations.
// The old values need to be retrieved first with a single
// getSlice. Build up a SlicePredicate to fill in the old values.
SlicePredicate slicePredicate = new SlicePredicate();
for (Map.Entry partialKeyValue : partials.entrySet()) {
// the columnName is the key of the map entry
String columnName = partialKeyValue.getKey();
// add this column to the predicate
slicePredicate.addToColumn_names(bytes(columnName));
}
// read old values from Cassandra
List columns = getSlice(rowKey,
columnFamily,
superColumnName,
slicePredicate,
cassandraTemplate,
keySpace);
for (Column column : columns) {
String columnName = string(column.getName());
partials.get(columnName).oldValue = column.bufferForValue();
}
}
private void buildMutationsFromPartials() {
for (Map.Entry partialKeyValue : partials.entrySet()) {
PartialMutation partialMutation = partialKeyValue.getValue();
PropertyValue> oldValuePropertyValue = null;
ByteBuffer oldValueBytes = partialMutation.oldValue;
if (oldValueBytes != null) {
oldValuePropertyValue = PropertyValueFactory.get().createPropertyValue(oldValueBytes);
}
Map> topMap;
if (oldValuePropertyValue == null || !oldValuePropertyValue.isNestedProperties()) {
// if not nested properties, then storing a nested property ON TOP of a non-nested value.
// in this case, ignore the old value, will be overwritten with a new NestedProperties
partialMutation.oldValue = null;
topMap = new HashMap>();
} else {
@SuppressWarnings({"unchecked"})
PropertyValue