@Override public RayoNode getNode(String rayoNode) { log.debug("Getting node with id: [%s]", rayoNode); RayoNode node = null; try { Selector selector = Pelops.createSelector(schemaName); Map<String, List<SuperColumn>> rows = selector.getSuperColumnsFromRowsUtf8Keys( "nodes", Selector.newKeyRange("", "", 100), // 100 platforms limit should be enough :) Selector.newColumnsPredicate(rayoNode), ConsistencyLevel.ONE); Iterator<Entry<String, List<SuperColumn>>> it = rows.entrySet().iterator(); while (it.hasNext()) { Entry<String, List<SuperColumn>> element = it.next(); String currPlatform = element.getKey(); List<SuperColumn> platformOccurrences = element.getValue(); for (SuperColumn column : platformOccurrences) { if (node == null) { node = new RayoNode(); node = buildNode(column.getColumns()); node.setHostname(rayoNode); } node.addPlatform(currPlatform); } } } catch (PelopsException pe) { log.error(pe.getMessage(), pe); return null; } return node; }
@SuppressWarnings("unchecked") public List<RayoNode> getRayoNodesForPlatform(String platformId) { try { log.debug("Finding rayo nodes for platform: [%s]", platformId); Set<String> platforms = new HashSet<String>(); platforms.add(platformId); List<RayoNode> nodes = new ArrayList<RayoNode>(); Selector selector = Pelops.createSelector(schemaName); List<SuperColumn> columns = selector.getSuperColumnsFromRow("nodes", platformId, false, ConsistencyLevel.ONE); for (SuperColumn column : columns) { String id = Bytes.toUTF8(column.getName()); RayoNode rayoNode = buildNode(column.getColumns()); rayoNode.setHostname(id); rayoNode.setPlatforms(platforms); nodes.add(rayoNode); } return nodes; } catch (PelopsException pe) { log.error(pe.getMessage(), pe); return Collections.EMPTY_LIST; } }
public int compareTo(SuperColumn other) { if (!getClass().equals(other.getClass())) { return getClass().getName().compareTo(other.getClass().getName()); } int lastComparison = 0; SuperColumn typedOther = (SuperColumn) other; lastComparison = Boolean.valueOf(isSetName()).compareTo(typedOther.isSetName()); if (lastComparison != 0) { return lastComparison; } if (isSetName()) { lastComparison = TBaseHelper.compareTo(this.name, typedOther.name); if (lastComparison != 0) { return lastComparison; } } lastComparison = Boolean.valueOf(isSetColumns()).compareTo(typedOther.isSetColumns()); if (lastComparison != 0) { return lastComparison; } if (isSetColumns()) { lastComparison = TBaseHelper.compareTo(this.columns, typedOther.columns); if (lastComparison != 0) { return lastComparison; } } return 0; }
/** Test insertion of a supercolumn using insert */ @Test public void testInsertSuper() throws IllegalArgumentException, NoSuchElementException, IllegalStateException, HNotFoundException, Exception { // insert value ColumnParent columnParent = new ColumnParent("Super1"); columnParent.setSuper_column(StringSerializer.get().toByteBuffer("testInsertSuper_super")); Column column = new Column( StringSerializer.get().toByteBuffer("testInsertSuper_column"), StringSerializer.get().toByteBuffer("testInsertSuper_value"), connectionManager.createClock()); keyspace.insert( StringSerializer.get().toByteBuffer("testInsertSuper_key"), columnParent, column); column.setName(StringSerializer.get().toByteBuffer("testInsertSuper_column2")); keyspace.insert( StringSerializer.get().toByteBuffer("testInsertSuper_key"), columnParent, column); // get value and assert ColumnPath cp2 = new ColumnPath("Super1"); cp2.setSuper_column(bytes("testInsertSuper_super")); SuperColumn sc = keyspace.getSuperColumn("testInsertSuper_key", cp2); assertNotNull(sc); assertEquals("testInsertSuper_super", string(sc.getName())); assertEquals(2, sc.getColumns().size()); assertEquals("testInsertSuper_value", string(sc.getColumns().get(0).getValue())); // remove value keyspace.remove("testInsertSuper_super", cp2); }
public List<NotificationEntry> getNotifications(String username, String startId) { try { byte[] timeUUIDAsBytes = EMPTY_BYTE_ARRAY; if (startId != null && startId.length() > 0) { UUID uuid = UUIDUtil.fromString(startId); final byte[] idAsBytes = UUIDUtil.toBytes(uuid); timeUUIDAsBytes = getTimeUUIDFromID(username, idAsBytes); if (timeUUIDAsBytes == null) { timeUUIDAsBytes = EMPTY_BYTE_ARRAY; } } Selector selector = Pelops.createSelector(TRENDOCEAN_POOL, TRENDOCEAN_KEYSPACE); SlicePredicate columnPredicate = Selector.newColumnsPredicate(timeUUIDAsBytes, EMPTY_BYTE_ARRAY, true, PAGE_SIZE); List<SuperColumn> columns = selector.getSuperColumnsFromRow(username, WALL_CF, columnPredicate, ConsistencyLevel.ONE); List<NotificationEntry> notificationList = new ArrayList<NotificationEntry>(columns.size()); for (SuperColumn superColumn : columns) { List<Column> wallEntryColumns = superColumn.getColumns(); String notificationId = null; NotificationType notificationType = null; String questionId = null; String relatedUser = null; long answerCount = -1L; long time = 0; for (Column notificationColumn : wallEntryColumns) { String notificationColumnName = toString(notificationColumn.getName()); if (notificationColumnName.equals("_id")) { notificationId = UUIDUtil.toUUID(notificationColumn.getValue()).toString(); long timeInMicroSeconds = notificationColumn.getTimestamp(); time = timeInMicroSeconds / 1000L; } else if (notificationColumnName.equals("_type")) { notificationType = NotificationType.fromId(toByte(notificationColumn)); } else if (notificationColumnName.equals("questionId")) { questionId = toString(notificationColumn); } else if (notificationColumnName.equals("relatedUser")) { relatedUser = toString(notificationColumn); } else if (notificationColumnName.equals("answerCount")) { answerCount = toLong(notificationColumn); } else { logger.warn("Invalid notification column:{}", notificationColumnName); } } NotificationEntry notificationEntry = new NotificationEntry(); notificationEntry.setId(notificationId); notificationEntry.setTime(time); notificationEntry.setNotificationType(notificationType); notificationEntry.setQuestionId(questionId); notificationList.add(notificationEntry); notificationEntry.setUsername(relatedUser); notificationEntry.setAnswerCount(answerCount); } return notificationList; } catch (NotFoundException ex) { return Collections.EMPTY_LIST; } catch (Exception ex) { logger.error("Can not get time uuid", ex); throw new TrendOceanException(ex); } }
/** Performs a deep copy on <i>other</i>. */ public SuperColumn(SuperColumn other) { if (other.isSetName()) { this.name = TBaseHelper.copyBinary(other.name); ; } if (other.isSetColumns()) { List<Column> __this__columns = new ArrayList<Column>(); for (Column other_element : other.columns) { __this__columns.add(new Column(other_element)); } this.columns = __this__columns; } }
/** * Purge expired entries. Expiration entries are stored in a single key (expirationKey) within a * specific ColumnFamily (set by configuration). The entries are grouped by expiration timestamp * in SuperColumns within which each entry's key is mapped to a column */ @Override protected void purgeInternal() throws CacheLoaderException { if (trace) log.trace("purgeInternal"); Cassandra.Client cassandraClient = null; try { cassandraClient = dataSource.getConnection(); // We need to get all supercolumns from the beginning of time until // now, in SLICE_SIZE chunks SlicePredicate predicate = new SlicePredicate(); predicate.setSlice_range( new SliceRange( ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.bytes(System.currentTimeMillis()), false, SLICE_SIZE)); Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); for (boolean complete = false; !complete; ) { // Get all columns List<ColumnOrSuperColumn> slice = cassandraClient.get_slice( expirationKey, expirationColumnParent, predicate, readConsistencyLevel); complete = slice.size() < SLICE_SIZE; // Delete all keys returned by the slice for (ColumnOrSuperColumn crumb : slice) { SuperColumn scol = crumb.getSuper_column(); for (Iterator<Column> i = scol.getColumnsIterator(); i.hasNext(); ) { Column col = i.next(); // Remove the entry row remove0(ByteBuffer.wrap(col.getName()), mutationMap); } // Remove the expiration supercolumn addMutation( mutationMap, expirationKey, config.expirationColumnFamily, ByteBuffer.wrap(scol.getName()), null, null); } } cassandraClient.batch_mutate(mutationMap, writeConsistencyLevel); } catch (Exception e) { throw new CacheLoaderException(e); } finally { dataSource.releaseConnection(cassandraClient); } }
public boolean equals(SuperColumn that) { if (that == null) return false; boolean this_present_name = true && this.isSetName(); boolean that_present_name = true && that.isSetName(); if (this_present_name || that_present_name) { if (!(this_present_name && that_present_name)) return false; if (!this.name.equals(that.name)) return false; } boolean this_present_columns = true && this.isSetColumns(); boolean that_present_columns = true && that.isSetColumns(); if (this_present_columns || that_present_columns) { if (!(this_present_columns && that_present_columns)) return false; if (!this.columns.equals(that.columns)) return false; } return true; }
private void doSlice(String keyspace, String key, String columnFamily, byte[] superColumnName) throws InvalidRequestException, UnavailableException, TimedOutException, TException, UnsupportedEncodingException, IllegalAccessException, NotFoundException, InstantiationException, ClassNotFoundException { SliceRange range = new SliceRange(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.EMPTY_BYTE_ARRAY, true, 1000000); List<ColumnOrSuperColumn> columns = thriftClient_.get_slice( keyspace, key, createColumnParent(columnFamily, superColumnName), createSlicePredicate(null, range), ConsistencyLevel.ONE); int size = columns.size(); // Print out super columns or columns. for (ColumnOrSuperColumn cosc : columns) { if (cosc.isSetSuper_column()) { SuperColumn superColumn = cosc.super_column; css_.out.printf( "=> (super_column=%s,", formatSuperColumnName(keyspace, columnFamily, superColumn)); for (Column col : superColumn.getColumns()) css_.out.printf( "\n (column=%s, value=%s, timestamp=%d)", formatSubcolumnName(keyspace, columnFamily, col), new String(col.value, "UTF-8"), col.timestamp); css_.out.println(")"); } else { Column column = cosc.column; css_.out.printf( "=> (column=%s, value=%s, timestamp=%d)\n", formatColumnName(keyspace, columnFamily, column), new String(column.value, "UTF-8"), column.timestamp); } } css_.out.println("Returned " + size + " results."); }
@Override public Collection<String> getCalls() { log.debug("Getting list with all active calls"); try { Selector selector = Pelops.createSelector(schemaName); List<String> calls = new ArrayList<String>(); List<SuperColumn> cols = selector.getSuperColumnsFromRow("jids", "nodes", false, ConsistencyLevel.ONE); for (SuperColumn col : cols) { List<Column> columns = col.getColumns(); for (Column column : columns) { calls.add(Bytes.toUTF8(column.getValue())); } } return calls; } catch (PelopsException pe) { log.error(pe.getMessage(), pe); return null; } }
@Override public ByteBuffer getRawName() { return ByteBuffer.wrap(column.getName()); }