Database methods

Note

The server version of CodernityDB is nothing more than HTTP mapper for CoderntiyDB methods. So all tutorials and howtos that are valid for CodernityDB itself, are obviously valid for HTTP version.

All methods can be requested using POST or GET method. The final parameters are joined from query-string and POST request body.

You just have to remember that, when doing GET request, all arguments after 2nd must be encoded using function that you want to use.

List of methods

All methods are exposed as full paths. So to execute open method sent request to /open.

Database operations

open(path=null)

Will open already existing database

Parameters:path (string) – path with database to open
Returns:result of operation
Return type:boolean
opened()

Checks if server has now opened database.

Returns:result of operation
Return type:boolean
initialize(path=null, makedir=true)

Initialize new database, create directory structure etc.

Parameters:
  • path (string) – path where to initialize
  • makedir (boolean) – create _indexes subdir or not. Do not change if you’re not sure that you need to.
Returns:

result of operation

Return type:

boolean

create(path=null, with_id_index=true, index_kwargs={})

Create new database in given path.

Parameters:
  • path (string) – path where to create database
  • with_id_index (boolean) – create default id index or

not. Do not change if you’re not sure that you need to.

Returns:database path
Return type:string
close()

Closes database. If it’s already opened

Returns:result of operation
Return type:boolean
fsync()

Flushes indexes. Use when you’re sure that you need to.

Returns:always null
fsync()

It forces the kernel buffer to be written to disk. Use when you’re sure that you need to.

Returns:always null
compact()

Compacts database.

Returns:result when finishes
Return type:boolean
reindex()

Reindex database.

Returns:result when finishes
Return type:boolean
destroy()

Destroyes database.

Returns:result of operation
Return type:boolean
exists(path=null)

Checks if database exists.

Parameters:path (string) – path to check for database (optional)
Returns:result
Return type:boolean

Index operations

add_index(new_index, create=true, ind_kwargs=null)

Will allow you to add new index to database

Parameters:
  • new_index (string) – New index to add, can index valid string or path to file with index code
  • create (bool) – Create the index after add or not (when database is not yet created you should not set it to true)
  • ind_kwargs (dict) – arguments to pass to index when created
Returns:

new index name

Return type:

string

edit_index(index, reindex=false, ind_kwargs=null)

Will allow you to modify index that’s already in database without destroying it.

Parameters:
  • new_index (string) – A string with index to change (the valid index code)
  • reindex (boolean) – after edit index, should be it reindexed ? When changing ``make_key``, ``make_key_value`` you for sure need this
  • ind_kwargs (dict) – arguments to pass to index when created
Returns:

index name

Return type:

string

compact_index(index)

Will compact your index data (refer to CodernityDB for more details about that process)

Parameters:index (string) – index name to perform compact operation
Returns:result of operation
Return type:boolean
reindex_index(index)

Will reindex your index data (refer to CodernityDB for more details about that process)

Parameters:index (string) – index name to perform reindex operation
Returns:result of operation
Return type:boolean
destroy_index(index)

Will destroy given index

Parameters:index (string) – index name to perform that operation
Returns:result of operation
Return type:boolean

Data operations

get(index_name, key, with_doc=false, with_storage=true)

Gets database data by given key from given index

Parameters:
  • index_name (string) – index to ask for data
  • key – a key you want to look for
  • with_doc (boolean) – if data from id index should be included in results
  • with_storage (boolean) – if you want to have also data from index storage
Returns:

data or raises exceptions

update(data)

Updates data in database. You have to pass full data (you can’t update just some fields / attributes). Object must have _id and rev fields. If rev is not matching with current one in database, the conflict will occurs, and exception will be returned.

Parameters:data (key-value) – a key-value (ie. python dict) data to update
Returns:key-value with _id and rev fields
insert(data)

Adds data to database. You may set _id field, but if you ommit it it will be generated automaticaly. You can’t add data with rev field.

Parameters:data (key-value) – a key-value (ie. python dict) data to add to db
Returns:key-value with _id and rev fields
Return type:key-value
delete(data)

Delete data from database. Object must have _id and rev fields. If rev is not matching with current one in database, the conflict will occurs, and exception will be returned. You don’t have to pass more than those 2 fields into data.

Parameters:data (key-value) – a key-value (ie. python dict) data to add to db
Returns:result of operation
Return type:boolean
all(index_name, limit=-1, offset=0, with_doc=false, with_storage=false)

Allows to get all records for given index.

Warning

Running that method with default parameters (limit=-1, offset=0) will require to dump big json/msgpack data! On ebeded mode it works using iterator, but there are no iterators over HTTP!

Parameters:
  • index_name (string) – a index to get data from
  • limit (int) – limit of results, -1 means no limit
  • offset (int) – how many results from beginning should be ignored
  • with_doc (boolean) – if data from id index should be included in results
  • with_storage (boolean) – if you want to have also data from index storage
Returns:

a data

Return type:

iterable

get_many(index_name, key=null, limit=1, offset=0, with_doc=false, with_storage=true, start=null, end=null, inclusive_start=null, inclusive_end=null)

Allows to get all records for given key from given index.

Parameters:
  • index_name (string) – a index to get data from
  • limit (int) – limit of results, -1 means no limit
  • offset (int) – how many results from beginning should be ignored
  • with_doc (boolean) – if data from id index should be included in results
  • with_storage (boolean) – if you want to have also data from index storage
  • key – a key to look for
  • start – a start key to look for
  • end – a end key to look for
  • inclusive_start (boolean) – start value included ?
  • inclusive_end (boolean) – end value included ?
Returns:

a data

Return type:

iterable

Note

If you will set key parameter then you can’t set start, end etc. Setting it makes you search for multiple values for the same key, setting those start and end parameters makes you to search between those values.

run(index_name, target_funct[, ...])

Allows you to execute function that is defined on index side (refer for CodernityDB documentation for more details)

Parameters:
  • index_name (string) – a index name to look for function into
  • target_funct (string) – a function name to execute
  • [...] – arguments for function
Returns:

function result

count(target_funct[, ...])

Will return number of elements found in function for example instead of counting number of elements from all() you can execute this one

Parameters:
  • target_funct (string) – target function to run
  • [...] – arguments to target function
Returns:

number of elements

Misc methods

get_indexes_names()

Will return indexes names that are in database.

Returns:indexes name
Return type:iterable
get_index_code(index_name)

It will return full index code from index file.

Parameters:index_name (string) – the name of index to look for code
Returns:index code
Rtype string:
get_index_details(index)

Will return index details (properties)

Parameters:index (string) – Index name to look for details
Returns:index properties
Rtype dict:
get_db_details()

Returns database details, environment settings, database size etc.

Returns:database details
Rtype dict:
get_version()

Will return server and CodernityDB version.

Returns:versions
Rtype dict:
get_index_header()

Will return header for index codes

Returns:index header
Rtype string:

Errors

Server returns 200 or 201 HTTP code when operation was correct. And it will return other codes with custom body like:

{
"exception": null,
"reason": "Invalid encoded data in GET parameters",
"error": "400 Bad Request",
"traceback": ""
}
exception
A Python exception name (used mostly for CodernityDB-PyClient)
reason
Reason of error
error
A http response error, the error code will be the same as HTTP response code
traceback
It’s optional it will be presented only when you will hit CodernityDB exception, not general python one. It can be used to see real problem on client side instead of guessing.