aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-mariadb.el
blob: 52ca8bc92f079f42b9823059a897e0c15aa6783f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
;;; my-mariadb.el -- Customization for mariadb development-branch -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotted.

;; dotted is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotted is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotted.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Customization for mariadb development-branch.

;;; Code:

(require 'my-prog)
(require 'sql)

(defun my-sql-maybe-mtrr ()
  (interactive)
  (if (equal (file-name-extension (buffer-file-name))
             "test")
      (call-interactively 'project-compile)
    (sql-send-buffer)))

(defun my-gdb-maria ()
  (interactive)
  (when (and (buffer-live-p gud-comint-buffer)
             (get-buffer-process gud-comint-buffer))
    (my-gdb-quit))
  (sleep-for 1)
  (my-gdb
   (format "rr replay %s -d %s"
           (expand-file-name
            (replace-regexp-in-string
             "/src"
             "/build/mysql-test/var/log/mysqld.1.rr/latest-trace"
             (project-root (project-current t))))
           (expand-file-name "~/bin/gdb-mi.sh"))))

(defun my-gdb-maria-spider ()
  (interactive)
  (when (and (buffer-live-p gud-comint-buffer)
             (get-buffer-process gud-comint-buffer))
    (my-gdb-quit))
  (sleep-for 1)
  (my-gdb
   (format "rr replay %s -d %s"
           (expand-file-name
            (replace-regexp-in-string
             "/src"
             "/build/mysql-test/var/log/mysqld.1.1.rr/latest-trace"
             ;; "/build/mysql-test/var/log/mysqld.3.1.rr/latest-trace"
             (project-root (project-current t))))
           (expand-file-name "~/bin/gdb-mi.sh"))))

(defvar my-c-style-maria-spider
  '("maria-spider"
    (c-basic-offset . 2)
    (c-offsets-alist
     . (;; no indentation when opening an brace
        (substatement-open . 0)
        ;; no extra space when aligning continued lines of block comments
        (c . +)
        (arglist-intro . +)
        (arglist-close . 0)
        (arglist-cont-nonempty c-lineup-gcc-asm-reg c-lineup-arglist)
        (arglist-cont . 0)
        (statement-block-intro . +)
        ))
    ;; empty comment start on block comment continuation refill
    (c-block-comment-prefix . "")
    ))

(defun my-mdev-22979 (beg end)
  (interactive "r")
  (save-excursion
    (goto-char end)
    (insert "  )},
")
    (goto-char beg)
    (insert "  {C_STRING_WITH_LEN(
")))

(defun my-gdb-mysql-parse-frame ()
  (interactive)
  (let ((old-frame) (level))
    (with-current-buffer (gdb-stack-buffer-name)
      (save-excursion
        (beginning-of-buffer)
        (setq old-frame (point))
        (text-property-search-forward
         'gdb-frame
         "mysql_parse"
         (lambda (val prop) (equal (alist-get 'func prop) val)))
        (beginning-of-line)
        (setq level (alist-get 'level (get-text-property (point) 'gdb-frame)))))
    (with-current-buffer gud-comint-buffer
      (insert "f " level)
      (comint-send-input))
    (with-current-buffer (gdb-stack-buffer-name)
      (save-excursion
        (goto-char old-frame)
        (gdb-select-frame)))))

(defun my-mdev-29502-seek-next ()
  (save-excursion
    (while (re-search-forward "\\(\\ *\\)SPIDER_CLEAR_FILE_POS(&\\(.*\\)->mta_conn_mutex_file_pos);
\\ *pthread_mutex_unlock(&\\2->mta_conn_mutex);" nil t)
      (replace-match "\\1spider_conn_mutex_unlock(\\2);")))
  (save-excursion
    (while (re-search-forward "\\(\\ *\\)DBUG_ASSERT(!\\(.*\\)->mta_conn_mutex_lock_already);
\\ *DBUG_ASSERT(!\\2->mta_conn_mutex_unlock_later);
\\ *\\2->mta_conn_mutex_lock_already = TRUE;
\\ *\\2->mta_conn_mutex_unlock_later = TRUE;" nil t)
      (replace-match "\\1spider_conn_mutex_locked(\\2);")))
  (save-excursion
    (while (re-search-forward "\\(\\ *\\)DBUG_ASSERT(\\(.*\\)->mta_conn_mutex_lock_already);
\\ *DBUG_ASSERT(\\2->mta_conn_mutex_unlock_later);
\\ *\\2->mta_conn_mutex_lock_already = FALSE;
\\ *\\2->mta_conn_mutex_unlock_later = FALSE;" nil t)
      (replace-match "\\1spider_conn_mutex_unlocking(\\2);")))
  (save-excursion
    (while (re-search-forward "\\(\\ *\\)pthread_mutex_lock(&\\(.*\\)->mta_conn_mutex);
\\ *SPIDER_SET_FILE_POS(&\\2->mta_conn_mutex_file_pos);" nil t)
      (replace-match "\\1spider_conn_mutex_lock(\\2);")))
  )

(defun my-mdev-29502-need-lock ()
  (save-excursion
    (while (re-search-forward "\\(\\ *\\)if ([^!].*need_lock_before_set_sql_for_exec(.*" nil t)
      (beginning-of-line)
      (kill-sexp 3)
      (kill-line)))
  (save-excursion
    (while (re-search-forward "\\(\\ *\\)if (!.*need_lock_before_set_sql_for_exec(.*" nil t)
      (beginning-of-line)
      (kill-sexp 2)
      (kill-line 2)
      (c-indent-line-or-region)
      (beginning-of-line 3)
      (kill-line 1)
      )))

(defvar my-mdev-32524-ids nil "List of ids used.")

(defun my-mdev-32524-make-id ()
  "Return an id on the current position."
  (let* ((base
          (replace-regexp-in-string
           "::" "_"
           (replace-regexp-in-string
            "^\\(SPIDER_\\)?" "SPD_MID_"
            (upcase (which-function)))))
         (last-id (car my-mdev-32524-ids))
         (counter (cdr last-id))
         (result))
    (if (equal (car last-id) base)
        (progn
          (setq result (format "%s_%d" base (1+ counter)))
          (push (cons base (1+ counter)) my-mdev-32524-ids))
      (setq result (format "%s_1" base))
      (push (cons base 1) my-mdev-32524-ids))
    result))

(defun my-mdev-32524-replace-ids ()
  "Replace magic id numbers with enum symbols."
  (setq my-mdev-32524-ids nil)
  (dolist (file (directory-files "./storage/spider" t ".\\(cc\\|h\\)$"))
    ;; Don't do this with spd_malloc.{cc,h}
    (unless (string-match "spd_malloc" file)
      (with-current-buffer (find-file-noselect file)
        (goto-char (point-min))
        (while (re-search-forward
                (concat
                 "\\(spider_alloc_calc_mem_init(.*?, \\|init_calc_mem(\\|"
                 "spider_malloc(.*?, \\|"
                 "spider_bulk_alloc_mem(.*?, \\|spider_bulk_malloc.*?, \\)"
                 "\\([0-9]+\\)"
                 ;; "\\(([^0-9)]*\\)\\([0-9]+\\)"
                 )
                nil t)
          (let ((beg (match-beginning 2))
                (end (match-end 2)))
            (replace-region-contents beg end #'my-mdev-32524-make-id)))
        (save-buffer)))))

(defun my-mdev-32524-insert-enum-def ()
  "Insert enum def at point according to `my-mdev-32524-ids'."
  (with-current-buffer (find-file-noselect "./storage/spider/spd_include.h")
    (goto-char (point-min))
    (re-search-forward "#define.* SPIDER_CONN_META_BUF_LEN")
    (beginning-of-line 2)
    (let ((pre
           "
/*
  IDs for spider mem alloc functions, including
  - spider_alloc_calc_mem_init()
  - spider_string::init_calc_mem()
  - spider_malloc()
  - spider_bulk_alloc_mem()
  - spider_bulk_malloc()
  In the format of
  SPD_MID_<CALLSITE_FUNC_NAME_SANS_SPIDER_PREFIX>_<NO>
*/
enum spider_malloc_id {
  ")
          (middle (string-join
                   (sort
                    (mapcar
                     (lambda (pair)
                       (format "%s_%d" (car pair) (cdr pair)))
                     my-mdev-32524-ids)
                    #'string<)
                   ",
  "))
          (post "
};
"))
      (insert (concat pre middle post))
      (save-buffer))))

(defun my-mdev-32524 ()
  "Make changes for MDEV-32524. Assuming we are at the source root."
  (interactive)
  (my-mdev-32524-replace-ids)
  (my-mdev-32524-insert-enum-def))

(defun my-mdev-28861-ps ()
  "Wrap the current line between --disable_ps_protocol and --enable_ps_protocol"
  (interactive)
  (save-excursion
    (beginning-of-line)
    (push-mark
     (save-excursion
       (beginning-of-line 2)
       (point))
     nil t)
    (tempel-insert 'ps)))

(provide 'my-mariadb)
;;; my-mariadb.el ends here