commit c7cc51563ad5632ab840bf182345a8e72cc22237
parent a70c58bebb03f441e1f4c2c2bdd4ac7a139797db
Author: rpa <rpa@laika>
Date: Sun, 19 Feb 2023 20:06:54 +0000
src/keyval: add cli
Diffstat:
3 files changed, 127 insertions(+), 111 deletions(-)
diff --git a/src/keyval/cli.c b/src/keyval/cli.c
@@ -0,0 +1,72 @@
+/*
+ primitive client for primitive key-value database
+*/
+
+#include <u.h>
+#include <libc.h>
+
+#include "keyval.h"
+
+int i, fd;
+char *opstr, *keystr;
+
+Keyval kv;
+
+struct Dict { char *s; int op; };
+struct Dict dict[] = {
+ {"get", KVGet},
+ {"set", KVSet},
+ {nil, 0},
+};
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s get|set key\n", argv0);
+ exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+ ARGBEGIN{
+ default:
+ usage();
+ }ARGEND;
+ fd = open("/mnt/keyval/ctl", ORDWR);
+ if (fd < 0) sysfatal("%r");
+
+ if (argc == 0) usage();
+ opstr = argv[0];
+ argc--, argv++;
+
+ for(kv.op = 0, i = 0; dict[i].s != nil; i++) {
+ if (strcmp(opstr, dict[i].s) == 0) {
+ kv.op = dict[i].op;
+ break;
+ }
+ }
+
+ if (argc == 0) usage();
+ keystr = argv[0];
+// argc--, argv++;
+
+ memcpy(kv.key, keystr, strlen(keystr));
+
+ switch(kv.op) {
+ case KVGet:
+ kv.count = MetaSize;
+ write(fd, &kv, MetaSize);
+ read(fd, &kv, BufSize);
+ write(1, kv.data, kv.count - MetaSize);
+ break;
+ case KVSet:
+ kv.count = MetaSize + read(0, kv.data, DataSize);
+ kv.count = write(fd, &kv, kv.count);
+ break;
+ default:
+ fprint(2, "bad op\n");
+ exits("bad op");
+ }
+ exits(nil);
+}
diff --git a/src/keyval/mkfile b/src/keyval/mkfile
@@ -1,5 +1,6 @@
</$objtype/mkfile
BIN=
-TARG=srv
-OFILES=srv.$O
-</sys/src/cmd/mkone
+TARG=srv cli
+OFILES=
+HFILES=keyval.h
+</sys/src/cmd/mkmany
diff --git a/src/keyval/srv.c b/src/keyval/srv.c
@@ -1,3 +1,7 @@
+/*
+ primitive key-value database
+*/
+
#include <u.h>
#include <libc.h>
#include <regexp.h>
@@ -5,38 +9,20 @@
#include <thread.h>
#include <9p.h>
-enum {
- BufSize = 8192,
- FauxSize = BufSize,
- MetaSize = 256,
- KeySize = MetaSize - 6,
-};
+#include "keyval.h"
-typedef struct Key Key;
-typedef struct Val Val;
typedef struct Keyval Faux;
-typedef struct Keyval Keyval;
-
-struct Val {
- long n;
- char *p;
-};
-
+typedef struct Key Key;
struct Key {
- char key[KeySize];
- Val val;
+ Keyval;
Key *next;
};
-struct Keyval {
- u16int count, op, reserved;
- char key[KeySize];
- char data[BufSize - MetaSize];
+enum {
+ FauxSize = BufSize,
};
void usage(void);
-void keyval(void);
-int eval(char *buf, int n);
void ctl_open(Req *r);
void ctl_read(Req *r);
@@ -48,8 +34,8 @@ void freefaux(void *v);
int fd, pd[2];
Key *keyp;
-char *serv = "wdb";
-char *mnt = "/mnt/wdb";
+char *serv = "keyval";
+char *mnt = "/mnt/keyval";
Reprog *reg;
Srv fs = {
@@ -91,58 +77,31 @@ usage(void)
exits("usage");
}
-void
-keyval(void)
-{
- int n;
- char buf[BufSize];
- for (;;) {
- memset(buf, 0, BufSize);
- n = read(pd[1], buf, BufSize);
- n = eval(buf, n);
- if (write(pd[1], buf, n) < n) break;
- }
- fprint(2, "%r\n");
-}
-
Key *
-findkey(Key *root, char *key)
+findkey(Key *kp, char *key)
{
- Key *kp;
- for (kp = root; kp != nil; kp = kp->next) {
+ for (; kp != nil; kp = kp->next) {
if (strncmp(key, kp->key, KeySize) == 0) return kp;
}
return nil;
}
-int
-setkey(Key **root, char *key, char *p, int n)
+Key *
+setkey(Key **root, Keyval *kv)
{
- Key *k = findkey(*root, key);
+ Key *k = findkey(*root, kv->key);
if (k == nil) {
k = mallocz(sizeof(Key), 1);
- strncpy(k->key, key, KeySize);
+ strncpy(k->key, kv->key, KeySize);
k->next = *root;
*root = k;
- } else if (k->val.p != nil) {
- free(k->val.p);
}
- if (k == nil) return -1;
- k->val.n = n;
- k->val.p = mallocz(n, 1);
- if (k->val.p == nil) return -1;
- memcpy(k->val.p, p, n);
- return 0;
-}
-
-Val *
-getkey(Key *root, char *key)
-{
- Key *k = findkey(root, key);
if (k == nil) return nil;
- return &(k->val);
+ memcpy(k, kv, kv->count);
+ return k;
}
+/*
int
keyreg(char *buf, Key *kp, Reprog *reg)
{
@@ -174,67 +133,54 @@ valreg(char *buf, Key *kp, Reprog *reg)
bp[-1] = '\0';
return n;
}
-
-int
-eval(char *buf, int n)
-{
- char *args[3], *act, *key, *data;
- for (; n > 0; buf++, n--) if (strchr(" \t\n", buf[0]) == nil) break;
- if (n <= 0) return 0;
- if (buf[0] == '\n') return 0;
- if (getfields(buf, args, 3, 1, " \t\n") < 2) return sprint(buf, "error malformed message");
- act = args[0], key = args[1], data = args[2];
- if (strlen(act) >= 8) return sprint(buf, "error bad command");
- if (strlen(key) >= KeySize) return sprint(buf, "error bad key");
- if (strncmp(act, "store", 8) == 0) {
- if (setkey(&keyp, key, data, data - buf + n) != 0) {
- return sprint(buf, "error store failed");
- } else {
- return sprint(buf, "ok %s", key);
- }
- }
- if (strncmp(act, "fetch", 8) == 0) {
- Val *v = getkey(keyp, key);
- if (v == nil) {
- return sprint(buf, "nil %s", key);
- } else {
- return sprint(buf, "ok %s %s", key, v->p);
- }
- }
- if (strncmp(act, "valreg", 8) == 0) {
- reg = regcompnl(args[1]);
- if (reg == nil) return sprint(buf, "error bad regex");
- if (valreg(buf, keyp, reg) == 0) return sprint(buf, "nil");
- return strlen(buf);
- }
- if (strncmp(act, "keyreg", 8) == 0) {
- reg = regcompnl(args[1]);
- if (reg == nil) return sprint(buf, "error bad regex");
- if (keyreg(buf, keyp, reg) == 0) return sprint(buf, "nil");
- return strlen(buf);
- }
- return sprint(buf, "error unknown command");
-}
+*/
void ctl_read(Req *r)
{
if (r->fid->aux == nil) {
- sysfatal("aux is nil");
+ sysfatal("read: aux is nil");
}
int count;
Faux *faux = r->fid->aux;
count = (r->ifcall.count > faux->count) ? faux->count : r->ifcall.count;
memcpy(r->ofcall.data, faux, count);
- r->ofcall.count = count;
+ r->ofcall.count = r->ifcall.count;
respond(r, nil);
}
void ctl_write(Req *r)
{
if (r->fid->aux == nil) {
- sysfatal("aux is nil");
+ sysfatal("write: aux is nil");
}
+ Faux *faux = r->fid->aux;
+ memset(faux, 0, FauxSize);
+ if (r->ifcall.count < 6) goto badwrite;
+ Keyval *kv = (Keyval *)(r->ifcall.data);
+ kv->count = r->ifcall.count;
+ r->ofcall.count = r->ifcall.count;
+ switch(kv->op){
+ case KVNop:
+ break;
+ case KVSet:
+ if (r->ifcall.count < MetaSize) goto badwrite;
+ setkey(&keyp, kv);
+ break;
+ case KVGet:
+ kv = findkey(keyp, kv->key);
+ if (kv != nil) {
+ memcpy(faux, kv, kv->count);
+ }
+ break;
+ default:
+ goto badwrite;
+ }
+
respond(r, nil);
+ return;
+
+ badwrite:
+ respond(r, "bad write");
}
void ctl_open(Req *r)
@@ -245,9 +191,6 @@ void ctl_open(Req *r)
void ctl_destroyfid(Fid *fid)
{
- if (fid->aux == nil) {
- sysfatal("aux is nil");
- }
freefaux(fid->aux);
}