public void setSubDoc(SubDocument subdoc) throws IllegalArgumentException { if (!table.getSubDocType().equals(subdoc.getType())) { throw new IllegalArgumentException("Type of table " + table + " is " + table.getSubDocType() + " which is " + "different than the type of the given subdocument (" + subdoc.getType() + ")"); } for (String key : subdoc.getType().getAttributeKeys()) { String fieldName = table.subDocHelper().toColumnName(key); //@gortiz: I think we can not use types here! Field f = field(fieldName); ScalarValue<?> v = subdoc.getValue(key); setValue(f, v); } }
@SuppressWarnings("unchecked") private void addToCopy( StringBuilder sb, SubDocument subDocument, String[] orderedFieldNames, String[] orderedAttributeNames) { for (int i = 0; i < orderedFieldNames.length; i++) { String fieldName = orderedFieldNames[i]; String attName = orderedAttributeNames[i]; if (fieldName.equals(SubDocTable.DID_COLUMN_NAME)) { sb.append(subDocument.getDocumentId()); } else if (fieldName.equals(SubDocTable.INDEX_COLUMN_NAME)) { Integer index = translateSubDocIndexToDatabase(subDocument.getIndex()); if (index == null) { sb.append("\\N"); } else { sb.append(index); } } else { subDocument.getValue(attName).accept(PostgreSQLValueToCopyConverter.INSTANCE, sb); } sb.append('\t'); } sb.replace(sb.length() - 1, sb.length(), "\n"); }
public SubDocument build() { SubDocument result = new SubDocument(documentId, index, calculeType(), values); return result; } }
@Override public void insertSubdocuments(String collection, SubDocType type, Iterable<? extends SubDocument> subDocuments) { try { SubDocTable table = meta.getCollectionSchema(collection).getSubDocTable(type); InsertSetMoreStep<?> insert = null; for (SubDocument subDocument : subDocuments) { assert subDocument.getType().equals(type); SubDocTableRecord record = new SubDocTableRecord(table, subDocTypeBuilderProvider); record.setDid(subDocument.getDocumentId()); record.setIndex(translateSubDocIndexToDatabase(subDocument.getIndex())); record.setSubDoc(subDocument); if (insert == null) { insert = dsl.insertInto(table).set(record); } else { insert = insert.newRecord().set(record); } } if (insert != null) { insert.execute(); } else { assert false : "Call to insertSubdocuments with an empty set of subdocuments"; LOGGER.warn("Call to insertSubdocuments with an empty set of subdocuments"); } } catch (DataAccessException ex) { //TODO: Change exception throw new RuntimeException(ex); } }
public Builder add(SubDocument subDocument) { Preconditions.checkState(!build, "This builder has already been used"); SubDocType type = subDocument.getType(); Integer index = indexBySubDoc.get(type); if (index == null) { index = 0; } else { index++; } indexBySubDoc.put(type, index); subDocuments.put(type, index, subDocument); return this; }
docCounter++; assert subDocument.getType().equals(type);
private static KVDocument translateDocStructure(SubDocValueToDocValueTranslator.Argument arg) { DocStructure structure = (DocStructure) arg.structure; SplitDocument splitDocument = arg.splitDoc; LinkedHashMap<String, KVValue<?>> map = new LinkedHashMap<>(); SubDocument subDoc = splitDocument.getSubDocuments().get( structure.getType(), structure.getIndex()); for (String keyName : subDoc.getType().getAttributeKeys()) { //childStructure will be null if the child is a scalar StructureElement childStructure = structure.getElements().get(keyName); arg.structure = childStructure; map.put( keyName, subDoc.getValue(keyName).accept(VALUE_TRANSLATOR, arg) ); } for (Map.Entry<String, StructureElement> entry : structure.getElements().entrySet()) { if (entry.getValue() instanceof DocStructure) { //arrays has already been mapped as values StructureElement childStructure = structure.getElements().get(entry.getKey()); arg.structure = childStructure; map.put( entry.getKey(), translateDocStructure(arg) ); } } return new MapKVDocument(map); }