ObjectBox(三)——objectBox数据库路径及自定义路径

前言

之前已经讲了ObjectBox的基本配置,网上已经讲了很多ObjectBox的相关知识,但是ObjectBox的数据库存储路径问题,却极少提到,这节就让我们来探究下ObjectBox的数据存储路径吧。

今天涉及的知识有:

  1. ObjectBox数据库默认存储路径
  2. ObjectBox自定义存储路径
  3. ObjectBox数据库管理类封装

一.ObjectBox数据库默认存储路径

在使用Objectbox进行数据处理之前,我们先要初始化ObjectBox数据库,这时会涉及到BoxStore的初始化,让我们来看看初始化ObjectBox数据库代码吧:

private static BoxStore boxStore;

boxStore = MyObjectBox.builder()
                .androidContext(context.getApplicationContext())
                .build();

这段代码会在你项目的application中初始化。其中,让我们来看看

androidContext(context.getApplicationContext())

方法,追踪源码,会看到以下代码:

    public BoxStoreBuilder androidContext(Object context) {
        if (context == null) {
            throw new NullPointerException("Context may not be null");
        }
        this.context = getApplicationContext(context);

        File baseDir = getAndroidBaseDir(context);
        if (!baseDir.exists()) {
            baseDir.mkdir();
            if (!baseDir.exists()) { // check baseDir.exists() because of potential concurrent processes
                throw new RuntimeException("Could not init Android base dir at " + baseDir.getAbsolutePath());
            }
        }
        if (!baseDir.isDirectory()) {
            throw new RuntimeException("Android base dir is not a dir: " + baseDir.getAbsolutePath());
        }
        baseDirectory = baseDir;
        android = true;
        return this;
    }

继续看其中涉及到的 getAndroidBaseDir(context) 方法:

    static File getAndroidBaseDir(Object context) {
        return new File(getAndroidFilesDir(context), "objectbox");
    }

这里我们看到创建了一个File,getAndroidFilesDir(context)是一个路径,然后后面跟了一个"objectbox"值,"objectbox"是该路径下的一个文件夹名称,接着往下看

    @Nonnull
    private static File getAndroidFilesDir(Object context) {
        File filesDir;
        try {
            Method getFilesDir = context.getClass().getMethod("getFilesDir");
            filesDir = (File) getFilesDir.invoke(context);
            if (filesDir == null) {
                // Race condition in Android before 4.4: https://issuetracker.google.com/issues/36918154 ?
                System.err.println("getFilesDir() returned null - retrying once...");
                filesDir = (File) getFilesDir.invoke(context);
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not init with given Android context (must be sub class of android.content.Context)", e);
        }
        if (filesDir == null) {
            throw new IllegalStateException("Android files dir is null");
        }
        if (!filesDir.exists()) {
            throw new IllegalStateException("Android files dir does not exist");
        }
        return filesDir;
    }

ok, getAndroidFilesDir(Object context) 方法其实是创建了一个文件夹路径。
接着我们看

boxStore = MyObjectBox.builder()
                .androidContext(context.getApplicationContext())
                .build();

中 的 “ .build()” 方法,追踪源码,显示如下:

    /**
     * Builds a {@link BoxStore} using any given configuration.
     */
    public BoxStore build() {
        if (directory == null) {
            name = dbName(name);
            directory = getDbDir(baseDirectory, name);
        }
        checkProvisionInitialDbFile();
        return new BoxStore(this);
    }

继续看 dbName(name) 方法:

    private static String dbName(@Nullable String dbNameOrNull) {
        return dbNameOrNull != null ? dbNameOrNull : DEFAULT_NAME;
    }

这里出现了当 dbName(name)方法中的 name为null时,会设置一个默认name=DEFAULT_NAME,接着看追踪DEFAULT_NAME:

public static final String DEFAULT_NAME = "objectbox";

结合“ .build()” 方法

    /**
     * Builds a {@link BoxStore} using any given configuration.
     */
    public BoxStore build() {
        if (directory == null) {
            name = dbName(name);
            directory = getDbDir(baseDirectory, name);
        }
        checkProvisionInitialDbFile();
        return new BoxStore(this);
    }

会发现以上 的都是在设置数据库的文件夹路径directory,所以由此看ObjectBox存储路径是 getAndroidFilesDir(Object context) / objectbox /objectbox/ ,接着我们看看ObjectBox数据库文件名及文件格式。接着看 “ .build()” 方法 中的:

 checkProvisionInitialDbFile();

追踪看看:

    private void checkProvisionInitialDbFile() {
        if (initialDbFileFactory != null) {
            String dataDir = BoxStore.getCanonicalPath(directory);
            File file = new File(dataDir, "data.mdb");
            if (!file.exists()) {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = initialDbFileFactory.provide();
                    if (in == null) {
                        throw new DbException("Factory did not provide a resource");
                    }
                    in = new BufferedInputStream(in);
                    out = new BufferedOutputStream(new FileOutputStream(file));
                    IoUtils.copyAllBytes(in, out);
                } catch (Exception e) {
                    throw new DbException("Could not provision initial data file", e);
                } finally {
                    IoUtils.safeClose(out);
                    IoUtils.safeClose(in);
                }
            }
        }
    }

其中有一行:

 File file = new File(dataDir, "data.mdb");

据此,我们知道ObjectBox的存储默认文件为:"data.mdb 。
ok,通过以上的追踪,得出ObjectBox默认存储路径为:

getAndroidFilesDir(Object context) / objectbox /objectbox/ data.mdb

由于getAndroidFilesDir(Object context)是源码中一个私有方法。第一种方案,你可以将此方法拷贝出来,然后运行查看路径。第二种方案:我们大致可以看出此路径是在项目app的内部的,这里我直接运行自己的项目,然后查看手机app安装目录,在
/data/data/app包名/files/objectbox/objectbox/ 目录下看到以下截图


image.png

即,当 BoxStore 在初始化时,以:

    public static void init(Context context) {
        boxStore = MyObjectBox.builder()
                .androidContext(context.getApplicationContext())
                .build();
        LogUtil.i("===BoxStore.version="+BoxStore.getVersion()+"  BoxStore.versionNative="+BoxStore.getVersionNative());
    }

方式创建的时候,ObjectBox数据库默认文件存储路径为:

/data/data/app包名/files/objectbox/objectbox/data.mdb

二.ObjectBox自定义存储路径

经过上面的研究,我们可以发现 boxStore对象是通过

还有 46% 的精彩内容
©著作权归作者所有,转载或内容合作请联系作者
支付 ¥3.00 继续阅读
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容

  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 4,867评论 0 9
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,618评论 0 10
  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 2,642评论 2 9
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,036评论 1 32
  • 每年生日 许的愿 能有几个可以灵验人总会变 抱歉励志要走到社会的前列, 可总是败在势力面前肺腑之言 抱歉我用了一半...
    HappyGhh阅读 210评论 2 2