Skip to content

Commit 9d91c40

Browse files
committed
feat: adding indexing feature
1 parent bda251e commit 9d91c40

File tree

9 files changed

+236
-3
lines changed

9 files changed

+236
-3
lines changed

expo-example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ allprojects {
3939
maven { url 'https://www.jitpack.io' }
4040
}
4141
}
42-
apply from: "../../android/build.gradle"
42+
apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"apply from: "../../android/build.gradle"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React, { useState } from 'react';
2+
import { Collection } from 'cbl-reactnative';
3+
import CBLCollectionActionContainer from '@/components/CBLCollectionActionContainer';
4+
import HeaderView from '@/components/HeaderView';
5+
import { Divider } from '@gluestack-ui/themed';
6+
import { StyledTextInput } from '@/components/StyledTextInput';
7+
8+
export default function IndexCreateScreen() {
9+
const [indexName, setIndexName] = useState<string>('');
10+
const [indexProperties, setIndexProperties] = useState<string>('');
11+
12+
function reset() {
13+
setIndexName('');
14+
setIndexProperties('');
15+
}
16+
17+
async function update(collection: Collection): Promise<string[]> {
18+
return [
19+
`Collection: <${collection.fullName()}> was retrieved from database <${collection.database.getName()}>`,
20+
];
21+
}
22+
23+
return (
24+
<CBLCollectionActionContainer
25+
handleUpdatePressed={update}
26+
handleResetPressed={reset}
27+
screenTitle="Create Index"
28+
>
29+
<HeaderView name="Index" iconName="magnify" />
30+
<StyledTextInput
31+
autoCapitalize="none"
32+
placeholder="IndexName"
33+
onChangeText={(newText) => setIndexName(newText)}
34+
defaultValue={indexName}
35+
/>
36+
<Divider style={{ marginLeft: 8, marginTop: 10, marginBottom: 10 }} />
37+
<StyledTextInput
38+
style={{
39+
height: 120,
40+
minHeight: 20,
41+
marginBottom: 10,
42+
}}
43+
autoCapitalize="none"
44+
placeholder="Index Properties (comma separated)"
45+
onChangeText={(newText) => setIndexProperties(newText)}
46+
defaultValue={indexProperties}
47+
multiline={true}
48+
/>
49+
</CBLCollectionActionContainer>
50+
);
51+
}

expo-example/ios/expoexample.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@
357357
);
358358
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_DEBUG";
359359
PRODUCT_BUNDLE_IDENTIFIER = "com.couchbase.rn.expo-example";
360-
PRODUCT_NAME = expoexample;
360+
PRODUCT_NAME = "expoexample";
361361
SWIFT_OBJC_BRIDGING_HEADER = "expoexample/expoexample-Bridging-Header.h";
362362
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
363363
SWIFT_VERSION = 5.0;
@@ -385,7 +385,7 @@
385385
);
386386
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_RELEASE";
387387
PRODUCT_BUNDLE_IDENTIFIER = "com.couchbase.rn.expo-example";
388-
PRODUCT_NAME = expoexample;
388+
PRODUCT_NAME = "expoexample";
389389
SWIFT_OBJC_BRIDGING_HEADER = "expoexample/expoexample-Bridging-Header.h";
390390
SWIFT_VERSION = 5.0;
391391
TARGETED_DEVICE_FAMILY = "1,2";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

expo-example/ios/expoexample/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
<key>NSUserActivityTypes</key>
5353
<array>
5454
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
55+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
56+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).expo.index_route</string>
5557
</array>
5658
<key>UILaunchStoryboardName</key>
5759
<string>SplashScreen</string>

ios/CblReactnative.mm

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ @interface RCT_EXTERN_MODULE(CblReactnative, NSObject)
1111
withResolver:(RCTPromiseResolveBlock)resolve
1212
withRejecter:(RCTPromiseRejectBlock)reject)
1313

14+
RCT_EXTERN_METHOD(collection_CreateIndex:
15+
(NSString *) indexName
16+
withIndexData:(NSDictionary *)index
17+
fromCollectionWithName:(NSString *) collectionName
18+
fromScopeWithName:(NSString *) scopeName
19+
fromDatabaseWithName:(NSString *) name
20+
withResolver:(RCTPromiseResolveBlock)resolve
21+
withRejecter:(RCTPromiseRejectBlock)reject)
22+
1423
RCT_EXTERN_METHOD(collection_DeleteCollection:
1524
(NSString *) collectionName
1625
fromDatabaseWithName:(NSString *) name
@@ -27,6 +36,14 @@ @interface RCT_EXTERN_MODULE(CblReactnative, NSObject)
2736
withResolver:(RCTPromiseResolveBlock)resolve
2837
withRejecter:(RCTPromiseRejectBlock)reject)
2938

39+
RCT_EXTERN_METHOD(collection_DeleteIndex:
40+
(NSString *) indexName
41+
fromCollectionWithName:(NSString *) collectionName
42+
fromScopeWithName:(NSString *) scopeName
43+
fromDatabaseWithName:(NSString *) name
44+
withResolver:(RCTPromiseResolveBlock)resolve
45+
withRejecter:(RCTPromiseRejectBlock)reject)
46+
3047
RCT_EXTERN_METHOD(collection_GetBlobContent:
3148
(NSString *) key
3249
fromDocumentWithId:(NSString *) docId
@@ -85,6 +102,13 @@ @interface RCT_EXTERN_MODULE(CblReactnative, NSObject)
85102
withResolver:(RCTPromiseResolveBlock)resolve
86103
withRejecter:(RCTPromiseRejectBlock)reject)
87104

105+
RCT_EXTERN_METHOD(collection_GetIndexes:
106+
(NSString *) collectionName
107+
fromScopeWithName:(NSString *) scopeName
108+
fromDatabaseWithName:(NSString *) name
109+
withResolver:(RCTPromiseResolveBlock)resolve
110+
withRejecter:(RCTPromiseRejectBlock)reject)
111+
88112
RCT_EXTERN_METHOD(collection_PurgeDocument:
89113
(NSString *) docId
90114
fromDatabaseWithName:(NSString *) name

ios/CblReactnative.swift

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ class CblReactnative: NSObject {
6060
}
6161
}
6262

63+
@objc(collection_CreateIndex:withIndexData:fromCollectionWithName:fromScopeWithName:fromDatabaseWithName:withResolver:withRejecter:)
64+
func collection_CreateIndex(
65+
indexName: NSString,
66+
index: NSDictionary,
67+
collectionName: NSString,
68+
scopeName: NSString,
69+
name: NSString,
70+
resolve: @escaping RCTPromiseResolveBlock,
71+
reject: @escaping RCTPromiseRejectBlock
72+
) -> Void {
73+
backgroundQueue.async {
74+
do {
75+
let (isError, args) = DataAdapter.shared.adaptCollectionArgs(name: name, collectionName: collectionName, scopeName: scopeName, reject: reject)
76+
let (isIndexNameError, idxName) = DataAdapter.shared.adaptNonEmptyString(value: indexName, propertyName: "indexName", reject:reject)
77+
78+
let (isIndexDataError, indexData) = DataAdapter.shared.adaptIndexToArrayAny(dict: index, reject: reject)
79+
if isError || isIndexNameError || isIndexDataError {
80+
return
81+
}
82+
83+
try CollectionManager.shared.createIndex(
84+
idxName,
85+
indexType: indexData.indexType,
86+
items: indexData.indexes,
87+
collectionName: args.collectionName,
88+
scopeName: args.scopeName,
89+
databaseName: args.databaseName)
90+
DispatchQueue.main.async {
91+
resolve(nil)
92+
}
93+
} catch let error as NSError {
94+
DispatchQueue.main.async {
95+
reject("DATABASE_ERROR", error.localizedDescription, nil)
96+
}
97+
} catch {
98+
DispatchQueue.main.async {
99+
reject("DATABASE_ERROR", error.localizedDescription, nil)
100+
}
101+
}
102+
}
103+
}
104+
63105
@objc(collection_DeleteCollection:fromDatabaseWithName:fromScopeWithName:withResolver:withRejecter:)
64106
func collection_DeleteCollection(
65107
collectionName: NSString,
@@ -127,6 +169,44 @@ class CblReactnative: NSObject {
127169
}
128170
}
129171

172+
@objc(collection_DeleteCollection:fromCollectionWithName:fromScopeWithName:fromDatabaseWithName:withResolver:withRejecter:)
173+
func collection_DeleteCollection(
174+
indexName: NSString,
175+
collectionName: NSString,
176+
scopeName: NSString,
177+
name: NSString,
178+
resolve: @escaping RCTPromiseResolveBlock,
179+
reject: @escaping RCTPromiseRejectBlock
180+
) -> Void {
181+
backgroundQueue.async {
182+
do {
183+
let (isError, args) = DataAdapter.shared.adaptCollectionArgs(name: name, collectionName: collectionName, scopeName: scopeName, reject: reject)
184+
let (isIndexNameError, idxName) = DataAdapter.shared.adaptNonEmptyString(value: indexName, propertyName: "indexName", reject:reject)
185+
186+
if isError || isIndexNameError {
187+
return
188+
}
189+
190+
try CollectionManager.shared.deleteIndex(
191+
idxName,
192+
collectionName: args.collectionName,
193+
scopeName: args.scopeName,
194+
databaseName: args.databaseName)
195+
DispatchQueue.main.async {
196+
resolve(nil)
197+
}
198+
} catch let error as NSError {
199+
DispatchQueue.main.async {
200+
reject("DATABASE_ERROR", error.localizedDescription, nil)
201+
}
202+
} catch {
203+
DispatchQueue.main.async {
204+
reject("DATABASE_ERROR", error.localizedDescription, nil)
205+
}
206+
}
207+
}
208+
}
209+
130210
@objc(collection_GetBlobContent:fromDocumentWithId:fromDatabaseWithName:fromScopeWithName:fromCollectionWithName:withResolver:withRejecter:)
131211
func collection_GetBlobContent(
132212
key: NSString,
@@ -279,6 +359,7 @@ class CblReactnative: NSObject {
279359
}
280360
}
281361

362+
282363
@objc(collection_GetDefault:withResolver:withRejecter:)
283364
func collection_GetDefault(
284365
name: NSString,
@@ -407,6 +488,43 @@ class CblReactnative: NSObject {
407488
}
408489
}
409490

491+
@objc(collection_GetIndexes:fromScopeWithName:fromDatabaseWithName:withResolver:withRejecter:)
492+
func collection_DeleteCollection(
493+
collectionName: NSString,
494+
scopeName: NSString,
495+
name: NSString,
496+
resolve: @escaping RCTPromiseResolveBlock,
497+
reject: @escaping RCTPromiseRejectBlock
498+
) -> Void {
499+
backgroundQueue.async {
500+
do {
501+
let (isError, args) = DataAdapter.shared.adaptCollectionArgs(name: name, collectionName: collectionName, scopeName: scopeName, reject: reject)
502+
503+
if isError {
504+
return
505+
}
506+
507+
let indexes = try CollectionManager.shared.indexes(
508+
args.collectionName,
509+
scopeName: args.scopeName,
510+
databaseName: args.databaseName)
511+
let dict:NSDictionary = [
512+
"indexes": indexes]
513+
DispatchQueue.main.async {
514+
resolve(dict)
515+
}
516+
} catch let error as NSError {
517+
DispatchQueue.main.async {
518+
reject("DATABASE_ERROR", error.localizedDescription, nil)
519+
}
520+
} catch {
521+
DispatchQueue.main.async {
522+
reject("DATABASE_ERROR", error.localizedDescription, nil)
523+
}
524+
}
525+
}
526+
}
527+
410528
@objc(collection_PurgeDocument:fromDatabaseWithName:fromScopeWithName:fromCollectionWithName:withResolver:withRejecter:)
411529
func collection_PurgeDocument(
412530
docId: NSString,

ios/DataAdapter.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,23 @@ public class DataAdapter {
173173
return (isError, args)
174174
}
175175

176+
public func adaptIndexToArrayAny(dict: NSDictionary, reject: @escaping RCTPromiseRejectBlock) -> (Bool, IndexArgs){
177+
let indexArgs = IndexArgs()
178+
var isError = false
179+
var result = [[Any]]()
180+
indexArgs.indexType = String(dict["type"] as! NSString)
181+
for (key, value) in dict["items"] as! NSDictionary {
182+
result.append([key, value])
183+
}
184+
indexArgs.indexes = result
185+
if (indexArgs.indexes.isEmpty || indexArgs.indexType.isEmpty){
186+
isError = true
187+
reject("INDEX_ERROR", "Can't parse Index information", nil)
188+
189+
}
190+
return (isError, indexArgs)
191+
}
192+
176193
public func adaptNonEmptyString(value: NSString, propertyName: String, reject: @escaping RCTPromiseRejectBlock) -> (Bool, String) {
177194
var isError = false
178195
let strValue = String(value)

ios/IndexArgs.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// IndexArgs.swift
3+
// cbl-reactnative
4+
//
5+
// Created by Aaron LaBeau on 8/1/24.
6+
//
7+
8+
import Foundation
9+
10+
public class IndexArgs {
11+
public var indexType: String = ""
12+
public var indexes: [[Any]] = []
13+
}

0 commit comments

Comments
 (0)