简体   繁体   English

sg-cdb Java库,用于使用cdb数据库

[英]sg-cdb Java library for working with cdb database

I have downloaded sg-cdb Java library from http://www.strangegizmo.com/products/sg-cdb/ and included it in my project. 我已经从http://www.strangegizmo.com/products/sg-cdb/下载了sg-cdb Java库,并将其包含在我的项目中。 I'm trying to read CDB database with the following code: 我正在尝试使用以下代码读取CDB数据库:

import com.strangegizmo.cdb.Cdb;
import com.strangegizmo.cdb.CdbElement;
import java.util.Enumeration;

class Start
{ 
    public static void main(String args[])
    {
        Cdb cdbFile = null;
        try{
            cdbFile = new Cdb("basefile.cdb");
        }catch (Exception e) { e.printStackTrace(); }

        try
        {
            Enumeration em = cdbFile.elements("basefile.cdb");
            while(em.hasMoreElements())
            {
                CdbElement cdbElt = (CdbElement) em.nextElement();
            }
        }catch (Exception e) {e.printStackTrace();}
    }
}

What am I doing wrong that I'm getting the following exception message: 我收到以下异常消息,这是在做什么错:

java.lang.IllegalArgumentException: invalid cdb format
    at com.strangegizmo.cdb.Cdb$1.nextElement(Cdb.java:358)

Can you provide any example of working with this library? 您能否提供使用此库的任何示例?

Thank you. 谢谢。

I am the author of sg-cdb and can help you get going with the library. 我是sg-cdb的作者,可以帮助您使用该库。

The primary issue with your sample is that you have to "make" a CDB file before you can enumerate over the file's elements. 样本的主要问题是必须先“制作” CDB文件,然后才能枚举文件的元素。 The portable way to do that is to generate a file in the cdbmake/cdbdump format and then use the static CdbMake.make(...) method to turn that file into a constant database. 可移植的方法是生成cdbmake / cdbdump格式的文件,然后使用静态CdbMake.make(...)方法将该文件转换为恒定数据库。 You can then load that constant database with the Cdb class and query for keys, enumerate over values, etc. 然后,您可以使用Cdb类加载该常量数据库,并查询键,枚举值等。

sg-cdb supports another mechanism for creating constant databases, which is to construct an instance of the CdbMake class directly, call start(...) to begin writing to the database, call add(...) to add keys to the database, and then call finish() when you are done writing keys. sg-cdb支持另一种创建常量数据库的机制,该机制将直接构造CdbMake类的实例,调用start(...)开始写入数据库,调用add(...)将密钥添加到数据库,完成键编写后再调用finish()

Here is an update to your example that uses CdbMake.start/.add/.finish to generate a file and then enumerate over the file: 这是您的示例的更新,该示例使用CdbMake.start / .add / .finish生成文件,然后枚举该文件:

import com.strangegizmo.cdb.Cdb;
import com.strangegizmo.cdb.CdbElement;
import com.strangegizmo.cdb.CdbMake;
import java.util.Enumeration;

class Start
{ 
    public static void main(String args[])
    {
        try{
            CdbMake maker = new CdbMake();
            maker.start("basefile.cdb");
            maker.add("one".getBytes(), "Hello".getBytes());
            maker.add("two".getBytes(), "Goodbye".getBytes());
            maker.finish();
        }catch (Exception e) {e.printStackTrace();}

        try
        {
            Enumeration em = Cdb.elements("basefile.cdb");
            while(em.hasMoreElements())
            {
                CdbElement cdbElt = (CdbElement) em.nextElement();
            }
        }catch (Exception e) {e.printStackTrace();}
    }
}

Note that elements(...) is a static method on the Cdb class, so you do not need a Cdb instance in order to dump everything in the database. 请注意, elements(...)Cdb类上的静态方法,因此不需要Cdb实例即可转储数据库中的所有内容。

In general my recommendation is that you ingest cdbdump -formatted files though. 通常,我的建议是您摄取cdbdump格式的文件。 You would get the same results if you run cdbmake (or the cdb.make utility that is included with sg-cdb) on the following text file: 如果在以下文本文件上运行cdbmake (或sg- cdb.make附带的cdb.make实用程序),将会得到相同的结果:

+3,5:one->Hello
+3,7:two->Goodbye

One final note: creating a new instance of the Cdb class always succeeds (even if the file does not exist), because it is perfectly valid to have a file with no keys. 最后一点:创建Cdb类的新实例总是成功(即使文件不存在),因为拥有没有键的文件是完全有效的。 A Cdb instance initialized with a non-existent file (as you have done in your example) will correctly return null in response to every find() call. 使用不存在的文件初始化的Cdb实例(如您在示例中所做的那样)将正确地返回null以响应每个find()调用。 Cdb.enumerate(...) is not quite as forgiving though and expects to be given the path to a valid CDB file, which is why you are getting that IllegalArgumentException. Cdb.enumerate(...)并不是那么宽容,它希望获得有效CDB文件的路径,这就是为什么要获取IllegalArgumentException的原因。

Let me know if you need any additional information! 让我知道您是否需要其他信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM