public <ID> Composite createKeyForCounter(String fqcn, ID key, PropertyMeta<Void, ID> idMeta) { Composite comp = new Composite(); comp.setComponent(0, fqcn, STRING_SRZ); comp.setComponent(1, idMeta.writeValueToString(key), STRING_SRZ); return comp; }
@SuppressWarnings("unchecked") public <K, V, T> Composite createForQuery( PropertyMeta<K, V> propertyMeta, T keyValue, ComponentEquality equality) { log.trace("Creating query composite for propertyMeta {}", propertyMeta.getPropertyName()); Composite composite = new Composite(); String propertyName = propertyMeta.getPropertyName(); if (propertyMeta.isSingleKey()) { if (keyValue == null) { composite = null; } else { composite.addComponent(0, keyValue, equality); } } else { MultiKeyProperties multiKeyProperties = propertyMeta.getMultiKeyProperties(); List<Serializer<?>> componentSerializers = multiKeyProperties.getComponentSerializers(); List<Object> keyValues = entityHelper.determineMultiKey(keyValue, multiKeyProperties.getComponentGetters()); int srzCount = componentSerializers.size(); int valueCount = keyValues.size(); Validator.validateTrue( srzCount >= valueCount, "There should be at most" + srzCount + " values for the key of WideMap '" + propertyName + "'"); int lastNotNullIndex = helper.findLastNonNullIndexForComponents(propertyName, keyValues); for (int i = 0; i <= lastNotNullIndex; i++) { Serializer<Object> srz = (Serializer<Object>) componentSerializers.get(i); Object value = keyValues.get(i); if (i < lastNotNullIndex) { composite.setComponent(i, value, srz, srz.getComparatorType().getTypeName(), EQUAL); } else { composite.setComponent(i, value, srz, srz.getComparatorType().getTypeName(), equality); } } } return composite; }
@SuppressWarnings("unchecked") public <K, V, T> Composite createBaseComposite(PropertyMeta<K, V> propertyMeta, T keyValue) { log.trace("Creating base composite for propertyMeta {}", propertyMeta.getPropertyName()); Composite composite = new Composite(); String propertyName = propertyMeta.getPropertyName(); if (propertyMeta.isSingleKey()) { Validator.validateNotNull( keyValue, "The values for the for the key of WideMap '" + propertyName + "' should not be null"); Serializer<T> keySerializer = (Serializer<T>) propertyMeta.getKeySerializer(); composite.setComponent( 0, keyValue, keySerializer, keySerializer.getComparatorType().getTypeName()); } else { MultiKeyProperties multiKeyProperties = propertyMeta.getMultiKeyProperties(); List<Serializer<?>> componentSerializers = multiKeyProperties.getComponentSerializers(); List<Object> keyValues = entityHelper.determineMultiKey(keyValue, multiKeyProperties.getComponentGetters()); int srzCount = componentSerializers.size(); int valueCount = keyValues.size(); Validator.validateTrue( srzCount == valueCount, "There should be " + srzCount + " values for the key of WideMap '" + propertyName + "'"); for (Object value : keyValues) { Validator.validateNotNull( value, "The values for the for the key of WideMap '" + propertyName + "' should not be null"); } for (int i = 0; i < srzCount; i++) { Serializer<Object> srz = (Serializer<Object>) componentSerializers.get(i); composite.setComponent(i, keyValues.get(i), srz, srz.getComparatorType().getTypeName()); } } return composite; }
private Composite getCompositeEnd(String ID, String URN, int situation, Node[] q) { Composite columnStop = new Composite(); switch (situation) { // ID:*:* case 1: columnStop.addComponent(0, ID, Composite.ComponentEquality.GREATER_THAN_EQUAL); break; case 2: // URN:*:* columnStop.addComponent(0, URN, Composite.ComponentEquality.GREATER_THAN_EQUAL); break; case 3: // ID:URN:* columnStop.addComponent(0, ID, Composite.ComponentEquality.EQUAL); columnStop.addComponent(1, URN, Composite.ComponentEquality.GREATER_THAN_EQUAL); break; case 4: // URN:ID:* columnStop.addComponent(0, URN, Composite.ComponentEquality.EQUAL); columnStop.addComponent(1, ID, Composite.ComponentEquality.GREATER_THAN_EQUAL); break; case 5: // *:*:* break; case 6: // ID:URN:prop columnStop.addComponent(0, ID, Composite.ComponentEquality.EQUAL); columnStop.addComponent(1, URN, Composite.ComponentEquality.EQUAL); columnStop.addComponent( 2, q[1].toN3() + "_", Composite.ComponentEquality.GREATER_THAN_EQUAL); break; case 7: // ID:URN:prop-value columnStop.addComponent(0, ID, Composite.ComponentEquality.EQUAL); columnStop.addComponent(1, URN, Composite.ComponentEquality.EQUAL); columnStop.addComponent( 2, q[1].toN3() + " " + q[2].toN3() + "_", Composite.ComponentEquality.GREATER_THAN_EQUAL); break; default: break; } return columnStop; }
@Override public int batchUpdateVersioning( String cf, List<Node[]> li, String keyspace, String URN_author, String txID) { Hashtable<String, List<Node[]>> versioned_entities = new Hashtable<String, List<Node[]>>(); Hashtable<String, String> previous_commit_id = new Hashtable<String, String>(); boolean successful_fetch = fetchMostRecentVersions( keyspace, cf, li, txID, URN_author, versioned_entities, previous_commit_id); if (!successful_fetch) { // it means that for one entity there is already a lastCID > txID // we have to abort here the insertion return 2; } // now update the properties into the recent versions fetched for (Iterator<Node[]> it = li.iterator(); it.hasNext(); ) { Node[] current = it.next(); String prop = current[1].toN3(); String value_old = current[2].toN3(); Node value_new = current[3]; List<Node[]> entity_version = versioned_entities.get("<" + current[0].toString() + "-VER>"); for (Iterator<Node[]> it_version = entity_version.iterator(); it_version.hasNext(); ) { Node[] ver_ent = it_version.next(); String ver_prop = ver_ent[1].toN3(); String ver_value = ver_ent[2].toN3(); // check if this is the triple to be replaces if (prop.equals(ver_prop) && value_old.equals(ver_value)) ver_ent[2] = value_new; } } // SPO // insert 's-VER' and 's-URN' new versions Mutator<String> m = HFactory.createMutator(getExistingKeyspace(keyspace), _ss); for (Iterator<String> it = versioned_entities.keySet().iterator(); it.hasNext(); ) { String row_entity_key = it.next(); // there is a list of properties to be added to the new version of this entity List<Node[]> entity_old_version = versioned_entities.get(row_entity_key); String old_version_num = previous_commit_id.get(row_entity_key); for (Iterator it_old_v = entity_old_version.iterator(); it_old_v.hasNext(); ) { Node[] nx = (Node[]) it_old_v.next(); // reorder for the key Node[] reordered = Util.reorder(nx, _maps.get(cf)); String rowKey = new Resource(reordered[0].toString()).toN3(); if (!reordered[0].toString().contains("-VER")) rowKey = new Resource(reordered[0].toString() + "-VER").toN3(); /* this is the approach without using Snowflake int next_ver = old_version_num+1;*/ String next_ver; // if txID is different than null, it means this is called in a transactional context if (txID != null) next_ver = txID; else next_ver = String.valueOf(Integer.valueOf(old_version_num) + 1); // VER, URN Composite colKey = new Composite(); colKey.addComponent(next_ver, StringSerializer.get()); colKey.addComponent(URN_author, StringSerializer.get()); String colKey_s = Nodes.toN3(new Node[] {reordered[1], reordered[2]}); colKey.addComponent(colKey_s, StringSerializer.get()); HColumn<Composite, String> hColumnObj_itemID = HFactory.createColumn(colKey, "", new CompositeSerializer(), StringSerializer.get()); m.addInsertion(rowKey, cf, hColumnObj_itemID); // URN, VER rowKey = new Resource(reordered[0].toString() + "-URN").toN3(); colKey = new Composite(); colKey.addComponent(URN_author, StringSerializer.get()); colKey.addComponent(String.valueOf(next_ver), StringSerializer.get()); colKey_s = Nodes.toN3(new Node[] {reordered[1], reordered[2]}); colKey.addComponent(colKey_s, StringSerializer.get()); hColumnObj_itemID = HFactory.createColumn(colKey, "", new CompositeSerializer(), StringSerializer.get()); m.addInsertion(rowKey, cf, hColumnObj_itemID); } m.execute(); } // now try to write all CID,prevCID; if check my writes is enabled, it can abort in case // there are conflicts return commitOrAbort(keyspace, txID, URN_author, versioned_entities, previous_commit_id); }
@Override protected int batchInsertVersioning( String cf, List<Node[]> li, String keyspace, String URN_author, String txID) { Hashtable<String, List<Node[]>> versioned_entities = new Hashtable<String, List<Node[]>>(); Hashtable<String, String> previous_commit_id = new Hashtable<String, String>(); boolean successful_fetch = fetchMostRecentVersions( keyspace, cf, li, txID, URN_author, versioned_entities, previous_commit_id); if (!successful_fetch) { // it means that for one entity there is already a lastCID > txID // we have to abort here the insertion return 2; } /* // SIMULATE SOME DELAY HERE !! try{ Thread.sleep(System.currentTimeMillis()%10000); }catch(Exception ex) {} // END [THIS MUST BE ERASED IN PRODUCTION!!]*/ // add the new triples to previous versions for (Iterator<Node[]> it_triples = li.iterator(); it_triples.hasNext(); ) { Node[] triple = it_triples.next(); String rowKey = new Resource(triple[0].toString() + "-VER").toN3(); List<Node[]> prev_version = versioned_entities.get(rowKey); prev_version.add(triple); versioned_entities.put(rowKey, prev_version); } // SPO // insert 's-VER' and 's-URN' new versions Mutator<String> m = HFactory.createMutator(getExistingKeyspace(keyspace), _ss); for (Iterator<String> it = versioned_entities.keySet().iterator(); it.hasNext(); ) { String row_entity_key = it.next(); // there is a list of properties to be added to the new version of this entity List<Node[]> entity_old_version = versioned_entities.get(row_entity_key); String old_version_num = previous_commit_id.get(row_entity_key); for (Iterator it_old_v = entity_old_version.iterator(); it_old_v.hasNext(); ) { Node[] nx = (Node[]) it_old_v.next(); // reorder for the key Node[] reordered = Util.reorder(nx, _maps.get(cf)); String rowKey = new Resource(reordered[0].toString()).toN3(); if (!reordered[0].toString().contains("-VER")) rowKey = new Resource(reordered[0].toString() + "-VER").toN3(); /* this is the approach without using Snowflake int next_ver = old_version_num+1;*/ String next_ver; // if txID is different than null, it means this is called in a transactional context if (txID != null) next_ver = txID; else next_ver = String.valueOf(Integer.valueOf(old_version_num) + 1); // VER, URN Composite colKey = new Composite(); colKey.addComponent(next_ver, StringSerializer.get()); colKey.addComponent(URN_author, StringSerializer.get()); String colKey_s = Nodes.toN3(new Node[] {reordered[1], reordered[2]}); colKey.addComponent(colKey_s, StringSerializer.get()); HColumn<Composite, String> hColumnObj_itemID = HFactory.createColumn(colKey, "", new CompositeSerializer(), StringSerializer.get()); m.addInsertion(rowKey, cf, hColumnObj_itemID); // URN, VER rowKey = new Resource(reordered[0].toString() + "-URN").toN3(); colKey = new Composite(); colKey.addComponent(URN_author, StringSerializer.get()); colKey.addComponent(String.valueOf(next_ver), StringSerializer.get()); colKey_s = Nodes.toN3(new Node[] {reordered[1], reordered[2]}); colKey.addComponent(colKey_s, StringSerializer.get()); hColumnObj_itemID = HFactory.createColumn(colKey, "", new CompositeSerializer(), StringSerializer.get()); m.addInsertion(rowKey, cf, hColumnObj_itemID); } m.execute(); } // now try to write all CID,prevCID; if check my writes is enabled, it can abort in case // there are conflicts return commitOrAbort(keyspace, txID, URN_author, versioned_entities, previous_commit_id); }
/** * Encapsulates the creation of Composite to make it easier to experiment with values * * @param componentName * @param equalityOp * @return */ public static Composite compositeFrom( String componentName, Composite.ComponentEquality equalityOp) { Composite composite = new Composite(); composite.addComponent(0, componentName, equalityOp); return composite; }
@Override public Float toEntityValue(Class<?> fieldType, int index, Composite columnName) { return columnName.get(index, DoubleSerializer.get()).floatValue(); }