-----------------------------------------------------------------------------------------------------------
irc من يحب المشاركة فى هذا المشروع المتميز والاوبن سورس فنحن نلتقى على
server:www.freenode.net
channel:sowarna
او راسلنى Ahmed Youssef إذا كان لديك استفسار عن الميعاد فقط راسل
تنويه : المعلومات التالية نتيجة بحث مكثف فى المشروع وبالتالى هى تخص هذا المشروع
إذا أردت الاستفادة وكنت تريد المشاركة فى مشروع متميز واوبن سورس فلا تردد ---------------------------------------------------------------------------------------------------------------------
installing tokyocabinet from source download "zlib", get it from
http://www.zlib.net/ and get it installed
[ if not already installed in your system]
download libbz2 and get it installed
[if you're using redhat/fedora systems you've to make sure libbz2-
dev(el) installed also - thanks to
Ahmed Soliman www.ahmedsoliman.com]
download tokyocabinet, last version from
http://tokyocabinet.sourceforge.net/misc/ [./configure make make install]
download pytc,
http://pypi.python.org/pypi/pytc/ and get it installed
[./setup.py build ./setup.py install]
for pytc, you can install it using another method : cd pytc
python setup.py build_ext -i -f
export PYTHONPATH=$PWD:$PYTHONPATH
then you're able to use the library from this path, meaning when you import pytc, it will be
actually imported from this path .
usage:So far w3're only concerned about the B+ Tree database so we're g0nna use 'BDB'
BDBCUR cursor is a mechanism to access each record of B+ tree database in ascending or descending order.
import pytc as tc
bTreeDataBase = tc.BDB()
open database, if not exists create it and open it - open(string file, mode)
bTreeDataBase.open("dict.db", tc.BDBOWRITER | tc.BDBOCREAT)
bTreeDataBase.put("key1", "1")
bTreeDataBase.put("key2", "2")
bTreeDataBase.adddouble("key3", 45.5) # add double value
bTreeDataBase.addint("key4", 45) # add int value
bTreeDataBase.putdup("key1", "11")
for key, value in bTreeDataBase.iteritems(): #iterate through items
print(key, value)
('key1', '1')
('key1', '11')
('key2', '2')
('key3', '\x00\x00\x00\x00\x00\xc0F@')
('key4', '-\x00\x00\x00')
for key in bTreeDataBase.iterkeys():
print(key)
key1
key1
key2
key3
key4
for val in bTreeDataBase.itervalues():
print(val)
1
11
2
�F@
-
bTreeDataBase.items()
[('key1', '1'),
('key1', '11'),
('key2', '2'),
('key3', '\x00\x00\x00\x00\x00\xc0F@'),
('key4', '-\x00\x00\x00')]
bTreeDataBase.get("key1") # result is 1
bTreeDataBase.getlist("key1") #get a list of all values assigned to a key, the result ['1', '11']
bTreeDataBase.copy("/root/newLocationOfDataBase.db") #copy to new location
!ls /root | grep new*
-rw-r--r-- 1 root root 135680 2009-07-22 01:49 newLocationOfDataBase.db
bTreeDataBase.path() # retrieves path of database file
bTreeDataBase.out("key1") # removes 1st occurance of key and its value
bTreeDataBase.items()
[('key1', '11'),
('key2', '2'),
('key3', '\x00\x00\x00\x00\x00\xc0F@'),
('key4', '-\x00\x00\x00')]
bTreeDataBase.getlist("key1") #output is ['11']
bTreeDataBase.putdup("key1", "4")
bTreeDataBase.outlist("key1") #removes all ocurances of a key
bTreeDataBase.items()
[('key2', '2'),
('key3', '\x00\x00\x00\x00\x00\xc0F@'),
('key4', '-\x00\x00\x00')]
put cat to concatenate the value of an existing key to the end of the record, if key doesn't exist it is added
suppose the result of invoking : bTreeDataBase.items() is[('key2', '2'),
('key3', '\x00\x00\x00\x00\x00\xc0F@'),
('key4', '4'),
('key5', '5'),
('key8', '8'),
('key9', '9')]
thenbTreeDataBase.putcat("key8", "7")
bTreeDataBase.items()
[('key2', '2'),
('key3', '\x00\x00\x00\x00\x00\xc0F@'),
('key4', '4'),
('key5', '5'),
('key8', '87'), # aaaaaaaaah , wow !
('key9', '9')]
Now we can do :bTreeDataBase.putkeep("key10", "10") # store a new key
bTreeDataBase.putlist("key10", ['2','9']) # don't forget to add quotes ' ' infront and end of the items,
otherwise you'll not get an error but the record will not be added
bTreeDataBase.getlist("key10") # ['10', '2', '9']
Now we have the method :range( string bkey, boolean binc, string ekey, boolean einc, int max)
bkey - the key of the beginning border. If it is `null', the first record is specified.
binc - whether the beginning border is inclusive or not.
ekey - the key of the ending border. If it is `null', the last record is specified.
einc - whether the ending border is inclusive or not.
max - the maximum number of keys to be fetched. If it is negative, no limit is specified.
bTreeDataBase.range(None, True, None, True, 3)
['key10', 'key2', 'key3']
bTreeDataBase.range(None, False, None, True, 3)
['key10', 'key2', 'key3']
bTreeDataBase.range(None, False, None, False, 3)
['key10', 'key2', 'key3']
bTreeDataBase.range(None, False, None, False, 4)
['key10', 'key2', 'key3', 'key4']
bTreeDataBase.range('key2', False, None, False, 4)
['key3', 'key4', 'key5', 'key8']
bTreeDataBase.range('key2', True, None, False, 4)
['key2', 'key3', 'key4', 'key5']
Now suppose when we invoke bTreeDataBase.items() we have :[('key10', '555'),
('key10', '5678'),
('key2', '2'),
('key3', '\x00\x00\x00\x00\x00\xc0F@'),
('key4', '4'),
('key5', '5'),
('key8', '87'),
('key9', '9')]
Now we can do :bTreeDataBase.vsiz("key10") # size of 1st occurance which is 3
bTreeDataBase.vnum("key10") # number of records concerning a key which is 2
You also can do : bTreeDataBase.values()
['555', '5678', '2', '\x00\x00\x00\x00\x00\xc0F@', '4', '5', '87', '9']
bTreeDataBase.keys()
['key10', 'key10', 'key2', 'key3', 'key4', 'key5', 'key8', 'key9']
bTreeDataBase.vanish() -> remove all records