Changes

Jump to navigation Jump to search
345 bytes added ,  14:32, 10 April 2019
no edit summary
This page is part of the [[Getting Started]] Section.
In BaseX, a ''database'' is a pretty light-weight concept and can be comparedto a ''collection''. It contains an arbitrary number of may contain one or more '''resources''',which are addressed by their a unique database path. There is no explicit layer for collections: Instead, collections are implicitly created and deleted, and collections result from the existence of documents in specific paths. Resources can either be'''XML documents''' or '''raw files''' (binaries).Some information on [[Binary Data|binary data]] can be found on an extra page. Multiple databases can be addressed (queries, updated) with a single XQuery expression. As a single database is restricted to 2 billion nodes (see [[Statistics]]), resources can be distributed across multiple database instances.
=Create Databases=
New databases Databases can be created via commands, via XQuery, in the GUI, or with any of our[[Developing|APIs]]. If some an initial input is specified along with the create operation, it some time can be saved, as the specified resources will be added to the database in a bulk operation:
* [[Startup#BaseX Standalone|Console]]: <code>CREATE DB db /path/to/resources</code> will add initial documents to a database
* [[Startup#BaseX GUI|GUI]]: Go to ''Database'' → ''New'', press ''Browse'' to choose an initial file or directory, and press ''OK''
Database must follow the The name of a database is restricted to a restricted set of characters (see [[Valid Names|valid names constraints]]).Various [[parsers]] can be chosen to influence control the database creationimport process, or to convert different formats to XML.
'''Note:''' A main-memory only database can will be created using if the the <code>SET {{Option|MAINMEM true</code> command before calling <code>CREATE DB</code> }} option is enabled ([[Databases#In Memory Database|see below]] for more).
=Access Resources=
!Description
|-
|<code>[[Database Module#db:open|db:open]]</code>
|{{Code|db:open("db", "path/to/docs")}}
|Returns all documents that are found in the database {{Code|db}} at the (optional) path {{Code|path/to/docs}}.
|-
|<code>[http://www.xqueryfunctions.com/xq/fn_collection.html fn:collection]</code>
|{{Code|collection("db/path/to/docs")}}
|Returns all documents at the location {{Code|path/to/docs}} in the database {{Code|db}}.<br/>If no path is specified after the database, all documents in the database will be returned.<br/>If no argument is specified, all documents of the database will be returned that has been opened in the global context.
|-
|<code>[http://www.xqueryfunctions.com/xq/fn_doc.html fn:doc]</code>
|{{Code|doc("db/path/to/doc.xml")}}
|Returns the document at the location {{Code|path/to/docs}} in the database {{Code|db}}.<br/>An error is raised if the specified yields zero or more than one document.
</pre>
If the [[Options#DEFAULTDB{{Option|DEFAULTDB]] }} option is turned on, the path argument of the {{Code|fn:doc}} or {{Code|fn:collection}} function will first be resolved against the globally opened database.
Two more functions are available for retrieving information on database nodes:
!Description
|-
|<code>[[Database Module#db:name|db:name]]</code>
|{{Code|db:name($node)}}
|Returns the name of the database in which the specified {{Code|$node}} is stored.
|-
|<code>[[Database Module#db:path|db:path]]</code>
|{{Code|db:path($node)}}
|Returns the path of the database document in which the specified {{Code|$node}} is stored.
* {{Code|doc("http://web.de")}}: retrieves the addressed URI and returns it as a main-memory document node.
* {{Code|doc("myfile.xml")}}: retrieves the given file from the file system and returns it as a main-memory document node. Note that updates to main-memory nodes are not automatically written back to disk unless the <code>[[Options#WRITEBACK{{Option|WRITEBACK]]</code> }} option is set.
* {{Code|collection("/path/to/docs")}}: returns a main-memory collection with all XML documents found at the addressed file path.
==Raw Files==
{{MarkThe <code>[[Commands#RETRIEVE|Updated with Version 8.4}}: items of binary type can be output without specifying RETRIEVE]]</code> command and the obsolete <code>raw[[Database Module#db:retrieve|db:retrieve]]</code> serialization methodfunction can be used to return files in their native byte representation. If the API you use does not support binary output (this is e.g.the case for various [[Clients|Client]] language bindings), you need to convert your binary data to its string representation before returning it to the client:
* XQuery: With <code>[[Database Module#db:retrieve|db:retrieve]]</code>, raw files are returned in their native byte representation:
<pre class="brush:xquery">
string(db:retrieve('multimedia', 'sample.avi'))
</pre>
* Commands: <code>[[Commands#RETRIEVE|RETRIEVE]]</code> can also be used to output raw files.
==HTTP Services==
* Resources can be deleted via <code>[[Commands#DELETE|DELETE]]</code>.
The [[Options#AUTOFLUSH{{Option|AUTOFLUSH]] }} option can be turned off before ''bulk operations'' (i.e. before a large number of new resources is added to the database).
The [[Options#ADDCACHEIf {{Option|ADDCACHE]] option will first cache }} is enabled, the input will be cached before adding it is added to the database. This is helpful when the input documents to be added are expected to eat up consume too much main memory.
The following commands create an empty database, add two resources, explicitly flush data structures to disk, and finally delete all inserted data:
</pre>
You may as well also use the BaseX-specific [[Database Module|XQuery Database Functions]] to create, add, replace, and delete XML documents:
<pre class="brush:xquery">
* WebDAV: Locate the database directory (or a sub-directory of it) and copy all contents to another location
=In Main-Memory DatabaseInstances=
* In the standalone context, a main-memory database can be created (using <code>CREATE DB</code>), which can then be accessed by subsequent commands.
* If a BaseX server instance is started, and if a database is created in its context (using <code>CREATE DB</code>), other BaseX client instances can access (and update) this database (using OPEN, db:open, etc.) as long as no other database is opened/created by the server.
* You can force an ordinary database to being copied to memory by using <code>db:open('some-db') update {}</code>
'''Note:''' main-memory database instances are also created by the invocation of If you address a URI with <code>fn:doc(...)</code> or <code>fn:collection(...)</code>for which no database exists, if the argument resulting internal representation is not aidentical to those of main-memory database instances (no matter which value is set for {{Option|MAINMEM}}). In other words:the same internal representation is used for main-memory databases anddocuments/collections generated via XQuery.
=Changelog=
* Updated: {{Code|fn:document-uri}} and {{Code|fn:base-uri}} now return strings that can be reused with {{Code|fn:doc}} or {{Code|fn:collection}} to reopen the original document.
 
[[Category:Beginner]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu