1616import com .scalar .db .util .ScalarDbUtils ;
1717import java .util .ArrayList ;
1818import java .util .Collections ;
19- import java .util .HashSet ;
19+ import java .util .Comparator ;
2020import java .util .Iterator ;
2121import java .util .List ;
2222import java .util .Optional ;
23- import java .util .Set ;
2423import java .util .stream .Collectors ;
2524import javax .annotation .Nonnull ;
2625import javax .annotation .concurrent .ThreadSafe ;
@@ -35,19 +34,15 @@ public SelectStatementHandler(
3534 @ Nonnull
3635 public Scanner handle (Selection selection ) throws ExecutionException {
3736 TableMetadata tableMetadata = metadataManager .getTableMetadata (selection );
37+ if (ScalarDbUtils .isSecondaryIndexSpecified (selection , tableMetadata )) {
38+ throw new UnsupportedOperationException (
39+ CoreError .OBJECT_STORAGE_INDEX_NOT_SUPPORTED .buildMessage ());
40+ }
3841 if (selection instanceof Get ) {
39- if (ScalarDbUtils .isSecondaryIndexSpecified (selection , tableMetadata )) {
40- throw new UnsupportedOperationException (
41- CoreError .OBJECT_STORAGE_INDEX_NOT_SUPPORTED .buildMessage ());
42- } else {
43- return executeGet ((Get ) selection , tableMetadata );
44- }
42+ return executeGet ((Get ) selection , tableMetadata );
4543 } else {
4644 if (selection instanceof ScanAll ) {
4745 return executeScanAll ((ScanAll ) selection , tableMetadata );
48- } else if (ScalarDbUtils .isSecondaryIndexSpecified (selection , tableMetadata )) {
49- throw new UnsupportedOperationException (
50- CoreError .OBJECT_STORAGE_INDEX_NOT_SUPPORTED .buildMessage ());
5146 } else {
5247 return executeScan ((Scan ) selection , tableMetadata );
5348 }
@@ -56,7 +51,6 @@ public Scanner handle(Selection selection) throws ExecutionException {
5651
5752 private Scanner executeGet (Get get , TableMetadata metadata ) throws ExecutionException {
5853 ObjectStorageOperation operation = new ObjectStorageOperation (get , metadata );
59- operation .checkArgument (Get .class );
6054 ObjectStoragePartition partition =
6155 getPartition (getNamespace (get ), getTable (get ), operation .getConcatenatedPartitionKey ());
6256 if (!partition .getRecord (operation .getRecordId ()).isPresent ()) {
@@ -71,18 +65,17 @@ private Scanner executeGet(Get get, TableMetadata metadata) throws ExecutionExce
7165
7266 private Scanner executeScan (Scan scan , TableMetadata metadata ) throws ExecutionException {
7367 ObjectStorageOperation operation = new ObjectStorageOperation (scan , metadata );
74- operation .checkArgument (Scan .class );
7568 ObjectStoragePartition partition =
7669 getPartition (getNamespace (scan ), getTable (scan ), operation .getConcatenatedPartitionKey ());
7770 List <ObjectStorageRecord > records = new ArrayList <>(partition .getRecords ().values ());
7871
79- records .sort (
80- (o1 , o2 ) ->
81- new ClusteringKeyComparator (metadata )
82- .compare (o1 .getClusteringKey (), o2 .getClusteringKey ()));
72+ ClusteringKeyComparator clusteringKeyComparator = new ClusteringKeyComparator (metadata );
73+ Comparator <ObjectStorageRecord > cmp =
74+ Comparator .comparing (ObjectStorageRecord ::getClusteringKey , clusteringKeyComparator );
8375 if (isReverseOrder (scan , metadata )) {
84- Collections . reverse ( records );
76+ cmp = cmp . reversed ( );
8577 }
78+ records .sort (cmp );
8679
8780 // If the scan is for DESC clustering order, use the end clustering key as a start key and the
8881 // start clustering key as an end key
@@ -118,16 +111,16 @@ private Scanner executeScan(Scan scan, TableMetadata metadata) throws ExecutionE
118111 }
119112
120113 private Scanner executeScanAll (ScanAll scan , TableMetadata metadata ) throws ExecutionException {
121- ObjectStorageOperation operation = new ObjectStorageOperation (scan , metadata );
122- operation .checkArgument (ScanAll .class );
123- Set <ObjectStorageRecord > records = getRecordsInTable (getNamespace (scan ), getTable (scan ));
124- if (scan .getLimit () > 0 ) {
125- records = records .stream ().limit (scan .getLimit ()).collect (Collectors .toSet ());
114+ try {
115+ List <String > partitionKeys = getPartitionKeysInTable (getNamespace (scan ), getTable (scan ));
116+ StreamingRecordIterator iterator =
117+ new StreamingRecordIterator (wrapper , getNamespace (scan ), getTable (scan ), partitionKeys );
118+ return new ScannerImpl (
119+ iterator , new ResultInterpreter (scan .getProjections (), metadata ), scan .getLimit ());
120+ } catch (Exception e ) {
121+ throw new ExecutionException (
122+ CoreError .OBJECT_STORAGE_ERROR_OCCURRED_IN_SELECTION .buildMessage (e .getMessage ()), e );
126123 }
127- return new ScannerImpl (
128- records .iterator (),
129- new ResultInterpreter (scan .getProjections (), metadata ),
130- scan .getLimit ());
131124 }
132125
133126 private ObjectStoragePartition getPartition (
@@ -145,22 +138,13 @@ private ObjectStoragePartition getPartition(
145138 }
146139 }
147140
148- private Set < ObjectStorageRecord > getRecordsInTable (String namespaceName , String tableName )
141+ private List < String > getPartitionKeysInTable (String namespaceName , String tableName )
149142 throws ExecutionException {
150143 try {
151- Set <String > partitionKeys =
152- wrapper .getKeys (ObjectStorageUtils .getObjectKey (namespaceName , tableName , "" )).stream ()
153- .map (
154- key ->
155- key .substring (key .lastIndexOf (ObjectStorageUtils .OBJECT_KEY_DELIMITER ) + 1 ))
156- .filter (partition -> !partition .isEmpty ())
157- .collect (Collectors .toSet ());
158- Set <ObjectStorageRecord > records = new HashSet <>();
159- for (String partitionKey : partitionKeys ) {
160- ObjectStoragePartition partition = getPartition (namespaceName , tableName , partitionKey );
161- records .addAll (partition .getRecords ().values ());
162- }
163- return records ;
144+ return wrapper .getKeys (ObjectStorageUtils .getObjectKey (namespaceName , tableName , "" )).stream ()
145+ .map (key -> key .substring (key .lastIndexOf (ObjectStorageUtils .OBJECT_KEY_DELIMITER ) + 1 ))
146+ .filter (partition -> !partition .isEmpty ())
147+ .collect (Collectors .toList ());
164148 } catch (Exception e ) {
165149 throw new ExecutionException (
166150 CoreError .OBJECT_STORAGE_ERROR_OCCURRED_IN_SELECTION .buildMessage (e .getMessage ()), e );
0 commit comments