Search in sources :

Example 1 with Column

use of com.netflix.astyanax.model.Column in project janusgraph by JanusGraph.

the class AstyanaxKeyColumnValueStore method getNamesSlice.

public Map<StaticBuffer, EntryList> getNamesSlice(List<StaticBuffer> keys, SliceQuery query, StoreTransaction txh) throws BackendException {
    /*
         * RowQuery<K,C> should be parametrized as
         * RowQuery<ByteBuffer,ByteBuffer>. However, this causes the following
         * compilation error when attempting to call withColumnRange on a
         * RowQuery<ByteBuffer,ByteBuffer> instance:
         *
         * java.lang.Error: Unresolved compilation problem: The method
         * withColumnRange(ByteBuffer, ByteBuffer, boolean, int) is ambiguous
         * for the type RowQuery<ByteBuffer,ByteBuffer>
         *
         * The compiler substitutes ByteBuffer=C for both startColumn and
         * endColumn, compares it to its identical twin with that type
         * hard-coded, and dies.
         *
         */
    // Add one for last column potentially removed in CassandraHelper.makeEntryList
    final int queryLimit = query.getLimit() + (query.hasLimit() ? 1 : 0);
    final int pageLimit = Math.min(this.readPageSize, queryLimit);
    ByteBuffer sliceStart = query.getSliceStart().asByteBuffer();
    final ByteBuffer sliceEnd = query.getSliceEnd().asByteBuffer();
    final RowSliceQuery rq = keyspace.prepareQuery(columnFamily).setConsistencyLevel(getTx(txh).getReadConsistencyLevel().getAstyanax()).withRetryPolicy(retryPolicy.duplicate()).getKeySlice(CassandraHelper.convert(keys));
    // Don't directly chain due to ambiguity resolution; see top comment
    rq.withColumnRange(sliceStart, sliceEnd, false, pageLimit);
    final OperationResult<Rows<ByteBuffer, ByteBuffer>> r;
    try {
        r = (OperationResult<Rows<ByteBuffer, ByteBuffer>>) rq.execute();
    } catch (ConnectionException e) {
        throw new TemporaryBackendException(e);
    }
    final Rows<ByteBuffer, ByteBuffer> rows = r.getResult();
    final Map<StaticBuffer, EntryList> result = new HashMap<>(rows.size());
    for (Row<ByteBuffer, ByteBuffer> row : rows) {
        assert !result.containsKey(row.getKey());
        final ByteBuffer key = row.getKey();
        ColumnList<ByteBuffer> pageColumns = row.getColumns();
        final List<Column<ByteBuffer>> queryColumns = new ArrayList();
        Iterables.addAll(queryColumns, pageColumns);
        while (pageColumns.size() == pageLimit && queryColumns.size() < queryLimit) {
            final Column<ByteBuffer> lastColumn = queryColumns.get(queryColumns.size() - 1);
            sliceStart = lastColumn.getName();
            // No possibility of two values at the same column name, so start the
            // next slice one bit after the last column found by the previous query.
            // byte[] is little-endian
            Integer position = null;
            for (int i = sliceStart.array().length - 1; i >= 0; i--) {
                if (sliceStart.array()[i] < Byte.MAX_VALUE) {
                    position = i;
                    sliceStart.array()[i]++;
                    break;
                }
            }
            if (null == position) {
                throw new PermanentBackendException("Column was not incrementable");
            }
            final RowQuery pageQuery = keyspace.prepareQuery(columnFamily).setConsistencyLevel(getTx(txh).getReadConsistencyLevel().getAstyanax()).withRetryPolicy(retryPolicy.duplicate()).getKey(row.getKey());
            // Don't directly chain due to ambiguity resolution; see top comment
            pageQuery.withColumnRange(sliceStart, sliceEnd, false, pageLimit);
            final OperationResult<ColumnList<ByteBuffer>> pageResult;
            try {
                pageResult = (OperationResult<ColumnList<ByteBuffer>>) pageQuery.execute();
            } catch (ConnectionException e) {
                throw new TemporaryBackendException(e);
            }
            if (Thread.interrupted()) {
                throw new TraversalInterruptedException();
            }
            // Reset the incremented position to avoid leaking mutations up the
            // stack to callers - sliceStart.array() in fact refers to a column name
            // that will be later read to deserialize an edge (since we assigned it
            // via de-referencing a column from the previous query).
            sliceStart.array()[position]--;
            pageColumns = pageResult.getResult();
            Iterables.addAll(queryColumns, pageColumns);
        }
        result.put(StaticArrayBuffer.of(key), CassandraHelper.makeEntryList(queryColumns, entryGetter, query.getSliceEnd(), query.getLimit()));
    }
    return result;
}
Also used : TraversalInterruptedException(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalInterruptedException) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) EntryList(org.janusgraph.diskstorage.EntryList) ByteBuffer(java.nio.ByteBuffer) TemporaryBackendException(org.janusgraph.diskstorage.TemporaryBackendException) Column(com.netflix.astyanax.model.Column) RowSliceQuery(com.netflix.astyanax.query.RowSliceQuery) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) ColumnList(com.netflix.astyanax.model.ColumnList) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException) RowQuery(com.netflix.astyanax.query.RowQuery) Rows(com.netflix.astyanax.model.Rows)

Example 2 with Column

use of com.netflix.astyanax.model.Column in project coprhd-controller by CoprHD.

the class DbClientImpl method removeObject.

public void removeObject(Class<? extends DataObject> clazz, DataObject... object) {
    tracer.newTracer("write");
    List<DataObject> allObjects = Arrays.asList(object);
    Keyspace ks = getKeyspace(clazz);
    DataObjectType doType = null;
    RemovedColumnsList removedList = new RemovedColumnsList();
    for (DataObject dataObject : allObjects) {
        _log.debug("Try to remove data object {}", dataObject.getId());
        checkGeoVersionForMutation(dataObject);
        doType = TypeMap.getDoType(dataObject.getClass());
        // delete all the index columns for this object first
        if (doType == null) {
            throw new IllegalArgumentException();
        }
        Row<String, CompositeColumnName> row = queryRowWithAllColumns(ks, dataObject.getId(), doType.getCF());
        if (row != null) {
            Iterator<Column<CompositeColumnName>> it = row.getColumns().iterator();
            String key = row.getKey();
            while (it.hasNext()) {
                Column<CompositeColumnName> column = it.next();
                removedList.add(key, column);
            }
        }
    }
    if (!removedList.isEmpty()) {
        boolean retryFailedWriteWithLocalQuorum = shouldRetryFailedWriteWithLocalQuorum(clazz);
        RowMutator mutator = new RowMutator(ks, retryFailedWriteWithLocalQuorum);
        _indexCleaner.removeColumnAndIndex(mutator, doType, removedList);
    }
}
Also used : PropertyListDataObject(com.emc.storageos.db.client.model.PropertyListDataObject) DataObject(com.emc.storageos.db.client.model.DataObject) Column(com.netflix.astyanax.model.Column) Keyspace(com.netflix.astyanax.Keyspace)

Example 3 with Column

use of com.netflix.astyanax.model.Column in project coprhd-controller by CoprHD.

the class DbClientImpl method queryObjectFields.

@Override
public <T extends DataObject> Collection<T> queryObjectFields(Class<T> clazz, Collection<String> fieldNames, Collection<URI> ids) {
    tracer.newTracer("read");
    DataObjectType doType = TypeMap.getDoType(clazz);
    if (doType == null || ids == null) {
        throw new IllegalArgumentException();
    }
    if (ids.isEmpty()) {
        // nothing to do, just an empty list
        return new ArrayList<T>();
    }
    Set<ColumnField> columnFields = new HashSet<ColumnField>(fieldNames.size());
    for (String fieldName : fieldNames) {
        ColumnField columnField = doType.getColumnField(fieldName);
        if (columnField == null) {
            throw new IllegalArgumentException();
        }
        columnFields.add(columnField);
    }
    Keyspace ks = getKeyspace(clazz);
    Map<URI, T> objectMap = new HashMap<URI, T>();
    for (ColumnField columnField : columnFields) {
        Rows<String, CompositeColumnName> rows = queryRowsWithAColumn(ks, ids, doType.getCF(), columnField);
        Iterator<Row<String, CompositeColumnName>> it = rows.iterator();
        while (it.hasNext()) {
            Row<String, CompositeColumnName> row = it.next();
            try {
                // we have to create an object to track both id and label, for now, lets use the same type
                if (row.getColumns().size() == 0) {
                    continue;
                }
                URI key = URI.create(row.getKey());
                T obj = objectMap.get(key);
                if (obj == null) {
                    obj = (T) DataObject.createInstance(clazz, URI.create(row.getKey()));
                    objectMap.put(key, obj);
                }
                Iterator<Column<CompositeColumnName>> columnIterator = row.getColumns().iterator();
                while (columnIterator.hasNext()) {
                    Column<CompositeColumnName> column = columnIterator.next();
                    columnField.deserialize(column, obj);
                }
            } catch (final InstantiationException e) {
                throw DatabaseException.fatals.queryFailed(e);
            } catch (final IllegalAccessException e) {
                throw DatabaseException.fatals.queryFailed(e);
            }
        }
    }
    // Begin tracking changes
    for (T obj : objectMap.values()) {
        obj.trackChanges();
    }
    return objectMap.values();
}
Also used : NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) Column(com.netflix.astyanax.model.Column) Keyspace(com.netflix.astyanax.Keyspace) Row(com.netflix.astyanax.model.Row)

Example 4 with Column

use of com.netflix.astyanax.model.Column in project coprhd-controller by CoprHD.

the class IndexCleanupList method getIndexesToClean.

// Returns a map contains <RowKey, Columns need to remove their index entries because .inactive is true>
// If changedOnly is true, we only returns rows those have indexed fields changed
// If changedOnly is false, we returns all touched rows with .inactive = true, even no field is actually changed
public Map<String, List<Column<CompositeColumnName>>> getIndexesToClean(boolean changedOnly) {
    Map<String, List<Column<CompositeColumnName>>> mapIndexes = new HashMap<>();
    // For each object we have touched
    for (Map.Entry<String, Map<CompositeColumnName, Column>> entry : _currentMap.entrySet()) {
        String rowKey = entry.getKey();
        Map<CompositeColumnName, Column> colMap = entry.getValue();
        if (changedOnly && !_cleanupList.containsKey(rowKey)) {
            continue;
        }
        // Check if this row's final "inactive" column is "true"
        Column inactiveColumn = colMap.get(INACTIVE_COLUMN);
        if (inactiveColumn != null && inactiveColumn.getBooleanValue()) {
            ArrayList<Column<CompositeColumnName>> cols = new ArrayList<Column<CompositeColumnName>>();
            for (Column<CompositeColumnName> col : colMap.values()) {
                // All indexed fields except "inactive" itself need to be removed
                if (!col.getName().getOne().equals("inactive") && !ColumnField.isDeletionMark(col)) {
                    cols.add(col);
                }
            }
            if (!cols.isEmpty()) {
                mapIndexes.put(rowKey, cols);
            }
        }
    }
    return mapIndexes;
}
Also used : HashMap(java.util.HashMap) Column(com.netflix.astyanax.model.Column) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with Column

use of com.netflix.astyanax.model.Column in project coprhd-controller by CoprHD.

the class DataObjectType method deserialize.

public <T extends DataObject> T deserialize(Class<T> clazz, Row<String, CompositeColumnName> row, IndexCleanupList cleanupList, LazyLoader lazyLoader) {
    if (!_clazz.isAssignableFrom(clazz)) {
        throw new IllegalArgumentException();
    }
    try {
        String key = row.getKey();
        Class<? extends DataObject> type = (_instrumentedClazz == null) ? clazz : _instrumentedClazz;
        DataObject obj = DataObject.createInstance(type, URI.create(row.getKey()));
        Iterator<Column<CompositeColumnName>> it = row.getColumns().iterator();
        while (it.hasNext()) {
            Column<CompositeColumnName> column = it.next();
            cleanupList.add(key, column);
            ColumnField columnField = _columnFieldMap.get(column.getName().getOne());
            if (columnField != null) {
                columnField.deserialize(column, obj);
            } else {
                _log.debug("an unexpected column in db, it might because geo system has multiple vdc but in different version");
            }
        }
        cleanupList.addObject(key, obj);
        obj.trackChanges();
        setLazyLoaders(obj, lazyLoader);
        return clazz.cast(obj);
    } catch (final InstantiationException e) {
        throw DatabaseException.fatals.deserializationFailed(clazz, e);
    } catch (final IllegalAccessException e) {
        throw DatabaseException.fatals.deserializationFailed(clazz, e);
    }
}
Also used : DataObject(com.emc.storageos.db.client.model.DataObject) Column(com.netflix.astyanax.model.Column)

Aggregations

Column (com.netflix.astyanax.model.Column)15 DataObject (com.emc.storageos.db.client.model.DataObject)8 Keyspace (com.netflix.astyanax.Keyspace)6 Row (com.netflix.astyanax.model.Row)5 URI (java.net.URI)4 ConnectionException (com.netflix.astyanax.connectionpool.exceptions.ConnectionException)3 Rows (com.netflix.astyanax.model.Rows)3 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)2 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DecommissionedConstraint (com.emc.storageos.db.client.constraint.DecommissionedConstraint)1 NamedURI (com.emc.storageos.db.client.model.NamedURI)1 PropertyListDataObject (com.emc.storageos.db.client.model.PropertyListDataObject)1 NotFoundException (com.netflix.astyanax.connectionpool.exceptions.NotFoundException)1 ColumnList (com.netflix.astyanax.model.ColumnList)1 RowQuery (com.netflix.astyanax.query.RowQuery)1 RowSliceQuery (com.netflix.astyanax.query.RowSliceQuery)1 ByteBuffer (java.nio.ByteBuffer)1