xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
Commit 17d8eb4b4592dd96e62a8da74d87d3d3b26aeb86

Improve state handling: do not check for an old value; verify if table exists before doing a select
Author: Catalin(ux) M. BOIE
Author date (UTC): 2017-03-26 09:37
Committer name: Catalin(ux) M. BOIE
Committer date (UTC): 2017-03-26 09:37
Parent(s): 4d6349f7143d61583e6d2d9612c89b55af4eea97
Signing key:
Tree: 496a709ab75f316eb621e00806bd53afe3170722
File Lines added Lines deleted
inc/state.inc.php 37 17
File inc/state.inc.php changed (mode: 100644) (index 54bfaab..e35035c)
... ... function rg_state_get($db, $var)
41 41 break; break;
42 42 } }
43 43
44 // Not found in cache, try the database
45 $params = array("var" => $var);
46 $sql = "SELECT value FROM state WHERE var = @@var@@ LIMIT 1";
47 $res = rg_sql_query_params($db, $sql, $params);
44 // It may be possible the database was not created
45 // We return an empty string
46 $k = '__internal' . '::' . 'STATE_TABLE_EXISTS';
47 $r = rg_cache_get($k);
48 if ($r === FALSE) {
49 // Let's see if the 'state' table is present
50 $r = rg_sql_rel_exists($db, 'state');
51 if (($r === FALSE) || ($r === 0)) {
52 $ret = FALSE;
53 break;
54 }
55 rg_cache_set($k, 1, RG_SOCKET_NO_WAIT);
56 }
57
58 if ($r === 2) { // state table was loaded
59 // but the var is not there
60 $ret = '';
61 break;
62 }
63
64 // We have to go in the database...
65 $sql = 'SELECT * FROM state';
66 $res = rg_sql_query($db, $sql);
48 67 if ($res === FALSE) { if ($res === FALSE) {
49 68 rg_state_set_error(rg_sql_error()); rg_state_set_error(rg_sql_error());
50 69 break; break;
 
... ... function rg_state_get($db, $var)
52 71
53 72 $rows = rg_sql_num_rows($res); $rows = rg_sql_num_rows($res);
54 73 if ($rows == 0) { if ($rows == 0) {
55 $ret = "";
74 $ret = '';
56 75 } else { } else {
57 $row = rg_sql_fetch_array($res);
58 $ret = $row['value'];
59 rg_cache_set("state::" . $var, $ret, RG_SOCKET_NO_WAIT);
76 $rows = array();
77 while (($row = rg_sql_fetch_array($res))) {
78 $v = $row['var'];
79 $rows[$v] = $row['value'];
80 }
81 rg_cache_set('state', $rows, RG_SOCKET_NO_WAIT);
82 rg_cache_set($k, 2, RG_SOCKET_NO_WAIT);
83
84 if (isset($rows[$var]))
85 $ret = $rows[$var];
86 else
87 $ret = '';
60 88 } }
61 89 rg_sql_free_result($res); rg_sql_free_result($res);
62
63 90 break; break;
64 91 } }
65 92
 
... ... function rg_state_set($db, $var, $value)
94 121
95 122 $ret = FALSE; $ret = FALSE;
96 123 while (1) { while (1) {
97 $params = array("var" => $var, "value" => $value);
98 $old = rg_state_get($db, $var);
99 if ($old === FALSE)
100 break;
101
102 if (strcmp($old, $value) == 0)
103 return TRUE;
104
124 $params = array('var' => $var, 'value' => $value);
105 125 $sql = 'UPDATE state SET value = @@value@@' $sql = 'UPDATE state SET value = @@value@@'
106 126 . ' WHERE var = @@var@@'; . ' WHERE var = @@var@@';
107 127 $res = rg_sql_query_params($db, $sql, $params); $res = rg_sql_query_params($db, $sql, $params);
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master