-
Notifications
You must be signed in to change notification settings - Fork 198
/
ElasticSecondaryIndex.java
785 lines (698 loc) · 33.6 KB
/
ElasticSecondaryIndex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
* Copyright (c) 2015 Vincent Royer (vroyer@vroyer.org).
* Contains some code from Elasticsearch (http://www.elastic.co)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.elasticsearch.cassandra;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import org.apache.cassandra.config.CFMetaData;
import org.apache.cassandra.config.ColumnDefinition;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.cql3.ColumnIdentifier;
import org.apache.cassandra.cql3.QueryProcessor;
import org.apache.cassandra.cql3.UntypedResultSet;
import org.apache.cassandra.cql3.UntypedResultSet.Row;
import org.apache.cassandra.db.Cell;
import org.apache.cassandra.db.ColumnFamily;
import org.apache.cassandra.db.ColumnFamilyStore;
import org.apache.cassandra.db.DecoratedKey;
import org.apache.cassandra.db.composites.CType;
import org.apache.cassandra.db.composites.CellName;
import org.apache.cassandra.db.composites.Composite;
import org.apache.cassandra.db.composites.CompoundSparseCellName;
import org.apache.cassandra.db.index.PerRowSecondaryIndex;
import org.apache.cassandra.db.index.SecondaryIndexSearcher;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.CollectionType;
import org.apache.cassandra.db.marshal.ListType;
import org.apache.cassandra.db.marshal.MapType;
import org.apache.cassandra.db.marshal.SetType;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.cassandra.db.marshal.UserType;
import org.apache.cassandra.dht.IPartitioner;
import org.apache.cassandra.dht.Token;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.cassandra.exceptions.RequestValidationException;
import org.apache.cassandra.serializers.CollectionSerializer;
import org.apache.cassandra.serializers.MarshalException;
import org.apache.cassandra.service.ElassandraDaemon;
import org.apache.cassandra.transport.Server;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.Pair;
import org.apache.cassandra.utils.concurrent.OpOrder.Group;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.node.ArrayNode;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
import org.elasticsearch.action.delete.XDeleteRequest;
import org.elasticsearch.action.delete.XDeleteResponse;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.SourceToParse;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.indices.IndicesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Maps;
/**
* Custom secondary index for CQL3 only.
* @author vroyer
*
*/
public class ElasticSecondaryIndex extends PerRowSecondaryIndex {
private static final Logger logger = LoggerFactory.getLogger(ElasticSecondaryIndex.class);
public static final String ELASTIC_TOKEN = "_token";
// targets='<index>.<type>,<index>.<type>...'
public static final String ELASTIC_OPTION_TARGETS = "targets";
public static final String ELASTIC_OPTION_PARTIAL_UPDATE = "partial_update";
// keyspace.table to elasticsearch index.type mapping used when keyspace.table <> index.type
protected static final ConcurrentMap<Pair<String, String>, Pair<String, String>> mapping = Maps.newConcurrentMap();
String index_name;
IPartitioner partitioner = DatabaseDescriptor.getPartitioner();
// elasticsearch index.type -> mappingMetaData
Set<Pair<String, String>> targets = new HashSet<Pair<String, String>>();
// Indexed columns and if we need to read it to deal with partial updates.
Map<String,Boolean> readOnUpdateColumnMap;
public ElasticSecondaryIndex() {
super();
}
public static Object deserialize(AbstractType<?> type, ByteBuffer bb) {
if (type instanceof UserType) {
UserType utype = (UserType) type;
Map<String, Object> mapValue = new HashMap<String, Object>();
ByteBuffer[] components = utype.split(bb);
for (int i = 0; i < components.length; i++) {
String fieldName = UTF8Type.instance.compose(utype.fieldName(i));
AbstractType<?> ctype = utype.type(i);
Object value = (components[i] == null) ? null : deserialize(ctype, components[i]);
mapValue.put(fieldName, value);
}
return mapValue;
} else if (type instanceof ListType) {
ListType ltype = (ListType)type;
ByteBuffer input = bb.duplicate();
int size = CollectionSerializer.readCollectionSize(input, Server.VERSION_3);
List list = new ArrayList(size);
for (int i = 0; i < size; i++) {
list.add( deserialize(ltype.getElementsType(), CollectionSerializer.readValue(input, Server.VERSION_3) ));
}
if (input.hasRemaining())
throw new MarshalException("Unexpected extraneous bytes after map value");
return list;
} else if (type instanceof SetType) {
SetType ltype = (SetType)type;
ByteBuffer input = bb.duplicate();
int size = CollectionSerializer.readCollectionSize(input, Server.VERSION_3);
Set set = new HashSet(size);
for (int i = 0; i < size; i++) {
set.add( deserialize(ltype.getElementsType(), CollectionSerializer.readValue(input, Server.VERSION_3) ));
}
if (input.hasRemaining())
throw new MarshalException("Unexpected extraneous bytes after map value");
return set;
} else if (type instanceof MapType) {
MapType ltype = (MapType)type;
ByteBuffer input = bb.duplicate();
int size = CollectionSerializer.readCollectionSize(input, Server.VERSION_3);
Map map = new LinkedHashMap(size);
for (int i = 0; i < size; i++) {
ByteBuffer kbb = CollectionSerializer.readValue(input, Server.VERSION_3);
ByteBuffer vbb = CollectionSerializer.readValue(input, Server.VERSION_3);
map.put(deserialize(ltype.getKeysType(), kbb), deserialize(ltype.getValuesType(), vbb));
}
if (input.hasRemaining())
throw new MarshalException("Unexpected extraneous bytes after map value");
return map;
} else {
return type.compose(bb);
}
}
class Document {
final CFMetaData metadata;
final ArrayNode an;
final Object[] pkColumns;
int pkLength;
boolean docLive = false;
final Map<String, Object> docMap = new Hashtable<String, Object>();
XContentBuilder builder;
String id = null;
Collection<String> tombstoneColumns = null;
// init document with partition keys;
private Document(final ByteBuffer rowKey, final ColumnFamily cf) throws IOException {
this.metadata = cf.metadata();
this.docMap.put(ELASTIC_TOKEN,partitioner.getToken(rowKey).getTokenValue());
this.an = ElasticSchemaService.jsonMapper.createArrayNode();
this.pkColumns = new Object[metadata.partitionKeyColumns().size()+metadata.clusteringColumns().size()];
this.pkLength = 0;
CType ctype = metadata.getKeyValidatorAsCType();
Composite composite = ctype.fromByteBuffer(rowKey);
for(int i=0; i<composite.size(); i++) {
ByteBuffer bb = composite.get(i);
AbstractType<?> type = ctype.subtype(i);
String name = metadata.partitionKeyColumns().get(i).name.toString();
Object value = type.compose(bb);
pkColumns[pkLength++] = value;
ElasticSchemaService.addToJsonArray(type, value, an);
if (readOnUpdateColumnMap.get(name) != null) {
docMap.put(name, value);
}
}
}
// init document with clustering keys
public Document(final ByteBuffer rowKey, final ColumnFamily cf, final Cell cell) throws IOException {
this(rowKey, cf);
List<ColumnDefinition> clusteringColumns = metadata.clusteringColumns();
CellName cellName = cell.name();
ColumnDefinition cd = metadata.getColumnDefinition(cellName);
int nrPartitonColumns = metadata.partitionKeyColumns().size();
if ((cd != null) && (clusteringColumns.size() > 0)) {
for(int i=0; i < clusteringColumns.size(); i++) {
ColumnDefinition ccd = clusteringColumns.get(i);
String name = ccd.name.toString();
Object value = deserialize(ccd.type, cellName.get(i));
if (logger.isTraceEnabled())
logger.trace("cell clustering column={} value={}", name, value);
pkColumns[pkLength++] = value;
ElasticSchemaService.addToJsonArray(ccd.type, value, an);
if (cell.isLive() && (readOnUpdateColumnMap.get(name) != null)) {
docLive = true;
docMap.put(name, value);
}
}
}
}
public void addRegularColumn(final String name, final Object value) throws IOException {
if (readOnUpdateColumnMap.get(name) != null) {
docLive = true;
docMap.put(name, value);
}
}
public void addListColumn(final String name, final Object value) throws IOException {
if (readOnUpdateColumnMap.get(name) != null) {
docLive = true;
List v = (List) docMap.get(name);
if (v == null) {
v = new ArrayList();
docMap.put(name, v);
}
v.add(value);
}
}
public void addSetColumn(final String name, final Object value) throws IOException {
if (readOnUpdateColumnMap.get(name) != null) {
docLive = true;
Set v = (Set) docMap.get(name);
if (v == null) {
v = new HashSet();
docMap.put(name, v);
}
v.add(value);
}
}
public void addMapColumn(final String name, final Object key, final Object value) throws IOException {
if (readOnUpdateColumnMap.get(name) != null) {
docLive = true;
Map v = (Map) docMap.get(name);
if (v == null) {
v = new HashMap();
docMap.put(name, v);
}
v.put(key,value);
}
}
public void addTombstoneColumn(String cql3name) {
if (readOnUpdateColumnMap.get(cql3name)) {
if (tombstoneColumns == null) {
tombstoneColumns = new HashSet<String>();
}
tombstoneColumns.add(cql3name);
}
}
public boolean isTombstone(String cql3name) {
if (tombstoneColumns == null) return false;
return (tombstoneColumns.contains(cql3name));
}
public void complete() throws JsonGenerationException, JsonMappingException, IOException {
if (builder == null) {
// add missing or collection columns that should be read before indexing the document.
Collection<String> mustReadColumns = null;
for(String fieldName: readOnUpdateColumnMap.keySet()) {
if (readOnUpdateColumnMap.get(fieldName)) {
Object value = docMap.get(fieldName);
if (value == null) {
if (!isTombstone(fieldName)) {
if (mustReadColumns == null) mustReadColumns = new ArrayList<String>();
mustReadColumns.add(fieldName);
}
} else {
if (value instanceof Collection || value instanceof Map) {
if (mustReadColumns == null) mustReadColumns = new ArrayList<String>();
mustReadColumns.add(fieldName);
}
}
}
}
if (mustReadColumns != null) {
try {
// fetch missing fields from the local cassandra row to update Elasticsearch index
logger.debug(" {}.{} id={} read fields={}",metadata.ksName, metadata.cfName, id(), mustReadColumns);
SchemaService schemaService = ElassandraDaemon.injector().getInstance(SchemaService.class);
Row row = schemaService.fetchRowInternal(metadata.ksName, metadata.cfName, mustReadColumns, pkColumns).one();
int putCount = schemaService.rowAsMap(metadata.ksName, metadata.cfName, row, docMap);
if (putCount > 0) docLive = true;
} catch (RequestValidationException | IOException e) {
logger.error("Failed to fetch columns {}",mustReadColumns,e);
}
}
// TODO: build one builder for each target index
this.builder = XContentFactory.contentBuilder(XContentType.JSON);
this.builder.startObject();
for(Entry<String,Object> entry : docMap.entrySet()) {
builder.field(entry.getKey(),entry.getValue());
}
this.builder.endObject();
}
}
public String id() {
if (id == null) {
try {
id = ElasticSchemaService.buildElasticId(an);
} catch (IOException e) {
logger.error("Unxepected error",e);
}
}
return id;
}
public BytesReference source() throws IOException {
return builder.bytes();
}
public void index() throws JsonGenerationException, JsonMappingException, IOException {
for (Pair<String, String> target : targets) {
try {
if (logger.isTraceEnabled()) {
logger.debug("indexing CF={} target={} id={} source={}",metadata.cfName, target, id(), builder.string());
}
// TODO: should customize source builder for each target index.
synchronousIndex(target.left, target.right, this, this.metadata.getDefaultTimeToLive(), false);
} catch (Throwable e1) {
logger.error("Failed to index document id=" + id() + " in index.type=" + target.left + "." + target.right, e1);
}
}
}
public void delete() {
logger.debug("deleting document from index " + getIndexName() + " id=" + id());
for (Pair<String, String> target : targets) {
logger.debug("xdeleting document from index={} type={} id={}", target.left, target.right, id());
// submit a delete request
XDeleteRequest request = new XDeleteRequest(target.left, target.right, id());
ActionListener<XDeleteResponse> listener = new ActionListener<XDeleteResponse>() {
@Override
public void onResponse(XDeleteResponse response) {
logger.debug("doc deleted id=" + response.getId());
}
@Override
public void onFailure(Throwable e) {
logger.error("failed to delete doc id=" + id, e);
}
};
ElassandraDaemon.client().xdelete(request, listener);
}
}
public void flush() throws JsonGenerationException, JsonMappingException, IOException {
complete();
if (docLive) {
index();
} else {
delete();
}
}
}
class DocumentFactory {
final ByteBuffer rowKey;
final ColumnFamily cf;
final CFMetaData metadata;
final List<ColumnDefinition> clusteringColumns;
final int nrPartitonColumns;
Document doc = null;
public DocumentFactory(final ByteBuffer rowKey, final ColumnFamily cf) {
this.rowKey = rowKey;
this.cf = cf;
this.metadata = cf.metadata();
this.clusteringColumns = metadata.clusteringColumns();
this.nrPartitonColumns = metadata.partitionKeyColumns().size();
}
public Document nextDocument(final Cell cell) throws IOException {
if (this.doc == null) {
this.doc = new Document(rowKey, cf, cell);
return this.doc;
}
CellName cellName = cell.name();
boolean sameRow = true;
if (metadata.getColumnDefinition(cellName) != null) {
for(int i=0; i < clusteringColumns.size(); i++) {
ColumnDefinition ccd = clusteringColumns.get(i);
String name = ccd.name.toString();
Object value = deserialize(ccd.type, cellName.get(i));
if (!value.equals(doc.pkColumns[nrPartitonColumns+i])) {
sameRow = false;
break;
}
}
if (!sameRow) {
doc.flush();
return new Document(rowKey, cf, cell);
}
}
return doc;
}
}
/**
* Index a mutation. Set empty field for deleted cells.
*/
@Override
public void index(ByteBuffer rowKey, ColumnFamily cf) {
if (ElassandraDaemon.client() == null) {
logger.warn("Elasticsearch not ready, cannot index");
return;
}
CFMetaData metadata = cf.metadata();
if (logger.isTraceEnabled()) {
CType ctype = metadata.getKeyValidatorAsCType();
Composite composite = ctype.fromByteBuffer(rowKey);
logger.debug("indexing " + getIndexName() + " cf=" + metadata.ksName + "." + metadata.cfName + " composite=" + composite + " cf=" + cf.toString());
}
try {
DocumentFactory docFactory = new DocumentFactory(rowKey, cf);
Document doc = null;
Cell cell = null;
CellName cellName = null;
Iterator<Cell> cellIterator = cf.iterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
cellName = cell.name();
assert cellName instanceof CompoundSparseCellName;
if (cellName.cql3ColumnName(metadata).toString().length()==0) {
// ignore fake cell with no CQL3 name.
continue;
}
doc = docFactory.nextDocument(cell);
ColumnDefinition cd = metadata.getColumnDefinition(cell.name());
if (cd != null) {
if (cell.isLive()) {
if (cd.type.isCollection()) {
CollectionType ctype = (CollectionType) cd.type;
Object value = null;
switch (ctype.kind) {
case LIST:
value = deserialize(((ListType)cd.type).getElementsType(), cell.value() );
if (logger.isTraceEnabled())
logger.trace("list name={} type={} value={}", cellName.cql3ColumnName(metadata), cd.type.asCQL3Type().toString(), value);
doc.addListColumn(cd.name.toString(), value);
break;
case SET:
value = deserialize(((SetType)cd.type).getElementsType(), cell.value() );
if (logger.isTraceEnabled())
logger.trace("set name={} type={} value={}", cellName.cql3ColumnName(metadata), cd.type.asCQL3Type().toString(), value);
doc.addSetColumn(cd.name.toString(), value);
break;
case MAP:
value = deserialize(((MapType)cd.type).getValuesType(), cell.value() );
String key = (String) deserialize(((MapType)cd.type).getKeysType(), cellName.get(cellName.size()-1));
if (logger.isTraceEnabled())
logger.trace("map name={} type={} key={} value={}",
cellName.cql3ColumnName(metadata),
cd.type.asCQL3Type().toString(),
key,
value);
doc.addMapColumn(cd.name.toString(), key, value);
break;
}
} else {
Object value = deserialize(cd.type, cell.value() );
if (logger.isTraceEnabled())
logger.trace("name={} type={} value={}", cellName.cql3ColumnName(metadata), cd.type.asCQL3Type().toString(), value);
doc.addRegularColumn(cd.name.toString(), value);
}
} else {
// tombstone => black list this column for later document.complete().
doc.addTombstoneColumn(cellName.cql3ColumnName(metadata).toString());
}
}
}
if (doc != null) {
doc.flush();
}
} catch (IOException e) {
logger.error("failed to index",e);
}
}
/**
* Elasticsearch synchronous document index.
*
* @param indexName
* @param type
* @param id
* @param sourceData
* @param ttl
* @throws Throwable
*/
private void synchronousIndex(String indexName, String type, Document doc, long ttl, boolean refresh) throws Throwable {
IndicesService indicesService = ElassandraDaemon.injector().getInstance(IndicesService.class);
IndexService indexService = indicesService.indexServiceSafe(indexName);
IndexShard indexShard = indexService.shardSafe(0);
SourceToParse sourceToParse = SourceToParse.source(SourceToParse.Origin.PRIMARY, doc.source()).type(type).id(doc.id())
.timestamp(Long.toString(System.currentTimeMillis()))
.ttl(ttl);
Engine.Index index = indexShard.prepareIndex(sourceToParse, Versions.MATCH_ANY, VersionType.INTERNAL, Engine.Operation.Origin.PRIMARY, false);
if (index.parsedDoc().mappingsModified()) {
// could be necessay when cqlStruct=map or if mapping update id not yet propagated.
SchemaService schemaService = ElassandraDaemon.injector().getInstance(SchemaService.class);
schemaService.blockingMappingUpdate(indexService, index.docMapper());
}
indexShard.index(index);
long version = index.version();
boolean created = index.created();
if (refresh) {
try {
indexShard.refresh("refresh_flag_index");
} catch (Throwable e) {
// ignore
}
}
logger.debug("document index.type={}.{} id={} version={} created={} ttl={} refresh={} doc={}",
indexName, type, doc.id(), version, created, ttl, refresh, doc.builder.string());
}
/**
* cleans up deleted columns from cassandra cleanup compaction
*
* @param key
*/
@Override
public void delete(DecoratedKey key, Group opGroup) {
if (ElassandraDaemon.client() == null) {
// TODO: save the update in a commit log to replay it later....
logger.warn("Elastic node not yet started, cannot delete document");
return;
}
Token token = key.getToken();
Long token_long = (Long) token.getTokenValue();
logger.debug("deleting document with _token = " + token_long);
// TODO: DeleteByQuery or Scan+Bulk Delete.
/*
for (Pair<String, String> target : targets.keySet()) {
logger.debug("xdeleting document from index={} type={} id={}", target.left, target.right);
// submit a delete request
XDeleteRequest request = new XDeleteRequest(target.left, target.right, );
ActionListener<XDeleteResponse> listener = new ActionListener<XDeleteResponse>() {
@Override
public void onResponse(XDeleteResponse response) {
logger.debug("doc deleted id=" + response.getId());
}
@Override
public void onFailure(Throwable e) {
logger.error("failed to delete doc id=" + id, e);
}
};
ElassandraDaemon.client().xdelete(request, listener);
}
*/
}
@Override
public void init() {
ColumnDefinition cdef = getColumnDefs().iterator().next();
for (String target : cdef.getIndexOptions().get(ELASTIC_OPTION_TARGETS).split(",")) {
String[] indexAndType = target.split("\\.");
if ((indexAndType != null) && (indexAndType.length == 2)) {
Pair<String, String> targetPair = Pair.<String, String> create(indexAndType[0], indexAndType[1]);
targets.add(targetPair);
if (!this.baseCfs.keyspace.getName().equals(targetPair.left) || !this.baseCfs.name.equals(targetPair.right)) {
// explicit mapping from Elastic index.type to Cassandra keyspace.table
mapping.put(targetPair, Pair.create(this.baseCfs.keyspace.getName(), this.baseCfs.name));
}
} else {
logger.warn("Ignoring invalid targets entry {}", indexAndType);
}
}
index_name = ElasticSchemaService.buildIndexName(this.baseCfs.name, cdef.name.toString());
logger.debug(" " + getIndexName() + " targets=" + targets);
buildReadOnUpdateColumnMap();
}
public Map<String,Boolean> indexedColumns(String ksName, String cfName) {
Map<String,Boolean> cols = new HashMap<String, Boolean>();
try {
UntypedResultSet result = QueryProcessor.executeInternal("SELECT column_name, index_type, index_options FROM system.schema_columns WHERE keyspace_name=? and columnfamily_name=?",
new Object[] { ksName, cfName });
for (Row row : result) {
if (row.has("index_type") && "CUSTOM".equals(row.getString("index_type"))) {
Map<String,String> index_options = FBUtilities.fromJsonMap(row.getString("index_options"));
if (index_options != null && "org.elasticsearch.cassandra.ElasticSecondaryIndex".equals(index_options.get("class_name"))) {
cols.put(row.getString("column_name"), Boolean.valueOf(index_options.get(ElasticSecondaryIndex.ELASTIC_OPTION_PARTIAL_UPDATE)));
}
}
}
} catch (Exception e) {
logger.warn("Failed to retreive column names from " + ksName + "." + cfName, e);
}
return cols;
}
private void buildReadOnUpdateColumnMap() {
this.readOnUpdateColumnMap = indexedColumns(this.baseCfs.keyspace.getName(), this.baseCfs.name);
if (logger.isDebugEnabled()) {
logger.debug("{} mapped readOnUpdateColumnMap = {}",this.index_name,this.readOnUpdateColumnMap.toString());
}
}
/**
* Reload an existing index following a change to its configuration, or that
* of the indexed column(s). Differs from init() in that we expect expect
* new resources (such as CFS for a KEYS index) to be created by init() but
* not here
*/
@Override
public void reload() {
buildReadOnUpdateColumnMap();
}
@Override
public void validateOptions() throws ConfigurationException {
for (ColumnDefinition cd : getColumnDefs()) {
for (String optionKey : cd.getIndexOptions().keySet()) {
if (!(optionKey.equals(ELASTIC_OPTION_TARGETS) || optionKey.equals(CUSTOM_INDEX_OPTION_NAME) || optionKey.equals(ELASTIC_OPTION_PARTIAL_UPDATE))) {
throw new ConfigurationException("Unknown elastic secondary index options: " + optionKey);
}
}
}
}
@Override
public String getIndexName() {
return index_name;
}
@Override
protected SecondaryIndexSearcher createSecondaryIndexSearcher(Set<ByteBuffer> columns) {
return new ElasticSecondaryIndexSearcher(this.baseCfs.indexManager, columns);
}
/**
* Cassandra index flush => Elasticsearch flush => lucene commit and disk
* sync.
*/
@Override
public void forceBlockingFlush() {
if (ElassandraDaemon.client() == null) {
logger.warn("Elasticsearch not ready, cannot flush Elasticsearch index");
return;
}
if (ElassandraDaemon.injector() != null) {
IndicesService indicesService = ElassandraDaemon.injector().getInstance(IndicesService.class);
if (indicesService != null) {
for (Pair<String, String> target : targets) {
try {
IndexShard indexShard = indicesService.indexServiceSafe(target.left).shardSafe(0);
logger.debug("Flushing Elasticsearch index=[{}] state=[{}]",target.left, indexShard.state());
indexShard.flush(new FlushRequest().force(false).waitIfOngoing(true));
logger.debug("Elasticsearch index=[{}] flushed",target.left);
} catch (ElasticsearchException e) {
logger.error("Unexpected error",e);
}
}
}
}
}
@Override
public ColumnFamilyStore getIndexCfs() {
// TODO Auto-generated method stub
return null;
}
@Override
public void removeIndex(ByteBuffer columnName) {
try {
logger.warn("removeIndex [{}] not implemented", ByteBufferUtil.string(columnName));
} catch (CharacterCodingException e) {
logger.warn("removeIndex error", e);
}
}
@Override
public void invalidate() {
// TODO Auto-generated method stub
logger.warn("invalidate");
}
@Override
public void truncateBlocking(long truncatedAt) {
// TODO implements truncate
logger.warn("truncateBlocking at [{}], not implemented", truncatedAt);
}
/**
* Returns true if the provided cell name is indexed by this secondary
* index.
*/
@Override
public boolean indexes(CellName name) {
ColumnDefinition cdef = this.baseCfs.metadata.getColumnDefinition(name);
if ((cdef != null) && (cdef.getIndexOptions() != null) && (this.getClass().getCanonicalName().equals(cdef.getIndexOptions().get(CUSTOM_INDEX_OPTION_NAME)))) {
return true;
}
/*
* ..However, some type of COMPACT STORAGE layout do not store the CQL3
* column name in the cell name and so this part can be null => return
* true
*/
ColumnIdentifier cql3Name = name.cql3ColumnName(this.baseCfs.metadata);
return ((cql3Name != null) && (ByteBufferUtil.EMPTY_BYTE_BUFFER.compareTo(cql3Name.bytes) == 0));
}
@Override
public long estimateResultRows() {
// TODO Auto-generated method stub
return 0;
}
}