Fix the InvertedIndex trigger example
[cassandra.git] / examples / triggers / src / org / apache / cassandra / triggers / InvertedIndex.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.cassandra.triggers;
19
20 import java.io.InputStream;
21 import java.nio.ByteBuffer;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.Properties;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import org.apache.cassandra.db.Column;
31 import org.apache.cassandra.db.ColumnFamily;
32 import org.apache.cassandra.db.RowMutation;
33 import org.apache.cassandra.io.util.FileUtils;
34
35 public class InvertedIndex implements ITrigger
36 {
37 private static final Logger logger = LoggerFactory.getLogger(InvertedIndex.class);
38 private Properties properties = loadProperties();
39
40 public Collection<RowMutation> augment(ByteBuffer key, ColumnFamily update)
41 {
42 List<RowMutation> mutations = new ArrayList<>();
43
44 for (Column cell : update)
45 {
46 // Skip the row marker and other empty values, since they lead to an empty key.
47 if (cell.value().remaining() > 0)
48 {
49 RowMutation mutation = new RowMutation(properties.getProperty("keyspace"), cell.value());
50 mutation.add(properties.getProperty("columnfamily"), cell.name(), key, System.currentTimeMillis());
51 mutations.add(mutation);
52 }
53 }
54
55 return mutations;
56 }
57
58 private static Properties loadProperties()
59 {
60 Properties properties = new Properties();
61 InputStream stream = InvertedIndex.class.getClassLoader().getResourceAsStream("InvertedIndex.properties");
62 try
63 {
64 properties.load(stream);
65 }
66 catch (Exception e)
67 {
68 throw new RuntimeException(e);
69 }
70 finally
71 {
72 FileUtils.closeQuietly(stream);
73 }
74 logger.info("loaded property file, InvertedIndex.properties");
75 return properties;
76 }
77 }