1414#include "object-file.h"
1515#include "object-name.h"
1616#include "odb.h"
17+ #include "odb/source-inmemory.h"
1718#include "packfile.h"
1819#include "path.h"
1920#include "promisor-remote.h"
3132KHASH_INIT (odb_path_map , const char * /* key: odb_path */ ,
3233 struct odb_source * , 1 , fspathhash , fspatheq )
3334
34- /*
35- * This is meant to hold a *small* number of objects that you would
36- * want odb_read_object() to be able to return, but yet you do not want
37- * to write them into the object store (e.g. a browse-only
38- * application).
39- */
40- struct cached_object_entry {
41- struct object_id oid ;
42- struct cached_object {
43- enum object_type type ;
44- const void * buf ;
45- unsigned long size ;
46- } value ;
47- };
48-
49- static const struct cached_object * find_cached_object (struct object_database * object_store ,
50- const struct object_id * oid )
51- {
52- static const struct cached_object empty_tree = {
53- .type = OBJ_TREE ,
54- .buf = "" ,
55- };
56- const struct cached_object_entry * co = object_store -> cached_objects ;
57-
58- for (size_t i = 0 ; i < object_store -> cached_object_nr ; i ++ , co ++ )
59- if (oideq (& co -> oid , oid ))
60- return & co -> value ;
61-
62- if (oid -> algo && oideq (oid , hash_algos [oid -> algo ].empty_tree ))
63- return & empty_tree ;
64-
65- return NULL ;
66- }
67-
6835int odb_mkstemp (struct object_database * odb ,
6936 struct strbuf * temp_filename , const char * pattern )
7037{
@@ -584,7 +551,6 @@ static int do_oid_object_info_extended(struct object_database *odb,
584551 const struct object_id * oid ,
585552 struct object_info * oi , unsigned flags )
586553{
587- const struct cached_object * co ;
588554 const struct object_id * real = oid ;
589555 int already_retried = 0 ;
590556
@@ -594,25 +560,8 @@ static int do_oid_object_info_extended(struct object_database *odb,
594560 if (is_null_oid (real ))
595561 return -1 ;
596562
597- co = find_cached_object (odb , real );
598- if (co ) {
599- if (oi ) {
600- if (oi -> typep )
601- * (oi -> typep ) = co -> type ;
602- if (oi -> sizep )
603- * (oi -> sizep ) = co -> size ;
604- if (oi -> disk_sizep )
605- * (oi -> disk_sizep ) = 0 ;
606- if (oi -> delta_base_oid )
607- oidclr (oi -> delta_base_oid , odb -> repo -> hash_algo );
608- if (oi -> contentp )
609- * oi -> contentp = xmemdupz (co -> buf , co -> size );
610- if (oi -> mtimep )
611- * oi -> mtimep = 0 ;
612- oi -> whence = OI_CACHED ;
613- }
563+ if (!odb_source_read_object_info (odb -> inmemory_objects , oid , oi , flags ))
614564 return 0 ;
615- }
616565
617566 odb_prepare_alternates (odb );
618567
@@ -784,24 +733,12 @@ int odb_pretend_object(struct object_database *odb,
784733 void * buf , unsigned long len , enum object_type type ,
785734 struct object_id * oid )
786735{
787- struct cached_object_entry * co ;
788- char * co_buf ;
789-
790736 hash_object_file (odb -> repo -> hash_algo , buf , len , type , oid );
791- if (odb_has_object (odb , oid , 0 ) ||
792- find_cached_object (odb , oid ))
737+ if (odb_has_object (odb , oid , 0 ))
793738 return 0 ;
794739
795- ALLOC_GROW (odb -> cached_objects ,
796- odb -> cached_object_nr + 1 , odb -> cached_object_alloc );
797- co = & odb -> cached_objects [odb -> cached_object_nr ++ ];
798- co -> value .size = len ;
799- co -> value .type = type ;
800- co_buf = xmalloc (len );
801- memcpy (co_buf , buf , len );
802- co -> value .buf = co_buf ;
803- oidcpy (& co -> oid , oid );
804- return 0 ;
740+ return odb_source_write_object (odb -> inmemory_objects ,
741+ buf , len , type , oid , NULL , 0 );
805742}
806743
807744void * odb_read_object (struct object_database * odb ,
@@ -1083,6 +1020,7 @@ struct object_database *odb_new(struct repository *repo,
10831020 o -> sources = odb_source_new (o , primary_source , true);
10841021 o -> sources_tail = & o -> sources -> next ;
10851022 o -> alternate_db = xstrdup_or_null (secondary_sources );
1023+ o -> inmemory_objects = & odb_source_inmemory_new (o )-> base ;
10861024
10871025 free (to_free );
10881026
@@ -1106,6 +1044,10 @@ static void odb_free_sources(struct object_database *o)
11061044 odb_source_free (o -> sources );
11071045 o -> sources = next ;
11081046 }
1047+
1048+ odb_source_free (o -> inmemory_objects );
1049+ o -> inmemory_objects = NULL ;
1050+
11091051 kh_destroy_odb_path_map (o -> source_by_path );
11101052 o -> source_by_path = NULL ;
11111053}
@@ -1123,10 +1065,6 @@ void odb_free(struct object_database *o)
11231065 odb_close (o );
11241066 odb_free_sources (o );
11251067
1126- for (size_t i = 0 ; i < o -> cached_object_nr ; i ++ )
1127- free ((char * ) o -> cached_objects [i ].value .buf );
1128- free (o -> cached_objects );
1129-
11301068 string_list_clear (& o -> submodule_source_paths , 0 );
11311069
11321070 free (o );
0 commit comments