-
Notifications
You must be signed in to change notification settings - Fork 234
/
PelopsClient.java
803 lines (710 loc) · 29.7 KB
/
PelopsClient.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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/*******************************************************************************
* * Copyright 2012 Impetus Infotech.
* *
* * 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 com.impetus.client.cassandra.pelops;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.PersistenceException;
import javax.persistence.metamodel.EmbeddableType;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.CounterColumn;
import org.apache.cassandra.thrift.CounterSuperColumn;
import org.apache.cassandra.thrift.IndexClause;
import org.apache.cassandra.thrift.IndexExpression;
import org.apache.cassandra.thrift.IndexOperator;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.Mutation;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.SchemaDisagreementException;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SuperColumn;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.thrift.TException;
import org.scale7.cassandra.pelops.Bytes;
import org.scale7.cassandra.pelops.Mutator;
import org.scale7.cassandra.pelops.Pelops;
import org.scale7.cassandra.pelops.RowDeletor;
import org.scale7.cassandra.pelops.Selector;
import org.scale7.cassandra.pelops.exceptions.PelopsException;
import org.scale7.cassandra.pelops.pool.IThriftPool;
import org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection;
import com.impetus.client.cassandra.CassandraClientBase;
import com.impetus.client.cassandra.common.CassandraUtilities;
import com.impetus.client.cassandra.datahandler.CassandraDataHandler;
import com.impetus.client.cassandra.index.InvertedIndexHandler;
import com.impetus.client.cassandra.query.CassQuery;
import com.impetus.client.cassandra.thrift.ThriftRow;
import com.impetus.kundera.Constants;
import com.impetus.kundera.KunderaException;
import com.impetus.kundera.client.Client;
import com.impetus.kundera.client.EnhanceEntity;
import com.impetus.kundera.db.RelationHolder;
import com.impetus.kundera.db.SearchResult;
import com.impetus.kundera.generator.TableGenerator;
import com.impetus.kundera.graph.Node;
import com.impetus.kundera.index.IndexManager;
import com.impetus.kundera.metadata.KunderaMetadataManager;
import com.impetus.kundera.metadata.model.EntityMetadata;
import com.impetus.kundera.metadata.model.KunderaMetadata;
import com.impetus.kundera.metadata.model.MetamodelImpl;
import com.impetus.kundera.metadata.model.TableGeneratorDiscriptor;
import com.impetus.kundera.persistence.EntityReader;
import com.impetus.kundera.persistence.api.Batcher;
import com.impetus.kundera.persistence.context.jointable.JoinTableData;
import com.impetus.kundera.property.PropertyAccessor;
import com.impetus.kundera.property.PropertyAccessorFactory;
import com.impetus.kundera.property.PropertyAccessorHelper;
/**
* Client implementation using Pelops. http://code.google.com/p/pelops/
*
* @author animesh.kumar
* @since 0.1
*/
public class PelopsClient extends CassandraClientBase implements Client, Batcher, TableGenerator
{
/** log for this class. */
private static Log log = LogFactory.getLog(PelopsClient.class);
/** The closed. */
private boolean closed = false;
/** The data handler. */
private PelopsDataHandler dataHandler;
/** Handler for Inverted indexing */
private InvertedIndexHandler invertedIndexHandler;
/** The timestamp. */
private long timestamp;
private List<Mutation> mutations;
/**
* default constructor.
*
* @param indexManager
* the index manager
* @param reader
* the reader
* @param persistenceUnit
* the persistence unit
*/
public PelopsClient(IndexManager indexManager, EntityReader reader, String persistenceUnit,
Map<String, Object> externalProperties)
{
super(persistenceUnit, externalProperties);
this.persistenceUnit = persistenceUnit;
this.indexManager = indexManager;
this.dataHandler = new PelopsDataHandler(externalProperties);
this.invertedIndexHandler = new PelopsInvertedIndexHandler(externalProperties);
this.reader = reader;
}
@Override
public final Object find(Class entityClass, Object rowId)
{
return super.find(entityClass, rowId);
}
@Override
public final <E> List<E> findAll(Class<E> entityClass, Object... rowIds)
{
return super.findAll(entityClass, rowIds);
}
/**
* Method to return list of entities for given below attributes:.
*
* @param entityClass
* entity class
* @param relationNames
* relation names
* @param isWrapReq
* true, in case it needs to populate enhance entity.
* @param metadata
* entity metadata.
* @param rowIds
* array of row key s
* @return list of wrapped entities.
*/
@Override
public final List find(Class entityClass, List<String> relationNames, boolean isWrapReq, EntityMetadata metadata,
Object... rowIds)
{
if (!isOpen())
{
throw new PersistenceException("PelopsClient is closed.");
}
List entities = null;
try
{
entities = dataHandler.fromThriftRow(entityClass, metadata, relationNames, isWrapReq,
getConsistencyLevel(), rowIds);
}
catch (Exception e)
{
throw new KunderaException(e);
}
return entities;
}
@Override
public void delete(Object entity, Object pKey, List<RelationHolder> rlHolders)
{
if (!isOpen())
{
throw new PersistenceException("PelopsClient is closed.");
}
EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(entity.getClass());
MetamodelImpl metaModel = (MetamodelImpl) KunderaMetadata.INSTANCE.getApplicationMetadata().getMetamodel(
metadata.getPersistenceUnit());
if (metaModel.isEmbeddable(metadata.getIdAttribute().getBindableJavaType()))
{
EmbeddableType compoundKey = metaModel.embeddable(metadata.getIdAttribute().getBindableJavaType());
onDeleteQuery(metadata, metaModel, pKey, compoundKey);
}
else
{
if (metadata.isCounterColumnType())
{
deleteRecordFromCounterColumnFamily(pKey, metadata, getConsistencyLevel());
}
else
{
RowDeletor rowDeletor = Pelops.createRowDeletor(PelopsUtils.generatePoolName(getPersistenceUnit(),
externalProperties));
rowDeletor.deleteRow(metadata.getTableName(),
CassandraUtilities.toBytes(pKey, metadata.getIdAttribute().getJavaType()),
getConsistencyLevel());
}
}
// Delete from Lucene if applicable
getIndexManager().remove(metadata, entity, pKey.toString());
// Delete from Inverted Index if applicable
invertedIndexHandler.delete(entity, metadata, getConsistencyLevel());
}
@Override
public final void close()
{
this.indexManager.flush();
this.dataHandler = null;
this.invertedIndexHandler = null;
closed = true;
}
/**
* Persists records into Join Table
*/
public void persistJoinTable(JoinTableData joinTableData)
{
String poolName = PelopsUtils.generatePoolName(getPersistenceUnit(), externalProperties);
Mutator mutator = Pelops.createMutator(poolName);
String joinTableName = joinTableData.getJoinTableName();
String invJoinColumnName = joinTableData.getInverseJoinColumnName();
Map<Object, Set<Object>> joinTableRecords = joinTableData.getJoinTableRecords();
EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(joinTableData.getEntityClass());
for (Object key : joinTableRecords.keySet())
{
Set<Object> values = joinTableRecords.get(key);
List<Column> columns = new ArrayList<Column>();
for (Object value : values)
{
Column column = new Column();
column.setName(PropertyAccessorFactory.STRING.toBytes(invJoinColumnName
+ Constants.JOIN_COLUMN_NAME_SEPARATOR + value.toString()));
// column.setValue(PropertyAccessorFactory.STRING.toBytes((String)
// value));
column.setValue(PropertyAccessorHelper.getBytes(value));
column.setTimestamp(System.currentTimeMillis());
columns.add(column);
}
createIndexesOnColumns(entityMetadata.getSchema(), joinTableName, poolName, columns);
// Object pk = key;
mutator.writeColumns(joinTableName, Bytes.fromByteArray(PropertyAccessorHelper.getBytes(key)),
Arrays.asList(columns.toArray(new Column[0])));
}
mutator.execute(getConsistencyLevel());
}
@Override
public <E> List<E> getColumnsById(String schemaName, String joinTableName, String joinColumnName,
String inverseJoinColumnName, Object parentId)
{
Selector selector = Pelops.createSelector(PelopsUtils
.generatePoolName(getPersistenceUnit(), externalProperties));
List<Column> columns = selector.getColumnsFromRow(joinTableName,
Bytes.fromByteArray(PropertyAccessorHelper.getBytes(parentId)),
Selector.newColumnsPredicateAll(true, 10), getConsistencyLevel());
List<E> foreignKeys = dataHandler.getForeignKeysFromJoinTable(inverseJoinColumnName, columns);
return foreignKeys;
}
@Override
public Object[] findIdsByColumn(String schemaName, String tableName, String pKeyName, String columnName,
Object columnValue, Class entityClazz)
{
Selector selector = Pelops.createSelector(PelopsUtils
.generatePoolName(getPersistenceUnit(), externalProperties));
SlicePredicate slicePredicate = Selector.newColumnsPredicateAll(false, 10000);
EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(entityClazz);
String childIdStr = (String) columnValue;
IndexClause ix = Selector.newIndexClause(Bytes.EMPTY, 10000, Selector.newIndexExpression(columnName
+ Constants.JOIN_COLUMN_NAME_SEPARATOR + childIdStr, IndexOperator.EQ,
Bytes.fromByteArray(childIdStr.getBytes())));
Map<Bytes, List<Column>> qResults = selector.getIndexedColumns(tableName, ix, slicePredicate,
getConsistencyLevel());
List<Object> rowKeys = new ArrayList<Object>();
// iterate through complete map and
Iterator<Bytes> rowIter = qResults.keySet().iterator();
while (rowIter.hasNext())
{
Bytes rowKey = rowIter.next();
PropertyAccessor<?> accessor = PropertyAccessorFactory.getPropertyAccessor((Field) metadata
.getIdAttribute().getJavaMember());
Object value = accessor.fromBytes(metadata.getIdAttribute().getJavaType(), rowKey.toByteArray());
rowKeys.add(value);
}
if (rowKeys != null && !rowKeys.isEmpty())
{
return rowKeys.toArray(new Object[0]);
}
return null;
}
@Override
public void deleteByColumn(String schemaName, String tableName, String columnName, Object columnValue)
{
if (!isOpen())
{
throw new PersistenceException("PelopsClient is closed.");
}
RowDeletor rowDeletor = Pelops.createRowDeletor(PelopsUtils.generatePoolName(getPersistenceUnit(),
externalProperties));
// rowDeletor.deleteRow(tableName, columnValue.toString(),
// getConsistencyLevel());
rowDeletor.deleteRow(tableName, CassandraUtilities.toBytes(columnValue, columnValue.getClass()),
getConsistencyLevel());
}
@Override
public List find(Class<?> entityClass, Map<String, String> embeddedColumnMap)
{
return super.find(entityClass, embeddedColumnMap, dataHandler);
}
/*
* (non-Javadoc)
*
* @see com.impetus.kundera.client.Client#find(java.lang.String,
* java.lang.String, com.impetus.kundera.metadata.model.EntityMetadata)
*/
@Override
public List<Object> findByRelation(String colName, Object colValue, Class clazz)
{
EntityMetadata m = KunderaMetadataManager.getEntityMetadata(clazz);
Selector selector = Pelops.createSelector(PelopsUtils
.generatePoolName(getPersistenceUnit(), externalProperties));
SlicePredicate slicePredicate = Selector.newColumnsPredicateAll(false, 10000);
List<Object> entities = null;
IndexClause ix = Selector.newIndexClause(
Bytes.EMPTY,
10000,
Selector.newIndexExpression(colName, IndexOperator.EQ,
Bytes.fromByteArray(PropertyAccessorHelper.getBytes(colValue))));
Map<Bytes, List<Column>> qResults;
try
{
qResults = selector.getIndexedColumns(m.getTableName(), ix, slicePredicate, getConsistencyLevel());
}
catch (PelopsException e)
{
log.info(e.getMessage());
return entities;
}
entities = new ArrayList<Object>(qResults.size());
// iterate through complete map and
populateData(m, qResults, entities, false, null, dataHandler);
return entities;
}
@Override
public Class<?> getDefaultQueryImplementor()
{
return CassQuery.class;
}
@Override
public void persist(Object entity, Object id, List<RelationHolder> rlHolders)
{
EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(entity.getClass());
if (!isOpen())
{
throw new PersistenceException("PelopsClient is closed.");
}
// check for counter column
if (isUpdate && metadata.isCounterColumnType())
{
throw new UnsupportedOperationException("Merge is not permitted on counter column! ");
}
MetamodelImpl metaModel = (MetamodelImpl) KunderaMetadata.INSTANCE.getApplicationMetadata().getMetamodel(
metadata.getPersistenceUnit());
if (metaModel.isEmbeddable(metadata.getIdAttribute().getBindableJavaType()))
{
Cassandra.Client client = getRawClient(metadata.getPersistenceUnit(), metadata.getSchema());
try
{
client.set_cql_version(getCqlVersion());
client.set_keyspace(metadata.getSchema());
onpersistOverCompositeKey(metadata, entity, client, rlHolders);
}
catch (InvalidRequestException e)
{
log.error("Error during persist, Caused by:" + e.getMessage());
throw new KunderaException(e);
}
catch (TException e)
{
log.error("Error during persist, Caused by:" + e.getMessage());
throw new KunderaException(e);
}
catch (UnsupportedEncodingException e)
{
log.error("Error during persist, Caused by:" + e.getMessage());
throw new KunderaException(e);
}
catch (UnavailableException e)
{
log.error("Error during persist, Caused by:" + e.getMessage());
throw new KunderaException(e);
}
catch (TimedOutException e)
{
log.error("Error during persist, Caused by:" + e.getMessage());
throw new KunderaException(e);
}
catch (SchemaDisagreementException e)
{
log.error("Error during persist, Caused by:" + e.getMessage());
throw new KunderaException(e);
}
}
else
{
ThriftRow tf = null;
try
{
String columnFamily = metadata.getTableName();
tf = dataHandler.toThriftRow(entity, id, metadata, columnFamily);
timestamp = System.currentTimeMillis();
}
catch (Exception e)
{
log.error("Error during persist, Caused by:" + e.getMessage());
throw new KunderaException(e);
}
addRelationsToThriftRow(metadata, tf, rlHolders);
Mutator mutator = Pelops.createMutator(PelopsUtils.generatePoolName(getPersistenceUnit(),
externalProperties));
if (metadata.isCounterColumnType())
{
List<CounterColumn> thriftCounterColumns = tf.getCounterColumns();
List<CounterSuperColumn> thriftCounterSuperColumns = tf.getCounterSuperColumns();
if (thriftCounterColumns != null && !thriftCounterColumns.isEmpty())
{
mutator.writeCounterColumns(metadata.getTableName(),
CassandraUtilities.toBytes(tf.getId(), tf.getId().getClass()),
Arrays.asList(tf.getCounterColumns().toArray(new CounterColumn[0])));
}
if (thriftCounterSuperColumns != null && !thriftCounterSuperColumns.isEmpty())
{
for (CounterSuperColumn sc : thriftCounterSuperColumns)
{
mutator.writeSubCounterColumns(metadata.getTableName(),
CassandraUtilities.toBytes(tf.getId(), tf.getId().getClass()),
Bytes.fromByteArray(sc.getName()), sc.getColumns());
}
}
}
else
{
List<Column> thriftColumns = tf.getColumns();
List<SuperColumn> thriftSuperColumns = tf.getSuperColumns();
if (thriftColumns != null && !thriftColumns.isEmpty())
{
// Bytes.fromL
mutator.writeColumns(metadata.getTableName(),
CassandraUtilities.toBytes(tf.getId(), tf.getId().getClass()),
Arrays.asList(tf.getColumns().toArray(new Column[0])));
}
if (thriftSuperColumns != null && !thriftSuperColumns.isEmpty())
{
for (SuperColumn sc : thriftSuperColumns)
{
mutator.writeSubColumns(metadata.getTableName(),
CassandraUtilities.toBytes(tf.getId(), tf.getId().getClass()),
Bytes.fromByteArray(sc.getName()), sc.getColumns());
}
}
}
mutator.execute(getConsistencyLevel());
tf = null;
}
}
/**
* Indexes @Embedded and @ElementCollection objects of this entity to a
* separate column family
*/
@Override
protected void indexNode(Node node, EntityMetadata entityMetadata)
{
// Index to lucene if applicable
super.indexNode(node, entityMetadata);
// Write to inverted index table if applicable
invertedIndexHandler.write(node, entityMetadata, getPersistenceUnit(), getConsistencyLevel(), dataHandler);
}
/**
* Load super columns.
*
* @param keyspace
* the keyspace
* @param columnFamily
* the column family
* @param rowId
* the row id
* @param superColumnNames
* the super column names
* @return the list
*/
@Override
public final List<SuperColumn> loadSuperColumns(String keyspace, String columnFamily, String rowId,
String... superColumnNames)
{
if (!isOpen())
throw new PersistenceException("PelopsClient is closed.");
Selector selector = Pelops.createSelector(PelopsUtils
.generatePoolName(getPersistenceUnit(), externalProperties));
List<ByteBuffer> rowKeys = new ArrayList<ByteBuffer>();
rowKeys.add(ByteBuffer.wrap(rowId.getBytes()));
// Pelops.getDbConnPool("").getConnection().getAPI().
// selector.getColumnOrSuperColumnsFromRows(new
// ColumnParent(columnFamily),rowKeys ,
// Selector.newColumnsPredicate(superColumnNames),
// getConsistencyLevel());
return selector.getSuperColumnsFromRow(columnFamily, rowId, Selector.newColumnsPredicate(superColumnNames),
getConsistencyLevel());
}
/** Query related methods */
/**
* Method to execute cql query and return back entity/enhance entities.
*
* @param cqlQuery
* cql query to be executed.
* @param clazz
* entity class.
* @param relationalField
* collection for relational fields.
* @return list of objects.
*
*/
@Override
public List executeQuery(String cqlQuery, Class clazz, List<String> relationalField)
{
return super.executeQuery(cqlQuery, clazz, relationalField, dataHandler);
}
/**
* Find.
*
* @param ixClause
* the ix clause
* @param m
* the m
* @param isRelation
* the is relation
* @param relations
* the relations
* @return the list
*/
@Override
public List find(List<IndexClause> ixClause, EntityMetadata m, boolean isRelation, List<String> relations,
int maxResult, List<String> columns)
{
// ixClause can be 0,1 or more!
Selector selector = Pelops.createSelector(PelopsUtils
.generatePoolName(getPersistenceUnit(), externalProperties));
SlicePredicate slicePredicate = Selector.newColumnsPredicateAll(false, Integer.MAX_VALUE);
if (columns != null && !columns.isEmpty())
{
slicePredicate = Selector.newColumnsPredicate(columns.toArray(new String[] {}));
}
List<Object> entities = null;
if (ixClause.isEmpty())
{
if (m.isCounterColumnType())
{
try
{
IThriftPool thrift = Pelops.getDbConnPool(PelopsUtils.generatePoolName(getPersistenceUnit(),
externalProperties));
// thrift.get
IPooledConnection connection = thrift.getConnection();
org.apache.cassandra.thrift.Cassandra.Client thriftClient = connection.getAPI();
List<KeySlice> ks = thriftClient.get_range_slices(new ColumnParent(m.getTableName()),
slicePredicate, Selector.newKeyRange("", "", maxResult), getConsistencyLevel());
connection.release();
entities = onCounterColumn(m, isRelation, relations, ks);
}
catch (InvalidRequestException irex)
{
log.error("Error during executing find, Caused by :" + irex.getMessage());
throw new PersistenceException(irex);
}
catch (UnavailableException uex)
{
log.error("Error during executing find, Caused by :" + uex.getMessage());
throw new PersistenceException(uex);
}
catch (TimedOutException tex)
{
log.error("Error during executing find, Caused by :" + tex.getMessage());
throw new PersistenceException(tex);
}
catch (TException tex)
{
log.error("Error during executing find, Caused by :" + tex.getMessage());
throw new PersistenceException(tex);
}
}
else
{
if (m.getType().isSuperColumnFamilyMetadata())
{
Map<Bytes, List<SuperColumn>> qResults = selector.getSuperColumnsFromRows(m.getTableName(),
selector.newKeyRange("", "", maxResult), slicePredicate, getConsistencyLevel());
entities = new ArrayList<Object>(qResults.size());
computeEntityViaSuperColumns(m, isRelation, relations, entities, qResults);
}
else
{
Map<Bytes, List<Column>> qResults = selector.getColumnsFromRows(m.getTableName(),
selector.newKeyRange("", "", maxResult), slicePredicate, getConsistencyLevel());
entities = new ArrayList<Object>(qResults.size());
// populateData(m, qResults, entities, isRelation,
// relations, dataHandler);
computeEntityViaColumns(m, isRelation, relations, entities, qResults);
}
}
}
else
{
entities = new ArrayList<Object>();
for (IndexClause ix : ixClause)
{
Map<Bytes, List<Column>> qResults = selector.getIndexedColumns(m.getTableName(), ix, slicePredicate,
getConsistencyLevel());
computeEntityViaColumns(m, isRelation, relations, entities, qResults);
// // iterate through complete map and
// populateData(m, qResults, entities, isRelation, relations,
// dataHandler);
}
}
return entities;
}
/**
* Find.
*
* @param m
* the m
* @param relationNames
* the relation names
* @param conditions
* the conditions
* @return the list
*/
public List<EnhanceEntity> find(EntityMetadata m, List<String> relationNames, List<IndexClause> conditions,
int maxResult, List<String> columns)
{
return (List<EnhanceEntity>) find(conditions, m, true, relationNames, maxResult, columns);
}
/**
* Find by range.
*
* @param minVal
* the min val
* @param maxVal
* the max val
* @param m
* the m
* @param isWrapReq
* the is wrap req
* @param relations
* the relations
* @return the list
* @throws Exception
* the exception
*/
@Override
public List findByRange(byte[] minVal, byte[] maxVal, EntityMetadata m, boolean isWrapReq, List<String> relations,
List<String> columns, List<IndexExpression> conditions) throws Exception
{
Selector selector = Pelops.createSelector(PelopsUtils
.generatePoolName(getPersistenceUnit(), externalProperties));
SlicePredicate slicePredicate = Selector.newColumnsPredicateAll(false, Integer.MAX_VALUE);
if (columns != null && !columns.isEmpty())
{
slicePredicate = Selector.newColumnsPredicate(columns.toArray(new String[] {}));
}
KeyRange keyRange = selector.newKeyRange(minVal != null ? Bytes.fromByteArray(minVal) : Bytes.fromUTF8(""),
maxVal != null ? Bytes.fromByteArray(maxVal) : Bytes.fromUTF8(""), 10000);
if (conditions != null)
{
keyRange.setRow_filter(conditions);
keyRange.setRow_filterIsSet(true);
}
// List<Object> entities = null;
List<KeySlice> keys = selector.getKeySlices(new ColumnParent(m.getTableName()), keyRange, slicePredicate,
getConsistencyLevel());
List results = null;
if (keys != null)
{
results = populateEntitiesFromKeySlices(m, isWrapReq, relations, keys, dataHandler);
}
return results;
}
public List<SearchResult> searchInInvertedIndex(String columnFamilyName, EntityMetadata m,
Map<Boolean, List<IndexClause>> indexClauseMap)
{
return invertedIndexHandler.search(m, getPersistenceUnit(), getConsistencyLevel(), indexClauseMap);
}
/*
* (non-Javadoc)
*
* @see com.impetus.client.cassandra.CassandraClientBase#getDataHandler()
*/
@Override
protected CassandraDataHandler getDataHandler()
{
return dataHandler;
}
protected IPooledConnection getPooledConection(String persistenceUnit)
{
return PelopsUtils.getCassandraConnection(persistenceUnit, externalProperties);
}
protected void releaseConnection(Object conn)
{
PelopsUtils.releaseConnection((IPooledConnection) conn);
}
@Override
public Long generate(TableGeneratorDiscriptor discriptor)
{
return getGeneratedValue(discriptor, getPersistenceUnit());
}
}