PHP code example of shibudb-org / shibudb-client-php

1. Go to this page and download the library: Download shibudb-org/shibudb-client-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shibudb-org / shibudb-client-php example snippets



ent = new ShibuDbClient('localhost', 4444);
$client->authenticate('admin', 'admin');

    


reate client and authenticate
$client = new ShibuDbClient('localhost', 4444);
$client->authenticate('admin', 'admin');

// Your operations here
$response = $client->listSpaces();
print_r($response);

// Close connection (or let destructor handle it)
$client->close();


// Using the convenience function
$client = shibudb_connect('localhost', 4444, 'admin', 'admin');
$response = $client->listSpaces();
$client->close();


reate a connection pool
$pool = new ShibuDbConnectionPool(
    'localhost', 4444,        // host, port
    'admin', 'admin',         // username, password
    30, 2, 10, 30             // timeout, min_size, max_size, acquire_timeout
);

// Use pooled connections
$client = $pool->getConnection();
$response = $client->listSpaces();
print_r($response);
$pool->releaseConnection($client);

// Get pool statistics
$stats = $pool->getStats();
print_r($stats);

// Close pool
$pool->close();


// Create and use a space
$client->createSpace('mytable', 'key-value');
$client->useSpace('mytable');

// Basic operations
$client->put('name', 'John Doe');
$response = $client->get('name');
echo $response['value'];  // "John Doe"

$client->delete('name');


// Create a vector space
$client->createSpace('vectors', 'vector', 128, 'Flat', 'L2');
$client->useSpace('vectors');

// Insert vectors
$client->insertVector(1, array(0.1, 0.2, 0.3, /* ... */));
$client->insertVector(2, array(0.4, 0.5, 0.6, /* ... */));

// Search for similar vectors
$results = $client->searchTopk(array(0.1, 0.2, 0.3, /* ... */), 5);
echo $results['message'];  // Search results

// Range search
$results = $client->rangeSearch(array(0.1, 0.2, 0.3, /* ... */), 0.5);

// Delete a vector by ID
$client->deleteVector(1);

new ShibuDbClient($host = 'localhost', $port = 4444, $timeout = 30)

$client->authenticate($username, $password) -> array

$client->createSpace($name, $engineType = 'key-value', $dimension = null, 
                    $indexType = 'Flat', $metric = 'L2') -> array
$client->deleteSpace($name) -> array
$client->listSpaces() -> array
$client->useSpace($name) -> array

$client->put($key, $value, $space = null) -> array
$client->get($key, $space = null) -> array
$client->delete($key, $space = null) -> array

$client->insertVector($vectorId, $vector, $space = null) -> array
$client->searchTopk($queryVector, $k = 1, $space = null) -> array
$client->rangeSearch($queryVector, $radius, $space = null) -> array
$client->getVector($vectorId, $space = null) -> array
$client->deleteVector($vectorId, $space = null) -> array

$client->createUser($user) -> array
$client->updateUserPassword($username, $newPassword) -> array
$client->updateUserRole($username, $newRole) -> array
$client->updateUserPermissions($username, $permissions) -> array
$client->deleteUser($username) -> array
$client->getUser($username) -> array

$user = array(
    'username' => 'user1',
    'password' => 'password123',
    'role' => 'user',  // 'admin' or 'user'
    'permissions' => array(
        'mytable' => 'read',
        'vectortable' => 'write'
    )
);


tion main() {
    try {
        // Connect and authenticate
        $client = new ShibuDbClient('localhost', 4444);
        $client->authenticate('admin', 'admin');
        
        // Create spaces
        $client->createSpace('users', 'key-value');
        $client->createSpace('embeddings', 'vector', 128);
        
        // Store user data
        $client->useSpace('users');
        $client->put('user1', 'Alice Johnson');
        $client->put('user2', 'Bob Smith');
        
        // Store embeddings
        $client->useSpace('embeddings');
        $vector1 = array_fill(0, 128, 0.1);  // Example vector
        $vector2 = array_fill(0, 128, 0.2);  // Example vector
        $client->insertVector(1, $vector1);
        $client->insertVector(2, $vector2);
        
        // Search for similar embeddings
        $queryVector = array_fill(0, 128, 0.1);
        $results = $client->searchTopk($queryVector, 5);
        echo "Search results: " . print_r($results, true);

        // Delete a vector by ID when no longer needed
        $client->deleteVector(1);

        $client->close();
        
    } catch (ShibuDbException $e) {
        echo "Error: " . $e->getMessage() . "\n";
    }
}

main();


{
    $client = new ShibuDbClient('localhost', 4444);
    $client->authenticate('admin', 'admin');
    
    // Your operations here
    
} catch (ShibuDbAuthenticationError $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
} catch (ShibuDbConnectionError $e) {
    echo "Connection failed: " . $e->getMessage() . "\n";
} catch (ShibuDbQueryError $e) {
    echo "Query failed: " . $e->getMessage() . "\n";
} finally {
    if (isset($client)) {
        $client->close();
    }
}


reate admin user
$adminUser = array(
    'username' => 'admin',
    'password' => 'adminpass',
    'role' => 'admin'
);

// Create regular user with permissions
$user = array(
    'username' => 'user1',
    'password' => 'userpass',
    'role' => 'user',
    'permissions' => array(
        'mytable' => 'read',
        'vectortable' => 'write'
    )
);

$client = new ShibuDbClient('localhost', 4444);
$client->authenticate('admin', 'admin');

// Create users
$client->createUser($user);

// Create spaces for different purposes
$client->createSpace('users', 'key-value');
$client->createSpace('products', 'key-value');
$client->createSpace('embeddings', 'vector', 256);
$client->createSpace('recommendations', 'vector', 512);

// Store data in different spaces
$client->useSpace('users');
$client->put('user1', 'Alice Johnson');

$client->useSpace('embeddings');
$queryVector = array_fill(0, 256, 0.1);
$client->insertVector(1, $queryVector);

// Search for recommendations
$results = $client->searchTopk($queryVector, 10);
print_r($results);

$client->close();


reate pool with custom configuration
$pool = new ShibuDbConnectionPool(
    'localhost', 4444,        // host, port
    'admin', 'admin',         // username, password
    30,                       // timeout (seconds)
    2,                        // min_size (minimum connections in pool)
    10,                       // max_size (maximum connections in pool)
    30                        // acquire_timeout (timeout for acquiring connection)
);


// Basic usage
$client = $pool->getConnection();
$response = $client->listSpaces();
print_r($response);
$pool->releaseConnection($client);

// Concurrent operations (example with multiple requests)
$clients = array();
for ($i = 0; $i < 5; $i++) {
    $client = $pool->getConnection();
    $client->createSpace("space_$i", 'key-value');
    $client->useSpace("space_$i");
    $client->put("key_$i", "value_$i");
    $response = $client->get("key_$i");
    print_r($response);
    $pool->releaseConnection($client);
}


// Get pool statistics
$stats = $pool->getStats();
echo "Pool size: " . $stats['pool_size'] . "\n";
echo "Active connections: " . $stats['active_connections'] . "\n";
echo "Min size: " . $stats['min_size'] . "\n";
echo "Max size: " . $stats['max_size'] . "\n";


{
    $client = $pool->getConnection();
    // Your operations here
    $pool->releaseConnection($client);
    
} catch (ShibuDbPoolExhaustedError $e) {
    echo "Pool exhausted: " . $e->getMessage() . "\n";
} catch (ShibuDbAuthenticationError $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
} catch (ShibuDbConnectionError $e) {
    echo "Connection failed: " . $e->getMessage() . "\n";
} finally {
    if (isset($pool)) {
        $pool->close();
    }
}


error_reporting(E_ALL);
ini_set('display_errors', 1);

// Your ShibuDb code here
bash
php simple_test.php
bash
php example.php
bash
php pool_test.php
bash
php comprehensive_pool_test.php
bash
# Tests that php
php pool_test.php
php comprehensive_pool_test.php

# Tests that run without server
php json_parsing_test.php
php special_chars_test.php