The DBA we are talking about today is not the traditional DBA, but rather an extension of the Buckley-style database in PHP. A Barkley-style database is essentially a K/V database in the form of key-value pairs. Just like memcached or Redis, which we use a lot, it’s just a key and a value, but memcached is mostly stored in memory, whereas DBA extensions store data in files, like SQLite in the form of a simple key-value pair.

The database type used by the DBA extension is basically open source and is very simple to deploy and distribute, just a DB file, so it is very similar to SQLite. The downside is that it loads the database file into memory all at once, so we don’t want to make the database too big or it will burst the memory. The DBA database is embedded in the program, so it doesn’t have a network interface, and we typically only use this database locally in our code.

At installation time, we need to add a –enable-dba=shared configuration at compile time, and then a — enable-xxxx configuration, which refers to the type of Buckley database we want to use, The more common are DBM, NDBM, GDBM, DB2 and so on. Similarly, the operating system also needs to install these related software, for example, our system is installed with GDBM, need to use yum Install to install.

A simple example

Let’s first look at the code to see how our DBA database is used.

// Open a database file
$id = dba_open("/tmp/test.db"."n"."gdbm");
//$id = dba_popen("/tmp/test1.db", "c", "gdbm");

// Add or replace a content
dba_replace("key1"."This is an example!".$id);

// If the content exists
if(dba_exists("key1".$id)) {// Read the content
    echo dba_fetch("key1".$id), PHP_EOL;
    // This is an example!
}

dba_close($id);
Copy the code

Dba_open () is used to open a database file. The first parameter is the path of the database file, and the second parameter is the opening method, including r, w, c, and n, where r means read-only, w means read and write, C means create plus read and write, and n means create and write if not. The third parameter is the specified database type. Our system only has the GDBM library installed, so we use GDBM as the parameter. Like mysql, we can also use dba_popen() to open a persistent link to a data file.

The dba_replace() function adds or replaces a piece of data, adding a new piece if it doesn’t exist, and replacing the corresponding key if it does. The first argument is key, and the second argument is the value of the data.

Dba_exists () checks whether the specified key exists. If so, we fetch the data specified by the key via dba_fetch() in the if.

Dba_close () closes the database connection handle just like any other data manipulation handle.

Add, iterate, and delete data

In the example above, we used dba_replace() to add data, but there is a function for adding data.

// Add data
dba_insert("key2"."This is key2!".$id);
dba_insert("key3"."This is key3!".$id);
dba_insert("key4"."This is key4!".$id);
dba_insert("key5"."This is key5!".$id);
dba_insert("key6"."This is key6!".$id);

// Get the first key
$key = dba_firstkey($id);

$handle_later = [];
while ($key! = =false) {
    if (true) {
        // Save the key to the array
        $handle_later[] = $key;
    }
    // Get the next key
    $key = dba_nextkey($id);
}

// Iterate over the key array to print the entire contents of the database
foreach ($handle_later as $val) {
    echo dba_fetch($val.$id), PHP_EOL;
    dba_delete($val.$id); // Delete the contents of the key
}
// This is key4!
// This is key2!
// This is key3!
// This is an example!
// This is key5!
// This is key6!
Copy the code

Dba_insert () is the insert data. It does not replace existing key data and returns false if it is inserting existing key information.

Dba_firstkey () is used to retrieve the firstkey, and dba_nextkey() is used to retrieve the nextkey. With these two functions, we can obtain information about all the keys in the entire database, which can then be used to traverse the entire database.

Dba_delete () deletes a piece of data by key.

Optimize and synchronize databases

Mysql optimize table name with SQL statement: optimize table name. Similarly, the DBA extension provides us with such a function.

// Optimize the database
var_dump(dba_optimize($id)); // bool(true)
Copy the code

In addition, just like the mysql cache, the DBA will cache the data as it operates, so you can use a function to flush the cached data to a disk file.

// Synchronize the database
var_dump(dba_sync($id)); // bool(true)
Copy the code

List of databases currently open

We can use a single function to see what data connections are currently open, and since the DBA is a simple file-based database, we can open multiple data connections in one piece of code.

// Get the list of currently open databases
var_dump(dba_list());
// array(1) {
/ / [4] = >
// string(12) "/tmp/test.db"
/ /}
Copy the code

Database type supported by the system

Finally, let’s look at a supporting function that returns the number of database types currently supported by our database.

// Database type currently supported
var_dump(dba_handlers(true));
// array(5) {
// ["gdbm"]=>
// string(58) "GDBM Version 1.18.21/08/2018 (built May 11 2019 01:10:11)"
// ["cdb"]=>
/ / string (53) "0.75, $Id: 841505 a20a8c9c8e35cac5b5dc3d5cf2fe917478 $"
// ["cdb_make"]=>
/ / string (53) "0.75, $Id: 95 b1c2518144e0151afa6b2b8c7bf31bf1f037ed $"
// ["inifile"]=>
/ / string (52) 42 cb3bb7617b5744add2ab117b45b3a1e37e7175 $1.0, $Id: ""
// ["flatfile"]=>
/ / string (52) "1.0, $Id: 410 f3405266f41bafffc8993929b8830b761436b $"
/ /}

var_dump(dba_handlers(false));
// array(5) {
/ / [0] = >
// string(4) "gdbm"
/ / [1] = >
// string(3) "cdb"
/ / [2] = >
// string(8) "cdb_make"
/ / [3] = >
// string(7) "inifile"
/ / [4] = >
// string(8) "flatfile"
/ /}
Copy the code

Dba_handlers () has a Boolean parameter, which we see in the code as the level of detail returned.

conclusion

Today’s introduction is a very simple set of database extension components, its functions are these, in the daily production environment, the actual application scenarios are not many. Simple key-value pairs can be stored using PHP file serialization, while caching is more often done using memcached or something like that.

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/b…

Blog.csdn.net/weixin_4023…

Follow the public account: [Hardcore project manager] to get the latest articles

Add WeChat/QQ friends: free xiaoyuezigonggong / 149844827 】 【 PHP, project management, learning materials

Zhihu, Public Account, Douyin, Toutiao Search [Hardcore Project Manager]

ID of station B: 482780532