@@ -1126,57 +1126,71 @@ private GarnetStatus SortedSetUnion<TObjectContext>(ReadOnlySpan<ArgSlice> keys,
1126
1126
1127
1127
// Get the first sorted set
1128
1128
var status = GET ( keys [ 0 ] . ToArray ( ) , out var firstObj , ref objectContext ) ;
1129
+
1130
+ if ( status == GarnetStatus . WRONGTYPE )
1131
+ {
1132
+ return GarnetStatus . WRONGTYPE ;
1133
+ }
1134
+
1135
+ Dictionary < byte [ ] , double > sortedSetDictionary = null ;
1136
+
1129
1137
if ( status == GarnetStatus . OK )
1130
1138
{
1131
1139
if ( firstObj . GarnetObject is not SortedSetObject firstSortedSet )
1132
1140
{
1133
1141
return GarnetStatus . WRONGTYPE ;
1134
1142
}
1143
+ sortedSetDictionary = firstSortedSet . Dictionary ;
1144
+ }
1135
1145
1136
- // Initialize pairs with the first set
1137
- if ( weights is null )
1138
- {
1139
- pairs = new Dictionary < byte [ ] , double > ( firstSortedSet . Dictionary , ByteArrayComparer . Instance ) ;
1140
- }
1141
- else
1146
+ // Initialize pairs with the first set
1147
+ if ( weights is null )
1148
+ {
1149
+ pairs = sortedSetDictionary is null ? new Dictionary < byte [ ] , double > ( ByteArrayComparer . Instance ) : new Dictionary < byte [ ] , double > ( sortedSetDictionary , ByteArrayComparer . Instance ) ;
1150
+ }
1151
+ else
1152
+ {
1153
+ pairs = new Dictionary < byte [ ] , double > ( ByteArrayComparer . Instance ) ;
1154
+ if ( sortedSetDictionary is not null )
1142
1155
{
1143
- pairs = new Dictionary < byte [ ] , double > ( ByteArrayComparer . Instance ) ;
1144
- foreach ( var ( key , score ) in firstSortedSet . Dictionary )
1156
+ foreach ( var ( key , score ) in sortedSetDictionary )
1145
1157
{
1146
1158
pairs [ key ] = weights [ 0 ] * score ;
1147
1159
}
1148
1160
}
1161
+ }
1149
1162
1150
- // Process remaining sets
1151
- for ( var i = 1 ; i < keys . Length ; i ++ )
1163
+ // Process remaining sets
1164
+ for ( var i = 1 ; i < keys . Length ; i ++ )
1165
+ {
1166
+ status = GET ( keys [ i ] . ToArray ( ) , out var nextObj , ref objectContext ) ;
1167
+ if ( status == GarnetStatus . WRONGTYPE )
1168
+ return GarnetStatus . WRONGTYPE ;
1169
+ if ( status != GarnetStatus . OK )
1170
+ continue ;
1171
+
1172
+ if ( nextObj . GarnetObject is not SortedSetObject nextSortedSet )
1152
1173
{
1153
- status = GET ( keys [ i ] . ToArray ( ) , out var nextObj , ref objectContext ) ;
1154
- if ( status != GarnetStatus . OK )
1155
- continue ;
1174
+ pairs = default ;
1175
+ return GarnetStatus . WRONGTYPE ;
1176
+ }
1156
1177
1157
- if ( nextObj . GarnetObject is not SortedSetObject nextSortedSet )
1178
+ foreach ( var ( key , score ) in nextSortedSet . Dictionary )
1179
+ {
1180
+ var weightedScore = weights is null ? score : score * weights [ i ] ;
1181
+ if ( pairs . TryGetValue ( key , out var existingScore ) )
1158
1182
{
1159
- pairs = default ;
1160
- return GarnetStatus . WRONGTYPE ;
1183
+ pairs [ key ] = aggregateType switch
1184
+ {
1185
+ SortedSetAggregateType . Sum => existingScore + weightedScore ,
1186
+ SortedSetAggregateType . Min => Math . Min ( existingScore , weightedScore ) ,
1187
+ SortedSetAggregateType . Max => Math . Max ( existingScore , weightedScore ) ,
1188
+ _ => existingScore + weightedScore // Default to SUM
1189
+ } ;
1161
1190
}
1162
-
1163
- foreach ( var ( key , score ) in nextSortedSet . Dictionary )
1191
+ else
1164
1192
{
1165
- var weightedScore = weights is null ? score : score * weights [ i ] ;
1166
- if ( pairs . TryGetValue ( key , out var existingScore ) )
1167
- {
1168
- pairs [ key ] = aggregateType switch
1169
- {
1170
- SortedSetAggregateType . Sum => existingScore + weightedScore ,
1171
- SortedSetAggregateType . Min => Math . Min ( existingScore , weightedScore ) ,
1172
- SortedSetAggregateType . Max => Math . Max ( existingScore , weightedScore ) ,
1173
- _ => existingScore + weightedScore // Default to SUM
1174
- } ;
1175
- }
1176
- else
1177
- {
1178
- pairs [ key ] = weightedScore ;
1179
- }
1193
+ pairs [ key ] = weightedScore ;
1180
1194
}
1181
1195
}
1182
1196
}
@@ -1190,37 +1204,47 @@ private GarnetStatus SortedSetDifference<TObjectContext>(ReadOnlySpan<ArgSlice>
1190
1204
pairs = default ;
1191
1205
1192
1206
var statusOp = GET ( keys [ 0 ] . ToArray ( ) , out var firstObj , ref objectContext ) ;
1193
- if ( statusOp == GarnetStatus . OK )
1207
+ if ( statusOp == GarnetStatus . WRONGTYPE )
1194
1208
{
1195
- if ( firstObj . GarnetObject is not SortedSetObject firstSortedSet )
1196
- {
1197
- return GarnetStatus . WRONGTYPE ;
1198
- }
1209
+ return GarnetStatus . WRONGTYPE ;
1210
+ }
1199
1211
1200
- if ( keys . Length == 1 )
1201
- {
1202
- pairs = firstSortedSet . Dictionary ;
1203
- return GarnetStatus . OK ;
1204
- }
1212
+ if ( statusOp == GarnetStatus . NOTFOUND )
1213
+ {
1214
+ pairs = new Dictionary < byte [ ] , double > ( ByteArrayComparer . Instance ) ;
1215
+ return GarnetStatus . OK ;
1216
+ }
1205
1217
1206
- // read the rest of the keys
1207
- for ( var item = 1 ; item < keys . Length ; item ++ )
1208
- {
1209
- statusOp = GET ( keys [ item ] . ToArray ( ) , out var nextObj , ref objectContext ) ;
1210
- if ( statusOp != GarnetStatus . OK )
1211
- continue ;
1218
+ if ( firstObj . GarnetObject is not SortedSetObject firstSortedSet )
1219
+ {
1220
+ return GarnetStatus . WRONGTYPE ;
1221
+ }
1212
1222
1213
- if ( nextObj . GarnetObject is not SortedSetObject nextSortedSet )
1214
- {
1215
- pairs = default ;
1216
- return GarnetStatus . WRONGTYPE ;
1217
- }
1223
+ if ( keys . Length == 1 )
1224
+ {
1225
+ pairs = firstSortedSet . Dictionary ;
1226
+ return GarnetStatus . OK ;
1227
+ }
1218
1228
1219
- if ( pairs == default )
1220
- pairs = SortedSetObject . CopyDiff ( firstSortedSet , nextSortedSet ) ;
1221
- else
1222
- SortedSetObject . InPlaceDiff ( pairs , nextSortedSet ) ;
1229
+ // read the rest of the keys
1230
+ for ( var item = 1 ; item < keys . Length ; item ++ )
1231
+ {
1232
+ statusOp = GET ( keys [ item ] . ToArray ( ) , out var nextObj , ref objectContext ) ;
1233
+ if ( statusOp == GarnetStatus . WRONGTYPE )
1234
+ return GarnetStatus . WRONGTYPE ;
1235
+ if ( statusOp != GarnetStatus . OK )
1236
+ continue ;
1237
+
1238
+ if ( nextObj . GarnetObject is not SortedSetObject nextSortedSet )
1239
+ {
1240
+ pairs = default ;
1241
+ return GarnetStatus . WRONGTYPE ;
1223
1242
}
1243
+
1244
+ if ( pairs == default )
1245
+ pairs = SortedSetObject . CopyDiff ( firstSortedSet , nextSortedSet ) ;
1246
+ else
1247
+ SortedSetObject . InPlaceDiff ( pairs , nextSortedSet ) ;
1224
1248
}
1225
1249
1226
1250
return GarnetStatus . OK ;
@@ -1410,71 +1434,82 @@ private GarnetStatus SortedSetIntersection<TObjectContext>(ReadOnlySpan<ArgSlice
1410
1434
pairs = default ;
1411
1435
1412
1436
var statusOp = GET ( keys [ 0 ] . ToArray ( ) , out var firstObj , ref objectContext ) ;
1413
- if ( statusOp == GarnetStatus . OK )
1437
+
1438
+ if ( statusOp == GarnetStatus . WRONGTYPE )
1414
1439
{
1415
- if ( firstObj . GarnetObject is not SortedSetObject firstSortedSet )
1416
- {
1417
- return GarnetStatus . WRONGTYPE ;
1418
- }
1440
+ return GarnetStatus . WRONGTYPE ;
1441
+ }
1419
1442
1420
- // Initialize result with first set
1421
- if ( weights is null )
1443
+ if ( statusOp == GarnetStatus . NOTFOUND )
1444
+ {
1445
+ pairs = new Dictionary < byte [ ] , double > ( ByteArrayComparer . Instance ) ;
1446
+ return GarnetStatus . OK ;
1447
+ }
1448
+
1449
+ if ( firstObj . GarnetObject is not SortedSetObject firstSortedSet )
1450
+ {
1451
+ return GarnetStatus . WRONGTYPE ;
1452
+ }
1453
+
1454
+ // Initialize result with first set
1455
+ if ( weights is null )
1456
+ {
1457
+ pairs = keys . Length == 1 ? firstSortedSet . Dictionary : new Dictionary < byte [ ] , double > ( firstSortedSet . Dictionary , ByteArrayComparer . Instance ) ;
1458
+ }
1459
+ else
1460
+ {
1461
+ pairs = new Dictionary < byte [ ] , double > ( ByteArrayComparer . Instance ) ;
1462
+ foreach ( var kvp in firstSortedSet . Dictionary )
1422
1463
{
1423
- pairs = keys . Length == 1 ? firstSortedSet . Dictionary : new Dictionary < byte [ ] , double > ( firstSortedSet . Dictionary , ByteArrayComparer . Instance ) ;
1464
+ pairs [ kvp . Key ] = kvp . Value * weights [ 0 ] ;
1424
1465
}
1425
- else
1466
+ }
1467
+
1468
+ if ( keys . Length == 1 )
1469
+ {
1470
+ return GarnetStatus . OK ;
1471
+ }
1472
+
1473
+ // Intersect with remaining sets
1474
+ for ( var i = 1 ; i < keys . Length ; i ++ )
1475
+ {
1476
+ statusOp = GET ( keys [ i ] . ToArray ( ) , out var nextObj , ref objectContext ) ;
1477
+ if ( statusOp == GarnetStatus . WRONGTYPE )
1478
+ return GarnetStatus . WRONGTYPE ;
1479
+ if ( statusOp != GarnetStatus . OK )
1426
1480
{
1427
- pairs = new Dictionary < byte [ ] , double > ( ByteArrayComparer . Instance ) ;
1428
- foreach ( var kvp in firstSortedSet . Dictionary )
1429
- {
1430
- pairs [ kvp . Key ] = kvp . Value * weights [ 0 ] ;
1431
- }
1481
+ pairs = default ;
1482
+ return statusOp ;
1432
1483
}
1433
1484
1434
- if ( keys . Length == 1 )
1485
+ if ( nextObj . GarnetObject is not SortedSetObject nextSortedSet )
1435
1486
{
1436
- return GarnetStatus . OK ;
1487
+ pairs = default ;
1488
+ return GarnetStatus . WRONGTYPE ;
1437
1489
}
1438
1490
1439
- // Intersect with remaining sets
1440
- for ( var i = 1 ; i < keys . Length ; i ++ )
1491
+ foreach ( var kvp in pairs )
1441
1492
{
1442
- statusOp = GET ( keys [ i ] . ToArray ( ) , out var nextObj , ref objectContext ) ;
1443
- if ( statusOp != GarnetStatus . OK )
1493
+ if ( ! nextSortedSet . TryGetScore ( kvp . Key , out var score ) )
1444
1494
{
1445
- pairs = default ;
1446
- return statusOp ;
1447
- }
1448
-
1449
- if ( nextObj . GarnetObject is not SortedSetObject nextSortedSet )
1450
- {
1451
- pairs = default ;
1452
- return GarnetStatus . WRONGTYPE ;
1495
+ pairs . Remove ( kvp . Key ) ;
1496
+ continue ;
1453
1497
}
1454
1498
1455
- foreach ( var kvp in pairs )
1499
+ var weightedScore = weights is null ? score : score * weights [ i ] ;
1500
+ pairs [ kvp . Key ] = aggregateType switch
1456
1501
{
1457
- if ( ! nextSortedSet . TryGetScore ( kvp . Key , out var score ) )
1458
- {
1459
- pairs . Remove ( kvp . Key ) ;
1460
- continue ;
1461
- }
1462
-
1463
- var weightedScore = weights is null ? score : score * weights [ i ] ;
1464
- pairs [ kvp . Key ] = aggregateType switch
1465
- {
1466
- SortedSetAggregateType . Sum => kvp . Value + weightedScore ,
1467
- SortedSetAggregateType . Min => Math . Min ( kvp . Value , weightedScore ) ,
1468
- SortedSetAggregateType . Max => Math . Max ( kvp . Value , weightedScore ) ,
1469
- _ => kvp . Value + weightedScore // Default to SUM
1470
- } ;
1471
- }
1502
+ SortedSetAggregateType . Sum => kvp . Value + weightedScore ,
1503
+ SortedSetAggregateType . Min => Math . Min ( kvp . Value , weightedScore ) ,
1504
+ SortedSetAggregateType . Max => Math . Max ( kvp . Value , weightedScore ) ,
1505
+ _ => kvp . Value + weightedScore // Default to SUM
1506
+ } ;
1507
+ }
1472
1508
1473
- // If intersection becomes empty, we can stop early
1474
- if ( pairs . Count == 0 )
1475
- {
1476
- break ;
1477
- }
1509
+ // If intersection becomes empty, we can stop early
1510
+ if ( pairs . Count == 0 )
1511
+ {
1512
+ break ;
1478
1513
}
1479
1514
}
1480
1515
0 commit comments