📄 const.mx
字号:
@' The contents of this file are subject to the MonetDB Public License@' Version 1.1 (the "License"); you may not use this file except in@' compliance with the License. You may obtain a copy of the License at@' http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html@'@' Software distributed under the License is distributed on an "AS IS"@' basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the@' License for the specific language governing rights and limitations@' under the License.@'@' The Original Code is the MonetDB Database System.@'@' The Initial Developer of the Original Code is CWI.@' Portions created by CWI are Copyright (C) 1997-2007 CWI.@' All Rights Reserved.@f const@a Martin Kersten@v 0.1@+ ConstantsThe const module provides a box abstraction store for global constants.Between sessions the value of the constants is saved on disk in the form of a simple MAL program, which is scanned and made available by opening the box. A future implementation should provide transaction support over the box, which would permit multiple clients to exchange (scalar) information easily.The default constant box is initialized with session variables,such as 'user','dbname', 'dbfarm', and 'dbdir'.These actions are encapsulated in the prelude routine called.A box should be opened before being used. It is typically usedto set-up the list of current users and to perform authorization.The constant box is protected with a simple authorization scheme,prohibiting all updates unless issued by the system administrator.@malmodule const;pattern open():voidaddress CSTopencomment "Locate and open the constant box";pattern close():voidaddress CSTclosecomment "Close the constant box ";pattern destroy():voidaddress CSTdestroycomment "Destroy the box";pattern take(name:str):any_1 address CSTtakecomment "Take a variable out of the box";pattern deposit(name:str,val:any_1) :void address CSTdepositcomment "Enter a new variable into the box";pattern releaseAll():void address CSTreleaseAllcomment "Release all variables in the box";pattern release(name:str) :void address CSTreleasecomment "Release a new constant value";pattern release(name:any_1):void address CSTreleasecomment "Release a new constant value";pattern toString(name:any_1):str address CSTtoStringcomment "Get the string representation of an element in the box";pattern discard(name:any_1) :void address CSTdiscardcomment "Release the const from the box";pattern newIterator()(:lng,:str)address CSTnewIteratorcomment "Locate next element in the box";pattern hasMoreElements()(:lng,:str)address CSThasMoreElementscomment "Locate next element in the box";@{pattern prelude():void address CSTpreludecomment "Initialize the const box";command epiloque():void address CSTepiloguecomment "Cleanup the const box";const.prelude();@-@+ Implementation@h#ifndef _CONST_H#define _CONST_H#include "clients.h"#include "mal.h"#include "mal_client.h"#include "mal_interpreter.h"#include "mal_authorize.h"#ifdef WIN32#ifndef LIBCONST#define const_export extern __declspec(dllimport)#else#define const_export extern __declspec(dllexport)#endif#else#define const_export extern#endifconst_export str CSTprelude(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTopen(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTclose(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTdestroy(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTdeposit(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTtake(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTrelease(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTreleaseAll(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTdiscard(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTtoString(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTnewIterator(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSThasMoreElements(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);const_export str CSTepilogue(int *ret);#endif /* _CONST_H */@- Module initializatonThe content of this box my only be changed by the Administrator.@= authorize { str tmp = NULL; rethrow("const.@1", tmp, AUTHrequireAdmin()); }@-@= insertBox msg = @2; insertToBox(box,@1,msg); GDKfree(msg);@c#include "mal_config.h"#include "const.h"strCSTprelude(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ Box box; (void) mb; (void) stk; (void) pci; /* fool compiler */ @:authorize(prelude)@ box = openBox("const"); if (box == 0) throw(MAL, "const.prelude", "failed to open box"); /* if the box was already filled we can skip initialization */ if (box->sym->vtop == 0) { char u[24 + 1]; snprintf(u, 24, OIDFMT, MCgetClient()->user); u[24] = '\0'; insertToBox(box, "user", u); insertToBox(box, "dbname", GDKgetenv("gdk_dbname")); insertToBox(box, "dbfarm", GDKgetenv("gdk_dbfarm")); insertToBox(box, "version", GDKgetenv("gdk_version")); insertToBox(box, "config", GDKgetenv("gdk_config")); /* old bat.mx and algebra.mx constants */ } return MAL_SUCCEED;}strCSTepilogue(int *ret){ (void)ret; closeBox("const", 0); return MAL_SUCCEED;}@- Operator implementation@cstrCSTopen(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ (void) mb; (void) stk; (void) pci; /* fool compiler */ @:authorize(open)@ if (openBox("const") != 0) return MAL_SUCCEED; throw(MAL, "const.open", "failed to open box");}strCSTclose(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ (void) mb; (void) stk; (void) pci; /* fool compiler */ @:authorize(close)@ if (closeBox("const", TRUE) == 0) return MAL_SUCCEED; throw(MAL, "const.close", "failed to close box");}strCSTdestroy(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ Box box; (void) mb; (void) stk; (void) pci; /* fool compiler */ @:OpenBox(destroy)@ destroyBox("const"); return MAL_SUCCEED;}@-Access to a box calls for resolving the first parameterto a named box.@= OpenBox @:authorize(@1)@ box= findBox("const"); if( box ==0) throw(MAL, "const.@1","box is not open");@-@cstrCSTdeposit(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ str name; ValPtr v; Box box; @:OpenBox(deposit)@ name = (str) getArgValue(stk, pci, 1); v = &stk->stk[getArg(pci, 2)]; if (depositBox(box, name, v)) throw(MAL, "const.deposit", "failed to deposit into box"); (void) mb; return MAL_SUCCEED;}strCSTtake(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ str name; Box box; ValPtr v; @:OpenBox(take)@ name = (str) getArgValue(stk, pci, 1); v = &stk->stk[getArg(pci, 0)]; if (takeBox(box, name, v, (int) getArgType(mb, pci, 0))) throw(MAL, "const.take", "failed to take object from box"); (void) mb; return MAL_SUCCEED;}strCSTrelease(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ str name; Box box; (void) mb; /* fool compiler */ @:OpenBox(release)@ name = (str) getArgValue(stk, pci, 1); if (releaseBox(box, name)) throw(MAL, "const.release", "failed to release object from box"); return MAL_SUCCEED;}strCSTreleaseAll(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ Box box; (void) mb; (void) stk; (void) pci; /* fool compiler */ @:OpenBox(release)@ releaseAllBox(box); return MAL_SUCCEED;}strCSTdiscard(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ str name; Box box; (void) mb; /* fool compiler */ @:OpenBox(discard)@ name = (str) getArgValue(stk, pci, 1); if (discardBox(box, name) == 0) throw(MAL, "const.discard", "failed to discard object from box"); return MAL_SUCCEED;}strCSTtoString(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ Box box; int i, len = 0; ValPtr v; str nme, s = 0; (void) mb; /* fool compiler */ @:OpenBox(toString)@ nme = (str) getArgValue(stk, pci, 1); i = findVariable(box->sym, nme); if (i < 0) throw(MAL, "const.toString", "failed to take object from box"); v = &box->val->stk[i]; if (v->vtype == TYPE_str) s = v->val.sval; else (*BATatoms[v->vtype].atomToStr) (&s, &len, v); if (s == NULL) throw(MAL, "const.toString", "illegal value"); VALset(&stk->stk[getArg(pci, 0)], TYPE_str, s); return MAL_SUCCEED;}strCSTnewIterator(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ Box box; lng *cursor; ValPtr v; (void) mb; /* fool compiler */ @:OpenBox(iterator)@ cursor = (lng *) getArgValue(stk, pci, 0); v = &stk->stk[getArg(pci, 1)]; nextBoxElement(box, cursor, v); return MAL_SUCCEED;}strCSThasMoreElements(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ Box box; lng *cursor; ValPtr v; (void) mb; /* fool compiler */ @:OpenBox(iterator)@ cursor = (lng *) getArgValue(stk, pci, 0); v = &stk->stk[getArg(pci, 1)]; nextBoxElement(box, cursor, v); return MAL_SUCCEED;}@}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -