⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bat5.mx

📁 一个内存数据库的源代码这是服务器端还有客户端
💻 MX
📖 第 1 页 / 共 5 页
字号:
@' 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 bat5@v 2.0@a Peter Boncz, M.L. Kersten@+ Binary Association TablesThis module contains the commands and patterns to manage BinaryAssociation Tables (BATs). The relational operations you can executeon BATs have the form of a neat algebra, described in algebra.mxBut a database system needs more that just this algebra, since often itis crucial to do table-updates (this would not be permitted in a strictalgebra).All commands needed for BAT updates, property management, basic I/O, persistence, and storage options can be found in this module.All parameters to the modules are passed by reference.In particular, this means that string values are passed to the modulelayer as (str *)and we have to de-reference them before entering the gdk library.(Actual a design error in gdk to differentiate passing int/str)This calls for knowledge on the underlying BAT types`s@-@{@= derefStr{ if( @1->@2type >= TYPE_str  && ATOMstorage(@1->@2type) >= TYPE_str) { if(@3== 0 || *(str*)@3==0) @3 = (str)str_nil;   else @3 = *(str *)@3; }}@-The code speaks for itself@mal#command bat(ht:int, tt:int) :bat[:any_1,:any_2] #address BKCnewBat#comment "Creates a new empty transient BAT with head- and tail-types #        as indicated.";##command bat(ht:int, tt:int, size:int) :bat[:any_1,:any_2] #address BKCnewBATint#comment "Creates a new empty BAT, allocating 'size' elements.";##command bat(ht:int, tt:int, size:lng) :bat[:any_1,:any_2] #address BKCnewBATlng#comment "Creates a new empty BAT, allocating 'size' elements.";module bat;command reverse(b:bat[:any_1,:any_2]) :bat[:any_2,:any_1] address BKCreversecomment "Returns the reverse view of a BAT (head is tail and tail is head).        BEWARE  no copying is involved; input and output refer to the         same object!";command mirror(b:bat[:any_1,:any_2]) :bat[:any_1,:any_1] address BKCmirrorcomment "Returns the head-mirror image of a BAT (two head columns).";#command convert(b:bat[:any_1,:any_2] ) :bat[:any_1,:any_2] #address BKCconvert#comment "Convert the contents of a BAT from little-endian to big-endian #        and vice versa. THIS command SHOULD NOT BE USED FROM MAL!#	DANGEROUS! DEBUGGING PURPOSES ONLY!";command order(b:bat[:any_1,:any_2]) :bat[:any_1,:any_2] address BKCordercomment "Sorts the BAT itself on the head, in place. ";command orderReverse(b:bat[:any_1,:any_2]) :bat[:any_1,:any_2] address BKCorder_revcomment "Reverse sorts the BAT itself on the head, in place. ";command revert(b:bat[:any_1,:any_2]) :bat[:any_1,:any_2] address BKCrevertcomment "Puts all BUNs in a BAT in reverse order.  (Belongs to the         BAT sequence module)";@+ BAT propertiesProperties of BATs not necessarily require loading the BAT completely.The BAT identifier can be used to access the descriptors.These properties can be inspected with the @emph{info(BAT[:any_1,:any_2]) :bat[str,str] } command:@verbatim> car_age.info.print;#---------------------------------## BAT:               tmp_31       ## (str)              (str)        ##---------------------------------#[ "batId",           "car_age"    ] # logical bat name[ "batCacheid",      "26"         ] # BBP index[ "batParentid",     "0"          ] # set if a BAT is a view[ "head",            "void"       ] # physical head-type[ "tail",            "int"        ] # physical tail-type[ "batPersistence",  "persistent" ] # transient/session/persistent[ "batRestricted",   "updatable"  ] # read-only/append-only/updatable[ "batDirty",        "clean"      ] # clean/dirty[ "batRefcnt",       "1"          ] # physical refcount[ "batLRefcnt",      "1"          ] # logical refcount (total)[ "batPlevel",       "1"          ] # logical refcount (persistent part)[ "batSet",          "0"          ] # [head,tail] combinations are unique[ "batCopiedtodisk", "1"          ] # has been saved or not@end verbatimPer column, a number of properties are kept. We only show the head-properties; the tail propertieshave the first character of their named replaced by 't' (@emph{sorted}, @emph{tdense}, etc.):@verbatim[ "hsorted",         "1"          ] # column is known to be sorted[ "hdense",          "1"          ] # column is known to be densely ascended[ "hseqbase",        "0@0"        ] # if densely ascending first value@end verbatimAs described in the @[<a href="www/gdk.html#mod_1_3_0">GDK Technical Reference</a>@,BATs store their data in one to five heaps, of which at least one called @emph{batBuns}is present always. For each column type that is @emph{variable-sized} - like @emph{str} - anotherheap might be present (@emph{hheap} and @emph{theap}). @verbatim[ "batBuns.free",    "400004"     ] # occupied size in bytes[ "batBuns.size",    "400012"     ] # allocated size in bytes[ "batBuns.maxsize", "400012"     ] # reserver virtual memory in bytes[ "batBuns.storage", "malloced"   ] # malloced/mmap/priv@end verbatimProperties steer the algorithms that Monet uses to execute algebra command. For instance,the @emph{find(bat[:any_1,:any_2] b, :any_1) :any_2} that looks up a tail value by head,uses binary search if and only if the head column is sorted (in other cases, hash-lookupand scan are execution options).Sometimes new (extension) code contains bugs in the property management, leaving falseproperties on produced BATs. You can imagine that later on, this leads to additionalbugs, as e,g, the binary search algorithms will yield erroneous results on a non-sortedsequence.@malcommand info ( b:bat[:any_1,:any_2]) :bat[:str,:str] address BKCinfocomment "Produce a BAT containing info about a BAT in [attribute,value] format.         It contains all properties of the BAT record. See the BAT documentation         in GDK for more information.";command getCapacity(b:bat[:any_1,:any_2]):lng address BKCgetCapacitycomment "Returns the current allocation size (in max number of elements) of a BAT.";command getHeadType(b:bat[:any_1,:any_2] ) :str address BKCgetHeadTypecomment "Returns the type of the head column of a BAT, as an integer type number.";command getTailType( b:bat[:any_1,:any_2] ) :str address BKCgetTailType comment "Returns the type of the tail column of a BAT, as an integer type number.";command getRole ( bid:bat[:any_1,:any_2] ) :str address BKCgetRolecomment "Returns the rolename of the head column of a BAT.";command setKey( b:bat[:any_1,:any_2], mode:bit) :bat[:any_1,:any_2] address BKCsetkeycomment "Sets the 'key' property of the head column to 'mode'. In 'key' mode,         the kernel will silently block insertions that cause a duplicate         entries in the head column. KNOWN BUG:when 'key' is set to TRUE, this 	function does not automatically eliminate duplicates.         Use b := b.kunique;";command isaKey( b:bat[:any_1,:any_2]) :bit address BKCgetKeycomment "return whether the head column of a BAT is unique (key).";command setSet( b:bat[:any_1,:any_2], mode:bit) :bat[:any_1,:any_2] address BKCsetSetcomment "Sets the 'set' property on this BAT to 'mode'. In 'set' mode,         the kernel will silently block insertions that cause a duplicate         BUN [head,tail] entries in the BAT.  KNOWN BUG:when 'set' is set         to TRUE, this function does not automatically eliminate duplicates.         Use b := b.sunique; Returns the BAT itself.";command isaSet( b:bat[:any_1,:any_1]) :bit address BKCisaSetcomment "return whether the BAT mode is set to unique.";command setAccess( b:bat[:any_1,:any_2], mode:str) :bat[:any_1,:any_2]address BKCsetAccesscomment "Try to change the update access priviliges 	to this BAT. Mode:	 r[ead-only]      - allow only read access.	 a[append-only]   - allow reads and update.	 w[riteable]      - allow all operations.	BATs are updatable by default. On making a BAT read-only,         all subsequent updates fail with an error message.Returns         the BAT itself.";#command access( b:bat[:any_1,:any_2], mode:int) :bat[:any_1,:any_2]#address BKCaccess;command setAppendMode( b:bat[:any_1,:any_2]) :bat[:any_1,:any_2]address BKCsetAppendModecomment "Change access privilige of BAT to append only";command setReadMode( b:bat[:any_1,:any_2]) :bat[:any_1,:any_2]address BKCsetReadModecomment "Change access privilige of BAT to read only";command setWriteMode( b:bat[:any_1,:any_2]) :bat[:any_1,:any_2]address BKCsetWriteModecomment "Change access privilige of BAT to read and write";command getAccess( b:bat[:any_1,:any_2]):str address BKCgetAccesscomment "return the access mode attached to this BAT as a character.";command hasAppendMode( b:bat[:any_1,:any_2]):bit address BKChasAppendModecomment "return true if to this BAT is append only.";command hasWriteMode( b:bat[:any_1,:any_2]):bit address BKChasWriteModecomment "return true if to this BAT is read and write.";command hasReadMode( b:bat[:any_1,:any_2]):bit address BKChasReadModecomment "return true if to this BAT is read only.";command getSequenceBase( b:bat[:oid,:any_1]):oid address BKCgetSequenceBasecomment "Get the sequence base for the void column of a BAT.";#command setSequenceBase( b:bat[:oid,:any_1], base:oid):void#address BKCsetSequenceBase#comment "Set the sequence base for the void column of a BAT.";command setSorted(b:bat[:any_1,:any_2]) :bit address BKCsetSortedcomment "Assure BAT is ordered on the head.";command isSorted(b:bat[:any_1,:any_2]) :bit address BKCisSortedcomment "Returns whether a BAT is ordered on head or not.";command isSortedReverse(b:bat[:any_1,:any_2]) :bit address BKCisSortedReversecomment "Returns whether a BAT is ordered on head or not.";command getBatSize(b:bat[:any_1,:any_2]) :lng address BKCbatsizecomment "A version of BATsize that does not require loading the BAT.";@- BAT updatesUpdate commands come in many disguises.  Note that we don;t returnthe BAT id, but merely a success/failure code.@malcommand insert(b:bat[:any_1,:any_2], src:bat[:any_1,:any_2]):voidaddress BKCinsert_batcomment "Insert all BUNs of the second BAT into the first.";command insert(b:bat[:any_1,:any_2], src:bat[:any_1,:any_2], force:bit):voidaddress BKCinsert_bat_forcecomment "Insert all BUNs of the second BAT into the first.";command insert(b:bat[:any_1,:any_2], h:any_1, t:any_2) :voidaddress BKCinsert_buncomment "Insert one BUN[h,t] in a BAT.";command insert(b:bat[:any_1,:any_2], h:any_1, t:any_2, force:bit) :voidaddress BKCinsert_bun_forcecomment "Insert one BUN[h,t] in a BAT.";@+@malcommand replace(b:bat[:any_1, :any_2], src:bat[:any_1,:any_2]) :voidaddress BKCreplace_batcomment "Perform replace for all BUNs of the second BAT into the first.";command replace(b:bat[:any_1, :any_2], src:bat[:any_1,:any_2], force:bit) :voidaddress BKCreplace_bat_forcecomment "Perform replace for all BUNs of the second BAT into the first.";command replace(b:bat[:any_1, :any_2], h:any_1, t:any_2) :voidaddress BKCreplace_buncomment "Replace the tail value of one BUN that has some head value.";command replace(b:bat[:any_1, :any_2], h:any_1, t:any_2, force:bit) :voidaddress BKCreplace_bun_forcecomment "Replace the tail value of one BUN that has some head value.";@-The SQL append/inplace commands@malcommand append( i:bat[:any_1,:any_2], u:bat[:any_1,:any_2] ) :voidaddress BKCappend_wrapcomment "append the content of u to i";command append( i:bat[:any_1,:any_2], u:bat[:any_1,:any_2], force:bit ) :voidaddress BKCappend_force_wrapcomment "append the content of u to i";command append(i:bat[:oid,:any_1], u:any_1):voidaddress BKCappend_val_wrapcomment "append the value u to i";command append(i:bat[:void,:any_1], u:any_1):voidaddress BKCappend_val_wrapcomment "append the value u to i";command append(i:bat[:any_1,:void], u:any_1):voidaddress BKCappend_reverse_val_wrapcomment "append the value u to i";command append(i:bat[:any_1,:any_2], u:any_2, force:bit):voidaddress BKCappend_val_force_wrapcomment "append the value u to i";command inplace( o:bat[:any_1,:any_2], id:any_1, t:any_2) :voidaddress BKCbun_inplacecomment "inplace replace values on the given locations";command inplace( o:bat[:any_1,:any_2], d:bat[:any_1,:any_2]) :voidaddress BKCbat_inplacecomment "inplace replace values on the given locations";command inplace( o:bat[:any_1,:any_2], id:any_1, t:any_2, force:bit) :voidaddress BKCbun_inplace_forcecomment "inplace replace values on the given locations";command inplace( o:bat[:any_1,:any_2], d:bat[:any_1,:any_2], force:bit) :voidaddress BKCbat_inplace_forcecomment "inplace replace values on the given locations";@malcommand delete(b:bat[:any_1, :any_2], h:any_1, t:any_2) :voidaddress BKCdelete_buncomment "Delete one specific BUN.";command delete(b:bat[:any_1, :any_2], h:any_1) :voidaddress BKCdeletecomment "Delete all BUNs with a certain tail value.";command delete(b:bat[:any_1, :any_2]) :voidaddress BKCdelete_allcomment "Delete all BUNs in a BAT.";@malcommand delete(b:bat[:any_1, :any_2], src:bat[:any_1,:any_2]) :voidaddress BKCdelete_bat_buncomment "Delete from the first BAT all BUNs with a corresponding BUN         in the second.";command getAlpha(b:bat[:any_1,:any_2]) :bat[:any_1,:any_2] address BKCgetAlphacomment "Obtain the list of BUNs added";command getDelta(b:bat[:any_1,:any_2]) :bat[:any_1,:any_2] address BKCgetDeltacomment "Obtain the list of BUNs deleted";@- BAT I/O, PersistencyThe BAT Buffer Pool (BBP) manages all known BATs. It administerstheir logical and physical names and a reference count. BATs caneither be @emph{persistent} or @emph{transient}. The BBP also managesswapping on a BAT level:a BAT is either loaded entirely or not.MAL variables of type @emph{bat} can either be loaded or not. Whenthe Monet server is started, all BATs are swapped out. If an unloadedbat-variable is used as an operand in a command, it is automaticallyloaded.  The BBP applies a simple but effective LRU based swappingalgorithm. BATs have a @emph{heat}, which drops over time, and isincreased when a BAT is used by some command.  If the size of theallocated arena gets to be large, BATs may be swapped out.Note, we should move old-fashioned mil operator definitionsinto a separate module.@malcommand setName ( b:bat[:any_1,:any_2] , s:str) :voidaddress BKCsetNamecomment "Give a logical name to a BAT. ";command getName ( b:bat[:any_1,:any_2]) :str address BKCgetBBPnamecomment "Gives back the logical name of a BAT.";command setRole( b:bat[:any_1,:any_2], h:str, t:str) :voidaddress BKCsetRolecomment "Give a logical name to the columns of a BAT.";command setColumn( b:bat[:any_1,:any_2], t:str) :voidaddress BKCsetColumncomment "Give a logical name to the tail column of a BAT.";command setColumn( b:bat[:any_1,:any_2], h:str, t:str) :voidaddress BKCsetColumnscomment "Give both columns of a BAT a new name.";command isTransient( b:bat[:any_1,:any_2]) :bit address BKCisTransient;command setTransient( b:bat[:any_1,:any_2]) :voidaddress BKCsetTransientcomment "Make the BAT transient.  Returns 	boolean which indicates if theBAT administration has indeed changed.";command isPersistent( b:bat[:any_1,:any_2]) :bit address BKCisPersistent;command setPersistent( b:bat[:any_1,:any_2],f:bit) :voidaddress BKCpersistscomment "Backward compatibility";command setPersistent( b:bat[:any_1,:any_2]) :voidaddress BKCsetPersistentcomment "Make the BAT persistent.  Returns boolean which indicates         if the BAT administration has indeed changed.";command save(nme:bat[:any_1,:any_2]) :voidaddress BKCsave2;command save(nme:str) :bit address BKCsavecomment "Save a BAT to storage, if it was loaded and dirty.          Returns whether IO was necessary.  Please realize that 	calling this function violates the atomic commit protocol!!";command load(name:str) :bat[:any_1,:any_2] address BKCloadcomment "Load a particular BAT from disk";command unload(name:str) :bit address BKCunloadcomment "Swapout a BAT to disk. Transient BATs can also be swapped out.             Returns whether the unload indeed happened. ";command isCached(b:bat[:any_1,:any_2]):bit address BKCisCachedcomment "Bat is stored in main memory.";command getHeat(b:bat[:any_1,:any_2]) :lng address BKCheatcomment "Return the current BBP heat (LRU stamp)";command setCold(b:bat[:any_1,:any_1]) :void address BKCcoldBATcomment "Makes a BAT very cold for the BBP. The chance of being choses         for swapout is big, afterwards.";command setHot(b:bat[:any_1,:any_2]) :void address BKChotBATcomment "Makes a BAT very hot for the BBP. The chance of being chosen for         swapout is small, afterwards.";@- Heap Specific CommandsBATs are stored in memory in a number of Heap objects. A heap is nothingmore than a contiguous range of memory. Bats are saved to disk by just writingaway their image. This approach without pointer swizzling makes it possibleto either load an image into an alloced range of memory (STORE_MEM), ormemory-map (STORE_MMAP) an image into virtual memory.The heap images of a BAT are stored in the @emph{$MONETHOME/dbfarm/$DB/bat/}directory.  For each bat X, the following heaps are stored:@table @code@item[X.buns]     an array with all the fixed-size parts of all BUNs.@item[X.hheap]     if the head column contains a variable sized atoms (e.g. str),then the fixed-size part of a BUN contains an integer byte-offset intothe heap. String values themself are stored in this the X.hheap.@item[X.theap]     similar to X.hheap, but for the tail column.@item[X.desc] the BAT descriptor. Stores most of the properties of a BAT.@end tableEach of these heaps can be compressed using the Unix @emph{compress}utility forming a X.ext.Z file. Monet will automatically decompress itupon load. Compressed heaps cannot be memory mapped.@For more technical information on BATs, we refer to the@[<a href="http://www.cwi.nl/~monet/www/scw/gdk/470_pseudo.html">GDK</a>@documentation.@malcommand setMemoryMap(b:bat[:any_1,:any_2], buns_mode:int, hheap_mode:int, 		theap_mode:int) :bit address BKCmmapcomment "For each individual heap, you can change the allocation mode         to either STORE_MEM or STORE_MMAP. Passing an int(nil) means:        no change.  Changing a dirty STORE_MEM heap into STORE_MMAP, 	will cause a BAT save (this has to happen before the heap can         be mapped into virtual memory). These modes are persistent. ";command setMemoryMap(b:bat[:any_1,:any_2], mode:int):bit address BKCmmap2

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -