org.apache.cassandra.db.marshal.UTF8Type Java Examples

The following examples show how to use org.apache.cassandra.db.marshal.UTF8Type. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserTypeConverter.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@Override
public UserType convert(DataType dataType) {
    com.datastax.driver.core.UserType userType = (com.datastax.driver.core.UserType) dataType;

    String typeNameString = userType.getTypeName();
    Collection<String> fieldNames = userType.getFieldNames();

    List<AbstractType<?>> innerAbstractTypes = new ArrayList<>(fieldNames.size());

    ByteBuffer typeNameBuffer = UTF8Type.instance.fromString(typeNameString);

    List<FieldIdentifier> fieldIdentifiers = new ArrayList<>(fieldNames.size());
    for (String fieldName : fieldNames) {
        fieldIdentifiers.add(FieldIdentifier.forInternalString(fieldName));
        innerAbstractTypes.add((CassandraTypeConverter.convert(userType.getFieldType(fieldName))));
    }

    return new UserType(userType.getKeyspace(),
            typeNameBuffer,
            fieldIdentifiers,
            innerAbstractTypes,
            !userType.isFrozen());
}
 
Example #2
Source File: CellValidatorTest.java    From deep-spark with Apache License 2.0 6 votes vote down vote up
public void testValidatorClassToKind() {
    assertEquals(Kind.validatorClassToKind(null), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(TimeUUIDType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(UTF8Type.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(Int32Type.class), Kind.NOT_A_COLLECTION);

    assertEquals(Kind.validatorClassToKind(BooleanType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(TimestampType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(DecimalType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(LongType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(DoubleType.class), Kind.NOT_A_COLLECTION);

    assertEquals(Kind.validatorClassToKind(FloatType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(InetAddressType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(IntegerType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(UUIDType.class), Kind.NOT_A_COLLECTION);

    assertEquals(Kind.validatorClassToKind(SetType.class), Kind.SET);
    assertEquals(Kind.validatorClassToKind(ListType.class), Kind.LIST);
    assertEquals(Kind.validatorClassToKind(MapType.class), Kind.MAP);
}
 
Example #3
Source File: OperationTest.java    From sasi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSatisfiedByWithMultipleTerms()
{
    final ByteBuffer comment = UTF8Type.instance.decompose("comment");
    final ColumnFamilyStore store = Keyspace.open("sasecondaryindex").getColumnFamilyStore("saindexed1");
    final IPartitioner<?> partitioner = StorageService.getPartitioner();

    ColumnFamily cf = ArrayBackedSortedColumns.factory.create(store.metadata);
    cf.addColumn(new Column(comment, UTF8Type.instance.decompose("software engineer is working on a project"), System.currentTimeMillis()));

    Operation.Builder builder = new Operation.Builder(OperationType.AND, UTF8Type.instance, controller,
                                        new IndexExpression(comment, IndexOperator.EQ, UTF8Type.instance.decompose("eng is a work")));
    Operation op = builder.complete();

    Assert.assertTrue(op.satisfiedBy(new Row(partitioner.decorateKey(UTF8Type.instance.decompose("key1")), cf), null, false));

    builder = new Operation.Builder(OperationType.AND, UTF8Type.instance, controller,
                                        new IndexExpression(comment, IndexOperator.EQ, UTF8Type.instance.decompose("soft works fine")));
    op = builder.complete();

    Assert.assertTrue(op.satisfiedBy(new Row(partitioner.decorateKey(UTF8Type.instance.decompose("key1")), cf), null, false));
}
 
Example #4
Source File: UserTypes.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void validateAssignableTo(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
    if (!(receiver.type instanceof UserType))
        throw new InvalidRequestException(String.format("Invalid user type literal for %s of type %s", receiver, receiver.type.asCQL3Type()));

    UserType ut = (UserType)receiver.type;
    for (int i = 0; i < ut.size(); i++)
    {
        ColumnIdentifier field = new ColumnIdentifier(ut.fieldName(i), UTF8Type.instance);
        Term.Raw value = entries.get(field);
        if (value == null)
            continue;

        ColumnSpecification fieldSpec = fieldSpecOf(receiver, i);
        if (!value.isAssignableTo(keyspace, fieldSpec))
            throw new InvalidRequestException(String.format("Invalid user type literal for %s: field %s is not of type %s", receiver, field, fieldSpec.type.asCQL3Type()));
    }
}
 
Example #5
Source File: OnDiskIndexTest.java    From sasi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuperBlockRetrieval() throws Exception
{
    OnDiskIndexBuilder builder = new OnDiskIndexBuilder(UTF8Type.instance, LongType.instance, OnDiskIndexBuilder.Mode.SPARSE);
    for (long i = 0; i < 100000; i++)
        builder.add(LongType.instance.decompose(i), keyAt(i), i);

    File index = File.createTempFile("on-disk-sa-multi-superblock-match", ".db");
    index.deleteOnExit();

    builder.finish(index);

    OnDiskIndex onDiskIndex = new OnDiskIndex(index, LongType.instance, new KeyConverter());

    testSearchRangeWithSuperBlocks(onDiskIndex, 0, 500);
    testSearchRangeWithSuperBlocks(onDiskIndex, 300, 93456);
    testSearchRangeWithSuperBlocks(onDiskIndex, 210, 1700);
    testSearchRangeWithSuperBlocks(onDiskIndex, 530, 3200);

    Random random = new Random(0xdeadbeef);
    for (int i = 0; i < 100000; i += random.nextInt(1500)) // random steps with max of 1500 elements
    {
        for (int j = 0; j < 3; j++)
            testSearchRangeWithSuperBlocks(onDiskIndex, i, ThreadLocalRandom.current().nextInt(i, 100000));
    }
}
 
Example #6
Source File: ColumnIdentifier.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ColumnIdentifier prepare(CFMetaData cfm)
{
    AbstractType<?> comparator = cfm.comparator.asAbstractType();
    if (cfm.getIsDense() || comparator instanceof CompositeType || comparator instanceof UTF8Type)
        return new ColumnIdentifier(text, true);

    // We have a Thrift-created table with a non-text comparator.  We need to parse column names with the comparator
    // to get the correct ByteBuffer representation.  However, this doesn't apply to key aliases, so we need to
    // make a special check for those and treat them normally.  See CASSANDRA-8178.
    ByteBuffer bufferName = ByteBufferUtil.bytes(text);
    for (ColumnDefinition def : cfm.partitionKeyColumns())
    {
        if (def.name.bytes.equals(bufferName))
            return new ColumnIdentifier(text, true);
    }
    return new ColumnIdentifier(comparator.fromString(rawText), text);
}
 
Example #7
Source File: CassandraAuthorizer.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public Set<PermissionDetails> list(AuthenticatedUser performer, Set<Permission> permissions, IResource resource, String of)
throws RequestValidationException, RequestExecutionException
{
    if (!performer.isSuper() && !performer.getName().equals(of))
        throw new UnauthorizedException(String.format("You are not authorized to view %s's permissions",
                                                      of == null ? "everyone" : of));

    Set<PermissionDetails> details = new HashSet<PermissionDetails>();

    for (UntypedResultSet.Row row : process(buildListQuery(resource, of)))
    {
        if (row.has(PERMISSIONS))
        {
            for (String p : row.getSet(PERMISSIONS, UTF8Type.instance))
            {
                Permission permission = Permission.valueOf(p);
                if (permissions.contains(permission))
                    details.add(new PermissionDetails(row.getString(USERNAME),
                                                      DataResource.fromName(row.getString(RESOURCE)),
                                                      permission));
            }
        }
    }

    return details;
}
 
Example #8
Source File: Mapping.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if this is consistent with the specified column family metadata.
 *
 * @param metadata A column family metadata.
 */
public void validate(CFMetaData metadata) {
    for (Map.Entry<String, ColumnMapper> entry : columnMappers.entrySet()) {

        String name = entry.getKey();
        ColumnMapper columnMapper = entry.getValue();
        ByteBuffer columnName = UTF8Type.instance.decompose(name);

        ColumnDefinition columnDefinition = metadata.getColumnDefinition(columnName);
        if (columnDefinition == null) {
            throw new RuntimeException("No column definition for mapper " + name);
        }

        if (columnDefinition.isStatic()) {
            throw new RuntimeException("Lucene indexes are not allowed on static columns as " + name);
        }

        AbstractType<?> type = columnDefinition.type;
        if (!columnMapper.supports(type)) {
            throw new RuntimeException(String.format("Type '%s' is not supported by mapper '%s'", type, name));
        }
    }
}
 
Example #9
Source File: NonTokenizingAnalyzerTest.java    From sasi with Apache License 2.0 6 votes vote down vote up
@Test
public void caseInsensitiveAnalizer() throws Exception
{
    NonTokenizingAnalyzer analyzer = new NonTokenizingAnalyzer();
    NonTokenizingOptions options = NonTokenizingOptions.getDefaultOptions();
    options.setCaseSensitive(false);
    analyzer.init(options, UTF8Type.instance);

    String testString = "Nip it in the bud";
    ByteBuffer toAnalyze = ByteBuffer.wrap(testString.getBytes());
    analyzer.reset(toAnalyze);
    ByteBuffer analyzed = null;
    while (analyzer.hasNext())
        analyzed = analyzer.next();
    Assert.assertTrue(testString.toLowerCase().equals(ByteBufferUtil.string(analyzed)));
}
 
Example #10
Source File: TriggerExecutorTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void differentKeyRowMutations() throws ConfigurationException, InvalidRequestException
{
    CFMetaData metadata = makeCfMetaData("ks1", "cf1", TriggerDefinition.create("test", DifferentKeyTrigger.class.getName()));
    ColumnFamily cf = makeCf(metadata, "v1", null);
    Mutation rm = new Mutation(UTF8Type.instance.fromString("k1"), cf);

    List<? extends IMutation> tmutations = new ArrayList<>(TriggerExecutor.instance.execute(Arrays.asList(rm)));
    assertEquals(2, tmutations.size());
    Collections.sort(tmutations, new RmComparator());

    assertEquals(bytes("k1"), tmutations.get(0).key());
    assertEquals(bytes("otherKey"), tmutations.get(1).key());

    List<ColumnFamily> mutatedCFs = new ArrayList<>(tmutations.get(0).getColumnFamilies());
    assertEquals(1, mutatedCFs.size());
    assertEquals(bytes("v1"), mutatedCFs.get(0).getColumn(getColumnName(metadata, "c1")).value());
    assertNull(mutatedCFs.get(0).getColumn(getColumnName(metadata, "c2")));

    mutatedCFs = new ArrayList<>(tmutations.get(1).getColumnFamilies());
    assertEquals(1, mutatedCFs.size());
    assertNull(mutatedCFs.get(0).getColumn(getColumnName(metadata, "c1")));
    assertEquals(bytes("trigger"), mutatedCFs.get(0).getColumn(getColumnName(metadata, "c2")).value());
}
 
Example #11
Source File: LazyCassandraUtils.java    From Hive-Cassandra with Apache License 2.0 6 votes vote down vote up
public static AbstractType getCassandraType(PrimitiveObjectInspector oi) {
  switch (oi.getPrimitiveCategory()) {
  case BOOLEAN:
    return BooleanType.instance;
  case INT:
    return Int32Type.instance;
  case LONG:
    return LongType.instance;
  case FLOAT:
    return FloatType.instance;
  case DOUBLE:
    return DoubleType.instance;
  case STRING:
    return UTF8Type.instance;
  case BYTE:
  case SHORT:
  case BINARY:
    return BytesType.instance;
  case TIMESTAMP:
    return DateType.instance;
  default:
    throw new RuntimeException("Hive internal error.");

  }
}
 
Example #12
Source File: RowIndexEntryTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializedSize() throws IOException
{
    final RowIndexEntry simple = new RowIndexEntry(123);

    DataOutputBuffer buffer = new DataOutputBuffer();
    RowIndexEntry.Serializer serializer = new RowIndexEntry.Serializer(new SimpleDenseCellNameType(UTF8Type.instance));

    serializer.serialize(simple, buffer);

    Assert.assertEquals(buffer.getLength(), serializer.serializedSize(simple));

    buffer = new DataOutputBuffer();
    ColumnFamily cf = ArrayBackedSortedColumns.factory.create("Keyspace1", "Standard1");
    ColumnIndex columnIndex = new ColumnIndex.Builder(cf, ByteBufferUtil.bytes("a"), new DataOutputBuffer())
    {{
        int idx = 0, size = 0;
        Cell column;
        do
        {
            column = new BufferCell(CellNames.simpleDense(ByteBufferUtil.bytes("c" + idx++)), ByteBufferUtil.bytes("v"), FBUtilities.timestampMicros());
            size += column.serializedSize(new SimpleDenseCellNameType(UTF8Type.instance), TypeSizes.NATIVE);

            add(column);
        }
        while (size < DatabaseDescriptor.getColumnIndexSize() * 3);

    }}.build();

    RowIndexEntry withIndex = RowIndexEntry.create(0xdeadbeef, DeletionTime.LIVE, columnIndex);

    serializer.serialize(withIndex, buffer);
    Assert.assertEquals(buffer.getLength(), serializer.serializedSize(withIndex));
}
 
Example #13
Source File: RowIndexSearcher.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void validate(IndexExpression indexExpression) throws InvalidRequestException {
    try {
        String json = UTF8Type.instance.compose(indexExpression.value);
        Search.fromJson(json).validate(schema);
    } catch (Exception e) {
        throw new InvalidRequestException(e.getMessage());
    }
}
 
Example #14
Source File: RowService.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the score of the specified {@link Row}.
 *
 * @param row A {@link Row}.
 * @return The score of the specified {@link Row}.
 */
protected Float score(Row row) {
    ColumnFamily cf = row.cf;
    CellName cellName = rowMapper.makeCellName(cf);
    Cell cell = cf.getColumn(cellName);
    String value = UTF8Type.instance.compose(cell.value());
    return Float.parseFloat(value);
}
 
Example #15
Source File: RowService.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Adds to the specified {@link Row} the specified Lucene score column.
 *
 * @param row       A {@link Row}.
 * @param timestamp The score column timestamp.
 * @param score     The score column value.
 * @return The {@link Row} with the score.
 */
protected Row addScoreColumn(Row row, long timestamp, Float score) {
    ColumnFamily cf = row.cf;
    CellName cellName = rowMapper.makeCellName(cf);
    ByteBuffer cellValue = UTF8Type.instance.decompose(score.toString());

    ColumnFamily dcf = ArrayBackedSortedColumns.factory.create(baseCfs.metadata);
    dcf.addColumn(cellName, cellValue, timestamp);
    dcf.addAll(row.cf);

    return new Row(row.key, dcf);
}
 
Example #16
Source File: GeoShapeMapper.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a new {@link GeoShapeMapper}.
 */
@JsonCreator
public GeoShapeMapper(@JsonProperty("max_levels") Integer maxLevels) {
    super(new AbstractType<?>[]{AsciiType.instance, UTF8Type.instance});
    this.maxLevels = maxLevels == null ? DEFAULT_MAX_LEVELS : maxLevels;
    this.grid = new GeohashPrefixTree(spatialContext, this.maxLevels);
}
 
Example #17
Source File: TriggerDefinition.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Deserialize triggers from storage-level representation.
 *
 * @param serializedTriggers storage-level partition containing the trigger definitions
 * @return the list of processed TriggerDefinitions
 */
public static List<TriggerDefinition> fromSchema(Row serializedTriggers)
{
    List<TriggerDefinition> triggers = new ArrayList<>();
    String query = String.format("SELECT * FROM %s.%s", Keyspace.SYSTEM_KS, SystemKeyspace.SCHEMA_TRIGGERS_CF);
    for (UntypedResultSet.Row row : QueryProcessor.resultify(query, serializedTriggers))
    {
        String name = row.getString(TRIGGER_NAME);
        String classOption = row.getMap(TRIGGER_OPTIONS, UTF8Type.instance, UTF8Type.instance).get(CLASS);
        triggers.add(new TriggerDefinition(name, classOption));
    }
    return triggers;
}
 
Example #18
Source File: DefsTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private CFMetaData addTestCF(String ks, String cf, String comment)
{
    CFMetaData newCFMD = new CFMetaData(ks, cf, ColumnFamilyType.Standard, new SimpleDenseCellNameType(UTF8Type.instance));
    newCFMD.comment(comment)
           .readRepairChance(0.0);

    return newCFMD;
}
 
Example #19
Source File: TriggerExecutorTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static CFMetaData makeCfMetaData(String ks, String cf, TriggerDefinition trigger)
{

    CFMetaData metadata = CFMetaData.sparseCFMetaData(ks, cf, CompositeType.getInstance(UTF8Type.instance));

    metadata.keyValidator(UTF8Type.instance);
    metadata.addOrReplaceColumnDefinition(ColumnDefinition.partitionKeyDef(metadata,
                                                                           UTF8Type.instance.fromString("pkey"),
                                                                           UTF8Type.instance,
                                                                           null));
    metadata.addOrReplaceColumnDefinition(ColumnDefinition.regularDef(metadata,
                                                                      UTF8Type.instance.fromString("c1"),
                                                                      UTF8Type.instance,
                                                                      0));
    metadata.addOrReplaceColumnDefinition(ColumnDefinition.regularDef(metadata,
                                                                      UTF8Type.instance.fromString("c2"),
                                                                      UTF8Type.instance,
                                                                      0));
    try
    {
        if (trigger != null)
            metadata.addTriggerDefinition(trigger);
    }
    catch (InvalidRequestException e)
    {
        throw new AssertionError(e);
    }

    return metadata.rebuild();
}
 
Example #20
Source File: CreateTypeStatement.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static void checkForDuplicateNames(UserType type) throws InvalidRequestException
{
    for (int i = 0; i < type.size() - 1; i++)
    {
        ByteBuffer fieldName = type.fieldName(i);
        for (int j = i+1; j < type.size(); j++)
        {
            if (fieldName.equals(type.fieldName(j)))
                throw new InvalidRequestException(String.format("Duplicate field name %s in type %s",
                                                                UTF8Type.instance.getString(fieldName),
                                                                UTF8Type.instance.getString(type.name)));
        }
    }
}
 
Example #21
Source File: ListPermissionsStatement.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private ResultMessage resultMessage(List<PermissionDetails> details)
{
    if (details.isEmpty())
        return new ResultMessage.Void();

    ResultSet result = new ResultSet(metadata);
    for (PermissionDetails pd : details)
    {
        result.addColumnValue(UTF8Type.instance.decompose(pd.username));
        result.addColumnValue(UTF8Type.instance.decompose(pd.resource.toString()));
        result.addColumnValue(UTF8Type.instance.decompose(pd.permission.toString()));
    }
    return new ResultMessage.Rows(result);
}
 
Example #22
Source File: UserTypes.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static ColumnSpecification fieldSpecOf(ColumnSpecification column, int field)
{
    UserType ut = (UserType)column.type;
    return new ColumnSpecification(column.ksName,
                                   column.cfName,
                                   new ColumnIdentifier(column.name + "." + UTF8Type.instance.compose(ut.fieldName(field)), true),
                                   ut.fieldType(field));
}
 
Example #23
Source File: MigrationInfoSerializationImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<MultiTenantColumnFamilyDefinition> getColumnFamilies() {
    return Collections.singletonList(
            new MultiTenantColumnFamilyDefinition( CF_MIGRATION_INFO, BytesType.class.getSimpleName(),
                    UTF8Type.class.getSimpleName(), BytesType.class.getSimpleName(),
                    MultiTenantColumnFamilyDefinition.CacheOption.KEYS ) );
}
 
Example #24
Source File: CassandraTypeConverterTest.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Test
public void testText() {
    DataType textType = DataType.text();
    AbstractType<?> convertedType = CassandraTypeConverter.convert(textType);

    UTF8Type expectedType = UTF8Type.instance;

    Assert.assertEquals(expectedType, convertedType);
}
 
Example #25
Source File: OnDiskIndexTest.java    From sasi with Apache License 2.0 5 votes vote down vote up
private int validateExclusions(OnDiskIndex sa, long lower, long upper, Set<Long> exclusions, boolean checkCount)
{
    int count = 0;
    for (DecoratedKey key : convert(sa.search(rangeWithExclusions(lower, true, upper, true, exclusions))))
    {
        String keyId = UTF8Type.instance.getString(key.key).split("key")[1];
        Assert.assertFalse("key" + keyId + " is present.", exclusions.contains(Long.valueOf(keyId)));
        count++;
    }

    if (checkCount)
        Assert.assertEquals(upper - (lower == 0 ? -1 : lower) - exclusions.size(), count);

    return count;
}
 
Example #26
Source File: NonTokenizingAnalyzerTest.java    From sasi with Apache License 2.0 5 votes vote down vote up
@Test
public void caseSensitiveAnalizer() throws Exception
{
    NonTokenizingAnalyzer analyzer = new NonTokenizingAnalyzer();
    NonTokenizingOptions options = NonTokenizingOptions.getDefaultOptions();
    analyzer.init(options, UTF8Type.instance);

    String testString = "Nip it in the bud";
    ByteBuffer toAnalyze = ByteBuffer.wrap(testString.getBytes());
    analyzer.reset(toAnalyze);
    ByteBuffer analyzed = null;
    while (analyzer.hasNext())
        analyzed = analyzer.next();
    Assert.assertFalse(testString.toLowerCase().equals(ByteBufferUtil.string(analyzed)));
}
 
Example #27
Source File: OnDiskIndexTest.java    From sasi with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiSuffixMatches() throws Exception
{
    OnDiskIndexBuilder builder = new OnDiskIndexBuilder(UTF8Type.instance, UTF8Type.instance, OnDiskIndexBuilder.Mode.SUFFIX)
    {{
            addAll(this, UTF8Type.instance.decompose("Eliza"), keyBuilder(1L, 2L));
            addAll(this, UTF8Type.instance.decompose("Elizabeth"), keyBuilder(3L, 4L));
            addAll(this, UTF8Type.instance.decompose("Aliza"), keyBuilder(5L, 6L));
            addAll(this, UTF8Type.instance.decompose("Taylor"), keyBuilder(7L, 8L));
            addAll(this, UTF8Type.instance.decompose("Pavel"), keyBuilder(9L, 10L));
    }};

    File index = File.createTempFile("on-disk-sa-multi-suffix-match", ".db");
    index.deleteOnExit();

    builder.finish(index);

    OnDiskIndex onDisk = new OnDiskIndex(index, UTF8Type.instance, new KeyConverter());

    Assert.assertEquals(convert(1, 2, 3, 4, 5, 6), convert(onDisk.search(expressionFor("liz"))));
    Assert.assertEquals(convert(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), convert(onDisk.search(expressionFor("a"))));
    Assert.assertEquals(convert(5, 6), convert(onDisk.search(expressionFor("A"))));
    Assert.assertEquals(convert(1, 2, 3, 4), convert(onDisk.search(expressionFor("E"))));
    Assert.assertEquals(convert(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), convert(onDisk.search(expressionFor("l"))));
    Assert.assertEquals(convert(3, 4), convert(onDisk.search(expressionFor("bet"))));
    Assert.assertEquals(convert(3, 4, 9, 10), convert(onDisk.search(expressionFor("e"))));
    Assert.assertEquals(convert(7, 8), convert(onDisk.search(expressionFor("yl"))));
    Assert.assertEquals(convert(7, 8), convert(onDisk.search(expressionFor("T"))));
    Assert.assertEquals(convert(1, 2, 3, 4, 5, 6), convert(onDisk.search(expressionFor("za"))));
    Assert.assertEquals(convert(3, 4), convert(onDisk.search(expressionFor("ab"))));

    Assert.assertEquals(Collections.<DecoratedKey>emptySet(), convert(onDisk.search(expressionFor("Pi"))));
    Assert.assertEquals(Collections.<DecoratedKey>emptySet(), convert(onDisk.search(expressionFor("ethz"))));
    Assert.assertEquals(Collections.<DecoratedKey>emptySet(), convert(onDisk.search(expressionFor("liw"))));
    Assert.assertEquals(Collections.<DecoratedKey>emptySet(), convert(onDisk.search(expressionFor("Taw"))));
    Assert.assertEquals(Collections.<DecoratedKey>emptySet(), convert(onDisk.search(expressionFor("Av"))));

    onDisk.close();
}
 
Example #28
Source File: View.java    From sasi with Apache License 2.0 5 votes vote down vote up
public View(ColumnIndex index, AbstractType<?> keyValidator,
            Collection<SSTableIndex> currentView,
            Collection<SSTableReader> oldSSTables,
            Set<SSTableIndex> newIndexes)
{
    Map<Descriptor, SSTableIndex> newView = new HashMap<>();

    AbstractType<?> validator = index.getValidator();
    TermTree.Builder termTreeBuilder = (validator instanceof AsciiType || validator instanceof UTF8Type)
                                        ? new PrefixTermTree.Builder(index.getMode().mode, validator)
                                        : new RangeTermTree.Builder(index.getMode().mode, validator);

    List<Interval<ByteBuffer, SSTableIndex>> keyIntervals = new ArrayList<>();
    for (SSTableIndex sstableIndex : Iterables.concat(currentView, newIndexes))
    {
        SSTableReader sstable = sstableIndex.getSSTable();
        if (oldSSTables.contains(sstable) || sstable.isMarkedCompacted() || newView.containsKey(sstable.descriptor))
        {
            sstableIndex.release();
            continue;
        }

        newView.put(sstable.descriptor, sstableIndex);

        termTreeBuilder.add(sstableIndex);
        keyIntervals.add(Interval.create(sstableIndex.minKey(), sstableIndex.maxKey(), sstableIndex));
    }

    this.view = newView;
    this.termTree = termTreeBuilder.build();
    this.keyIntervalTree = IntervalTree.build(keyIntervals, keyValidator);

    if (keyIntervalTree.intervalCount() != termTree.intervalCount())
        throw new IllegalStateException(String.format("mismatched sizes for intervals tree for keys vs terms: %d != %d", keyIntervalTree.intervalCount(), termTree.intervalCount()));
}
 
Example #29
Source File: OnDiskIndexTest.java    From sasi with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotEqualsQueryForNumbers() throws Exception
{
    final Map<ByteBuffer, TokenTreeBuilder> data = new HashMap<ByteBuffer, TokenTreeBuilder>()
    {{
            put(Int32Type.instance.decompose(5),  keyBuilder(1L));
            put(Int32Type.instance.decompose(7),  keyBuilder(2L));
            put(Int32Type.instance.decompose(1),  keyBuilder(3L));
            put(Int32Type.instance.decompose(3),  keyBuilder(1L, 4L));
            put(Int32Type.instance.decompose(8),  keyBuilder(8L, 6L));
            put(Int32Type.instance.decompose(10), keyBuilder(5L));
            put(Int32Type.instance.decompose(6),  keyBuilder(7L));
            put(Int32Type.instance.decompose(4),  keyBuilder(9L, 10L));
            put(Int32Type.instance.decompose(0),  keyBuilder(11L, 12L, 1L));
    }};

    OnDiskIndexBuilder builder = new OnDiskIndexBuilder(UTF8Type.instance, Int32Type.instance, OnDiskIndexBuilder.Mode.ORIGINAL);
    for (Map.Entry<ByteBuffer, TokenTreeBuilder> e : data.entrySet())
        addAll(builder, e.getKey(), e.getValue());

    File index = File.createTempFile("on-disk-sa-except-int-test", "db");
    index.deleteOnExit();

    builder.finish(index);

    OnDiskIndex onDisk = new OnDiskIndex(index, Int32Type.instance, new KeyConverter());

    Assert.assertEquals(convert(1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12), convert(onDisk.search(expressionForNot(0, 10, 1))));
    Assert.assertEquals(convert(1, 2, 4, 5, 7, 9, 10, 11, 12), convert(onDisk.search(expressionForNot(0, 10, 1, 8))));
    Assert.assertEquals(convert(1, 2, 4, 5, 7, 11, 12), convert(onDisk.search(expressionForNot(0, 10, 1, 8, 4))));

    onDisk.close();
}
 
Example #30
Source File: OnDiskIndexTest.java    From sasi with Apache License 2.0 5 votes vote down vote up
@Test
public void testDescriptor() throws Exception
{
    final Map<ByteBuffer, Pair<DecoratedKey, Long>> data = new HashMap<ByteBuffer, Pair<DecoratedKey, Long>>()
    {{
            put(Int32Type.instance.decompose(5), Pair.create(keyAt(1L), 1L));
    }};

    OnDiskIndexBuilder builder1 = new OnDiskIndexBuilder(UTF8Type.instance, Int32Type.instance, OnDiskIndexBuilder.Mode.ORIGINAL);
    OnDiskIndexBuilder builder2 = new OnDiskIndexBuilder(UTF8Type.instance, Int32Type.instance, OnDiskIndexBuilder.Mode.ORIGINAL);
    for (Map.Entry<ByteBuffer, Pair<DecoratedKey, Long>> e : data.entrySet())
    {
        DecoratedKey key = e.getValue().left;
        Long position = e.getValue().right;

        builder1.add(e.getKey(), key, position);
        builder2.add(e.getKey(), key, position);
    }

    File index1 = File.createTempFile("on-disk-sa-int", "db");
    File index2 = File.createTempFile("on-disk-sa-int2", "db");
    index1.deleteOnExit();
    index2.deleteOnExit();

    builder1.finish(index1);
    builder2.finish(new Descriptor(Descriptor.VERSION_AA), index2);

    OnDiskIndex onDisk1 = new OnDiskIndex(index1, Int32Type.instance, new KeyConverter());
    OnDiskIndex onDisk2 = new OnDiskIndex(index2, Int32Type.instance, new KeyConverter());

    ByteBuffer number = Int32Type.instance.decompose(5);

    Assert.assertEquals(Collections.singleton(data.get(number).left), convert(onDisk1.search(expressionFor(Int32Type.instance, number))));
    Assert.assertEquals(Collections.singleton(data.get(number).left), convert(onDisk2.search(expressionFor(Int32Type.instance, number))));

    Assert.assertEquals(onDisk1.descriptor.version.version, Descriptor.CURRENT_VERSION);
    Assert.assertEquals(onDisk2.descriptor.version.version, Descriptor.VERSION_AA);
}