1 /*
2    bdb2d is BerkeleyDB for D language
3    It is part of unDE project (http://unde.su)
4 
5    Copyright (C) 2009-2014 Nikolay (unDEFER) Krivchenkov <undefer@gmail.com>
6 
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 module main;
22 import std.algorithm;
23 import std.stdio;
24 import std.string;
25 import berkeleydb.all;
26 import core.thread;
27 import core.sys.posix.stdlib;
28 import std.file;
29 import std.stdint;
30 import std.conv;
31 
32 void main()
33 {
34     string tmp = fromStringz(getenv("TMP".toStringz())).idup();
35     if (tmp == "") tmp = "/tmp";
36     try{
37         mkdir(tmp~"/berkeleydb.locks");
38     } catch (Exception file)
39     {
40     }
41 
42     DbEnv dbenv = new DbEnv(0);
43 
44     uint32_t env_flags = DB_CREATE |    /* Create the environment if it does 
45                                 * not already exist. */
46                 DB_INIT_TXN  | /* Initialize transactions */
47                 DB_INIT_LOCK | /* Initialize locking. */
48                 DB_INIT_LOG  | /* Initialize logging */
49                 DB_INIT_MPOOL| /* Initialize the in-memory cache. */
50                 DB_RECOVER;
51 
52     dbenv.open(tmp~"/berkeleydb.locks/", env_flags, octal!666);
53 
54     Db db = new Db(dbenv, 0);
55     db.open(null, "numbers.db", null, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT | DB_MULTIVERSION, octal!600);
56 
57     while (true)
58     {
59         auto txn = dbenv.txn_begin(null, DB_TXN_SNAPSHOT);
60 
61         for (int i=0; i <5; i++)
62         {
63             Dbt key;
64             Dbt data;
65             string str = "the_number";
66             key = str;
67 
68             auto res = db.get(txn, &key, &data);
69             if (res == 0)
70             {
71                 uint the_number = data.to!uint;
72                 writeln("number is ", the_number);
73 		stdout.flush();
74             }
75             else
76             {
77                 dbenv.err(res, "number not readed");
78             }
79 
80             Thread.sleep( dur!("seconds")( 1 ) );
81         }
82 
83         txn.commit();
84     }
85 }